Amazon.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\OAuth2\Token\StdOAuth2Token;
  4. use OAuth\Common\Http\Exception\TokenResponseException;
  5. use OAuth\Common\Http\Uri\Uri;
  6. use OAuth\Common\Consumer\CredentialsInterface;
  7. use OAuth\Common\Http\Client\ClientInterface;
  8. use OAuth\Common\Storage\TokenStorageInterface;
  9. use OAuth\Common\Http\Uri\UriInterface;
  10. /**
  11. * Amazon service.
  12. *
  13. * @author Flávio Heleno <flaviohbatista@gmail.com>
  14. * @link https://images-na.ssl-images-amazon.com/images/G/01/lwa/dev/docs/website-developer-guide._TTH_.pdf
  15. */
  16. class Amazon extends AbstractService
  17. {
  18. /**
  19. * Defined scopes
  20. * @link https://images-na.ssl-images-amazon.com/images/G/01/lwa/dev/docs/website-developer-guide._TTH_.pdf
  21. */
  22. const SCOPE_PROFILE = 'profile';
  23. const SCOPE_POSTAL_CODE = 'postal_code';
  24. public function __construct(
  25. CredentialsInterface $credentials,
  26. ClientInterface $httpClient,
  27. TokenStorageInterface $storage,
  28. $scopes = array(),
  29. UriInterface $baseApiUri = null
  30. ) {
  31. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  32. if (null === $baseApiUri) {
  33. $this->baseApiUri = new Uri('https://api.amazon.com/');
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getAuthorizationEndpoint()
  40. {
  41. return new Uri('https://www.amazon.com/ap/oa');
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getAccessTokenEndpoint()
  47. {
  48. return new Uri('https://www.amazon.com/ap/oatoken');
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function getAuthorizationMethod()
  54. {
  55. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. protected function parseAccessTokenResponse($responseBody)
  61. {
  62. $data = json_decode($responseBody, true);
  63. if (null === $data || !is_array($data)) {
  64. throw new TokenResponseException('Unable to parse response.');
  65. } elseif (isset($data['error_description'])) {
  66. throw new TokenResponseException('Error in retrieving token: "' . $data['error_description'] . '"');
  67. } elseif (isset($data['error'])) {
  68. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  69. }
  70. $token = new StdOAuth2Token();
  71. $token->setAccessToken($data['access_token']);
  72. $token->setLifeTime($data['expires_in']);
  73. if (isset($data['refresh_token'])) {
  74. $token->setRefreshToken($data['refresh_token']);
  75. unset($data['refresh_token']);
  76. }
  77. unset($data['access_token']);
  78. unset($data['expires_in']);
  79. $token->setExtraParams($data);
  80. return $token;
  81. }
  82. }