sso-functions.php 12 KB

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