Buffer.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\OAuth2\Token\StdOAuth2Token;
  4. use OAuth\Common\Http\Exception\TokenResponseException;
  5. use OAuth\Common\Http\Uri\Uri;
  6. use OAuth\Common\Consumer\CredentialsInterface;
  7. use OAuth\Common\Http\Uri\UriInterface;
  8. use OAuth\Common\Storage\TokenStorageInterface;
  9. use OAuth\Common\Http\Client\ClientInterface;
  10. /**
  11. * Buffer API.
  12. * @author Sumukh Sridhara <@sumukhsridhara>
  13. * @link https://bufferapp.com/developers/api
  14. */
  15. class Buffer extends AbstractService
  16. {
  17. public function __construct(
  18. CredentialsInterface $credentials,
  19. ClientInterface $httpClient,
  20. TokenStorageInterface $storage,
  21. $scopes = array(),
  22. UriInterface $baseApiUri = null
  23. ) {
  24. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  25. if ($baseApiUri === null) {
  26. $this->baseApiUri = new Uri('https://api.bufferapp.com/1/');
  27. }
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getAuthorizationEndpoint()
  33. {
  34. return new Uri('https://bufferapp.com/oauth2/authorize');
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getAccessTokenEndpoint()
  40. {
  41. return new Uri('https://api.bufferapp.com/1/oauth2/token.json');
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function getAuthorizationMethod()
  47. {
  48. return static::AUTHORIZATION_METHOD_QUERY_STRING;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getAuthorizationUri(array $additionalParameters = array())
  54. {
  55. $parameters = array_merge(
  56. $additionalParameters,
  57. array(
  58. 'client_id' => $this->credentials->getConsumerId(),
  59. 'redirect_uri' => $this->credentials->getCallbackUrl(),
  60. 'response_type' => 'code',
  61. )
  62. );
  63. // Build the url
  64. $url = clone $this->getAuthorizationEndpoint();
  65. foreach ($parameters as $key => $val) {
  66. $url->addToQuery($key, $val);
  67. }
  68. return $url;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function requestRequestToken()
  74. {
  75. $responseBody = $this->httpClient->retrieveResponse(
  76. $this->getRequestTokenEndpoint(),
  77. array(
  78. 'client_key' => $this->credentials->getConsumerId(),
  79. 'redirect_uri' => $this->credentials->getCallbackUrl(),
  80. 'response_type' => 'code',
  81. )
  82. );
  83. $code = $this->parseRequestTokenResponse($responseBody);
  84. return $code;
  85. }
  86. protected function parseRequestTokenResponse($responseBody)
  87. {
  88. parse_str($responseBody, $data);
  89. if (null === $data || !is_array($data)) {
  90. throw new TokenResponseException('Unable to parse response.');
  91. } elseif (!isset($data['code'])) {
  92. throw new TokenResponseException('Error in retrieving code.');
  93. }
  94. return $data['code'];
  95. }
  96. public function requestAccessToken($code)
  97. {
  98. $bodyParams = array(
  99. 'client_id' => $this->credentials->getConsumerId(),
  100. 'client_secret' => $this->credentials->getConsumerSecret(),
  101. 'redirect_uri' => $this->credentials->getCallbackUrl(),
  102. 'code' => $code,
  103. 'grant_type' => 'authorization_code',
  104. );
  105. $responseBody = $this->httpClient->retrieveResponse(
  106. $this->getAccessTokenEndpoint(),
  107. $bodyParams,
  108. $this->getExtraOAuthHeaders()
  109. );
  110. $token = $this->parseAccessTokenResponse($responseBody);
  111. $this->storage->storeAccessToken($this->service(), $token);
  112. return $token;
  113. }
  114. protected function parseAccessTokenResponse($responseBody)
  115. {
  116. $data = json_decode($responseBody, true);
  117. if ($data === null || !is_array($data)) {
  118. throw new TokenResponseException('Unable to parse response.');
  119. } elseif (isset($data['error'])) {
  120. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  121. }
  122. $token = new StdOAuth2Token();
  123. $token->setAccessToken($data['access_token']);
  124. $token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES);
  125. unset($data['access_token']);
  126. $token->setExtraParams($data);
  127. return $token;
  128. }
  129. }