[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/lusitanian/oauth/src/OAuth/OAuth2/Service/ -> Buffer.php (source)

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


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1