4
0

oauth.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. trait OAuthFunctions
  3. {
  4. public function traktOAuth()
  5. {
  6. $provider = new Bogstag\OAuth2\Client\Provider\Trakt(
  7. [
  8. 'clientId' => $this->config['traktClientId'],
  9. 'clientSecret' => $this->config['traktClientSecret'],
  10. 'redirectUri' => $this->getServerPath() . 'api/v2/oauth/trakt'
  11. ],
  12. [
  13. 'httpClient' => new GuzzleHttp\Client(['verify' => $this->getCert()]),
  14. ]
  15. );
  16. if (!isset($_GET['code'])) {
  17. $authUrl = $provider->getAuthorizationUrl();
  18. header('Location: ' . $authUrl);
  19. exit;
  20. } elseif (empty($_GET['state'])) {
  21. exit('Invalid state');
  22. } else {
  23. try {
  24. $token = $provider->getAccessToken('authorization_code', [
  25. 'code' => $_GET['code']
  26. ]);
  27. $traktDetails = [
  28. 'traktAccessToken' => $token->getToken(),
  29. 'traktAccessTokenExpires' => gmdate('Y-m-d\TH:i:s\Z', $token->getExpires()),
  30. 'traktRefreshToken' => $token->getRefreshToken()
  31. ];
  32. $this->updateConfig($traktDetails);
  33. echo '
  34. <!DOCTYPE html>
  35. <html lang="en">
  36. <head>
  37. <link rel="stylesheet" href="' . $this->getServerPath() . '/css/mvp.css">
  38. <meta charset="utf-8">
  39. <meta name="description" content="Trakt OAuth">
  40. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  41. <title>Trakt OAuth</title>
  42. </head>
  43. <script language=javascript>
  44. function closemyself() {
  45. window.opener=self;
  46. window.close();
  47. }
  48. </script>
  49. <body onLoad="setTimeout(\'closemyself()\',3000);">
  50. <main>
  51. <section>
  52. <aside>
  53. <h3>Details Saved</h3>
  54. <p><sup>(This window will close automatically)</sup></p>
  55. </aside>
  56. </section>
  57. </main>
  58. </body>
  59. </html>
  60. ';
  61. exit;
  62. } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
  63. exit($e->getMessage());
  64. }
  65. }
  66. }
  67. public function traktOAuthRefresh()
  68. {
  69. $exp = $this->config['traktAccessTokenExpires'];
  70. $exp = date('Y-m-d\TH:i:s\Z', strtotime($exp . ' - 30 days'));
  71. if (time() - 2592000 > strtotime($exp)) {
  72. $headers = [
  73. 'Content-Type' => 'application/json'
  74. ];
  75. $data = [
  76. 'refresh_token' => $this->config['traktRefreshToken'],
  77. 'clientId' => $this->config['traktClientId'],
  78. 'clientSecret' => $this->config['traktClientSecret'],
  79. 'redirectUri' => $this->getServerPath() . 'api/v2/oauth/trakt',
  80. 'grant_type' => 'refresh_token'
  81. ];
  82. $url = $this->qualifyURL('https://api.trakt.tv/oauth/token');
  83. try {
  84. $response = Requests::post($url, $headers, json_encode($data), []);
  85. if ($response->success) {
  86. $data = json_decode($response->body, true);
  87. $newExp = date('Y-m-d\TH:i:s\Z', strtotime($this->currentTime . ' + 90 days'));
  88. $traktDetails = [
  89. 'traktAccessToken' => $data['access_token'],
  90. 'traktAccessTokenExpires' => $newExp,
  91. 'traktRefreshToken' => $data['refresh_token']
  92. ];
  93. $this->updateConfig($traktDetails);
  94. return true;
  95. }
  96. } catch (Requests_Exception $e) {
  97. $this->setLoggerChannel('Trakt')->error($e);
  98. $this->setResponse(500, $e->getMessage());
  99. return false;
  100. }
  101. }
  102. }
  103. }