xref: /plugin/oauth/vendor/lusitanian/oauth/src/OAuth/OAuth2/Service/Nest.php (revision 98a3611675f696f14f2d12205fb81f7db0cf7b25)
1*98a36116SAndreas Gohr<?php
2*98a36116SAndreas Gohr/**
3*98a36116SAndreas Gohr * Nest service.
4*98a36116SAndreas Gohr *
5*98a36116SAndreas Gohr * @author  Pedro Amorim <contact@pamorim.fr>
6*98a36116SAndreas Gohr * @license http://www.opensource.org/licenses/mit-license.html MIT License
7*98a36116SAndreas Gohr * @link    https://developer.nest.com/documentation
8*98a36116SAndreas Gohr */
9*98a36116SAndreas Gohr
10*98a36116SAndreas Gohrnamespace OAuth\OAuth2\Service;
11*98a36116SAndreas Gohr
12*98a36116SAndreas Gohruse OAuth\OAuth2\Token\StdOAuth2Token;
13*98a36116SAndreas Gohruse OAuth\Common\Http\Exception\TokenResponseException;
14*98a36116SAndreas Gohruse OAuth\Common\Http\Uri\Uri;
15*98a36116SAndreas Gohruse OAuth\Common\Consumer\CredentialsInterface;
16*98a36116SAndreas Gohruse OAuth\Common\Http\Client\ClientInterface;
17*98a36116SAndreas Gohruse OAuth\Common\Storage\TokenStorageInterface;
18*98a36116SAndreas Gohruse OAuth\Common\Http\Uri\UriInterface;
19*98a36116SAndreas Gohr
20*98a36116SAndreas Gohr/**
21*98a36116SAndreas Gohr * Nest service.
22*98a36116SAndreas Gohr *
23*98a36116SAndreas Gohr * @author  Pedro Amorim <contact@pamorim.fr>
24*98a36116SAndreas Gohr * @license http://www.opensource.org/licenses/mit-license.html MIT License
25*98a36116SAndreas Gohr * @link    https://developer.nest.com/documentation
26*98a36116SAndreas Gohr */
27*98a36116SAndreas Gohrclass Nest extends AbstractService
28*98a36116SAndreas Gohr{
29*98a36116SAndreas Gohr
30*98a36116SAndreas Gohr    public function __construct(
31*98a36116SAndreas Gohr        CredentialsInterface $credentials,
32*98a36116SAndreas Gohr        ClientInterface $httpClient,
33*98a36116SAndreas Gohr        TokenStorageInterface $storage,
34*98a36116SAndreas Gohr        $scopes = array(),
35*98a36116SAndreas Gohr        UriInterface $baseApiUri = null
36*98a36116SAndreas Gohr    ) {
37*98a36116SAndreas Gohr        parent::__construct(
38*98a36116SAndreas Gohr            $credentials,
39*98a36116SAndreas Gohr            $httpClient,
40*98a36116SAndreas Gohr            $storage,
41*98a36116SAndreas Gohr            $scopes,
42*98a36116SAndreas Gohr            $baseApiUri,
43*98a36116SAndreas Gohr            true
44*98a36116SAndreas Gohr        );
45*98a36116SAndreas Gohr
46*98a36116SAndreas Gohr        if (null === $baseApiUri) {
47*98a36116SAndreas Gohr            $this->baseApiUri = new Uri('https://developer-api.nest.com/');
48*98a36116SAndreas Gohr        }
49*98a36116SAndreas Gohr    }
50*98a36116SAndreas Gohr
51*98a36116SAndreas Gohr    /**
52*98a36116SAndreas Gohr     * {@inheritdoc}
53*98a36116SAndreas Gohr     */
54*98a36116SAndreas Gohr    public function getAuthorizationEndpoint()
55*98a36116SAndreas Gohr    {
56*98a36116SAndreas Gohr        return new Uri('https://home.nest.com/login/oauth2');
57*98a36116SAndreas Gohr    }
58*98a36116SAndreas Gohr
59*98a36116SAndreas Gohr    /**
60*98a36116SAndreas Gohr     * {@inheritdoc}
61*98a36116SAndreas Gohr     */
62*98a36116SAndreas Gohr    public function getAccessTokenEndpoint()
63*98a36116SAndreas Gohr    {
64*98a36116SAndreas Gohr        return new Uri('https://api.home.nest.com/oauth2/access_token');
65*98a36116SAndreas Gohr    }
66*98a36116SAndreas Gohr
67*98a36116SAndreas Gohr    /**
68*98a36116SAndreas Gohr     * {@inheritdoc}
69*98a36116SAndreas Gohr     */
70*98a36116SAndreas Gohr    protected function getAuthorizationMethod()
71*98a36116SAndreas Gohr    {
72*98a36116SAndreas Gohr        return static::AUTHORIZATION_METHOD_QUERY_STRING_V4;
73*98a36116SAndreas Gohr    }
74*98a36116SAndreas Gohr
75*98a36116SAndreas Gohr    /**
76*98a36116SAndreas Gohr     * {@inheritdoc}
77*98a36116SAndreas Gohr     */
78*98a36116SAndreas Gohr    protected function parseAccessTokenResponse($responseBody)
79*98a36116SAndreas Gohr    {
80*98a36116SAndreas Gohr        $data = json_decode($responseBody, true);
81*98a36116SAndreas Gohr
82*98a36116SAndreas Gohr        if (null === $data || !is_array($data)) {
83*98a36116SAndreas Gohr            throw new TokenResponseException('Unable to parse response.');
84*98a36116SAndreas Gohr        } elseif (isset($data['error'])) {
85*98a36116SAndreas Gohr            throw new TokenResponseException(
86*98a36116SAndreas Gohr                'Error in retrieving token: "' . $data['error'] . '"'
87*98a36116SAndreas Gohr            );
88*98a36116SAndreas Gohr        }
89*98a36116SAndreas Gohr
90*98a36116SAndreas Gohr        $token = new StdOAuth2Token();
91*98a36116SAndreas Gohr        $token->setAccessToken($data['access_token']);
92*98a36116SAndreas Gohr        $token->setLifeTime($data['expires_in']);
93*98a36116SAndreas Gohr
94*98a36116SAndreas Gohr        if (isset($data['refresh_token'])) {
95*98a36116SAndreas Gohr            $token->setRefreshToken($data['refresh_token']);
96*98a36116SAndreas Gohr            unset($data['refresh_token']);
97*98a36116SAndreas Gohr        }
98*98a36116SAndreas Gohr
99*98a36116SAndreas Gohr        unset($data['access_token']);
100*98a36116SAndreas Gohr        unset($data['expires_in']);
101*98a36116SAndreas Gohr
102*98a36116SAndreas Gohr        $token->setExtraParams($data);
103*98a36116SAndreas Gohr
104*98a36116SAndreas Gohr        return $token;
105*98a36116SAndreas Gohr    }
106*98a36116SAndreas Gohr}
107