-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathTokenAuthenticator.php
More file actions
165 lines (145 loc) · 5.34 KB
/
TokenAuthenticator.php
File metadata and controls
165 lines (145 loc) · 5.34 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
<?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 Authentication\Identifier\IdentifierCollection;
use Authentication\Identifier\IdentifierInterface;
use Authentication\Identifier\TokenIdentifier;
use Psr\Http\Message\ServerRequestInterface;
/**
* Token Authenticator
*
* Authenticates an identity based on a token in a query param or the header.
*/
class TokenAuthenticator extends AbstractAuthenticator implements StatelessInterface
{
/**
* @inheritDoc
*/
protected array $_defaultConfig = [
'header' => null,
'queryParam' => null,
'tokenPrefix' => null,
];
/**
* Constructor
*
* @param \Authentication\Identifier\IdentifierInterface $identifier Identifier or identifiers collection.
* @param array<string, mixed> $config Configuration settings.
*/
public function __construct(IdentifierInterface $identifier, array $config = [])
{
// If no identifier is configured, set up a default Token identifier
if ($identifier instanceof IdentifierCollection && $identifier->isEmpty()) {
$identifier = new IdentifierCollection(['Authentication.Token']);
}
parent::__construct($identifier, $config);
}
/**
* Checks if the token is in the headers or a request parameter
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request that contains login information.
* @return string|null
*/
protected function getToken(ServerRequestInterface $request): ?string
{
$token = $this->getTokenFromHeader($request, $this->getConfig('header'))
?? $this->getTokenFromQuery($request, $this->getConfig('queryParam'));
$prefix = $this->getConfig('tokenPrefix');
if ($prefix !== null && $token !== null) {
return $this->stripTokenPrefix($token, $prefix);
}
return $token;
}
/**
* Strips a prefix from a token
*
* @param string $token Token string
* @param string $prefix Prefix to strip
* @return string
*/
protected function stripTokenPrefix(string $token, string $prefix): string
{
$prefixLength = mb_strlen($prefix);
if (mb_substr(mb_strtolower($token), 0, $prefixLength) === mb_strtolower($prefix)) {
$token = mb_substr($token, $prefixLength);
}
return trim($token);
}
/**
* Gets the token from the request headers
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request that contains login information.
* @param string|null $headerLine Header name
* @return string|null
*/
protected function getTokenFromHeader(ServerRequestInterface $request, ?string $headerLine): ?string
{
if ($headerLine) {
$header = $request->getHeaderLine($headerLine);
if ($header) {
return $header;
}
}
return null;
}
/**
* Gets the token from the request query
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request that contains login information.
* @param string|null $queryParam Request query parameter name
* @return string|null
*/
protected function getTokenFromQuery(ServerRequestInterface $request, ?string $queryParam): ?string
{
if (!$queryParam) {
return null;
}
$queryParams = $request->getQueryParams();
return $queryParams[$queryParam] ?? null;
}
/**
* Authenticates the identity by token contained in a request.
* Token could be passed as query using `config.queryParam` or as header param using `config.header`. Token
* prefix will be stripped if `config.tokenPrefix` is set. Will return false if no token is provided or if the
* scope conditions have not been met.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request that contains login information.
* @return \Authentication\Authenticator\ResultInterface
*/
public function authenticate(ServerRequestInterface $request): ResultInterface
{
$token = $this->getToken($request);
if ($token === null) {
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING);
}
$user = $this->_identifier->identify([
TokenIdentifier::CREDENTIAL_TOKEN => $token,
]);
if (!$user) {
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $this->_identifier->getErrors());
}
return new Result($user, Result::SUCCESS);
}
/**
* No-op method.
*
* @param \Psr\Http\Message\ServerRequestInterface $request A request object.
* @return void
*/
public function unauthorizedChallenge(ServerRequestInterface $request): void
{
}
}