Heroku.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * Heroku service.
  12. *
  13. * @author Thomas Welton <thomaswelton@me.com>
  14. * @link https://devcenter.heroku.com/articles/oauth
  15. */
  16. class Heroku extends AbstractService
  17. {
  18. /**
  19. * Defined scopes
  20. * @link https://devcenter.heroku.com/articles/oauth#scopes
  21. */
  22. const SCOPE_GLOBAL = 'global';
  23. const SCOPE_IDENTITY = 'identity';
  24. const SCOPE_READ = 'read';
  25. const SCOPE_WRITE = 'write';
  26. const SCOPE_READ_PROTECTED = 'read-protected';
  27. const SCOPE_WRITE_PROTECTED = 'write-protected';
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function __construct(
  32. CredentialsInterface $credentials,
  33. ClientInterface $httpClient,
  34. TokenStorageInterface $storage,
  35. $scopes = array(),
  36. UriInterface $baseApiUri = null
  37. ) {
  38. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  39. if (null === $baseApiUri) {
  40. $this->baseApiUri = new Uri('https://api.heroku.com/');
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getAuthorizationEndpoint()
  47. {
  48. return new Uri('https://id.heroku.com/oauth/authorize');
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getAccessTokenEndpoint()
  54. {
  55. return new Uri('https://id.heroku.com/oauth/token');
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. protected function getAuthorizationMethod()
  61. {
  62. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function parseAccessTokenResponse($responseBody)
  68. {
  69. $data = json_decode($responseBody, true);
  70. if (null === $data || !is_array($data)) {
  71. throw new TokenResponseException('Unable to parse response.');
  72. } elseif (isset($data['error_description']) || isset($data['error'])) {
  73. throw new TokenResponseException(
  74. sprintf(
  75. 'Error in retrieving token: "%s"',
  76. isset($data['error_description']) ? $data['error_description'] : $data['error']
  77. )
  78. );
  79. }
  80. $token = new StdOAuth2Token();
  81. $token->setAccessToken($data['access_token']);
  82. $token->setLifeTime($data['expires_in']);
  83. if (isset($data['refresh_token'])) {
  84. $token->setRefreshToken($data['refresh_token']);
  85. unset($data['refresh_token']);
  86. }
  87. unset($data['access_token']);
  88. unset($data['expires_in']);
  89. $token->setExtraParams($data);
  90. return $token;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. protected function getExtraOAuthHeaders()
  96. {
  97. return array('Accept' => 'application/vnd.heroku+json; version=3');
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. protected function getExtraApiHeaders()
  103. {
  104. return array('Accept' => 'application/vnd.heroku+json; version=3', 'Content-Type' => 'application/json');
  105. }
  106. }