sso-functions.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 $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. $headers = array(
  54. 'X-Emby-Authorization' => 'MediaBrowser Client="Organizr Jellyfin Tab", Device="Organizr_PHP", DeviceId="Organizr_SSO", Version="1.0"',
  55. "Accept" => "application/json",
  56. "Content-Type" => "application/json"
  57. );
  58. $data = array(
  59. "Username" => $username,
  60. "Pw" => $password
  61. );
  62. $endpoint = '/Users/authenticatebyname';
  63. $options = $this->requestOptions($url, false, 60);
  64. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  65. if ($response->success) {
  66. $token = json_decode($response->body, true);
  67. $this->writeLog('success', 'Jellyfin Token Function - Grabbed token.', $username);
  68. } else {
  69. $this->writeLog('error', 'Jellyfin Token Function - Jellyfin did not return Token', $username);
  70. }
  71. } catch (Requests_Exception $e) {
  72. $this->writeLog('error', 'Jellyfin Token Function - Error: ' . $e->getMessage(), $username);
  73. }
  74. return '{"Servers":[{"ManualAddress":"' . $url . '","Id":"' . $token['ServerId'] . '","UserId":"' . $token['User']['Id'] . '","AccessToken":"' . $token['AccessToken'] . '"}]}';
  75. }
  76. public function getOmbiToken($username, $password, $oAuthToken = null, $fallback = false)
  77. {
  78. $token = null;
  79. try {
  80. $url = $this->qualifyURL($this->config['ombiURL']);
  81. $headers = array(
  82. "Accept" => "application/json",
  83. "Content-Type" => "application/json"
  84. );
  85. $data = array(
  86. "username" => ($oAuthToken ? "" : $username),
  87. "password" => ($oAuthToken ? "" : $password),
  88. "rememberMe" => "true",
  89. "plexToken" => $oAuthToken
  90. );
  91. $endpoint = ($oAuthToken) ? '/api/v1/Token/plextoken' : '/api/v1/Token';
  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)['access_token'];
  96. $this->writeLog('success', 'Ombi Token Function - Grabbed token.', $username);
  97. } else {
  98. if ($fallback) {
  99. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token - Will retry using fallback credentials', $username);
  100. } else {
  101. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token', $username);
  102. }
  103. }
  104. } catch (Requests_Exception $e) {
  105. $this->writeLog('error', 'Ombi Token Function - Error: ' . $e->getMessage(), $username);
  106. }
  107. if ($token) {
  108. return $token;
  109. } elseif ($fallback) {
  110. return $this->getOmbiToken($this->config['ombiFallbackUser'], $this->decrypt($this->config['ombiFallbackPassword']), null, false);
  111. } else {
  112. return false;
  113. }
  114. }
  115. public function getTautulliToken($username, $password, $plexToken = null)
  116. {
  117. $token = null;
  118. $tautulliURLList = explode(',', $this->config['tautulliURL']);
  119. if (count($tautulliURLList) !== 0) {
  120. foreach ($tautulliURLList as $key => $value) {
  121. try {
  122. $url = $this->qualifyURL($value);
  123. $headers = array(
  124. "Accept" => "application/json",
  125. "Content-Type" => "application/x-www-form-urlencoded",
  126. "User-Agent" => isset($_SERVER ['HTTP_USER_AGENT']) ? $_SERVER ['HTTP_USER_AGENT'] : null
  127. );
  128. $data = array(
  129. "username" => ($plexToken ? "" : $username),
  130. "password" => ($plexToken ? "" : $password),
  131. "token" => $plexToken,
  132. "remember_me" => 1,
  133. );
  134. $options = $this->requestOptions($url, false, 60);
  135. $response = Requests::post($url . '/auth/signin', $headers, $data, $options);
  136. if ($response->success) {
  137. $qualifiedURL = $this->qualifyURL($url, true);
  138. $path = ($qualifiedURL['path']) ? $qualifiedURL['path'] : '/';
  139. $token[$key]['token'] = json_decode($response->body, true)['token'];
  140. $token[$key]['uuid'] = json_decode($response->body, true)['uuid'];
  141. $token[$key]['path'] = $path;
  142. $this->writeLog('success', 'Tautulli Token Function - Grabbed token from: ' . $url, $username);
  143. } else {
  144. $this->writeLog('error', 'Tautulli Token Function - Error on URL: ' . $url, $username);
  145. }
  146. } catch (Requests_Exception $e) {
  147. $this->writeLog('error', 'Tautulli Token Function - Error: [' . $url . ']' . $e->getMessage(), $username);
  148. }
  149. }
  150. }
  151. return ($token) ? $token : false;
  152. }
  153. public function getOverseerrToken($username, $password, $oAuthToken = null, $fallback = false)
  154. {
  155. $token = null;
  156. try {
  157. $url = $this->qualifyURL($this->config['overseerrURL']);
  158. $headers = array(
  159. "Content-Type" => "application/json"
  160. );
  161. $data = array(
  162. //"username" => ($oAuthToken ? "" : $username), // not needed yet
  163. //"password" => ($oAuthToken ? "" : $password), // not needed yet
  164. "authToken" => $oAuthToken
  165. );
  166. $endpoint = '/api/v1/auth/login';
  167. $options = $this->requestOptions($url, false, 60);
  168. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  169. if ($response->success) {
  170. $user = json_decode($response->body, true); // not really needed yet
  171. $token = $response->cookies['connect.sid']->value;
  172. $this->writeLog('success', 'Overseerr Token Function - Grabbed token', $user['username']);
  173. } else {
  174. if ($fallback) {
  175. $this->writeLog('error', 'Overseerr Token Function - Overseerr did not return Token - Will retry using fallback credentials', $username);
  176. } else {
  177. $this->writeLog('error', 'Overseerr Token Function - Overseerr did not return Token', $username);
  178. }
  179. }
  180. } catch (Requests_Exception $e) {
  181. $this->writeLog('error', 'Overseerr Token Function - Error: ' . $e->getMessage(), $username);
  182. }
  183. if ($token) {
  184. return urldecode($token);
  185. } elseif ($fallback) {
  186. return $this->getOverseerrToken($this->config['overseerrFallbackUser'], $this->decrypt($this->config['overseerrFallbackPassword']), null, false);
  187. } else {
  188. return false;
  189. }
  190. }
  191. }