This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright (c) 2016, 2017 François Kooman <[email protected]>. |
||
5 | * |
||
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
||
7 | * of this software and associated documentation files (the "Software"), to deal |
||
8 | * in the Software without restriction, including without limitation the rights |
||
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
10 | * copies of the Software, and to permit persons to whom the Software is |
||
11 | * furnished to do so, subject to the following conditions: |
||
12 | * |
||
13 | * The above copyright notice and this permission notice shall be included in all |
||
14 | * copies or substantial portions of the Software. |
||
15 | * |
||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
22 | * SOFTWARE. |
||
23 | */ |
||
24 | |||
25 | namespace fkooman\OAuth\Client; |
||
26 | |||
27 | use DateInterval; |
||
28 | use DateTime; |
||
29 | use fkooman\OAuth\Client\Exception\AccessTokenException; |
||
30 | |||
31 | class AccessToken |
||
32 | { |
||
33 | /** @var string */ |
||
34 | private $providerId; |
||
35 | |||
36 | /** @var \DateTime */ |
||
37 | private $issuedAt; |
||
38 | |||
39 | /** @var string */ |
||
40 | private $accessToken; |
||
41 | |||
42 | /** @var string */ |
||
43 | private $tokenType; |
||
44 | |||
45 | /** @var int|null */ |
||
46 | private $expiresIn = null; |
||
47 | |||
48 | /** @var string|null */ |
||
49 | private $refreshToken = null; |
||
50 | |||
51 | /** @var string|null */ |
||
52 | private $scope = null; |
||
53 | |||
54 | /** |
||
55 | * @param array $tokenData |
||
56 | */ |
||
57 | public function __construct(array $tokenData) |
||
58 | { |
||
59 | $requiredKeys = ['provider_id', 'issued_at', 'access_token', 'token_type']; |
||
60 | foreach ($requiredKeys as $requiredKey) { |
||
61 | if (!array_key_exists($requiredKey, $tokenData)) { |
||
62 | throw new AccessTokenException(sprintf('missing key "%s"', $requiredKey)); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | // set required keys |
||
67 | $this->setProviderId($tokenData['provider_id']); |
||
68 | $this->setIssuedAt($tokenData['issued_at']); |
||
69 | $this->setAccessToken($tokenData['access_token']); |
||
70 | $this->setTokenType($tokenData['token_type']); |
||
71 | |||
72 | // set optional keys |
||
73 | if (array_key_exists('expires_in', $tokenData)) { |
||
74 | $this->setExpiresIn($tokenData['expires_in']); |
||
75 | } |
||
76 | if (array_key_exists('refresh_token', $tokenData)) { |
||
77 | $this->setRefreshToken($tokenData['refresh_token']); |
||
78 | } |
||
79 | if (array_key_exists('scope', $tokenData)) { |
||
80 | $this->setScope($tokenData['scope']); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param Provider $provider |
||
86 | * @param \DateTime $dateTime |
||
87 | * @param array $tokenData |
||
88 | * @param string $scope |
||
89 | */ |
||
90 | public static function fromCodeResponse(Provider $provider, DateTime $dateTime, array $tokenData, $scope) |
||
91 | { |
||
92 | $tokenData['provider_id'] = $provider->getProviderId(); |
||
93 | |||
94 | // if the scope was not part of the response, add the request scope, |
||
95 | // because according to the RFC, if the scope is ommitted the requested |
||
96 | // scope was granted! |
||
97 | if (!array_key_exists('scope', $tokenData)) { |
||
98 | $tokenData['scope'] = $scope; |
||
99 | } |
||
100 | // add the current DateTime as well to be able to figure out if the |
||
101 | // token expired |
||
102 | $tokenData['issued_at'] = $dateTime->format('Y-m-d H:i:s'); |
||
103 | |||
104 | return new self($tokenData); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param Provider $provider |
||
109 | * @param \DateTime $dateTime |
||
110 | * @param array $tokenData |
||
111 | * @param AccessToken $accessToken to steal the old scope and refresh_token from! |
||
112 | */ |
||
113 | public static function fromRefreshResponse(Provider $provider, DateTime $dateTime, array $tokenData, AccessToken $accessToken) |
||
114 | { |
||
115 | $tokenData['provider_id'] = $provider->getProviderId(); |
||
116 | |||
117 | // if the scope is not part of the response, add the request scope, |
||
118 | // because according to the RFC, if the scope is ommitted the requested |
||
119 | // scope was granted! |
||
120 | if (!array_key_exists('scope', $tokenData)) { |
||
121 | $tokenData['scope'] = $accessToken->getScope(); |
||
122 | } |
||
123 | // if the refresh_token is not part of the response, we wil reuse the |
||
124 | // existing refresh_token for future refresh_token requests |
||
125 | if (!array_key_exists('refresh_token', $tokenData)) { |
||
126 | $tokenData['refresh_token'] = $accessToken->getRefreshToken(); |
||
127 | } |
||
128 | // add the current DateTime as well to be able to figure out if the |
||
129 | // token expired |
||
130 | $tokenData['issued_at'] = $dateTime->format('Y-m-d H:i:s'); |
||
131 | |||
132 | return new self($tokenData); |
||
133 | } |
||
134 | |||
135 | public function getProviderId() |
||
136 | { |
||
137 | return $this->providerId; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return \DateTime |
||
142 | */ |
||
143 | public function getIssuedAt() |
||
144 | { |
||
145 | return $this->issuedAt; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | * |
||
151 | * @see https://tools.ietf.org/html/rfc6749#section-5.1 |
||
152 | */ |
||
153 | public function getToken() |
||
154 | { |
||
155 | return $this->accessToken; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | * |
||
161 | * @see https://tools.ietf.org/html/rfc6749#section-7.1 |
||
162 | */ |
||
163 | public function getTokenType() |
||
164 | { |
||
165 | return $this->tokenType; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @return int|null |
||
170 | * |
||
171 | * @see https://tools.ietf.org/html/rfc6749#section-5.1 |
||
172 | */ |
||
173 | public function getExpiresIn() |
||
174 | { |
||
175 | return $this->expiresIn; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return string|null the refresh token |
||
180 | * |
||
181 | * @see https://tools.ietf.org/html/rfc6749#section-1.5 |
||
182 | */ |
||
183 | public function getRefreshToken() |
||
184 | { |
||
185 | return $this->refreshToken; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @return string|null |
||
190 | * |
||
191 | * @see https://tools.ietf.org/html/rfc6749#section-3.3 |
||
192 | */ |
||
193 | public function getScope() |
||
194 | { |
||
195 | return $this->scope; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param \DateTime $dateTime |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | public function isExpired(DateTime $dateTime) |
||
204 | { |
||
205 | if (is_null($this->getExpiresIn())) { |
||
206 | // if no expiry was indicated, assume it is valid |
||
207 | return false; |
||
208 | } |
||
209 | |||
210 | // check to see if issuedAt + expiresIn > provided DateTime |
||
211 | $issuedAt = clone $this->issuedAt; // XXX do we need to clone here? |
||
212 | $expiresAt = date_add($issuedAt, new DateInterval(sprintf('PT%dS', $this->getExpiresIn()))); |
||
213 | |||
214 | return $dateTime >= $expiresAt; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param string $jsonString |
||
219 | */ |
||
220 | public static function fromJson($jsonString) |
||
221 | { |
||
222 | $tokenData = json_decode($jsonString, true); |
||
223 | View Code Duplication | if (is_null($tokenData) && JSON_ERROR_NONE !== json_last_error()) { |
|
0 ignored issues
–
show
|
|||
224 | $errorMsg = function_exists('json_last_error_msg') ? json_last_error_msg() : json_last_error(); |
||
225 | throw new AccessTokenException(sprintf('unable to decode JSON from storage: %s', $errorMsg)); |
||
226 | } |
||
227 | |||
228 | return new self($tokenData); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @return string |
||
233 | */ |
||
234 | public function toJson() |
||
235 | { |
||
236 | return json_encode( |
||
237 | [ |
||
238 | 'provider_id' => $this->getProviderId(), |
||
239 | 'issued_at' => $this->issuedAt->format('Y-m-d H:i:s'), |
||
240 | 'access_token' => $this->getToken(), |
||
241 | 'token_type' => $this->getTokenType(), |
||
242 | 'expires_in' => $this->getExpiresIn(), |
||
243 | 'refresh_token' => $this->getRefreshToken(), |
||
244 | 'scope' => $this->getScope(), |
||
245 | ] |
||
246 | ); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @param string $providerId |
||
251 | */ |
||
252 | private function setProviderId($providerId) |
||
253 | { |
||
254 | $this->providerId = $providerId; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param string $issuedAt |
||
259 | */ |
||
260 | private function setIssuedAt($issuedAt) |
||
261 | { |
||
262 | self::requireString('expires_at', $issuedAt); |
||
263 | if (1 !== preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/', $issuedAt)) { |
||
264 | throw new AccessTokenException('invalid "expires_at"'); |
||
265 | } |
||
266 | $this->issuedAt = new DateTime($issuedAt); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param string $accessToken |
||
271 | */ |
||
272 | private function setAccessToken($accessToken) |
||
273 | { |
||
274 | self::requireString('access_token', $accessToken); |
||
275 | // access-token = 1*VSCHAR |
||
276 | // VSCHAR = %x20-7E |
||
277 | if (1 !== preg_match('/^[\x20-\x7E]+$/', $accessToken)) { |
||
278 | throw new AccessTokenException('invalid "access_token"'); |
||
279 | } |
||
280 | $this->accessToken = $accessToken; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param string $tokenType |
||
285 | */ |
||
286 | private function setTokenType($tokenType) |
||
287 | { |
||
288 | self::requireString('token_type', $tokenType); |
||
289 | if ('bearer' !== $tokenType) { |
||
290 | throw new AccessTokenException('unsupported "token_type"'); |
||
291 | } |
||
292 | $this->tokenType = $tokenType; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * @param int|null $expiresIn |
||
297 | */ |
||
298 | private function setExpiresIn($expiresIn) |
||
299 | { |
||
300 | if (!is_null($expiresIn)) { |
||
301 | self::requireInt('expires_in', $expiresIn); |
||
302 | if (0 >= $expiresIn) { |
||
303 | throw new AccessTokenException('invalid "expires_in"'); |
||
304 | } |
||
305 | } |
||
306 | $this->expiresIn = $expiresIn; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * @param string|null $refreshToken |
||
311 | */ |
||
312 | View Code Duplication | private function setRefreshToken($refreshToken) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
313 | { |
||
314 | if (!is_null($refreshToken)) { |
||
315 | self::requireString('refresh_token', $refreshToken); |
||
316 | // refresh-token = 1*VSCHAR |
||
317 | // VSCHAR = %x20-7E |
||
318 | if (1 !== preg_match('/^[\x20-\x7E]+$/', $refreshToken)) { |
||
319 | throw new AccessTokenException('invalid "refresh_token"'); |
||
320 | } |
||
321 | } |
||
322 | $this->refreshToken = $refreshToken; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @param string|null $scope |
||
327 | */ |
||
328 | View Code Duplication | private function setScope($scope) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
329 | { |
||
330 | if (!is_null($scope)) { |
||
331 | self::requireString('scope', $scope); |
||
332 | // scope = scope-token *( SP scope-token ) |
||
333 | // scope-token = 1*NQCHAR |
||
334 | // NQCHAR = %x21 / %x23-5B / %x5D-7E |
||
335 | foreach (explode(' ', $scope) as $scopeToken) { |
||
336 | if (1 !== preg_match('/^[\x21\x23-\x5B\x5D-\x7E]+$/', $scopeToken)) { |
||
337 | throw new AccessTokenException('invalid "scope"'); |
||
338 | } |
||
339 | } |
||
340 | } |
||
341 | $this->scope = $scope; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * @param string $k |
||
346 | * @param string $v |
||
347 | */ |
||
348 | private static function requireString($k, $v) |
||
349 | { |
||
350 | if (!is_string($v)) { |
||
351 | throw new AccessTokenException(sprintf('"%s" must be string', $k)); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @param string $k |
||
357 | * @param int $v |
||
358 | */ |
||
359 | private static function requireInt($k, $v) |
||
360 | { |
||
361 | if (!is_int($v)) { |
||
362 | throw new AccessTokenException(sprintf('"%s" must be int', $k)); |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.