1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Netatmo service. |
4
|
|
|
* |
5
|
|
|
* @author Pedro Amorim <[email protected]> |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
7
|
|
|
* @link https://dev.netatmo.com/doc/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace OAuth\OAuth2\Service; |
11
|
|
|
|
12
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
13
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
14
|
|
|
use OAuth\Common\Http\Uri\Uri; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Netatmo service. |
18
|
|
|
* |
19
|
|
|
* @author Pedro Amorim <[email protected]> |
20
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
21
|
|
|
* @link https://dev.netatmo.com/doc/ |
22
|
|
|
*/ |
23
|
|
|
class Netatmo extends AbstractService |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
// SCOPES |
27
|
|
|
// @link https://dev.netatmo.com/doc/authentication/scopes |
28
|
|
|
|
29
|
|
|
// Used to read weather station's data (devicelist, getmeasure) |
30
|
|
|
const SCOPE_STATION_READ = 'read_station'; |
31
|
|
|
// Used to read thermostat's data (devicelist, getmeasure, getthermstate) |
32
|
|
|
const SCOPE_THERMOSTAT_READ = 'read_thermostat'; |
33
|
|
|
// Used to configure the thermostat (syncschedule, setthermpoint) |
34
|
|
|
const SCOPE_THERMOSTAT_WRITE = 'write_thermostat'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
protected function init() |
40
|
|
|
{ |
41
|
|
|
$this->stateParameterInAuthUrl = true; |
42
|
|
|
|
43
|
|
|
if( $this->baseApiUri === null ) { |
|
|
|
|
44
|
|
|
$this->baseApiUri = new Uri('https://api.netatmo.net/'); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getAuthorizationEndpoint() |
52
|
|
|
{ |
53
|
|
|
return new Uri($this->baseApiUri.'oauth2/authorize'); |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function getAccessTokenEndpoint() |
61
|
|
|
{ |
62
|
|
|
return new Uri($this->baseApiUri.'oauth2/token'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
protected function getAuthorizationMethod() |
69
|
|
|
{ |
70
|
|
|
return static::AUTHORIZATION_METHOD_QUERY_STRING; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
protected function parseAccessTokenResponse($responseBody) |
77
|
|
|
{ |
78
|
|
|
$data = json_decode($responseBody, true); |
79
|
|
|
|
80
|
|
|
if (null === $data || !is_array($data)) { |
81
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
82
|
|
|
} elseif (isset($data['error'])) { |
83
|
|
|
throw new TokenResponseException( |
84
|
|
|
'Error in retrieving token: "' . $data['error'] . '"' |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$token = new StdOAuth2Token(); |
89
|
|
|
$token->setAccessToken($data['access_token']); |
90
|
|
|
$token->setLifetime($data['expires_in']); |
91
|
|
|
|
92
|
|
|
if (isset($data['refresh_token'])) { |
93
|
|
|
$token->setRefreshToken($data['refresh_token']); |
94
|
|
|
unset($data['refresh_token']); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
unset($data['access_token']); |
98
|
|
|
unset($data['expires_in']); |
99
|
|
|
|
100
|
|
|
$token->setExtraParams($data); |
101
|
|
|
|
102
|
|
|
return $token; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|