[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/carlos-mg89/oauth/src/OAuth/OAuth2/Service/ -> ParrotFlowerPower.php (source)

   1  <?php
   2  /**
   3   * ParrotFlowerPower service.
   4   *
   5   * @author  Pedro Amorim <contact@pamorim.fr>
   6   * @license http://www.opensource.org/licenses/mit-license.html MIT License
   7   *
   8   * @see    https://flowerpowerdev.parrot.com/projects/flower-power-web-service-api/wiki
   9   */
  10  
  11  namespace OAuth\OAuth2\Service;
  12  
  13  use OAuth\Common\Consumer\CredentialsInterface;
  14  use OAuth\Common\Http\Client\ClientInterface;
  15  use OAuth\Common\Http\Exception\TokenResponseException;
  16  use OAuth\Common\Http\Uri\Uri;
  17  use OAuth\Common\Http\Uri\UriInterface;
  18  use OAuth\Common\Storage\TokenStorageInterface;
  19  use OAuth\Common\Token\TokenInterface;
  20  use OAuth\OAuth2\Service\Exception\MissingRefreshTokenException;
  21  use OAuth\OAuth2\Token\StdOAuth2Token;
  22  
  23  /**
  24   * ParrotFlowerPower service.
  25   *
  26   * @author  Pedro Amorim <contact@pamorim.fr>
  27   * @license http://www.opensource.org/licenses/mit-license.html MIT License
  28   *
  29   * @see    https://flowerpowerdev.parrot.com/projects/flower-power-web-service-api/wiki
  30   */
  31  class ParrotFlowerPower extends AbstractService
  32  {
  33      public function __construct(
  34          CredentialsInterface $credentials,
  35          ClientInterface $httpClient,
  36          TokenStorageInterface $storage,
  37          $scopes = [],
  38          ?UriInterface $baseApiUri = null
  39      ) {
  40          parent::__construct(
  41              $credentials,
  42              $httpClient,
  43              $storage,
  44              $scopes,
  45              $baseApiUri,
  46              true
  47          );
  48  
  49          if (null === $baseApiUri) {
  50              $this->baseApiUri = new Uri('https://apiflowerpower.parrot.com/');
  51          }
  52      }
  53  
  54      /**
  55       * {@inheritdoc}
  56       */
  57      public function getAuthorizationEndpoint()
  58      {
  59          return new Uri($this->baseApiUri . 'oauth2/v1/authorize');
  60      }
  61  
  62      /**
  63       * {@inheritdoc}
  64       */
  65      public function getAccessTokenEndpoint()
  66      {
  67          return new Uri($this->baseApiUri . 'user/v1/authenticate');
  68      }
  69  
  70      /**
  71       * {@inheritdoc}
  72       */
  73      protected function getAuthorizationMethod()
  74      {
  75          return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  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(
  89                  'Error in retrieving token: "' . $data['error'] . '"'
  90              );
  91          }
  92  
  93          $token = new StdOAuth2Token();
  94          $token->setAccessToken($data['access_token']);
  95          $token->setLifetime($data['expires_in']);
  96  
  97          if (isset($data['refresh_token'])) {
  98              $token->setRefreshToken($data['refresh_token']);
  99              unset($data['refresh_token']);
 100          }
 101  
 102          unset($data['access_token'], $data['expires_in']);
 103  
 104          $token->setExtraParams($data);
 105  
 106          return $token;
 107      }
 108  
 109      /**
 110       * Parrot use a different endpoint for refresh a token.
 111       *
 112       * {@inheritdoc}
 113       */
 114      public function refreshAccessToken(TokenInterface $token)
 115      {
 116          $refreshToken = $token->getRefreshToken();
 117  
 118          if (empty($refreshToken)) {
 119              throw new MissingRefreshTokenException();
 120          }
 121  
 122          $parameters = [
 123              'grant_type' => 'refresh_token',
 124              'type' => 'web_server',
 125              'client_id' => $this->credentials->getConsumerId(),
 126              'client_secret' => $this->credentials->getConsumerSecret(),
 127              'refresh_token' => $refreshToken,
 128          ];
 129  
 130          $responseBody = $this->httpClient->retrieveResponse(
 131              new Uri($this->baseApiUri . 'user/v1/refresh'),
 132              $parameters,
 133              $this->getExtraOAuthHeaders()
 134          );
 135          $token = $this->parseAccessTokenResponse($responseBody);
 136          $this->storage->storeAccessToken($this->service(), $token);
 137  
 138          return $token;
 139      }
 140  }


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