sso-functions.php 3.5 KB

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