Mondo.php 2.2 KB

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