ParrotFlowerPower.php 3.9 KB

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