-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathCookieAuthenticator.php
65 lines (56 loc) · 1.93 KB
/
CookieAuthenticator.php
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
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Auth\Authenticator;
use Authentication\Authenticator\CookieAuthenticator as BaseAuthenticator;
use Authentication\Authenticator\PersistenceInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Cookie Authenticator
*
* Authenticates an identity based on a cookies data.
*/
class CookieAuthenticator extends BaseAuthenticator implements PersistenceInterface
{
/**
* Session key used to save tmp data
*/
public const SESSION_DATA_KEY = 'CookieAuth';
/**
* @inheritDoc
*/
public function persistIdentity(ServerRequestInterface $request, ResponseInterface $response, $identity): array
{
$field = $this->getConfig('rememberMeField');
$bodyData = $request->getParsedBody();
if (empty($bodyData)) {
/**
* @var \Cake\Http\Session $session
*/
$session = $request->getAttribute('session');
$bodyData = $session->read(self::SESSION_DATA_KEY);
$session->delete(self::SESSION_DATA_KEY);
}
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()),
];
}
}