4
0

sso-functions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. try {
  118. $credentials = array('auth' => new Requests_Auth_Digest(array($email, $password)));
  119. $url = $this->qualifyURL($this->config['komgaURL']);
  120. $options = $this->requestOptions($url, 60000, true, false, $credentials);
  121. $response = Requests::get($url . '/api/v1/users/me', ['X-Auth-Token' => 'organizrSSO'], $options);
  122. if ($response->success) {
  123. if ($response->headers['x-auth-token']) {
  124. $this->writeLog('success', 'Komga Token Function - Grabbed token.', $email);
  125. $token = $response->headers['x-auth-token'];
  126. } else {
  127. $this->writeLog('error', 'Komga Token Function - Komga did not return Token', $email);
  128. }
  129. } else {
  130. if ($fallback) {
  131. $this->writeLog('error', 'Komga Token Function - Komga did not return Token - Will retry using fallback credentials', $email);
  132. } else {
  133. $this->writeLog('error', 'Komga Token Function - Komga did not return Token', $email);
  134. }
  135. }
  136. } catch (Requests_Exception $e) {
  137. $this->writeLog('error', 'Komga Token Function - Error: ' . $e->getMessage(), $email);
  138. }
  139. if ($token) {
  140. return $token;
  141. } elseif ($fallback) {
  142. return $this->getKomgaToken($this->config['komgaFallbackUser'], $this->decrypt($this->config['komgaFallbackPassword']), false);
  143. } else {
  144. return false;
  145. }
  146. }
  147. public function getJellyfinToken($username, $password)
  148. {
  149. $token = null;
  150. try {
  151. $url = $this->qualifyURL($this->config['jellyfinURL']);
  152. $ssoUrl = $this->qualifyURL($this->config['jellyfinSSOURL']);
  153. $headers = array(
  154. 'X-Emby-Authorization' => 'MediaBrowser Client="Organizr Jellyfin Tab", Device="Organizr_PHP", DeviceId="Organizr_SSO", Version="1.0"',
  155. "Accept" => "application/json",
  156. "Content-Type" => "application/json",
  157. "X-Forwarded-For" => $this->userIP()
  158. );
  159. $data = array(
  160. "Username" => $username,
  161. "Pw" => $password
  162. );
  163. $endpoint = '/Users/authenticatebyname';
  164. $options = $this->requestOptions($url, 60000);
  165. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  166. if ($response->success) {
  167. $token = json_decode($response->body, true);
  168. $this->writeLog('success', 'Jellyfin Token Function - Grabbed token.', $username);
  169. $key = 'user-' . $token['User']['Id'] . '-' . $token['ServerId'];
  170. $jellyfin[$key] = json_encode($token['User']);
  171. $jellyfin['jellyfin_credentials'] = '{"Servers":[{"ManualAddress":"' . $ssoUrl . '","Id":"' . $token['ServerId'] . '","UserId":"' . $token['User']['Id'] . '","AccessToken":"' . $token['AccessToken'] . '"}]}';
  172. return $jellyfin;
  173. } else {
  174. $this->writeLog('error', 'Jellyfin Token Function - Jellyfin did not return Token', $username);
  175. }
  176. } catch (Requests_Exception $e) {
  177. $this->writeLog('error', 'Jellyfin Token Function - Error: ' . $e->getMessage(), $username);
  178. }
  179. return false;
  180. }
  181. public function getOmbiToken($username, $password, $oAuthToken = null, $fallback = false)
  182. {
  183. $token = null;
  184. try {
  185. $url = $this->qualifyURL($this->config['ombiURL']);
  186. $headers = array(
  187. "Accept" => "application/json",
  188. "Content-Type" => "application/json",
  189. "X-Forwarded-For" => $this->userIP()
  190. );
  191. $data = array(
  192. "username" => ($oAuthToken ? "" : $username),
  193. "password" => ($oAuthToken ? "" : $password),
  194. "rememberMe" => "true",
  195. "plexToken" => $oAuthToken
  196. );
  197. $endpoint = ($oAuthToken) ? '/api/v1/Token/plextoken' : '/api/v1/Token';
  198. $options = $this->requestOptions($url, 60000);
  199. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  200. if ($response->success) {
  201. $token = json_decode($response->body, true)['access_token'];
  202. $this->writeLog('success', 'Ombi Token Function - Grabbed token.', $username);
  203. } else {
  204. if ($fallback) {
  205. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token - Will retry using fallback credentials', $username);
  206. } else {
  207. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token', $username);
  208. }
  209. }
  210. } catch (Requests_Exception $e) {
  211. $this->writeLog('error', 'Ombi Token Function - Error: ' . $e->getMessage(), $username);
  212. }
  213. if ($token) {
  214. return $token;
  215. } elseif ($fallback) {
  216. return $this->getOmbiToken($this->config['ombiFallbackUser'], $this->decrypt($this->config['ombiFallbackPassword']), null, false);
  217. } else {
  218. return false;
  219. }
  220. }
  221. public function getTautulliToken($username, $password, $plexToken = null)
  222. {
  223. $token = null;
  224. $tautulliURLList = explode(',', $this->config['tautulliURL']);
  225. if (count($tautulliURLList) !== 0) {
  226. foreach ($tautulliURLList as $key => $value) {
  227. try {
  228. $url = $this->qualifyURL($value);
  229. $headers = array(
  230. "Accept" => "application/json",
  231. "Content-Type" => "application/x-www-form-urlencoded",
  232. "User-Agent" => $_SERVER ['HTTP_USER_AGENT'] ?? null,
  233. "X-Forwarded-For" => $this->userIP()
  234. );
  235. $data = array(
  236. "username" => ($plexToken ? "" : $username),
  237. "password" => ($plexToken ? "" : $password),
  238. "token" => $plexToken,
  239. "remember_me" => 1,
  240. );
  241. $options = $this->requestOptions($url, 60000);
  242. $response = Requests::post($url . '/auth/signin', $headers, $data, $options);
  243. if ($response->success) {
  244. $qualifiedURL = $this->qualifyURL($url, true);
  245. $path = ($qualifiedURL['path']) ? $qualifiedURL['path'] : '/';
  246. $token[$key]['token'] = json_decode($response->body, true)['token'];
  247. $token[$key]['uuid'] = json_decode($response->body, true)['uuid'];
  248. $token[$key]['path'] = $path;
  249. $this->writeLog('success', 'Tautulli Token Function - Grabbed token from: ' . $url, $username);
  250. } else {
  251. $this->writeLog('error', 'Tautulli Token Function - Error on URL: ' . $url, $username);
  252. }
  253. } catch (Requests_Exception $e) {
  254. $this->writeLog('error', 'Tautulli Token Function - Error: [' . $url . ']' . $e->getMessage(), $username);
  255. }
  256. }
  257. }
  258. return ($token) ? $token : false;
  259. }
  260. public function getOverseerrToken($email, $password, $oAuthToken = null, $fallback = false)
  261. {
  262. $token = null;
  263. try {
  264. $url = $this->qualifyURL($this->config['overseerrURL']);
  265. $headers = array(
  266. "Content-Type" => "application/json",
  267. "X-Forwarded-For" => $this->userIP()
  268. );
  269. $data = array(
  270. "email" => ($oAuthToken ? "" : $email), // not needed yet
  271. "password" => ($oAuthToken ? "" : $password), // not needed yet
  272. "authToken" => $oAuthToken
  273. );
  274. $endpoint = ($oAuthToken ? '/api/v1/auth/plex' : '/api/v1/auth/local');
  275. $options = $this->requestOptions($url, 60000);
  276. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  277. if ($response->success) {
  278. $user = json_decode($response->body, true); // not really needed yet
  279. $token = $response->cookies['connect.sid']->value;
  280. $this->writeLog('success', 'Overseerr Token Function - Grabbed token', $user['plexUsername'] ?? $email);
  281. } else {
  282. if ($fallback) {
  283. $this->writeLog('error', 'Overseerr Token Function - Overseerr did not return Token - Will retry using fallback credentials', $email);
  284. } else {
  285. $this->writeLog('error', 'Overseerr Token Function - Overseerr did not return Token', $email);
  286. }
  287. }
  288. } catch (Requests_Exception $e) {
  289. $this->writeLog('error', 'Overseerr Token Function - Error: ' . $e->getMessage(), $email);
  290. }
  291. if ($token) {
  292. return urldecode($token);
  293. } elseif ($fallback) {
  294. return $this->getOverseerrToken($this->config['overseerrFallbackUser'], $this->decrypt($this->config['overseerrFallbackPassword']), null, false);
  295. } else {
  296. return false;
  297. }
  298. }
  299. public function getPetioToken($username, $password, $oAuthToken = null, $fallback = false)
  300. {
  301. $token = null;
  302. try {
  303. $url = $this->qualifyURL($this->config['petioURL']);
  304. $headers = array(
  305. "Content-Type" => "application/json",
  306. "X-Forwarded-For" => $this->userIP()
  307. );
  308. $data = array(
  309. 'user' => [
  310. 'username' => ($oAuthToken ? '' : $username),
  311. 'password' => ($oAuthToken ? '' : $password),
  312. 'type' => 1,
  313. ],
  314. 'authToken' => false,
  315. 'token' => $oAuthToken
  316. );
  317. $endpoint = ($oAuthToken) ? '/api/login/plex_login' : '/api/login';
  318. $options = $this->requestOptions($url, 60000);
  319. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  320. if ($response->success) {
  321. $user = json_decode($response->body, true)['user'];
  322. $token = json_decode($response->body, true)['token'];
  323. $this->writeLog('success', 'Petio Token Function - Grabbed token', $user['username']);
  324. } else {
  325. if ($fallback) {
  326. $this->writeLog('error', 'Petio Token Function - Petio did not return Token - Will retry using fallback credentials', $username);
  327. } else {
  328. $this->writeLog('error', 'Petio Token Function - Petio did not return Token', $username);
  329. }
  330. }
  331. } catch (Requests_Exception $e) {
  332. $this->writeLog('error', 'Petio Token Function - Error: ' . $e->getMessage(), $username);
  333. }
  334. if ($token) {
  335. return $token;
  336. } elseif ($fallback) {
  337. return $this->getPetioToken($this->config['petioFallbackUser'], $this->decrypt($this->config['petioFallbackPassword']), null, false);
  338. } else {
  339. return false;
  340. }
  341. }
  342. }