[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

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

   1  <?php
   2  
   3  namespace OAuth\OAuth2\Service;
   4  
   5  use OAuth\Common\Consumer\CredentialsInterface;
   6  use OAuth\Common\Http\Client\ClientInterface;
   7  use OAuth\Common\Http\Exception\TokenResponseException;
   8  use OAuth\Common\Http\Uri\Uri;
   9  use OAuth\Common\Http\Uri\UriInterface;
  10  use OAuth\Common\Storage\TokenStorageInterface;
  11  use OAuth\Common\Token\TokenInterface;
  12  use OAuth\OAuth2\Token\StdOAuth2Token;
  13  
  14  class Harvest extends AbstractService
  15  {
  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  
  26          if (null === $baseApiUri) {
  27              $this->baseApiUri = new Uri('https://api.harvestapp.com/');
  28          }
  29      }
  30  
  31      /**
  32       * {@inheritdoc}
  33       */
  34      public function getAuthorizationUri(array $additionalParameters = array())
  35      {
  36          $parameters = array_merge(
  37              $additionalParameters,
  38              array(
  39                  'client_id'     => $this->credentials->getConsumerId(),
  40                  'redirect_uri'  => $this->credentials->getCallbackUrl(),
  41                  'state' => 'optional-csrf-token',
  42                  'response_type' => 'code',
  43              )
  44          );
  45  
  46          // Build the url
  47          $url = clone $this->getAuthorizationEndpoint();
  48          foreach ($parameters as $key => $val) {
  49              $url->addToQuery($key, $val);
  50          }
  51  
  52          return $url;
  53      }
  54  
  55      /**
  56       * {@inheritdoc}
  57       */
  58      public function getAuthorizationEndpoint()
  59      {
  60          return new Uri('https://api.harvestapp.com/oauth2/authorize');
  61      }
  62  
  63      /**
  64       * {@inheritdoc}
  65       */
  66      public function getAccessTokenEndpoint()
  67      {
  68          return new Uri('https://api.harvestapp.com/oauth2/token');
  69      }
  70  
  71      /**
  72       * {@inheritdoc}
  73       */
  74      protected function getAuthorizationMethod()
  75      {
  76          return static::AUTHORIZATION_METHOD_QUERY_STRING;
  77      }
  78  
  79      /**
  80       * {@inheritdoc}
  81       */
  82      protected function parseAccessTokenResponse($responseBody)
  83      {
  84          $data = json_decode($responseBody, true);
  85  
  86          if (null === $data || ! is_array($data)) {
  87              throw new TokenResponseException('Unable to parse response.');
  88          } elseif (isset($data['error'])) {
  89              throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  90          }
  91  
  92          $token = new StdOAuth2Token();
  93          $token->setAccessToken($data['access_token']);
  94          $token->setLifetime($data['expires_in']);
  95          $token->setRefreshToken($data['refresh_token']);
  96  
  97          unset($data['access_token']);
  98  
  99          $token->setExtraParams($data);
 100  
 101          return $token;
 102      }
 103  
 104      /**
 105       * Refreshes an OAuth2 access token.
 106       *
 107       * @param TokenInterface $token
 108       *
 109       * @return TokenInterface $token
 110       *
 111       * @throws MissingRefreshTokenException
 112       */
 113      public function refreshAccessToken(TokenInterface $token)
 114      {
 115          $refreshToken = $token->getRefreshToken();
 116  
 117          if (empty($refreshToken)) {
 118              throw new MissingRefreshTokenException();
 119          }
 120  
 121          $parameters = array(
 122              'grant_type'    => 'refresh_token',
 123              'type'          => 'web_server',
 124              'client_id'     => $this->credentials->getConsumerId(),
 125              'client_secret' => $this->credentials->getConsumerSecret(),
 126              'refresh_token' => $refreshToken,
 127          );
 128  
 129          $responseBody = $this->httpClient->retrieveResponse(
 130              $this->getAccessTokenEndpoint(),
 131              $parameters,
 132              $this->getExtraOAuthHeaders()
 133          );
 134          $token = $this->parseAccessTokenResponse($responseBody);
 135          $this->storage->storeAccessToken($this->service(), $token);
 136  
 137          return $token;
 138      }
 139  
 140      /**
 141       * @return array
 142       */
 143      protected function getExtraOAuthHeaders()
 144      {
 145          return array('Accept' => 'application/json');
 146      }
 147  
 148      /**
 149       * Return any additional headers always needed for this service implementation's API calls.
 150       *
 151       * @return array
 152       */
 153      protected function getExtraApiHeaders()
 154      {
 155          return array('Accept' => 'application/json');
 156      }
 157  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1