EveOnline.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Contains EveOnline class.
  4. * PHP version 5.4
  5. * @copyright 2014 Michael Cummings
  6. * @author Michael Cummings <mgcummings@yahoo.com>
  7. */
  8. namespace OAuth\OAuth2\Service;
  9. use OAuth\Common\Consumer\CredentialsInterface;
  10. use OAuth\Common\Http\Client\ClientInterface;
  11. use OAuth\Common\Http\Exception\TokenResponseException;
  12. use OAuth\Common\Http\Uri\Uri;
  13. use OAuth\Common\Http\Uri\UriInterface;
  14. use OAuth\Common\Storage\TokenStorageInterface;
  15. use OAuth\Common\Token\TokenInterface;
  16. use OAuth\OAuth2\Token\StdOAuth2Token;
  17. /**
  18. * Class EveOnline
  19. */
  20. class EveOnline extends AbstractService
  21. {
  22. public function __construct(
  23. CredentialsInterface $credentials,
  24. ClientInterface $httpClient,
  25. TokenStorageInterface $storage,
  26. $scopes = array(),
  27. UriInterface $baseApiUri = null
  28. ) {
  29. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  30. if (null === $baseApiUri) {
  31. $this->baseApiUri = new Uri('https://login.eveonline.com');
  32. }
  33. }
  34. /**
  35. * Returns the authorization API endpoint.
  36. * @return UriInterface
  37. */
  38. public function getAuthorizationEndpoint()
  39. {
  40. return new Uri($this->baseApiUri . '/oauth/authorize');
  41. }
  42. /**
  43. * Returns the access token API endpoint.
  44. * @return UriInterface
  45. */
  46. public function getAccessTokenEndpoint()
  47. {
  48. return new Uri($this->baseApiUri . '/oauth/token');
  49. }
  50. /**
  51. * Parses the access token response and returns a TokenInterface.
  52. *
  53. * @param string $responseBody
  54. *
  55. * @return TokenInterface
  56. * @throws TokenResponseException
  57. */
  58. protected function parseAccessTokenResponse($responseBody)
  59. {
  60. $data = json_decode($responseBody, true);
  61. if (null === $data || !is_array($data)) {
  62. throw new TokenResponseException('Unable to parse response.');
  63. } elseif (isset($data['error_description'])) {
  64. throw new TokenResponseException('Error in retrieving token: "' . $data['error_description'] . '"');
  65. } elseif (isset($data['error'])) {
  66. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  67. }
  68. $token = new StdOAuth2Token();
  69. $token->setAccessToken($data['access_token']);
  70. $token->setLifeTime($data['expires_in']);
  71. if (isset($data['refresh_token'])) {
  72. $token->setRefreshToken($data['refresh_token']);
  73. unset($data['refresh_token']);
  74. }
  75. unset($data['access_token']);
  76. unset($data['expires_in']);
  77. $token->setExtraParams($data);
  78. return $token;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. protected function getAuthorizationMethod()
  84. {
  85. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  86. }
  87. }