Hubic.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Hubic service.
  4. *
  5. * @author Pedro Amorim <contact@pamorim.fr>
  6. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  7. * @link https://api.hubic.com/docs/
  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. /**
  18. * Hubic service.
  19. *
  20. * @author Pedro Amorim <contact@pamorim.fr>
  21. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  22. * @link https://api.hubic.com/docs/
  23. */
  24. class Hubic extends AbstractService
  25. {
  26. // Scopes
  27. const SCOPE_USAGE_GET = 'usage.r';
  28. const SCOPE_ACCOUNT_GET = 'account.r';
  29. const SCOPE_GETALLLINKS_GET = 'getAllLinks.r';
  30. const SCOPE_CREDENTIALS_GET = 'credentials.r';
  31. const SCOPE_SPONSORCODE_GET = 'sponsorCode.r';
  32. const SCOPE_ACTIVATE_POST = 'activate.w';
  33. const SCOPE_SPONSORED_GET = 'sponsored.r';
  34. const SCOPE_LINKS_GET = 'links.r';
  35. const SCOPE_LINKS_POST = 'links.rw';
  36. const SCOPE_LINKS_ALL = 'links.drw';
  37. public function __construct(
  38. CredentialsInterface $credentials,
  39. ClientInterface $httpClient,
  40. TokenStorageInterface $storage,
  41. $scopes = array(),
  42. UriInterface $baseApiUri = null
  43. ) {
  44. parent::__construct(
  45. $credentials,
  46. $httpClient,
  47. $storage,
  48. $scopes,
  49. $baseApiUri,
  50. true
  51. );
  52. if (null === $baseApiUri) {
  53. $this->baseApiUri = new Uri('https://api.hubic.com/');
  54. }
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getAuthorizationEndpoint()
  60. {
  61. return new Uri('https://api.hubic.com/oauth/auth');
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getAccessTokenEndpoint()
  67. {
  68. return new Uri('https://api.hubic.com/oauth/token');
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. protected function getAuthorizationMethod()
  74. {
  75. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function parseAccessTokenResponse($responseBody)
  81. {
  82. $data = json_decode($responseBody, true);
  83. if (null === $data || !is_array($data)) {
  84. throw new TokenResponseException('Unable to parse response.');
  85. } elseif (isset($data['error'])) {
  86. throw new TokenResponseException(
  87. 'Error in retrieving token: "' . $data['error'] . '"'
  88. );
  89. }
  90. $token = new StdOAuth2Token();
  91. $token->setAccessToken($data['access_token']);
  92. $token->setLifetime($data['expires_in']);
  93. if (isset($data['refresh_token'])) {
  94. $token->setRefreshToken($data['refresh_token']);
  95. unset($data['refresh_token']);
  96. }
  97. unset($data['access_token']);
  98. unset($data['expires_in']);
  99. $token->setExtraParams($data);
  100. return $token;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getAuthorizationUri(array $additionalParameters = array())
  106. {
  107. $parameters = array_merge(
  108. $additionalParameters,
  109. array(
  110. 'type' => 'web_server',
  111. 'client_id' => $this->credentials->getConsumerId(),
  112. 'redirect_uri' => $this->credentials->getCallbackUrl(),
  113. 'response_type' => 'code',
  114. )
  115. );
  116. // special, hubic use a param scope with commas
  117. // between scopes instead of spaces
  118. $parameters['scope'] = implode(',', $this->scopes);
  119. if ($this->needsStateParameterInAuthUrl()) {
  120. if (!isset($parameters['state'])) {
  121. $parameters['state'] = $this->generateAuthorizationState();
  122. }
  123. $this->storeAuthorizationState($parameters['state']);
  124. }
  125. // Build the url
  126. $url = clone $this->getAuthorizationEndpoint();
  127. foreach ($parameters as $key => $val) {
  128. $url->addToQuery($key, $val);
  129. }
  130. return $url;
  131. }
  132. }