sso-functions.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. return true;
  25. }
  26. public function getOmbiToken($username, $password, $oAuthToken = null, $fallback = false)
  27. {
  28. $token = null;
  29. try {
  30. $url = $this->qualifyURL($this->config['ombiURL']);
  31. $headers = array(
  32. "Accept" => "application/json",
  33. "Content-Type" => "application/json"
  34. );
  35. $data = array(
  36. "username" => ($oAuthToken ? "" : $username),
  37. "password" => ($oAuthToken ? "" : $password),
  38. "rememberMe" => "true",
  39. "plexToken" => $oAuthToken
  40. );
  41. $endpoint = ($oAuthToken) ? '/api/v1/Token/plextoken' : '/api/v1/Token';
  42. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  43. $response = Requests::post($url . $endpoint, $headers, json_encode($data), $options);
  44. if ($response->success) {
  45. $token = json_decode($response->body, true)['access_token'];
  46. $this->writeLog('success', 'Ombi Token Function - Grabbed token.', $username);
  47. } else {
  48. if ($fallback) {
  49. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token - Will retry using fallback credentials', $username);
  50. } else {
  51. $this->writeLog('error', 'Ombi Token Function - Ombi did not return Token', $username);
  52. }
  53. }
  54. } catch (Requests_Exception $e) {
  55. $this->writeLog('error', 'Ombi Token Function - Error: ' . $e->getMessage(), $username);
  56. }
  57. if ($token) {
  58. return $token;
  59. } elseif ($fallback) {
  60. return $this->getOmbiToken($this->config['ombiFallbackUser'], $this->decrypt($this->config['ombiFallbackPassword']), null, false);
  61. } else {
  62. return false;
  63. }
  64. }
  65. public function getTautulliToken($username, $password, $plexToken = null)
  66. {
  67. $token = null;
  68. $tautulliURLList = explode(',', $this->config['tautulliURL']);
  69. if (count($tautulliURLList) !== 0) {
  70. foreach ($tautulliURLList as $key => $value) {
  71. try {
  72. $url = $this->qualifyURL($value);
  73. $headers = array(
  74. "Accept" => "application/json",
  75. "Content-Type" => "application/x-www-form-urlencoded",
  76. "User-Agent" => isset($_SERVER ['HTTP_USER_AGENT']) ? $_SERVER ['HTTP_USER_AGENT'] : null
  77. );
  78. $data = array(
  79. "username" => ($plexToken ? "" : $username),
  80. "password" => ($plexToken ? "" : $password),
  81. "token" => $plexToken,
  82. "remember_me" => 1,
  83. );
  84. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  85. $response = Requests::post($url . '/auth/signin', $headers, $data, $options);
  86. if ($response->success) {
  87. $qualifiedURL = $this->qualifyURL($url, true);
  88. $path = ($qualifiedURL['path']) ? $qualifiedURL['path'] : '/';
  89. $token[$key]['token'] = json_decode($response->body, true)['token'];
  90. $token[$key]['uuid'] = json_decode($response->body, true)['uuid'];
  91. $token[$key]['path'] = $path;
  92. $this->writeLog('success', 'Tautulli Token Function - Grabbed token from: ' . $url, $username);
  93. } else {
  94. $this->writeLog('error', 'Tautulli Token Function - Error on URL: ' . $url, $username);
  95. }
  96. } catch (Requests_Exception $e) {
  97. $this->writeLog('error', 'Tautulli Token Function - Error: [' . $url . ']' . $e->getMessage(), $username);
  98. }
  99. }
  100. }
  101. return ($token) ? $token : false;
  102. }
  103. }