sso-functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. if ($this->config['ssoPlex'] && $token) {
  39. $this->coookie('set', 'mpt', $token, $this->config['rememberMeDays'], false);
  40. }
  41. if ($this->config['ssoOmbi']) {
  42. $fallback = ($this->config['ombiFallbackUser'] !== '' && $this->config['ombiFallbackPassword'] !== '');
  43. $ombiToken = $this->getOmbiToken($this->getSSOUserFor('ombi', $userobj), $password, $token, $fallback);
  44. if ($ombiToken) {
  45. $this->coookie('set', 'Auth', $ombiToken, $this->config['rememberMeDays'], false);
  46. }
  47. }
  48. if ($this->config['ssoTautulli'] && $this->qualifyRequest($this->config['ssoTautulliAuth'])) {
  49. $tautulliToken = $this->getTautulliToken($this->getSSOUserFor('tautulli', $userobj), $password, $token);
  50. if ($tautulliToken) {
  51. foreach ($tautulliToken as $key => $value) {
  52. $this->coookie('set', 'tautulli_token_' . $value['uuid'], $value['token'], $this->config['rememberMeDays'], true, $value['path']);
  53. }
  54. }
  55. }
  56. if ($this->config['ssoJellyfin']) {
  57. $jellyfinToken = $this->getJellyfinToken($this->getSSOUserFor('jellyfin', $userobj), $password);
  58. if ($jellyfinToken) {
  59. foreach ($jellyfinToken as $k => $v) {
  60. $this->coookie('set', $k, $v, $this->config['rememberMeDays'], false);
  61. }
  62. }
  63. }
  64. if ($this->config['ssoOverseerr']) {
  65. $fallback = ($this->config['overseerrFallbackUser'] !== '' && $this->config['overseerrFallbackPassword'] !== '');
  66. $overseerrToken = $this->getOverseerrToken($this->getSSOUserFor('overseerr', $userobj), $password, $token, $fallback);
  67. if ($overseerrToken) {
  68. $this->coookie('set', 'connect.sid', $overseerrToken, $this->config['rememberMeDays'], false);
  69. }
  70. }
  71. if ($this->config['ssoPetio']) {
  72. $fallback = ($this->config['petioFallbackUser'] !== '' && $this->config['petioFallbackPassword'] !== '');
  73. $petioToken = $this->getPetioToken($this->getSSOUserFor('petio', $userobj), $password, $token, $fallback);
  74. if ($petioToken) {
  75. $this->coookie('set', 'petio_jwt', $petioToken, $this->config['rememberMeDays'], false);
  76. }
  77. }
  78. if ($this->config['ssoKomga'] && $this->qualifyRequest($this->config['ssoKomgaAuth'])) {
  79. $komga = $this->getKomgaToken($this->getSSOUserFor('komga', $userobj), $password);
  80. if ($komga) {
  81. $this->coookie('set', 'komga_token', $komga, $this->config['rememberMeDays'], false);
  82. }
  83. }
  84. return true;
  85. }
  86. public function getKomgaToken($email, $password)
  87. {
  88. try {
  89. $credentials = array('auth' => new Requests_Auth_Digest(array($email, $password)));
  90. $url = $this->qualifyURL($this->config['komgaURL']);
  91. $options = $this->requestOptions($url, 60000, true, false, $credentials);
  92. $response = Requests::get($url . '/api/v1/users/me', ['X-Auth-Token' => 'organizrSSO'], $options);
  93. if ($response->success) {
  94. if ($response->headers['x-auth-token']) {
  95. $this->writeLog('success', 'Komga Token Function - Grabbed token.', $email);
  96. return $response->headers['x-auth-token'];
  97. } else {
  98. $this->writeLog('error', 'Komga Token Function - Komga did not return Token', $email);
  99. }
  100. } else {
  101. $this->writeLog('error', 'Komga Token Function - Komga did not return Token', $email);
  102. }
  103. } catch (Requests_Exception $e) {
  104. $this->writeLog('error', 'Komga Token Function - Error: ' . $e->getMessage(), $email);
  105. }
  106. return false;
  107. }
  108. public function getJellyfinToken($username, $password)
  109. {
  110. $token = null;
  111. try {
  112. $url = $this->qualifyURL($this->config['jellyfinURL']);
  113. $ssoUrl = $this->qualifyURL($this->config['jellyfinSSOURL']);
  114. $headers = array(
  115. 'X-Emby-Authorization' => 'MediaBrowser Client="Organizr Jellyfin Tab", Device="Organizr_PHP", DeviceId="Organizr_SSO", Version="1.0"',
  116. "Accept" => "application/json",
  117. "Content-Type" => "application/json",
  118. "X-Forwarded-For" => $this->userIP()
  119. );
  120. $data = array(
  121. "Username" => $username,
  122. "Pw" => $password
  123. );
  124. $endpoint = '/Users/authenticatebyname';
  125. $options = $this->requestOptions($url, 60000);
  126. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  127. if ($response->success) {
  128. $token = json_decode($response->body, true);
  129. $this->writeLog('success', 'Jellyfin Token Function - Grabbed token.', $username);
  130. $key = 'user-' . $token['User']['Id'] . '-' . $token['ServerId'];
  131. $jellyfin[$key] = json_encode($token['User']);
  132. $jellyfin['jellyfin_credentials'] = '{"Servers":[{"ManualAddress":"' . $ssoUrl . '","Id":"' . $token['ServerId'] . '","UserId":"' . $token['User']['Id'] . '","AccessToken":"' . $token['AccessToken'] . '"}]}';
  133. return $jellyfin;
  134. } else {
  135. $this->writeLog('error', 'Jellyfin Token Function - Jellyfin did not return Token', $username);
  136. }
  137. } catch (Requests_Exception $e) {
  138. $this->writeLog('error', 'Jellyfin Token Function - Error: ' . $e->getMessage(), $username);
  139. }
  140. return false;
  141. }
  142. public function getOmbiToken($username, $password, $oAuthToken = null, $fallback = false)
  143. {
  144. $token = null;
  145. try {
  146. $url = $this->qualifyURL($this->config['ombiURL']);
  147. $headers = array(
  148. "Accept" => "application/json",
  149. "Content-Type" => "application/json",
  150. "X-Forwarded-For" => $this->userIP()
  151. );
  152. $data = array(
  153. "username" => ($oAuthToken ? "" : $username),
  154. "password" => ($oAuthToken ? "" : $password),
  155. "rememberMe" => "true",
  156. "plexToken" => $oAuthToken
  157. );
  158. $endpoint = ($oAuthToken) ? '/api/v1/Token/plextoken' : '/api/v1/Token';
  159. $options = $this->requestOptions($url, 60000);
  160. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  161. if ($response->success) {
  162. $token = json_decode($response->body, true)['access_token'];
  163. $this->writeLog('success', 'Ombi Token Function - Grabbed token.', $username);
  164. } else {
  165. if ($fallback) {
  166. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token - Will retry using fallback credentials', $username);
  167. } else {
  168. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token', $username);
  169. }
  170. }
  171. } catch (Requests_Exception $e) {
  172. $this->writeLog('error', 'Ombi Token Function - Error: ' . $e->getMessage(), $username);
  173. }
  174. if ($token) {
  175. return $token;
  176. } elseif ($fallback) {
  177. return $this->getOmbiToken($this->config['ombiFallbackUser'], $this->decrypt($this->config['ombiFallbackPassword']), null, false);
  178. } else {
  179. return false;
  180. }
  181. }
  182. public function getTautulliToken($username, $password, $plexToken = null)
  183. {
  184. $token = null;
  185. $tautulliURLList = explode(',', $this->config['tautulliURL']);
  186. if (count($tautulliURLList) !== 0) {
  187. foreach ($tautulliURLList as $key => $value) {
  188. try {
  189. $url = $this->qualifyURL($value);
  190. $headers = array(
  191. "Accept" => "application/json",
  192. "Content-Type" => "application/x-www-form-urlencoded",
  193. "User-Agent" => $_SERVER ['HTTP_USER_AGENT'] ?? null,
  194. "X-Forwarded-For" => $this->userIP()
  195. );
  196. $data = array(
  197. "username" => ($plexToken ? "" : $username),
  198. "password" => ($plexToken ? "" : $password),
  199. "token" => $plexToken,
  200. "remember_me" => 1,
  201. );
  202. $options = $this->requestOptions($url, 60000);
  203. $response = Requests::post($url . '/auth/signin', $headers, $data, $options);
  204. if ($response->success) {
  205. $qualifiedURL = $this->qualifyURL($url, true);
  206. $path = ($qualifiedURL['path']) ? $qualifiedURL['path'] : '/';
  207. $token[$key]['token'] = json_decode($response->body, true)['token'];
  208. $token[$key]['uuid'] = json_decode($response->body, true)['uuid'];
  209. $token[$key]['path'] = $path;
  210. $this->writeLog('success', 'Tautulli Token Function - Grabbed token from: ' . $url, $username);
  211. } else {
  212. $this->writeLog('error', 'Tautulli Token Function - Error on URL: ' . $url, $username);
  213. }
  214. } catch (Requests_Exception $e) {
  215. $this->writeLog('error', 'Tautulli Token Function - Error: [' . $url . ']' . $e->getMessage(), $username);
  216. }
  217. }
  218. }
  219. return ($token) ? $token : false;
  220. }
  221. public function getOverseerrToken($email, $password, $oAuthToken = null, $fallback = false)
  222. {
  223. $token = null;
  224. try {
  225. $url = $this->qualifyURL($this->config['overseerrURL']);
  226. $headers = array(
  227. "Content-Type" => "application/json",
  228. "X-Forwarded-For" => $this->userIP()
  229. );
  230. $data = array(
  231. "email" => ($oAuthToken ? "" : $email), // not needed yet
  232. "password" => ($oAuthToken ? "" : $password), // not needed yet
  233. "authToken" => $oAuthToken
  234. );
  235. $endpoint = ($oAuthToken ? '/api/v1/auth/plex' : '/api/v1/auth/local');
  236. $options = $this->requestOptions($url, 60000);
  237. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  238. if ($response->success) {
  239. $user = json_decode($response->body, true); // not really needed yet
  240. $token = $response->cookies['connect.sid']->value;
  241. $this->writeLog('success', 'Overseerr Token Function - Grabbed token', $user['plexUsername'] ?? $email);
  242. } else {
  243. if ($fallback) {
  244. $this->writeLog('error', 'Overseerr Token Function - Overseerr did not return Token - Will retry using fallback credentials', $email);
  245. } else {
  246. $this->writeLog('error', 'Overseerr Token Function - Overseerr did not return Token', $email);
  247. }
  248. }
  249. } catch (Requests_Exception $e) {
  250. $this->writeLog('error', 'Overseerr Token Function - Error: ' . $e->getMessage(), $email);
  251. }
  252. if ($token) {
  253. return urldecode($token);
  254. } elseif ($fallback) {
  255. return $this->getOverseerrToken($this->config['overseerrFallbackUser'], $this->decrypt($this->config['overseerrFallbackPassword']), null, false);
  256. } else {
  257. return false;
  258. }
  259. }
  260. public function getPetioToken($username, $password, $oAuthToken = null, $fallback = false)
  261. {
  262. $token = null;
  263. try {
  264. $url = $this->qualifyURL($this->config['petioURL']);
  265. $headers = array(
  266. "Content-Type" => "application/json",
  267. "X-Forwarded-For" => $this->userIP()
  268. );
  269. $data = array(
  270. 'user' => [
  271. 'username' => ($oAuthToken ? '' : $username),
  272. 'password' => ($oAuthToken ? '' : $password),
  273. 'type' => 1,
  274. ],
  275. 'authToken' => false,
  276. 'token' => $oAuthToken
  277. );
  278. $endpoint = ($oAuthToken) ? '/api/login/plex_login' : '/api/login';
  279. $options = $this->requestOptions($url, 60000);
  280. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  281. if ($response->success) {
  282. $user = json_decode($response->body, true)['user'];
  283. $token = json_decode($response->body, true)['token'];
  284. $this->writeLog('success', 'Petio Token Function - Grabbed token', $user['username']);
  285. } else {
  286. if ($fallback) {
  287. $this->writeLog('error', 'Petio Token Function - Petio did not return Token - Will retry using fallback credentials', $username);
  288. } else {
  289. $this->writeLog('error', 'Petio Token Function - Petio did not return Token', $username);
  290. }
  291. }
  292. } catch (Requests_Exception $e) {
  293. $this->writeLog('error', 'Petio Token Function - Error: ' . $e->getMessage(), $username);
  294. }
  295. if ($token) {
  296. return $token;
  297. } elseif ($fallback) {
  298. return $this->getPetioToken($this->config['petioFallbackUser'], $this->decrypt($this->config['petioFallbackPassword']), null, false);
  299. } else {
  300. return false;
  301. }
  302. }
  303. }