sso-functions.php 2.9 KB

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