sso-functions.php 9.5 KB

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