sso-functions.php 10 KB

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