sso-functions.php 9.6 KB

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