sso-functions.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. trait SSOFunctions
  3. {
  4. public function getSSOUserFor($app, $userobj)
  5. {
  6. $map = array(
  7. 'jellyfin' => 'username',
  8. 'ombi' => 'username',
  9. 'overseerr' => 'username',
  10. 'tautulli' => 'username'
  11. );
  12. return (gettype($userobj) == 'string') ? $userobj : $userobj[$map[$app]];
  13. }
  14. public function ssoCheck($userobj, $password, $token = null)
  15. {
  16. if ($this->config['ssoPlex'] && $token) {
  17. $this->coookie('set', 'mpt', $token, $this->config['rememberMeDays'], false);
  18. }
  19. if ($this->config['ssoOmbi']) {
  20. $fallback = ($this->config['ombiFallbackUser'] !== '' && $this->config['ombiFallbackPassword'] !== '');
  21. $ombiToken = $this->getOmbiToken($this->getSSOUserFor('ombi', $userobj), $password, $token, $fallback);
  22. if ($ombiToken) {
  23. $this->coookie('set', 'Auth', $ombiToken, $this->config['rememberMeDays'], false);
  24. }
  25. }
  26. if ($this->config['ssoTautulli']) {
  27. $tautulliToken = $this->getTautulliToken($this->getSSOUserFor('tautulli', $userobj), $password, $token);
  28. if ($tautulliToken) {
  29. foreach ($tautulliToken as $key => $value) {
  30. $this->coookie('set', 'tautulli_token_' . $value['uuid'], $value['token'], $this->config['rememberMeDays'], true, $value['path']);
  31. }
  32. }
  33. }
  34. if ($this->config['ssoJellyfin']) {
  35. $jellyfinToken = $this->getJellyfinToken($this->getSSOUserFor('jellyfin', $userobj), $password);
  36. if ($jellyfinToken) {
  37. $this->coookie('set', 'jellyfin_credentials', $jellyfinToken, $this->config['rememberMeDays'], false);
  38. }
  39. }
  40. if ($this->config['ssoOverseerr']) {
  41. $overseerrToken = $this->getOverseerrToken($this->getSSOUserFor('overseerr', $userobj), $password, $token);
  42. if ($overseerrToken) {
  43. $this->coookie('set', 'connect.sid', $overseerrToken, $this->config['rememberMeDays'], false);
  44. }
  45. }
  46. return true;
  47. }
  48. public function getJellyfinToken($username, $password)
  49. {
  50. $token = null;
  51. try {
  52. $url = $this->qualifyURL($this->config['jellyfinURL']);
  53. $ssoUrl = $this->qualifyURL($this->config['jellyfinSSOURL']);
  54. $headers = array(
  55. 'X-Emby-Authorization' => 'MediaBrowser Client="Organizr Jellyfin Tab", Device="Organizr_PHP", DeviceId="Organizr_SSO", Version="1.0"',
  56. "Accept" => "application/json",
  57. "Content-Type" => "application/json"
  58. );
  59. $data = array(
  60. "Username" => $username,
  61. "Pw" => $password
  62. );
  63. $endpoint = '/Users/authenticatebyname';
  64. $options = $this->requestOptions($url, false, 60);
  65. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  66. if ($response->success) {
  67. $token = json_decode($response->body, true);
  68. $this->writeLog('success', 'Jellyfin Token Function - Grabbed token.', $username);
  69. return '{"Servers":[{"ManualAddress":"' . $ssoUrl . '","Id":"' . $token['ServerId'] . '","UserId":"' . $token['User']['Id'] . '","AccessToken":"' . $token['AccessToken'] . '"}]}';
  70. } else {
  71. $this->writeLog('error', 'Jellyfin Token Function - Jellyfin did not return Token', $username);
  72. }
  73. } catch (Requests_Exception $e) {
  74. $this->writeLog('error', 'Jellyfin Token Function - Error: ' . $e->getMessage(), $username);
  75. }
  76. return false;
  77. }
  78. public function getOmbiToken($username, $password, $oAuthToken = null, $fallback = false)
  79. {
  80. $token = null;
  81. try {
  82. $url = $this->qualifyURL($this->config['ombiURL']);
  83. $headers = array(
  84. "Accept" => "application/json",
  85. "Content-Type" => "application/json"
  86. );
  87. $data = array(
  88. "username" => ($oAuthToken ? "" : $username),
  89. "password" => ($oAuthToken ? "" : $password),
  90. "rememberMe" => "true",
  91. "plexToken" => $oAuthToken
  92. );
  93. $endpoint = ($oAuthToken) ? '/api/v1/Token/plextoken' : '/api/v1/Token';
  94. $options = $this->requestOptions($url, false, 60);
  95. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  96. if ($response->success) {
  97. $token = json_decode($response->body, true)['access_token'];
  98. $this->writeLog('success', 'Ombi Token Function - Grabbed token.', $username);
  99. } else {
  100. if ($fallback) {
  101. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token - Will retry using fallback credentials', $username);
  102. } else {
  103. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token', $username);
  104. }
  105. }
  106. } catch (Requests_Exception $e) {
  107. $this->writeLog('error', 'Ombi Token Function - Error: ' . $e->getMessage(), $username);
  108. }
  109. if ($token) {
  110. return $token;
  111. } elseif ($fallback) {
  112. return $this->getOmbiToken($this->config['ombiFallbackUser'], $this->decrypt($this->config['ombiFallbackPassword']), null, false);
  113. } else {
  114. return false;
  115. }
  116. }
  117. public function getTautulliToken($username, $password, $plexToken = null)
  118. {
  119. $token = null;
  120. $tautulliURLList = explode(',', $this->config['tautulliURL']);
  121. if (count($tautulliURLList) !== 0) {
  122. foreach ($tautulliURLList as $key => $value) {
  123. try {
  124. $url = $this->qualifyURL($value);
  125. $headers = array(
  126. "Accept" => "application/json",
  127. "Content-Type" => "application/x-www-form-urlencoded",
  128. "User-Agent" => isset($_SERVER ['HTTP_USER_AGENT']) ? $_SERVER ['HTTP_USER_AGENT'] : null
  129. );
  130. $data = array(
  131. "username" => ($plexToken ? "" : $username),
  132. "password" => ($plexToken ? "" : $password),
  133. "token" => $plexToken,
  134. "remember_me" => 1,
  135. );
  136. $options = $this->requestOptions($url, false, 60);
  137. $response = Requests::post($url . '/auth/signin', $headers, $data, $options);
  138. if ($response->success) {
  139. $qualifiedURL = $this->qualifyURL($url, true);
  140. $path = ($qualifiedURL['path']) ? $qualifiedURL['path'] : '/';
  141. $token[$key]['token'] = json_decode($response->body, true)['token'];
  142. $token[$key]['uuid'] = json_decode($response->body, true)['uuid'];
  143. $token[$key]['path'] = $path;
  144. $this->writeLog('success', 'Tautulli Token Function - Grabbed token from: ' . $url, $username);
  145. } else {
  146. $this->writeLog('error', 'Tautulli Token Function - Error on URL: ' . $url, $username);
  147. }
  148. } catch (Requests_Exception $e) {
  149. $this->writeLog('error', 'Tautulli Token Function - Error: [' . $url . ']' . $e->getMessage(), $username);
  150. }
  151. }
  152. }
  153. return ($token) ? $token : false;
  154. }
  155. public function getOverseerrToken($username, $password, $oAuthToken = null, $fallback = false)
  156. {
  157. $token = null;
  158. try {
  159. $url = $this->qualifyURL($this->config['overseerrURL']);
  160. $headers = array(
  161. "Content-Type" => "application/json"
  162. );
  163. $data = array(
  164. //"username" => ($oAuthToken ? "" : $username), // not needed yet
  165. //"password" => ($oAuthToken ? "" : $password), // not needed yet
  166. "authToken" => $oAuthToken
  167. );
  168. $endpoint = '/api/v1/auth/login';
  169. $options = $this->requestOptions($url, false, 60);
  170. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  171. if ($response->success) {
  172. $user = json_decode($response->body, true); // not really needed yet
  173. $token = $response->cookies['connect.sid']->value;
  174. $this->writeLog('success', 'Overseerr Token Function - Grabbed token', $user['username']);
  175. } else {
  176. if ($fallback) {
  177. $this->writeLog('error', 'Overseerr Token Function - Overseerr did not return Token - Will retry using fallback credentials', $username);
  178. } else {
  179. $this->writeLog('error', 'Overseerr Token Function - Overseerr did not return Token', $username);
  180. }
  181. }
  182. } catch (Requests_Exception $e) {
  183. $this->writeLog('error', 'Overseerr Token Function - Error: ' . $e->getMessage(), $username);
  184. }
  185. if ($token) {
  186. return urldecode($token);
  187. } elseif ($fallback) {
  188. return $this->getOverseerrToken($this->config['overseerrFallbackUser'], $this->decrypt($this->config['overseerrFallbackPassword']), null, false);
  189. } else {
  190. return false;
  191. }
  192. }
  193. }