1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
6
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
7
|
|
|
use OAuth\Common\Http\Uri\Uri; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Jawbone UP service. |
11
|
|
|
* |
12
|
|
|
* @author Andrii Gakhov <[email protected]> |
13
|
|
|
* @link https://jawbone.com/up/developer/authentication |
14
|
|
|
*/ |
15
|
|
|
class JawboneUP extends AbstractService |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Defined scopes |
19
|
|
|
* |
20
|
|
|
* |
21
|
|
|
* @link https://jawbone.com/up/developer/authentication |
22
|
|
|
*/ |
23
|
|
|
// general information scopes |
24
|
|
|
const SCOPE_BASIC_READ = 'basic_read'; |
25
|
|
|
const SCOPE_EXTENDED_READ = 'extended_read'; |
26
|
|
|
const SCOPE_LOCATION_READ = 'location_read'; |
27
|
|
|
const SCOPE_FRIENDS_READ = 'friends_read'; |
28
|
|
|
// mood scopes |
29
|
|
|
const SCOPE_MOOD_READ = 'mood_read'; |
30
|
|
|
const SCOPE_MOOD_WRITE = 'mood_write'; |
31
|
|
|
// move scopes |
32
|
|
|
const SCOPE_MOVE_READ = 'move_read'; |
33
|
|
|
const SCOPE_MOVE_WRITE = 'move_write'; |
34
|
|
|
// sleep scopes |
35
|
|
|
const SCOPE_SLEEP_READ = 'sleep_read'; |
36
|
|
|
const SCOPE_SLEEP_WRITE = 'sleep_write'; |
37
|
|
|
// meal scopes |
38
|
|
|
const SCOPE_MEAL_READ = 'meal_read'; |
39
|
|
|
const SCOPE_MEAL_WRITE = 'meal_write'; |
40
|
|
|
// weight scopes |
41
|
|
|
const SCOPE_WEIGHT_READ = 'weight_read'; |
42
|
|
|
const SCOPE_WEIGHT_WRITE = 'weight_write'; |
43
|
|
|
// generic event scopes |
44
|
|
|
const SCOPE_GENERIC_EVENT_READ = 'generic_event_read'; |
45
|
|
|
const SCOPE_GENERIC_EVENT_WRITE = 'generic_event_write'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
protected function init() |
51
|
|
|
{ |
52
|
|
|
if( $this->baseApiUri === null ) { |
|
|
|
|
53
|
|
|
$this->baseApiUri = new Uri('https://jawbone.com/nudge/api/v.1.1/'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function getAuthorizationUri(array $additionalParameters = array()) |
61
|
|
|
{ |
62
|
|
|
$parameters = array_merge( |
63
|
|
|
$additionalParameters, |
64
|
|
|
array( |
65
|
|
|
'client_id' => $this->credentials->getConsumerId(), |
66
|
|
|
'redirect_uri' => $this->credentials->getCallbackUrl(), |
67
|
|
|
'response_type' => 'code', |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$parameters['scope'] = implode(' ', $this->scopes); |
72
|
|
|
|
73
|
|
|
// Build the url |
74
|
|
|
$url = clone $this->getAuthorizationEndpoint(); |
75
|
|
|
foreach ($parameters as $key => $val) { |
76
|
|
|
$url->addToQuery($key, $val); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $url; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function getAuthorizationEndpoint() |
86
|
|
|
{ |
87
|
|
|
return new Uri('https://jawbone.com/auth/oauth2/auth'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getAccessTokenEndpoint() |
94
|
|
|
{ |
95
|
|
|
return new Uri('https://jawbone.com/auth/oauth2/token'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
protected function getAuthorizationMethod() |
102
|
|
|
{ |
103
|
|
|
return static::AUTHORIZATION_METHOD_HEADER_BEARER; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
protected function parseAccessTokenResponse($responseBody) |
110
|
|
|
{ |
111
|
|
|
$data = json_decode($responseBody, true); |
112
|
|
|
|
113
|
|
|
if (null === $data || !is_array($data)) { |
114
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
115
|
|
|
} elseif (isset($data['error'])) { |
116
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$token = new StdOAuth2Token(); |
120
|
|
|
$token->setAccessToken($data['access_token']); |
121
|
|
|
$token->setLifeTime($data['expires_in']); |
122
|
|
|
|
123
|
|
|
if (isset($data['refresh_token'])) { |
124
|
|
|
$token->setRefreshToken($data['refresh_token']); |
125
|
|
|
unset($data['refresh_token']); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
unset($data['access_token']); |
129
|
|
|
unset($data['expires_in']); |
130
|
|
|
|
131
|
|
|
$token->setExtraParams($data); |
132
|
|
|
|
133
|
|
|
return $token; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|