|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jayS-de <[email protected]> |
|
4
|
|
|
* @created: 22.01.15, 12:34 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Commercetools\Core\Client\OAuth; |
|
8
|
|
|
|
|
9
|
|
|
use Commercetools\Core\Config; |
|
10
|
|
|
use Commercetools\Core\Error\ApiException; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use Commercetools\Core\AbstractHttpClient; |
|
13
|
|
|
use Commercetools\Core\Cache\CacheAdapterFactory; |
|
14
|
|
|
use Commercetools\Core\Cache\CacheAdapterInterface; |
|
15
|
|
|
use Commercetools\Core\Error\InvalidClientCredentialsException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @package Commercetools\Core\OAuth |
|
19
|
|
|
* @internal |
|
20
|
|
|
*/ |
|
21
|
|
|
class Manager extends AbstractHttpClient |
|
22
|
|
|
{ |
|
23
|
|
|
const TOKEN_CACHE_KEY = 'commercetools-io-access-token'; |
|
24
|
|
|
|
|
25
|
|
|
const REFRESH_TOKEN = 'refresh_token'; |
|
26
|
|
|
const ACCESS_TOKEN = 'access_token'; |
|
27
|
|
|
const EXPIRES_IN = 'expires_in'; |
|
28
|
|
|
const ERROR = 'error'; |
|
29
|
|
|
const SCOPE = 'scope'; |
|
30
|
|
|
const ERROR_DESCRIPTION = 'error_description'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $cacheKeys; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var CacheAdapterInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $cacheAdapter; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var CacheAdapterFactory |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $cacheAdapterFactory; |
|
46
|
|
|
|
|
47
|
38 |
|
public function __construct($config, $cache = null) |
|
48
|
|
|
{ |
|
49
|
38 |
|
parent::__construct($config); |
|
50
|
38 |
|
$this->cacheKeys = []; |
|
51
|
38 |
|
$this->setCacheAdapter($cache); |
|
52
|
38 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return CacheAdapterFactory |
|
56
|
|
|
*/ |
|
57
|
38 |
|
public function getCacheAdapterFactory() |
|
58
|
|
|
{ |
|
59
|
38 |
|
if (is_null($this->cacheAdapterFactory)) { |
|
60
|
38 |
|
$this->cacheAdapterFactory = new CacheAdapterFactory(); |
|
61
|
|
|
} |
|
62
|
38 |
|
return $this->cacheAdapterFactory; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $cache |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
38 |
|
public function setCacheAdapter($cache) |
|
70
|
|
|
{ |
|
71
|
38 |
|
$this->cacheAdapter = $this->getCacheAdapterFactory()->get($cache); |
|
72
|
|
|
|
|
73
|
38 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return CacheAdapterInterface |
|
78
|
|
|
*/ |
|
79
|
282 |
|
public function getCacheAdapter() |
|
80
|
|
|
{ |
|
81
|
282 |
|
return $this->cacheAdapter; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $scope |
|
|
|
|
|
|
86
|
|
|
* @return Token |
|
87
|
|
|
* @throws InvalidClientCredentialsException |
|
88
|
|
|
*/ |
|
89
|
282 |
|
public function getToken() |
|
90
|
|
|
{ |
|
91
|
282 |
|
$scope = $this->getConfig()->getScope(); |
|
92
|
282 |
|
if ($token = $this->getCacheToken()) { |
|
93
|
281 |
|
return new Token($token, null, $scope); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
9 |
|
return $this->refreshToken(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return Token |
|
101
|
|
|
* @throws InvalidClientCredentialsException |
|
102
|
|
|
*/ |
|
103
|
12 |
|
public function refreshToken() |
|
104
|
|
|
{ |
|
105
|
12 |
|
$scope = $this->getConfig()->getScope(); |
|
106
|
12 |
|
$grantType = $this->getConfig()->getGrantType(); |
|
107
|
12 |
|
$data = [Config::SCOPE => $scope, Config::GRANT_TYPE => $grantType]; |
|
108
|
|
|
|
|
109
|
12 |
|
if ($grantType === Config::GRANT_TYPE_PASSWORD) { |
|
110
|
4 |
|
$user = $this->getConfig()->getUsername(); |
|
111
|
4 |
|
$password = $this->getConfig()->getPassword(); |
|
112
|
4 |
|
$data[Config::USER_NAME] = $user; |
|
113
|
4 |
|
$data[Config::PASSWORD] = $password; |
|
114
|
9 |
|
} elseif ($grantType === Config::GRANT_TYPE_REFRESH) { |
|
115
|
1 |
|
$refreshToken = $this->getConfig()->getRefreshToken(); |
|
116
|
1 |
|
$data[Config::REFRESH_TOKEN] = $refreshToken; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
12 |
|
$token = $this->getBearerToken($data); |
|
120
|
|
|
|
|
121
|
11 |
|
if ($grantType === Config::GRANT_TYPE_PASSWORD) { |
|
122
|
4 |
|
$this->getConfig()->setGrantType(Config::GRANT_TYPE_REFRESH); |
|
123
|
4 |
|
$this->getConfig()->setRefreshToken($token->getRefreshToken()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// ensure token to be invalidated in cache before TTL |
|
127
|
11 |
|
$ttl = max(1, floor($token->getTtl()/2)); |
|
128
|
11 |
|
$this->getCacheAdapter()->store($this->getCacheKey(), $token->getToken(), $ttl); |
|
129
|
|
|
|
|
130
|
11 |
|
return $token; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
282 |
|
protected function getCacheToken() |
|
134
|
|
|
{ |
|
135
|
282 |
|
return $this->getCacheAdapter()->fetch($this->getCacheKey()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
282 |
|
protected function getCacheKey() |
|
142
|
|
|
{ |
|
143
|
282 |
|
$scope = $this->getConfig()->getScope(); |
|
144
|
282 |
|
$grantType = $this->getConfig()->getGrantType(); |
|
145
|
282 |
|
$cacheScope = $scope . '-' . $grantType; |
|
146
|
|
|
|
|
147
|
282 |
|
if ($grantType === Config::GRANT_TYPE_PASSWORD) { |
|
148
|
3 |
|
$user = $this->getConfig()->getUsername(); |
|
149
|
3 |
|
$cacheScope .= '-' . $user; |
|
150
|
282 |
|
} elseif ($grantType === Config::GRANT_TYPE_REFRESH) { |
|
151
|
4 |
|
$token = $this->getConfig()->getRefreshToken(); |
|
152
|
4 |
|
$cacheScope .= '-' . $token; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
282 |
|
if (!isset($this->cacheKeys[$cacheScope])) { |
|
156
|
11 |
|
$this->cacheKeys[$cacheScope] = static::TOKEN_CACHE_KEY . '-' . |
|
157
|
11 |
|
sha1($cacheScope); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
282 |
|
return $this->cacheKeys[$cacheScope]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param array $data |
|
165
|
|
|
* @return Token |
|
166
|
|
|
* @throws ApiException |
|
167
|
|
|
* @throws \Commercetools\Core\Error\BadGatewayException |
|
168
|
|
|
* @throws \Commercetools\Core\Error\GatewayTimeoutException |
|
169
|
|
|
* @throws \Commercetools\Core\Error\ServiceUnavailableException |
|
170
|
|
|
*/ |
|
171
|
12 |
|
protected function getBearerToken(array $data) |
|
172
|
|
|
{ |
|
173
|
|
|
try { |
|
174
|
12 |
|
$response = $this->execute($data); |
|
175
|
1 |
|
} catch (ApiException $exception) { |
|
176
|
1 |
|
throw ApiException::create($exception->getRequest(), $exception->getResponse()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
11 |
|
$result = json_decode($response->getBody(), true); |
|
180
|
|
|
|
|
181
|
11 |
|
$token = new Token($result[static::ACCESS_TOKEN], $result[static::EXPIRES_IN], $result[static::SCOPE]); |
|
182
|
11 |
|
$token->setValidTo(new \DateTime('now +' . $result[static::EXPIRES_IN] . ' seconds')); |
|
183
|
11 |
|
if (isset($result[static::REFRESH_TOKEN])) { |
|
184
|
4 |
|
$token->setRefreshToken($result[static::REFRESH_TOKEN]); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
11 |
|
return $token; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param $data |
|
192
|
|
|
* @return ResponseInterface |
|
193
|
|
|
*/ |
|
194
|
12 |
|
public function execute($data) |
|
195
|
|
|
{ |
|
196
|
12 |
|
return $this->getHttpClient()->authenticate( |
|
197
|
12 |
|
$this->getConfig()->getOauthUrl(), |
|
198
|
12 |
|
$this->getConfig()->getClientId(), |
|
199
|
12 |
|
$this->getConfig()->getClientSecret(), |
|
200
|
|
|
$data |
|
201
|
|
|
); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
12 |
|
protected function getBaseUrl() |
|
208
|
|
|
{ |
|
209
|
12 |
|
return $this->getConfig()->getOauthUrl(); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.