| 1 |  |  | <?php namespace Bogstag\OAuth2\Client\Provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use League\OAuth2\Client\Provider\Exception\IdentityProviderException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use League\OAuth2\Client\Token\AccessToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use League\OAuth2\Client\Tool\BearerAuthorizationTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class Trakt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package Bogstag\OAuth2\Client\Provider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Trakt extends AbstractExtendedProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     use BearerAuthorizationTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $baseUrlApi = 'https://api.trakt.tv'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $baseUrl = 'https://trakt.tv'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $traktApiVersion = 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Get authorization url to begin OAuth flow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * As noted in api docs you should use the normal url not the api url. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function getBaseAuthorizationUrl() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 6 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return $this->baseUrl.'/oauth/authorize'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @inheritDoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     public function getBaseAccessTokenUrl(array $params) | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 12 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         return $this->baseUrlApi.'/oauth/token'; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function getBaseRevokeAccessTokenUrl() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return $this->baseUrlApi.'/oauth/revoke'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function getResourceOwnerDetailsUrl(AccessToken $token) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 9 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return $this->baseUrlApi.'/users/settings'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 9 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |      * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 3 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |     public function getHeaders($token = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |         $headers = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if ($token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 9 |  |             $headers = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 'Content-Type'      => 'application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 'trakt-api-version' => $this->traktApiVersion, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 'trakt-api-key'     => $this->clientId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 6 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return array_merge(parent::getHeaders($token), $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     protected function getDefaultScopes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 9 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  |      * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 2 |  |     protected function checkResponse(ResponseInterface $response, $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 6 |  |         if (isset($data['error'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             throw new IdentityProviderException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 (isset($data['error']['message']) ? $data['error']['message'] : $response->getReasonPhrase()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $response->getStatusCode(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 3 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     protected function createResourceOwner(array $response, AccessToken $token) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return new TraktResourceOwner($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  |