-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathCookieAuthenticator.php
More file actions
451 lines (400 loc) · 15.7 KB
/
Copy pathCookieAuthenticator.php
File metadata and controls
451 lines (400 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 1.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Authentication\Authenticator;
use ArrayAccess;
use Authentication\Identifier\IdentifierFactory;
use Authentication\Identifier\IdentifierInterface;
use Authentication\Identifier\PasswordIdentifier;
use Authentication\PasswordHasher\PasswordHasherFactory;
use Authentication\PasswordHasher\PasswordHasherInterface;
use Authentication\PasswordHasher\PasswordHasherTrait;
use Authentication\UrlChecker\UrlCheckerTrait;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieInterface;
use Cake\Utility\Security;
use DateTimeInterface;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use UnexpectedValueException;
/**
* Cookie Authenticator
*
* Authenticates an identity based on a cookie data.
*
* You *must* enable encrypted cookies with `EncryptedCookieMiddleware` before using CookieAuthenticator.
* Without encryption remember-me cookie values can be tampered with.
*/
class CookieAuthenticator extends AbstractAuthenticator implements PersistenceInterface
{
use PasswordHasherTrait;
use UrlCheckerTrait;
/**
* @inheritDoc
*/
protected array $_defaultConfig = [
'loginUrl' => null,
'urlChecker' => 'Authentication.Default',
'rememberMeField' => 'remember_me',
'fields' => [
PasswordIdentifier::CREDENTIAL_USERNAME => 'username',
PasswordIdentifier::CREDENTIAL_PASSWORD => 'password',
],
'cookie' => [
'name' => 'CookieAuth',
'expires' => '+7 days',
],
// Used only to verify legacy (v1) cookie tokens during the
// `legacyTokens` grace period. Deprecated: removed together with
// `legacyTokens` support.
'passwordHasher' => 'Authentication.Default',
'salt' => true,
// While true (default), legacy 2-part tokens issued before the
// HMAC token format are accepted (hardened) and holders are
// upgraded at their next login. Set to false to end the grace
// period and enforce the new token format only.
'legacyTokens' => true,
// Upper bounds on the work factor of legacy token hashes. A forged
// legacy token could otherwise embed a valid bcrypt/argon2 hash with
// an arbitrarily large cost, turning password_verify() into a CPU or
// memory exhaustion vector. Hashes above these limits are rejected
// before verification. Defaults comfortably exceed the
// PASSWORD_DEFAULT parameters used to issue real cookies; raise them
// only if your application issued cookies with higher work factors.
'legacyHashLimits' => [
'cost' => 15,
'memory_cost' => 131072,
'time_cost' => 10,
],
];
/**
* Gets the identifier, loading a default Password identifier if none configured.
*
* This is done lazily to allow configuration to be fully set before creating the identifier.
*
* @return \Authentication\Identifier\IdentifierInterface
*/
public function getIdentifier(): IdentifierInterface
{
if (!$this->_identifier instanceof IdentifierInterface) {
$identifierConfig = [];
if ($this->getConfig('fields')) {
$identifierConfig['fields'] = $this->getConfig('fields');
}
$this->_identifier = IdentifierFactory::create('Authentication.Password', $identifierConfig);
}
return $this->_identifier;
}
/**
* @inheritDoc
*/
public function authenticate(ServerRequestInterface $request): ResultInterface
{
$cookies = $request->getCookieParams();
$cookieName = $this->getConfig('cookie.name');
if (!isset($cookies[$cookieName])) {
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING, [
'Login credentials not found',
]);
}
$token = is_array($cookies[$cookieName])
? $cookies[$cookieName]
: json_decode((string)$cookies[$cookieName], true);
if (!is_array($token) || !array_is_list($token)) {
return new Result(null, Result::FAILURE_CREDENTIALS_INVALID, [
'Cookie token is invalid.',
]);
}
if (count($token) === 3) {
return $this->_authenticateToken($token);
}
if (count($token) === 2 && $this->getConfig('legacyTokens')) {
return $this->_authenticateLegacyToken($token);
}
return new Result(null, Result::FAILURE_CREDENTIALS_INVALID, [
'Cookie token is invalid.',
]);
}
/**
* Validates a three-part `[username, expires, hmac]` token.
*
* Checks part types, then expiry (before touching the identifier), then
* locates the user and verifies the HMAC of
* `username + password hash + expires` in constant time.
*
* @param array $token The decoded token parts.
* @return \Authentication\Authenticator\ResultInterface
*/
protected function _authenticateToken(array $token): ResultInterface
{
[$username, $expires, $tokenHash] = $token;
if (!is_string($username) || !is_numeric($expires) || !is_string($tokenHash)) {
return new Result(null, Result::FAILURE_CREDENTIALS_INVALID, [
'Cookie token is invalid.',
]);
}
$expires = (int)$expires;
if ($expires < time()) {
return new Result(null, Result::FAILURE_CREDENTIALS_INVALID, [
'Cookie token has expired.',
]);
}
$identifier = $this->getIdentifier();
$identity = $identifier->identify(compact('username'));
if (!$identity) {
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $identifier->getErrors());
}
$usernameField = $this->getConfig('fields.username');
$passwordField = $this->getConfig('fields.password');
$plain = $identity[$usernameField] . $identity[$passwordField] . $expires;
if (!hash_equals(hash_hmac('sha256', $plain, $this->_hmacKey()), $tokenHash)) {
return new Result(null, Result::FAILURE_CREDENTIALS_INVALID, [
'Cookie token does not match',
]);
}
return new Result($identity, Result::SUCCESS);
}
/**
* Validates a legacy two-part `[username, passwordHashedToken]` token.
*
* Legitimate legacy tokens were created with `password_hash()`, so any
* hash that is not a known `password_hash()` algorithm is rejected
* before it can reach `password_verify()`. This blocks forged
* crypt()-format hashes (e.g. DES, which truncates its input to
* 8 bytes) while all real legacy cookies keep working.
*
* @param array $token The decoded token parts.
* @return \Authentication\Authenticator\ResultInterface
*/
protected function _authenticateLegacyToken(array $token): ResultInterface
{
[$username, $tokenHash] = $token;
if (!is_string($username) || !is_string($tokenHash)) {
return new Result(null, Result::FAILURE_CREDENTIALS_INVALID, [
'Cookie token is invalid.',
]);
}
$info = password_get_info($tokenHash);
if ($info['algoName'] === 'unknown' || !$this->_legacyHashWithinLimits($info)) {
return new Result(null, Result::FAILURE_CREDENTIALS_INVALID, [
'Cookie token is invalid.',
]);
}
$identifier = $this->getIdentifier();
$identity = $identifier->identify(compact('username'));
if (!$identity) {
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $identifier->getErrors());
}
$plain = $this->_createLegacyPlainToken($identity);
if (!$this->getPasswordHasher()->check($plain, $tokenHash)) {
return new Result(null, Result::FAILURE_CREDENTIALS_INVALID, [
'Cookie token does not match',
]);
}
return new Result($identity, Result::SUCCESS);
}
/**
* Checks a legacy token hash uses a bounded work factor.
*
* A forged legacy token could embed a valid bcrypt/argon2 hash with an
* arbitrarily large cost, turning password_verify() into a CPU or memory
* exhaustion vector. Legitimately issued cookies use the PASSWORD_DEFAULT
* parameters, so bounding the work factor rejects abusive hashes without
* affecting real tokens. Bounds come from the `legacyHashLimits` config.
*
* @param array $info Result of password_get_info() for the token hash.
* @return bool
*/
protected function _legacyHashWithinLimits(array $info): bool
{
$limits = $this->getConfig('legacyHashLimits');
$options = $info['options'] ?? [];
if (isset($options['cost']) && $options['cost'] > $limits['cost']) {
return false;
}
if (isset($options['memory_cost']) && $options['memory_cost'] > $limits['memory_cost']) {
return false;
}
return !(isset($options['time_cost']) && $options['time_cost'] > $limits['time_cost']);
}
/**
* Recreates the plain part of a legacy cookie token.
*
* This must match the pre-HMAC token construction byte for byte,
* including the legacy `salt` config semantics, or existing cookies
* would not survive the grace period.
*
* @param \ArrayAccess<string, mixed>|array<string, mixed> $identity Identity data.
* @return string
*/
protected function _createLegacyPlainToken(ArrayAccess|array $identity): string
{
$usernameField = $this->getConfig('fields.username');
$passwordField = $this->getConfig('fields.password');
if ($identity[$usernameField] === null || $identity[$passwordField] === null) {
throw new InvalidArgumentException(
sprintf('Fields %s cannot be found in entity', '`' . $usernameField . '`/`' . $passwordField . '`'),
);
}
$value = $identity[$usernameField] . $identity[$passwordField];
$salt = $this->getConfig('salt', '');
if ($salt === false) {
return $value;
}
if ($salt === true) {
$salt = Security::getSalt();
} elseif (!is_string($salt) || $salt === '') {
throw new InvalidArgumentException('Salt must be a non-empty string.');
}
$hmac = hash_hmac('sha1', $value, $salt);
return $value . $hmac;
}
/**
* Return the password hasher built from the `passwordHasher` config.
*
* Overrides the trait accessor so the config option is honored.
*
* @return \Authentication\PasswordHasher\PasswordHasherInterface
*/
public function getPasswordHasher(): PasswordHasherInterface
{
if (!$this->_passwordHasher instanceof PasswordHasherInterface) {
$this->_passwordHasher = PasswordHasherFactory::build($this->getConfig('passwordHasher'));
}
return $this->_passwordHasher;
}
/**
* @inheritDoc
*/
public function persistIdentity(
ServerRequestInterface $request,
ResponseInterface $response,
ArrayAccess|array $identity,
): array {
$field = $this->getConfig('rememberMeField');
$bodyData = $request->getParsedBody();
if (!$this->_checkUrl($request) || !is_array($bodyData) || empty($bodyData[$field])) {
return [
'request' => $request,
'response' => $response,
];
}
$value = $this->_createToken($identity);
$cookie = $this->_createCookie($value);
return [
'request' => $request,
'response' => $response->withAddedHeader('Set-Cookie', $cookie->toHeaderValue()),
];
}
/**
* Creates a full cookie token serialized as a JSON string.
*
* Cookie token consists of the username, an expiry timestamp, and an
* HMAC-SHA256 of `username + password hash + expires`.
*
* @param \ArrayAccess<string, mixed>|array<string, mixed> $identity Identity data.
* @return string
* @throws \JsonException
*/
protected function _createToken(ArrayAccess|array $identity): string
{
$usernameField = $this->getConfig('fields.username');
$passwordField = $this->getConfig('fields.password');
if ($identity[$usernameField] === null || $identity[$passwordField] === null) {
throw new InvalidArgumentException(
sprintf('Fields %s cannot be found in entity', '`' . $usernameField . '`/`' . $passwordField . '`'),
);
}
$expires = $this->_expiryTimestamp();
$plain = $identity[$usernameField] . $identity[$passwordField] . $expires;
$hash = hash_hmac('sha256', $plain, $this->_hmacKey());
return json_encode([$identity[$usernameField], $expires, $hash], JSON_THROW_ON_ERROR);
}
/**
* Returns the HMAC key for token creation and verification.
*
* The `salt` config is used as the key when it is a string. Any other
* value falls back to the application salt — the HMAC key cannot be
* disabled.
*
* @return string
*/
protected function _hmacKey(): string
{
$salt = $this->getConfig('salt');
if (is_string($salt)) {
if ($salt === '') {
throw new InvalidArgumentException('Salt must be a non-empty string.');
}
return $salt;
}
return Security::getSalt();
}
/**
* Converts the `cookie.expires` config value to a timestamp.
*
* Supported values: `DateTimeInterface` instances, numeric UNIX
* timestamps (consistent with `Cake\Http\Cookie\Cookie`), and
* `strtotime()` compatible strings such as the `+7 days` default.
*
* @return int Timestamp the token will expire at.
*/
protected function _expiryTimestamp(): int
{
$expires = $this->getConfig('cookie.expires');
if ($expires instanceof DateTimeInterface) {
return $expires->getTimestamp();
}
if (is_numeric($expires)) {
return (int)$expires;
}
if (is_string($expires)) {
$time = strtotime($expires);
if ($time !== false) {
return $time;
}
}
throw new UnexpectedValueException('Invalid `cookie.expires` value');
}
/**
* @inheritDoc
*/
public function clearIdentity(ServerRequestInterface $request, ResponseInterface $response): array
{
$cookie = $this->_createCookie('')->withExpired();
return [
'request' => $request,
'response' => $response->withAddedHeader('Set-Cookie', $cookie->toHeaderValue()),
];
}
/**
* Creates a cookie instance with configured defaults.
*
* @param mixed $value Cookie value.
* @return \Cake\Http\Cookie\CookieInterface
*/
protected function _createCookie(mixed $value): CookieInterface
{
$options = $this->getConfig('cookie');
$name = $options['name'];
unset($options['name']);
return Cookie::create(
$name,
$value,
$options,
);
}
}