Просмотр исходного кода

implemented Jellyfin pseudo-sso

mark 5 лет назад
Родитель
Сommit
b53295d4fc
3 измененных файлов с 58 добавлено и 4 удалено
  1. 17 1
      api/classes/organizr.class.php
  2. 38 1
      api/functions/sso-functions.php
  3. 3 2
      api/v2/routes/root.php

+ 17 - 1
api/classes/organizr.class.php

@@ -2302,6 +2302,22 @@ class Organizr
 					'label' => 'Enable',
 					'value' => $this->config['ssoOmbi']
 				)
+			),
+			'Jellyfin' => array(
+				array(
+					'type' => 'input',
+					'name' => 'jellyfinURL',
+					'label' => 'Jellyfin URL',
+					'value' => $this->config['jellyfinURL'],
+					'help' => 'Please make sure to use the same (sub)domain to access Jellyfin as Organizr\'s',
+					'placeholder' => 'http(s)://hostname:port'
+				),
+				array(
+					'type' => 'switch',
+					'name' => 'ssoJellyfin',
+					'label' => 'Enable',
+					'value' => $this->config['ssoJellyfin']
+				)
 			)
 		);
 	}
@@ -5888,4 +5904,4 @@ class Organizr
 		return count($request) > 1 ? $results : $results[$firstKey];
 	}
 	
-}
+}

+ 38 - 1
api/functions/sso-functions.php

@@ -23,9 +23,46 @@ trait SSOFunctions
 				}
 			}
 		}
+		if ($this->config['ssoJellyfin']) {
+			$jellyfinToken = $this->getJellyfinToken($username, $password);
+			if ($jellyfinToken) {
+				$this->coookie('set', 'jellyfin_credentials', $jellyfinToken, $this->config['rememberMeDays'], false);
+				$this->writeLog('success', 'ITTATOKEN: ' . $jellyfinToken);
+			}
+		}
 		return true;
 	}
-	
+
+	public function getJellyfinToken($username, $password)
+	{
+		$token = null;
+		try {
+			$url = $this->qualifyURL($this->config['jellyfinURL']);
+			$headers = array(
+				'X-Emby-Authorization' => 'MediaBrowser Client="Organizr Jellyfin Tab", Device="Organizr_PHP", DeviceId="Organizr_SSO", Version="1.0"',
+				"Accept" => "application/json",
+				"Content-Type" => "application/json"
+			);
+			$data = array(
+				"Username" => $username,
+				"Pw" => $password
+			);
+			$endpoint = '/Users/authenticatebyname';
+			$options = ($this->localURL($url)) ? array('verify' => false) : array();
+			$response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
+			if ($response->success) {
+				$token = json_decode($response->body, true);
+				$this->writeLog('success', 'Jellyfin Token Function - Grabbed token.', $username);
+			} else {
+				$this->writeLog('error', 'Jellyfin Token Function - Jellyfin did not return Token', $username);
+			}
+		} catch (Requests_Exception $e) {
+			$this->writeLog('error', 'Jellyfin Token Function - Error: ' . $e->getMessage(), $username);
+		}
+		
+		return '{"Servers":[{"ManualAddress":"'. $url . '","Id":"' . $token['ServerId'] . '","UserId":"' . $token['User']['Id'] . '","AccessToken":"' . $token['AccessToken'] . '"}]}';
+	}
+
 	public function getOmbiToken($username, $password, $oAuthToken = null, $fallback = false)
 	{
 		$token = null;

+ 3 - 2
api/v2/routes/root.php

@@ -116,11 +116,12 @@ $app->get('/launch', function ($request, $response, $args) {
 	$GLOBALS['api']['response']['data']['status'] = $Organizr->status();
 	$GLOBALS['api']['response']['data']['sso'] = array(
 		'myPlexAccessToken' => isset($_COOKIE['mpt']) ? $_COOKIE['mpt'] : false,
-		'id_token' => isset($_COOKIE['Auth']) ? $_COOKIE['Auth'] : false
+		'id_token' => isset($_COOKIE['Auth']) ? $_COOKIE['Auth'] : false,
+		'jellyfin_credentials' => isset($_COOKIE['jellyfin_credentials']) ? $_COOKIE['jellyfin_credentials'] : false
 	);
 	$response->getBody()->write(jsonE($GLOBALS['api']));
 	return $response
 		->withHeader('Content-Type', 'application/json;charset=UTF-8')
 		->withStatus($GLOBALS['responseCode']);
 	
-});
+});