sso-functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. trait SSOFunctions
  3. {
  4. public function ssoCookies()
  5. {
  6. $cookies = array(
  7. 'myPlexAccessToken' => isset($_COOKIE['mpt']) ? $_COOKIE['mpt'] : false,
  8. 'id_token' => isset($_COOKIE['Auth']) ? $_COOKIE['Auth'] : false,
  9. 'jellyfin_credentials' => isset($_COOKIE['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' => 'username',
  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. $overseerrToken = $this->getOverseerrToken($this->getSSOUserFor('overseerr', $userobj), $password, $token);
  63. if ($overseerrToken) {
  64. $this->coookie('set', 'connect.sid', $overseerrToken, $this->config['rememberMeDays'], false);
  65. }
  66. }
  67. if ($this->config['ssoPetio']) {
  68. $fallback = ($this->config['petioFallbackUser'] !== '' && $this->config['petioFallbackPassword'] !== '');
  69. $petioToken = $this->getPetioToken($this->getSSOUserFor('petio', $userobj), $password, $token, $fallback);
  70. if ($petioToken) {
  71. $this->coookie('set', 'petio_jwt', $petioToken, $this->config['rememberMeDays'], false);
  72. }
  73. }
  74. return true;
  75. }
  76. public function getJellyfinToken($username, $password)
  77. {
  78. $token = null;
  79. try {
  80. $url = $this->qualifyURL($this->config['jellyfinURL']);
  81. $ssoUrl = $this->qualifyURL($this->config['jellyfinSSOURL']);
  82. $headers = array(
  83. 'X-Emby-Authorization' => 'MediaBrowser Client="Organizr Jellyfin Tab", Device="Organizr_PHP", DeviceId="Organizr_SSO", Version="1.0"',
  84. "Accept" => "application/json",
  85. "Content-Type" => "application/json"
  86. );
  87. $data = array(
  88. "Username" => $username,
  89. "Pw" => $password
  90. );
  91. $endpoint = '/Users/authenticatebyname';
  92. $options = $this->requestOptions($url, false, 60);
  93. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  94. if ($response->success) {
  95. $token = json_decode($response->body, true);
  96. $this->writeLog('success', 'Jellyfin Token Function - Grabbed token.', $username);
  97. $key = 'user-' . $token['User']['Id'] . '-' . $token['ServerId'];
  98. $jellyfin[$key] = json_encode($token['User']);
  99. $jellyfin['jellyfin_credentials'] = '{"Servers":[{"ManualAddress":"' . $ssoUrl . '","Id":"' . $token['ServerId'] . '","UserId":"' . $token['User']['Id'] . '","AccessToken":"' . $token['AccessToken'] . '"}]}';
  100. return $jellyfin;
  101. } else {
  102. $this->writeLog('error', 'Jellyfin Token Function - Jellyfin did not return Token', $username);
  103. }
  104. } catch (Requests_Exception $e) {
  105. $this->writeLog('error', 'Jellyfin Token Function - Error: ' . $e->getMessage(), $username);
  106. }
  107. return false;
  108. }
  109. public function getOmbiToken($username, $password, $oAuthToken = null, $fallback = false)
  110. {
  111. $token = null;
  112. try {
  113. $url = $this->qualifyURL($this->config['ombiURL']);
  114. $headers = array(
  115. "Accept" => "application/json",
  116. "Content-Type" => "application/json"
  117. );
  118. $data = array(
  119. "username" => ($oAuthToken ? "" : $username),
  120. "password" => ($oAuthToken ? "" : $password),
  121. "rememberMe" => "true",
  122. "plexToken" => $oAuthToken
  123. );
  124. $endpoint = ($oAuthToken) ? '/api/v1/Token/plextoken' : '/api/v1/Token';
  125. $options = $this->requestOptions($url, false, 60);
  126. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  127. if ($response->success) {
  128. $token = json_decode($response->body, true)['access_token'];
  129. $this->writeLog('success', 'Ombi Token Function - Grabbed token.', $username);
  130. } else {
  131. if ($fallback) {
  132. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token - Will retry using fallback credentials', $username);
  133. } else {
  134. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token', $username);
  135. }
  136. }
  137. } catch (Requests_Exception $e) {
  138. $this->writeLog('error', 'Ombi Token Function - Error: ' . $e->getMessage(), $username);
  139. }
  140. if ($token) {
  141. return $token;
  142. } elseif ($fallback) {
  143. return $this->getOmbiToken($this->config['ombiFallbackUser'], $this->decrypt($this->config['ombiFallbackPassword']), null, false);
  144. } else {
  145. return false;
  146. }
  147. }
  148. public function getTautulliToken($username, $password, $plexToken = null)
  149. {
  150. $token = null;
  151. $tautulliURLList = explode(',', $this->config['tautulliURL']);
  152. if (count($tautulliURLList) !== 0) {
  153. foreach ($tautulliURLList as $key => $value) {
  154. try {
  155. $url = $this->qualifyURL($value);
  156. $headers = array(
  157. "Accept" => "application/json",
  158. "Content-Type" => "application/x-www-form-urlencoded",
  159. "User-Agent" => isset($_SERVER ['HTTP_USER_AGENT']) ? $_SERVER ['HTTP_USER_AGENT'] : null
  160. );
  161. $data = array(
  162. "username" => ($plexToken ? "" : $username),
  163. "password" => ($plexToken ? "" : $password),
  164. "token" => $plexToken,
  165. "remember_me" => 1,
  166. );
  167. $options = $this->requestOptions($url, false, 60);
  168. $response = Requests::post($url . '/auth/signin', $headers, $data, $options);
  169. if ($response->success) {
  170. $qualifiedURL = $this->qualifyURL($url, true);
  171. $path = ($qualifiedURL['path']) ? $qualifiedURL['path'] : '/';
  172. $token[$key]['token'] = json_decode($response->body, true)['token'];
  173. $token[$key]['uuid'] = json_decode($response->body, true)['uuid'];
  174. $token[$key]['path'] = $path;
  175. $this->writeLog('success', 'Tautulli Token Function - Grabbed token from: ' . $url, $username);
  176. } else {
  177. $this->writeLog('error', 'Tautulli Token Function - Error on URL: ' . $url, $username);
  178. }
  179. } catch (Requests_Exception $e) {
  180. $this->writeLog('error', 'Tautulli Token Function - Error: [' . $url . ']' . $e->getMessage(), $username);
  181. }
  182. }
  183. }
  184. return ($token) ? $token : false;
  185. }
  186. public function getOverseerrToken($username, $password, $oAuthToken = null, $fallback = false)
  187. {
  188. $token = null;
  189. try {
  190. $url = $this->qualifyURL($this->config['overseerrURL']);
  191. $headers = array(
  192. "Content-Type" => "application/json"
  193. );
  194. $data = array(
  195. //"username" => ($oAuthToken ? "" : $username), // not needed yet
  196. //"password" => ($oAuthToken ? "" : $password), // not needed yet
  197. "authToken" => $oAuthToken
  198. );
  199. $endpoint = '/api/v1/auth/plex';
  200. $options = $this->requestOptions($url, false, 60);
  201. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  202. if ($response->success) {
  203. $user = json_decode($response->body, true); // not really needed yet
  204. $token = $response->cookies['connect.sid']->value;
  205. $this->writeLog('success', 'Overseerr Token Function - Grabbed token', $user['plexUsername']);
  206. } else {
  207. if ($fallback) {
  208. $this->writeLog('error', 'Overseerr Token Function - Overseerr did not return Token - Will retry using fallback credentials', $username);
  209. } else {
  210. $this->writeLog('error', 'Overseerr Token Function - Overseerr did not return Token', $username);
  211. }
  212. }
  213. } catch (Requests_Exception $e) {
  214. $this->writeLog('error', 'Overseerr Token Function - Error: ' . $e->getMessage(), $username);
  215. }
  216. if ($token) {
  217. return urldecode($token);
  218. } elseif ($fallback) {
  219. return $this->getOverseerrToken($this->config['overseerrFallbackUser'], $this->decrypt($this->config['overseerrFallbackPassword']), null, false);
  220. } else {
  221. return false;
  222. }
  223. }
  224. public function getPetioToken($username, $password, $oAuthToken = null, $fallback = false)
  225. {
  226. $token = null;
  227. try {
  228. $url = $this->qualifyURL($this->config['petioURL']);
  229. $headers = array(
  230. "Content-Type" => "application/json"
  231. );
  232. $data = array(
  233. 'user' => [
  234. 'username' => ($oAuthToken ? '' : $username),
  235. 'password' => ($oAuthToken ? '' : $password),
  236. 'type' => 1,
  237. ],
  238. 'authToken' => false,
  239. 'token' => $oAuthToken
  240. );
  241. $endpoint = ($oAuthToken) ? '/api/login/plex_login' : '/api/login';
  242. $options = $this->requestOptions($url, false, 60);
  243. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  244. if ($response->success) {
  245. $user = json_decode($response->body, true)['user'];
  246. $token = json_decode($response->body, true)['token'];
  247. $this->writeLog('success', 'Petio Token Function - Grabbed token', $user['username']);
  248. } else {
  249. if ($fallback) {
  250. $this->writeLog('error', 'Petio Token Function - Petio did not return Token - Will retry using fallback credentials', $username);
  251. } else {
  252. $this->writeLog('error', 'Petio Token Function - Petio did not return Token', $username);
  253. }
  254. }
  255. } catch (Requests_Exception $e) {
  256. $this->writeLog('error', 'Petio Token Function - Error: ' . $e->getMessage(), $username);
  257. }
  258. if ($token) {
  259. return $token;
  260. } elseif ($fallback) {
  261. return $this->getPetioToken($this->config['petioFallbackUser'], $this->decrypt($this->config['petioFallbackPassword']), null, false);
  262. } else {
  263. return false;
  264. }
  265. }
  266. }