[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

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


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1