|
1
|
|
|
<?php |
|
2
|
|
|
namespace League\OAuth2\Client\Provider; |
|
3
|
|
|
|
|
4
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
|
5
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
|
6
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
class TomTomMySports extends AbstractProvider |
|
10
|
|
|
{ |
|
11
|
|
|
use BearerAuthorizationTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
const BASE_MYSPORTS_URL = 'https://api.tomtom.com/mysports'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $apiVersion = '1'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $apikey; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritDoc |
|
31
|
9 |
|
*/ |
|
32
|
|
|
protected function getAuthorizationParameters(array $options) |
|
33
|
9 |
|
{ |
|
34
|
9 |
|
$options = parent::getAuthorizationParameters($options); |
|
35
|
3 |
|
if (empty($options['api_key'])) { |
|
36
|
|
|
$options['Api-Key'] = $this->apikey; |
|
37
|
9 |
|
} |
|
38
|
6 |
|
|
|
39
|
2 |
|
return $options; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
9 |
|
/** |
|
43
|
|
|
* @inheritDoc |
|
44
|
3 |
|
*/ |
|
45
|
|
|
public function getAccessToken($grant, array $options = []) |
|
46
|
9 |
|
{ |
|
47
|
9 |
|
$grant = $this->verifyGrant($grant); |
|
48
|
9 |
|
|
|
49
|
3 |
|
$params = [ |
|
50
|
|
|
'client_id' => $this->clientId, |
|
51
|
|
|
'client_secret' => $this->clientSecret, |
|
52
|
9 |
|
'redirect_uri' => $this->redirectUri, |
|
53
|
|
|
'Api-Key' => $this->apikey |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
9 |
|
$params = $grant->prepareRequestParameters($params, $options); |
|
57
|
9 |
|
$request = $this->getAccessTokenRequest($params); |
|
58
|
3 |
|
$response = $this->getParsedResponse($request); |
|
59
|
|
|
$prepared = $this->prepareAccessTokenResponse($response); |
|
|
|
|
|
|
60
|
9 |
|
$token = $this->createAccessToken($prepared, $grant); |
|
61
|
|
|
|
|
62
|
9 |
|
return $token; |
|
63
|
9 |
|
} |
|
64
|
3 |
|
|
|
65
|
|
|
/** |
|
66
|
9 |
|
* Builds request options used for requesting an access token. |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $params |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getAccessTokenOptions(array $params) |
|
72
|
|
|
{ |
|
73
|
|
|
$options = parent::getAccessTokenOptions($params); |
|
74
|
|
|
|
|
75
|
12 |
|
$options['headers']['Api-Key'] = $this->apikey; |
|
76
|
|
|
$options['headers']['Authorization'] = 'Basic ' . base64_encode(implode(':', [ |
|
77
|
12 |
|
$this->clientId, |
|
78
|
|
|
$this->clientSecret, |
|
79
|
|
|
])); |
|
80
|
|
|
return $options; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get authorization url to begin OAuth flow |
|
86
|
|
|
* |
|
87
|
12 |
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
12 |
|
public function getBaseAuthorizationUrl() |
|
90
|
|
|
{ |
|
91
|
|
|
return self::BASE_MYSPORTS_URL . '/oauth2/authorize'; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritDoc |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getBaseAccessTokenUrl(array $params) |
|
98
|
|
|
{ |
|
99
|
|
|
return self::BASE_MYSPORTS_URL . '/oauth2/token'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritDoc |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
106
|
|
|
{ |
|
107
|
|
|
return self::BASE_MYSPORTS_URL . '/' . $this->apiVersion . '/athlete'; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @link http://developer.tomtom.com/products/sports/mysportscloud/authorization/ |
|
112
|
|
|
* |
|
113
|
|
|
* Get the default scopes used by this provider. |
|
114
|
6 |
|
* |
|
115
|
|
|
* This should not be a complete list of all scopes, but the minimum |
|
116
|
6 |
|
* required for the provider user interface! |
|
117
|
|
|
* |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getDefaultScopes() |
|
121
|
|
|
{ |
|
122
|
|
|
return ['activities']; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @inheritDoc |
|
127
|
9 |
|
*/ |
|
128
|
|
|
protected function checkResponse(ResponseInterface $response, $data) |
|
129
|
9 |
|
{ |
|
130
|
3 |
|
if ($response->getStatusCode() >= 400) { |
|
131
|
3 |
|
throw new IdentityProviderException( |
|
132
|
3 |
|
'Forbidden', |
|
133
|
1 |
|
$response->getStatusCode(), |
|
134
|
1 |
|
$response |
|
|
|
|
|
|
135
|
|
|
); |
|
136
|
6 |
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @inheritDoc |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function createResourceOwner(array $response, AccessToken $token) |
|
143
|
|
|
{ |
|
144
|
|
|
return null; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getBaseMySportsUrl() |
|
151
|
|
|
{ |
|
152
|
|
|
return self::BASE_MYSPORTS_URL; |
|
153
|
3 |
|
} |
|
154
|
|
|
|
|
155
|
3 |
|
/** |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getApiVersion() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->apiVersion; |
|
161
|
3 |
|
} |
|
162
|
|
|
|
|
163
|
3 |
|
/** |
|
164
|
|
|
* @inheritDoc |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function getDefaultHeaders() |
|
167
|
|
|
{ |
|
168
|
|
|
return [ |
|
169
|
|
|
'Accept' => 'application/json', |
|
170
|
|
|
'Accept-Encoding' => 'gzip', |
|
171
|
|
|
]; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.