Nest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Nest service.
  4. *
  5. * @author Pedro Amorim <contact@pamorim.fr>
  6. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  7. * @link https://developer.nest.com/documentation
  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. * Nest service.
  19. *
  20. * @author Pedro Amorim <contact@pamorim.fr>
  21. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  22. * @link https://developer.nest.com/documentation
  23. */
  24. class Nest extends AbstractService
  25. {
  26. public function __construct(
  27. CredentialsInterface $credentials,
  28. ClientInterface $httpClient,
  29. TokenStorageInterface $storage,
  30. $scopes = array(),
  31. UriInterface $baseApiUri = null
  32. ) {
  33. parent::__construct(
  34. $credentials,
  35. $httpClient,
  36. $storage,
  37. $scopes,
  38. $baseApiUri,
  39. true
  40. );
  41. if (null === $baseApiUri) {
  42. $this->baseApiUri = new Uri('https://developer-api.nest.com/');
  43. }
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getAuthorizationEndpoint()
  49. {
  50. return new Uri('https://home.nest.com/login/oauth2');
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getAccessTokenEndpoint()
  56. {
  57. return new Uri('https://api.home.nest.com/oauth2/access_token');
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function getAuthorizationMethod()
  63. {
  64. return static::AUTHORIZATION_METHOD_QUERY_STRING_V4;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function parseAccessTokenResponse($responseBody)
  70. {
  71. $data = json_decode($responseBody, true);
  72. if (null === $data || !is_array($data)) {
  73. throw new TokenResponseException('Unable to parse response.');
  74. } elseif (isset($data['error'])) {
  75. throw new TokenResponseException(
  76. 'Error in retrieving token: "' . $data['error'] . '"'
  77. );
  78. }
  79. $token = new StdOAuth2Token();
  80. $token->setAccessToken($data['access_token']);
  81. $token->setLifeTime($data['expires_in']);
  82. if (isset($data['refresh_token'])) {
  83. $token->setRefreshToken($data['refresh_token']);
  84. unset($data['refresh_token']);
  85. }
  86. unset($data['access_token']);
  87. unset($data['expires_in']);
  88. $token->setExtraParams($data);
  89. return $token;
  90. }
  91. }