1<?php 2 3namespace OAuth\OAuth2\Service; 4 5use OAuth\OAuth2\Service\Exception\InvalidServiceConfigurationException; 6use OAuth\OAuth2\Token\StdOAuth2Token; 7use OAuth\Common\Http\Exception\TokenResponseException; 8use OAuth\Common\Http\Uri\Uri; 9use OAuth\Common\Consumer\CredentialsInterface; 10use OAuth\Common\Http\Client\ClientInterface; 11use OAuth\Common\Storage\TokenStorageInterface; 12use OAuth\Common\Http\Uri\UriInterface; 13 14class Generic extends AbstractService 15{ 16 17 protected $authorizationEndpoint = null; 18 19 protected $accessTokenEndpoint = null; 20 21 /** 22 * {@inheritdoc} 23 */ 24 public function getAuthorizationEndpoint() 25 { 26 if(is_null($this->authorizationEndpoint)) { 27 throw new InvalidServiceConfigurationException('No AuthorizationEndpoint set'); 28 } 29 return $this->authorizationEndpoint; 30 } 31 32 /** 33 * Set the authorization endpoint. 34 * 35 * has to be called before using the service 36 * 37 * @param $url 38 */ 39 public function setAuthorizationEndpoint($url) 40 { 41 $this->authorizationEndpoint = new Uri($url); 42 } 43 44 /** 45 * {@inheritdoc} 46 */ 47 public function getAccessTokenEndpoint() 48 { 49 if(is_null($this->accessTokenEndpoint)) { 50 throw new InvalidServiceConfigurationException('No AccessTokenEndpoint set'); 51 } 52 return $this->accessTokenEndpoint; 53 } 54 55 /** 56 * Set the access token endpoint. 57 * 58 * has to be called before using the service 59 * 60 * @param $url 61 */ 62 public function setAccessTokenEndpoint($url) 63 { 64 $this->accessTokenEndpoint = new Uri($url); 65 } 66 67 /** 68 * {@inheritdoc} 69 */ 70 protected function getAuthorizationMethod() 71 { 72 return static::AUTHORIZATION_METHOD_QUERY_STRING; 73 } 74 75 /** 76 * {@inheritdoc} 77 */ 78 protected function parseAccessTokenResponse($responseBody) 79 { 80 $data = json_decode($responseBody, true); 81 82 if (null === $data || !is_array($data)) { 83 throw new TokenResponseException('Unable to parse response.'); 84 } elseif (isset($data['error'])) { 85 throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); 86 } 87 88 $token = new StdOAuth2Token(); 89 $token->setAccessToken($data['access_token']); 90 91 if (isset($data['expires'])) { 92 $token->setLifeTime($data['expires']); 93 } 94 95 if (isset($data['refresh_token'])) { 96 $token->setRefreshToken($data['refresh_token']); 97 unset($data['refresh_token']); 98 } 99 100 unset($data['access_token']); 101 unset($data['expires']); 102 103 $token->setExtraParams($data); 104 105 return $token; 106 } 107} 108