sso-functions.php 11 KB

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