sso-functions.php 5.5 KB

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