sso-functions.php 5.7 KB

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