sso-functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. trait SSOFunctions
  3. {
  4. public function ssoCookies()
  5. {
  6. $cookies = array(
  7. 'myPlexAccessToken' => $_COOKIE['mpt'] ?? false,
  8. 'id_token' => $_COOKIE['Auth'] ?? false,
  9. 'jellyfin_credentials' => $_COOKIE['jellyfin_credentials'] ?? false,
  10. 'komga_token' => $_COOKIE['komga_token'] ?? false
  11. );
  12. // Jellyfin cookie
  13. foreach (array_keys($_COOKIE) as $k => $v) {
  14. if (strpos($v, 'user-') !== false) {
  15. $cookiesToAdd = [
  16. $v => $_COOKIE[$v]
  17. ];
  18. $cookies = array_merge($cookies, $cookiesToAdd);
  19. }
  20. }
  21. return $cookies;
  22. }
  23. public function getSSOUserFor($app, $userobj)
  24. {
  25. $map = array(
  26. 'jellyfin' => 'username',
  27. 'ombi' => 'username',
  28. 'overseerr' => 'email',
  29. 'tautulli' => 'username',
  30. 'petio' => 'username',
  31. 'komga' => 'email'
  32. );
  33. return (gettype($userobj) == 'string') ? $userobj : $userobj[$map[$app]];
  34. }
  35. public function ssoCheck($userobj, $password, $token = null)
  36. {
  37. $this->setCurrentUser(false);
  38. $this->setLoggerChannel('Authentication', $this->user['username']);
  39. $this->logger->debug('Starting SSO check function');
  40. if ($this->config['ssoPlex'] && $token) {
  41. $this->logger->debug('Setting Plex SSO cookie');
  42. $this->coookie('set', 'mpt', $token, $this->config['rememberMeDays'], false);
  43. }
  44. if ($this->config['ssoOmbi']) {
  45. $this->logger->debug('Starting Ombi SSO check function');
  46. $fallback = ($this->config['ombiFallbackUser'] !== '' && $this->config['ombiFallbackPassword'] !== '');
  47. $ombiToken = $this->getOmbiToken($this->getSSOUserFor('ombi', $userobj), $password, $token, $fallback);
  48. if ($ombiToken) {
  49. $this->logger->debug('Setting Ombi SSO cookie');
  50. $this->coookie('set', 'Auth', $ombiToken, $this->config['rememberMeDays'], false);
  51. } else {
  52. $this->logger->debug('No Ombi token received from backend');
  53. }
  54. }
  55. if ($this->config['ssoTautulli'] && $this->qualifyRequest($this->config['ssoTautulliAuth'])) {
  56. $this->logger->debug('Starting Tautulli SSO check function');
  57. $tautulliToken = $this->getTautulliToken($this->getSSOUserFor('tautulli', $userobj), $password, $token);
  58. if ($tautulliToken) {
  59. foreach ($tautulliToken as $key => $value) {
  60. $this->logger->debug('Setting Tautulli SSO cookie');
  61. $this->coookie('set', 'tautulli_token_' . $value['uuid'], $value['token'], $this->config['rememberMeDays'], true, $value['path']);
  62. }
  63. } else {
  64. $this->logger->debug('No Tautulli token received from backend');
  65. }
  66. }
  67. if ($this->config['ssoJellyfin']) {
  68. $this->logger->debug('Starting Jellyfin SSO check function');
  69. $jellyfinToken = $this->getJellyfinToken($this->getSSOUserFor('jellyfin', $userobj), $password);
  70. if ($jellyfinToken) {
  71. foreach ($jellyfinToken as $k => $v) {
  72. $this->logger->debug('Setting Jellyfin SSO cookie');
  73. $this->coookie('set', $k, $v, $this->config['rememberMeDays'], false);
  74. }
  75. } else {
  76. $this->logger->debug('No Jellyfin token received from backend');
  77. }
  78. }
  79. if ($this->config['ssoOverseerr']) {
  80. $this->logger->debug('Starting Overseerr SSO check function');
  81. $fallback = ($this->config['overseerrFallbackUser'] !== '' && $this->config['overseerrFallbackPassword'] !== '');
  82. $overseerrToken = $this->getOverseerrToken($this->getSSOUserFor('overseerr', $userobj), $password, $token, $fallback);
  83. if ($overseerrToken) {
  84. $this->logger->debug('Setting Overseerr SSO cookie');
  85. $this->coookie('set', 'connect.sid', $overseerrToken, $this->config['rememberMeDays'], false);
  86. } else {
  87. $this->logger->debug('No Overseerr token received from backend');
  88. }
  89. }
  90. if ($this->config['ssoPetio']) {
  91. $this->logger->debug('Starting Petio SSO check function');
  92. $fallback = ($this->config['petioFallbackUser'] !== '' && $this->config['petioFallbackPassword'] !== '');
  93. $petioToken = $this->getPetioToken($this->getSSOUserFor('petio', $userobj), $password, $token, $fallback);
  94. if ($petioToken) {
  95. $this->logger->debug('Setting Petio SSO cookie');
  96. $this->coookie('set', 'petio_jwt', $petioToken, $this->config['rememberMeDays'], false);
  97. } else {
  98. $this->logger->debug('No Petio token received from backend');
  99. }
  100. }
  101. if ($this->config['ssoKomga'] && $this->qualifyRequest($this->config['ssoKomgaAuth'])) {
  102. $this->logger->debug('Starting Komga SSO check function');
  103. $fallback = ($this->config['komgaFallbackUser'] !== '' && $this->config['komgaFallbackPassword'] !== '');
  104. $komga = $this->getKomgaToken($this->getSSOUserFor('komga', $userobj), $password, $fallback);
  105. if ($komga) {
  106. $this->logger->debug('Setting Komga SSO cookie');
  107. $this->coookie('set', 'komga_token', $komga, $this->config['rememberMeDays'], false);
  108. } else {
  109. $this->logger->debug('No Komga token received from backend');
  110. }
  111. }
  112. return true;
  113. }
  114. public function getKomgaToken($email, $password, $fallback = false)
  115. {
  116. $token = null;
  117. $useMaster = false;
  118. try {
  119. if ($password) {
  120. if ($password == '') {
  121. $useMaster = true;
  122. }
  123. } else {
  124. $useMaster = true;
  125. }
  126. if ($useMaster) {
  127. if ($this->config['komgaSSOMasterPassword'] !== '') {
  128. $password = $this->decrypt($this->config['komgaSSOMasterPassword']);
  129. }
  130. }
  131. $credentials = array('auth' => new Requests_Auth_Digest(array($email, $password)));
  132. $url = $this->qualifyURL($this->config['komgaURL']);
  133. $options = $this->requestOptions($url, 60000, true, false, $credentials);
  134. $response = Requests::get($url . '/api/v1/users/me', ['X-Auth-Token' => 'organizrSSO'], $options);
  135. if ($response->success) {
  136. if ($response->headers['x-auth-token']) {
  137. $this->setLoggerChannel('Komga')->info('Grabbed token');
  138. $token = $response->headers['x-auth-token'];
  139. } else {
  140. $this->setLoggerChannel('Komga')->warning('Komga did not return Token');
  141. }
  142. } else {
  143. if ($fallback) {
  144. $this->setLoggerChannel('Komga')->warning('Komga did not return Token - Will retry using fallback credentials');
  145. } else {
  146. $this->setLoggerChannel('Komga')->warning('Komga did not return Token');
  147. }
  148. }
  149. } catch (Requests_Exception $e) {
  150. $this->setLoggerChannel('Komga')->error($e);
  151. }
  152. if ($token) {
  153. return $token;
  154. } elseif ($fallback) {
  155. return $this->getKomgaToken($this->config['komgaFallbackUser'], $this->decrypt($this->config['komgaFallbackPassword']), false);
  156. } else {
  157. return false;
  158. }
  159. }
  160. public function getJellyfinToken($username, $password)
  161. {
  162. $token = null;
  163. try {
  164. $url = $this->qualifyURL($this->config['jellyfinURL']);
  165. $ssoUrl = $this->qualifyURL($this->config['jellyfinSSOURL']);
  166. $headers = array(
  167. 'X-Emby-Authorization' => 'MediaBrowser Client="Organizr Jellyfin Tab", Device="Organizr_PHP", DeviceId="Organizr_SSO", Version="1.0"',
  168. "Accept" => "application/json",
  169. "Content-Type" => "application/json",
  170. "X-Forwarded-For" => $this->userIP()
  171. );
  172. $data = array(
  173. "Username" => $username,
  174. "Pw" => $password
  175. );
  176. $endpoint = '/Users/authenticatebyname';
  177. $options = $this->requestOptions($url, 60000);
  178. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  179. if ($response->success) {
  180. $token = json_decode($response->body, true);
  181. $this->setLoggerChannel('JellyFin')->info('Grabbed token');
  182. $key = 'user-' . $token['User']['Id'] . '-' . $token['ServerId'];
  183. $jellyfin[$key] = json_encode($token['User']);
  184. $jellyfin['jellyfin_credentials'] = '{"Servers":[{"ManualAddress":"' . $ssoUrl . '","Id":"' . $token['ServerId'] . '","UserId":"' . $token['User']['Id'] . '","AccessToken":"' . $token['AccessToken'] . '"}]}';
  185. return $jellyfin;
  186. } else {
  187. $this->setLoggerChannel('JellyFin')->warning('JellyFin did not return Token');
  188. }
  189. } catch (Requests_Exception $e) {
  190. $this->setLoggerChannel('Jellyfin')->error($e);
  191. }
  192. return false;
  193. }
  194. public function getOmbiToken($username, $password, $oAuthToken = null, $fallback = false)
  195. {
  196. $token = null;
  197. try {
  198. $url = $this->qualifyURL($this->config['ombiURL']);
  199. $headers = array(
  200. "Accept" => "application/json",
  201. "Content-Type" => "application/json",
  202. "X-Forwarded-For" => $this->userIP()
  203. );
  204. $data = array(
  205. "username" => ($oAuthToken ? "" : $username),
  206. "password" => ($oAuthToken ? "" : $password),
  207. "rememberMe" => "true",
  208. "plexToken" => $oAuthToken
  209. );
  210. $endpoint = ($oAuthToken) ? '/api/v1/Token/plextoken' : '/api/v1/Token';
  211. $options = $this->requestOptions($url, 60000);
  212. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  213. if ($response->success) {
  214. $token = json_decode($response->body, true)['access_token'];
  215. $this->setLoggerChannel('Ombi')->info('Grabbed token');
  216. } else {
  217. if ($fallback) {
  218. $this->setLoggerChannel('Ombi')->warning('Ombi did not return Token - Will retry using fallback credentials');
  219. } else {
  220. $this->setLoggerChannel('Ombi')->warning('Ombi did not return Token');
  221. }
  222. }
  223. } catch (Requests_Exception $e) {
  224. $this->setLoggerChannel('Ombi')->error($e);
  225. }
  226. if ($token) {
  227. return $token;
  228. } elseif ($fallback) {
  229. return $this->getOmbiToken($this->config['ombiFallbackUser'], $this->decrypt($this->config['ombiFallbackPassword']), null, false);
  230. } else {
  231. return false;
  232. }
  233. }
  234. public function getTautulliToken($username, $password, $plexToken = null)
  235. {
  236. $token = null;
  237. $tautulliURLList = explode(',', $this->config['tautulliURL']);
  238. if (count($tautulliURLList) !== 0) {
  239. foreach ($tautulliURLList as $key => $value) {
  240. try {
  241. $url = $this->qualifyURL($value);
  242. $headers = array(
  243. "Accept" => "application/json",
  244. "Content-Type" => "application/x-www-form-urlencoded",
  245. "User-Agent" => $_SERVER ['HTTP_USER_AGENT'] ?? null,
  246. "X-Forwarded-For" => $this->userIP()
  247. );
  248. $data = array(
  249. "username" => ($plexToken ? "" : $username),
  250. "password" => ($plexToken ? "" : $password),
  251. "token" => $plexToken,
  252. "remember_me" => 1,
  253. );
  254. $options = $this->requestOptions($url, 60000);
  255. $response = Requests::post($url . '/auth/signin', $headers, $data, $options);
  256. if ($response->success) {
  257. $qualifiedURL = $this->qualifyURL($url, true);
  258. $path = ($qualifiedURL['path']) ? $qualifiedURL['path'] : '/';
  259. $token[$key]['token'] = json_decode($response->body, true)['token'];
  260. $token[$key]['uuid'] = json_decode($response->body, true)['uuid'];
  261. $token[$key]['path'] = $path;
  262. $this->setLoggerChannel('Tautulli')->info('Grabbed token from: ' . $url);
  263. } else {
  264. $this->setLoggerChannel('Tautulli')->warning('Error on URL: ' . $url);
  265. }
  266. } catch (Requests_Exception $e) {
  267. $this->setLoggerChannel('Tautulli')->error($e);
  268. }
  269. }
  270. }
  271. return ($token) ? $token : false;
  272. }
  273. public function getOverseerrToken($email, $password, $oAuthToken = null, $fallback = false)
  274. {
  275. $token = null;
  276. try {
  277. $url = $this->qualifyURL($this->config['overseerrURL']);
  278. $headers = array(
  279. "Content-Type" => "application/json",
  280. "X-Forwarded-For" => $this->userIP()
  281. );
  282. $data = array(
  283. "email" => ($oAuthToken ? "" : $email), // not needed yet
  284. "password" => ($oAuthToken ? "" : $password), // not needed yet
  285. "authToken" => $oAuthToken
  286. );
  287. $endpoint = ($oAuthToken ? '/api/v1/auth/plex' : '/api/v1/auth/local');
  288. $options = $this->requestOptions($url, 60000);
  289. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  290. if ($response->success) {
  291. $user = json_decode($response->body, true); // not really needed yet
  292. $token = $response->cookies['connect.sid']->value;
  293. $this->setLoggerChannel('Overseerr')->info('Grabbed token');
  294. } else {
  295. if ($fallback) {
  296. $this->setLoggerChannel('Overseerr')->warning('Overseerr did not return Token - Will retry using fallback credentials');
  297. } else {
  298. $this->setLoggerChannel('Overseerr')->warning('Overseerr did not return Token');
  299. }
  300. }
  301. } catch (Requests_Exception $e) {
  302. $this->setLoggerChannel('Overseerr')->error($e);
  303. }
  304. if ($token) {
  305. return urldecode($token);
  306. } elseif ($fallback) {
  307. return $this->getOverseerrToken($this->config['overseerrFallbackUser'], $this->decrypt($this->config['overseerrFallbackPassword']), null, false);
  308. } else {
  309. return false;
  310. }
  311. }
  312. public function getPetioToken($username, $password, $oAuthToken = null, $fallback = false)
  313. {
  314. $token = null;
  315. try {
  316. $url = $this->qualifyURL($this->config['petioURL']);
  317. $headers = array(
  318. "Content-Type" => "application/json",
  319. "X-Forwarded-For" => $this->userIP()
  320. );
  321. $data = array(
  322. 'user' => [
  323. 'username' => ($oAuthToken ? '' : $username),
  324. 'password' => ($oAuthToken ? '' : $password),
  325. 'type' => 1,
  326. ],
  327. 'authToken' => false,
  328. 'token' => $oAuthToken
  329. );
  330. $endpoint = ($oAuthToken) ? '/api/login/plex_login' : '/api/login';
  331. $options = $this->requestOptions($url, 60000);
  332. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  333. if ($response->success) {
  334. $user = json_decode($response->body, true)['user'];
  335. $token = json_decode($response->body, true)['token'];
  336. $this->setLoggerChannel('Petio')->info('Grabbed token');
  337. } else {
  338. if ($fallback) {
  339. $this->setLoggerChannel('Petio')->warning('Petio did not return Token - Will retry using fallback credentials');
  340. } else {
  341. $this->setLoggerChannel('Petio')->warning('Petio did not return Token');
  342. }
  343. }
  344. } catch (Requests_Exception $e) {
  345. $this->setLoggerChannel('Petio')->error($e);
  346. }
  347. if ($token) {
  348. return $token;
  349. } elseif ($fallback) {
  350. return $this->getPetioToken($this->config['petioFallbackUser'], $this->decrypt($this->config['petioFallbackPassword']), null, false);
  351. } else {
  352. return false;
  353. }
  354. }
  355. }