| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Risan\OAuth1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Risan\OAuth1\Credentials\ClientCredentials; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Risan\OAuth1\Request\RequestConfigInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Risan\OAuth1\Credentials\TemporaryCredentials; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Risan\OAuth1\Credentials\CredentialsFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class OAuth1 implements OAuth1Interface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * The RequestConfigInterface instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var \Risan\OAuth1\Request\RequestConfigInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $requestConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * The HttpClientInterface instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var \Risan\OAuth1\HttpClientInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $httpClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * The CredentialsFactoryInterface instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var \Risan\OAuth1\Credentials\CredentialsFactoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $credentialsFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Create a new OAuth1 instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param \Risan\OAuth1\Request\RequestConfigInterface $requestConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param \Risan\OAuth1\HttpClientInterface $httpClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param \Risan\OAuth1\Credentials\CredentialsFactoryInterface $credentialsFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function __construct(RequestConfigInterface $requestConfig, HttpClientInterface $httpClient, CredentialsFactoryInterface $credentialsFactory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->requestConfig = $requestConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->httpClient = $httpClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->credentialsFactory = $credentialsFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 59 |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 59 |  |     public function getRequestConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 59 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 59 |  |         return $this->requestConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 59 |  |      * {@inheritDoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 | 59 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 59 |  |     public function getHttpClient() | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return $this->httpClient; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 14 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function getCredentialsFactory() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 14 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 14 |  |         return $this->credentialsFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 14 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 14 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function getTemporaryCredentials() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $response = $this->httpClient->post($this->requestConfig->getTemporaryCredentialsUrl(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             'headers' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 'Authorization' => $this->requestConfig->getTemporaryCredentialsAuthorizationHeader(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 43 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 43 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this->credentialsFactory->createTemporaryCredentialsFromResponse($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function buildAuthorizationUrl(TemporaryCredentials $temporaryCredentials) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 19 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $this->requestConfig->buildAuthorizationUrl($temporaryCredentials); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 19 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 19 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 19 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 19 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, $temporaryIdentifier, $verificationCode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         if ($temporaryCredentials->getIdentifier() !== $temporaryIdentifier) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             throw new InvalidArgumentException('The given temporary identifier does not match the temporary credentials.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $response = $this->httpClient->post($this->requestConfig->getTokenCredentialsUrl(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 16 |  |             'headers' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 'Authorization' => $this->requestConfig->getTokenCredentialsAuthorizationHeader($temporaryCredentials, $verificationCode), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 16 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             'form_params' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 'oauth_verifier' => $verificationCode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  |