-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRememberMeTokenIdentifier.php
More file actions
186 lines (157 loc) · 5.53 KB
/
Copy pathRememberMeTokenIdentifier.php
File metadata and controls
186 lines (157 loc) · 5.53 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
<?php
declare(strict_types=1);
namespace RememberMe\Identifier;
use ArrayAccess;
use Authentication\Identifier\AbstractIdentifier;
use Authentication\Identifier\Resolver\OrmResolver;
use Authentication\Identifier\Resolver\ResolverAwareTrait;
use Authentication\Identifier\Resolver\ResolverInterface;
use Cake\Datasource\EntityInterface;
use Cake\I18n\DateTime;
use InvalidArgumentException;
use RuntimeException;
/**
* Class RememberMeTokenIdentifier
*
* @method \Authentication\Identifier\Resolver\OrmResolver getResolver()
*/
class RememberMeTokenIdentifier extends AbstractIdentifier
{
use ResolverAwareTrait {
buildResolver as traitBuildResolver;
}
protected const CREDENTIAL_TOKEN = 'token';
protected const CREDENTIAL_SERIES = 'series';
/**
* Default configuration.
* - `fields` The fields to use to identify a user by:
* - `username`: one or many username fields.
* - `resolver` The resolver implementation to use. the class must be
* OrmResolver or the inherited class.
* - `tokenStorageModel`: A model used for storing login cookie tokens.
* - `userTokenFieldName`: A property name when adding token data to identity.
*
* @var array
*/
protected array $_defaultConfig = [
'fields' => [
self::CREDENTIAL_USERNAME => 'username',
],
'resolver' => 'Authentication.Orm',
'tokenStorageModel' => 'RememberMe.RememberMeTokens',
'userTokenFieldName' => 'remember_me_token',
];
/**
* @inheritDoc
*/
protected function buildResolver(array|string $config): OrmResolver
{
$instance = $this->traitBuildResolver($config);
if (!$instance instanceof OrmResolver) {
$message = sprintf('Resolver must implement `%s`.', OrmResolver::class);
throw new RuntimeException($message);
}
return $instance;
}
/**
* @inheritDoc
*/
public function identify(array $credentials): ArrayAccess|array|null
{
if (
!isset(
$credentials[self::CREDENTIAL_USERNAME],
$credentials[self::CREDENTIAL_SERIES],
$credentials[self::CREDENTIAL_TOKEN],
)
) {
return null;
}
$identity = $this->_findIdentity($credentials[self::CREDENTIAL_USERNAME]);
if (!$identity instanceof EntityInterface) {
return null;
}
$token = $this->_findToken($identity, $credentials[self::CREDENTIAL_SERIES]);
if ($token === null) {
return null;
}
if (!$this->_verifyToken($token, $credentials[self::CREDENTIAL_TOKEN])) {
$this->_dropInvalidToken($token);
return null;
}
$identity->set($this->getConfig('userTokenFieldName'), $token);
return $identity;
}
/**
* Find a user record using the username/identifier provided.
*
* @param string $identifier The username/identifier.
* @return \Cake\Datasource\EntityInterface|\ArrayAccess|array|null
*/
protected function _findIdentity(string $identifier): ArrayAccess|array|EntityInterface|null
{
$fields = $this->getConfig('fields.' . self::CREDENTIAL_USERNAME);
$conditions = [];
foreach ((array)$fields as $field) {
$conditions[$field] = $identifier;
}
return $this->getResolver()->find($conditions, ResolverInterface::TYPE_OR);
}
/**
* find some user's remember me token.
*
* @param \Cake\Datasource\EntityInterface $identity the identity
* @param string $series the credential series
* @return \Cake\Datasource\EntityInterface|null
*/
protected function _findToken(EntityInterface $identity, string $series): ?EntityInterface
{
$userModel = $identity->getSource();
if ($userModel === '') {
throw new InvalidArgumentException('Can\'t get user model from identity.');
}
$usersTable = $this->getResolver()->fetchTable($userModel);
$tokenStorageTable = $this->getResolver()->fetchTable($this->getConfig('tokenStorageModel'));
$primaryKey = $usersTable->getPrimaryKey();
if (!is_string($primaryKey)) {
throw new InvalidArgumentException('User model must have a single primary key.');
}
return $tokenStorageTable->find()
->where([
'model' => $userModel,
'foreign_id' => $identity->get($primaryKey),
'series' => $series,
])
->first();
}
/**
* verify user token, match and expires
*
* @param \Cake\Datasource\EntityInterface $token the remember-me token
* @param string $verifyToken token from credentials
* @return bool
*/
protected function _verifyToken(EntityInterface $token, string $verifyToken): bool
{
if ($token['token'] !== $verifyToken) {
$this->_errors[] = 'token does not match';
return false;
}
if (DateTime::now()->greaterThan($token['expires'])) {
$this->_errors[] = 'token expired';
return false;
}
return true;
}
/**
* drop invalid token
*
* @param \Cake\Datasource\EntityInterface $token the remember-me token
* @return bool
*/
protected function _dropInvalidToken(EntityInterface $token): bool
{
$tokenStorageTable = $this->getResolver()->fetchTable($this->getConfig('tokenStorageModel'));
return $tokenStorageTable->delete($token);
}
}