Box.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * Box service.
  12. *
  13. * @author Antoine Corcy <contact@sbin.dk>
  14. * @link https://developers.box.com/oauth/
  15. */
  16. class Box extends AbstractService
  17. {
  18. public function __construct(
  19. CredentialsInterface $credentials,
  20. ClientInterface $httpClient,
  21. TokenStorageInterface $storage,
  22. $scopes = array(),
  23. UriInterface $baseApiUri = null
  24. ) {
  25. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
  26. if (null === $baseApiUri) {
  27. $this->baseApiUri = new Uri('https://api.box.com/2.0/');
  28. }
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getAuthorizationEndpoint()
  34. {
  35. return new Uri('https://www.box.com/api/oauth2/authorize');
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getAccessTokenEndpoint()
  41. {
  42. return new Uri('https://www.box.com/api/oauth2/token');
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function getAuthorizationMethod()
  48. {
  49. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function parseAccessTokenResponse($responseBody)
  55. {
  56. $data = json_decode($responseBody, true);
  57. if (null === $data || !is_array($data)) {
  58. throw new TokenResponseException('Unable to parse response.');
  59. } elseif (isset($data['error'])) {
  60. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  61. }
  62. $token = new StdOAuth2Token();
  63. $token->setAccessToken($data['access_token']);
  64. $token->setLifeTime($data['expires_in']);
  65. if (isset($data['refresh_token'])) {
  66. $token->setRefreshToken($data['refresh_token']);
  67. unset($data['refresh_token']);
  68. }
  69. unset($data['access_token']);
  70. unset($data['expires_in']);
  71. $token->setExtraParams($data);
  72. return $token;
  73. }
  74. }