Skip to content

Commit 788944a

Browse files
committed
Adjust as per review.
1 parent 1057e2b commit 788944a

File tree

5 files changed

+98
-40
lines changed

5 files changed

+98
-40
lines changed

src/Authenticator/CookieAuthenticator.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use ArrayAccess;
2020
use Authentication\Identifier\AbstractIdentifier;
2121
use Authentication\Identifier\IdentifierCollection;
22+
use Authentication\Identifier\IdentifierInterface;
2223
use Authentication\PasswordHasher\PasswordHasherTrait;
2324
use Authentication\UrlChecker\UrlCheckerTrait;
2425
use Cake\Http\Cookie\Cookie;
@@ -57,12 +58,14 @@ class CookieAuthenticator extends AbstractAuthenticator implements PersistenceIn
5758
];
5859

5960
/**
60-
* @inheritDoc
61+
* Gets the identifier, loading a default Password identifier if none configured.
62+
*
63+
* This is done lazily to allow loadIdentifier() to be called after loadAuthenticator().
64+
*
65+
* @return \Authentication\Identifier\IdentifierInterface
6166
*/
62-
public function authenticate(ServerRequestInterface $request): ResultInterface
67+
public function getIdentifier(): IdentifierInterface
6368
{
64-
// If no identifier is configured, set up a default Password identifier
65-
// This is done lazily to allow loadIdentifier() to be called after loadAuthenticator()
6669
if ($this->_identifier instanceof IdentifierCollection && $this->_identifier->isEmpty()) {
6770
$identifierConfig = [];
6871
if ($this->getConfig('fields')) {
@@ -71,6 +74,14 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
7174
$this->_identifier->load('Authentication.Password', $identifierConfig);
7275
}
7376

77+
return $this->_identifier;
78+
}
79+
80+
/**
81+
* @inheritDoc
82+
*/
83+
public function authenticate(ServerRequestInterface $request): ResultInterface
84+
{
7485
$cookies = $request->getCookieParams();
7586
$cookieName = $this->getConfig('cookie.name');
7687
if (!isset($cookies[$cookieName])) {
@@ -93,10 +104,11 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
93104

94105
[$username, $tokenHash] = $token;
95106

96-
$identity = $this->_identifier->identify(compact('username'));
107+
$identifier = $this->getIdentifier();
108+
$identity = $identifier->identify(compact('username'));
97109

98110
if (!$identity) {
99-
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $this->_identifier->getErrors());
111+
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $identifier->getErrors());
100112
}
101113

102114
if (!$this->_checkToken($identity, $tokenHash)) {

src/Authenticator/FormAuthenticator.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use Authentication\Identifier\AbstractIdentifier;
2020
use Authentication\Identifier\IdentifierCollection;
21+
use Authentication\Identifier\IdentifierInterface;
2122
use Authentication\UrlChecker\UrlCheckerTrait;
2223
use Cake\Routing\Router;
2324
use Psr\Http\Message\ServerRequestInterface;
@@ -49,17 +50,14 @@ class FormAuthenticator extends AbstractAuthenticator
4950
];
5051

5152
/**
52-
* Authenticates the identity contained in a request. Will use the `config.userModel`, and `config.fields`
53-
* to find POST data that is used to find a matching record in the `config.userModel`. Will return false if
54-
* there is no post data, either username or password is missing, or if the scope conditions have not been met.
53+
* Gets the identifier, loading a default Password identifier if none configured.
5554
*
56-
* @param \Psr\Http\Message\ServerRequestInterface $request The request that contains login information.
57-
* @return \Authentication\Authenticator\ResultInterface
55+
* This is done lazily to allow loadIdentifier() to be called after loadAuthenticator().
56+
*
57+
* @return \Authentication\Identifier\IdentifierInterface
5858
*/
59-
public function authenticate(ServerRequestInterface $request): ResultInterface
59+
public function getIdentifier(): IdentifierInterface
6060
{
61-
// If no identifier is configured, set up a default Password identifier
62-
// This is done lazily to allow loadIdentifier() to be called after loadAuthenticator()
6361
if ($this->_identifier instanceof IdentifierCollection && $this->_identifier->isEmpty()) {
6462
$identifierConfig = [];
6563
if ($this->getConfig('fields')) {
@@ -68,6 +66,19 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
6866
$this->_identifier->load('Authentication.Password', $identifierConfig);
6967
}
7068

69+
return $this->_identifier;
70+
}
71+
72+
/**
73+
* Authenticates the identity contained in a request. Will use the `config.userModel`, and `config.fields`
74+
* to find POST data that is used to find a matching record in the `config.userModel`. Will return false if
75+
* there is no post data, either username or password is missing, or if the scope conditions have not been met.
76+
*
77+
* @param \Psr\Http\Message\ServerRequestInterface $request The request that contains login information.
78+
* @return \Authentication\Authenticator\ResultInterface
79+
*/
80+
public function authenticate(ServerRequestInterface $request): ResultInterface
81+
{
7182
if (!$this->_checkUrl($request)) {
7283
return $this->_buildLoginUrlErrorResult($request);
7384
}
@@ -79,10 +90,11 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
7990
]);
8091
}
8192

82-
$user = $this->_identifier->identify($data);
93+
$identifier = $this->getIdentifier();
94+
$user = $identifier->identify($data);
8395

8496
if (!$user) {
85-
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $this->_identifier->getErrors());
97+
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $identifier->getErrors());
8698
}
8799

88100
return new Result($user, Result::SUCCESS);

src/Authenticator/HttpBasicAuthenticator.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use Authentication\Identifier\AbstractIdentifier;
1919
use Authentication\Identifier\IdentifierCollection;
20+
use Authentication\Identifier\IdentifierInterface;
2021
use Psr\Http\Message\ServerRequestInterface;
2122

2223
/**
@@ -43,16 +44,14 @@ class HttpBasicAuthenticator extends AbstractAuthenticator implements StatelessI
4344
];
4445

4546
/**
46-
* Authenticate a user using HTTP auth. Will use the configured User model and attempt a
47-
* login using HTTP auth.
47+
* Gets the identifier, loading a default Password identifier if none configured.
4848
*
49-
* @param \Psr\Http\Message\ServerRequestInterface $request The request to authenticate with.
50-
* @return \Authentication\Authenticator\ResultInterface
49+
* This is done lazily to allow loadIdentifier() to be called after loadAuthenticator().
50+
*
51+
* @return \Authentication\Identifier\IdentifierInterface
5152
*/
52-
public function authenticate(ServerRequestInterface $request): ResultInterface
53+
public function getIdentifier(): IdentifierInterface
5354
{
54-
// If no identifier is configured, set up a default Password identifier
55-
// This is done lazily to allow loadIdentifier() to be called after loadAuthenticator()
5655
if ($this->_identifier instanceof IdentifierCollection && $this->_identifier->isEmpty()) {
5756
$identifierConfig = [];
5857
if ($this->getConfig('fields')) {
@@ -61,6 +60,18 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
6160
$this->_identifier->load('Authentication.Password', $identifierConfig);
6261
}
6362

63+
return $this->_identifier;
64+
}
65+
66+
/**
67+
* Authenticate a user using HTTP auth. Will use the configured User model and attempt a
68+
* login using HTTP auth.
69+
*
70+
* @param \Psr\Http\Message\ServerRequestInterface $request The request to authenticate with.
71+
* @return \Authentication\Authenticator\ResultInterface
72+
*/
73+
public function authenticate(ServerRequestInterface $request): ResultInterface
74+
{
6475
$server = $request->getServerParams();
6576
$username = $server['PHP_AUTH_USER'] ?? '';
6677
$password = $server['PHP_AUTH_PW'] ?? '';
@@ -69,7 +80,7 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
6980
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING);
7081
}
7182

72-
$user = $this->_identifier->identify([
83+
$user = $this->getIdentifier()->identify([
7384
AbstractIdentifier::CREDENTIAL_USERNAME => $username,
7485
AbstractIdentifier::CREDENTIAL_PASSWORD => $password,
7586
]);

src/Authenticator/JwtAuthenticator.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ public function __construct(IdentifierInterface $identifier, array $config = [])
6767
}
6868
}
6969

70+
/**
71+
* Gets the identifier, loading a default JwtSubject identifier if none configured.
72+
*
73+
* This is done lazily to allow loadIdentifier() to be called after loadAuthenticator().
74+
*
75+
* @return \Authentication\Identifier\IdentifierInterface
76+
*/
77+
public function getIdentifier(): IdentifierInterface
78+
{
79+
if ($this->_identifier instanceof IdentifierCollection && $this->_identifier->isEmpty()) {
80+
$this->_identifier->load('Authentication.JwtSubject');
81+
}
82+
83+
return $this->_identifier;
84+
}
85+
7086
/**
7187
* Authenticates the identity based on a JWT token contained in a request.
7288
*
@@ -76,12 +92,6 @@ public function __construct(IdentifierInterface $identifier, array $config = [])
7692
*/
7793
public function authenticate(ServerRequestInterface $request): ResultInterface
7894
{
79-
// If no identifier is configured, set up a default JwtSubject identifier
80-
// This is done lazily to allow loadIdentifier() to be called after loadAuthenticator()
81-
if ($this->_identifier instanceof IdentifierCollection && $this->_identifier->isEmpty()) {
82-
$this->_identifier->load('Authentication.JwtSubject');
83-
}
84-
8595
try {
8696
$result = $this->getPayload($request);
8797
} catch (Exception $e) {
@@ -113,12 +123,13 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
113123
return new Result($user, Result::SUCCESS);
114124
}
115125

116-
$user = $this->_identifier->identify([
126+
$identifier = $this->getIdentifier();
127+
$user = $identifier->identify([
117128
$subjectKey => $result[$subjectKey],
118129
]);
119130

120131
if (!$user) {
121-
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $this->_identifier->getErrors());
132+
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $identifier->getErrors());
122133
}
123134

124135
return new Result($user, Result::SUCCESS);

src/Authenticator/TokenAuthenticator.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace Authentication\Authenticator;
1818

1919
use Authentication\Identifier\IdentifierCollection;
20+
use Authentication\Identifier\IdentifierInterface;
2021
use Authentication\Identifier\TokenIdentifier;
2122
use Psr\Http\Message\ServerRequestInterface;
2223

@@ -36,6 +37,22 @@ class TokenAuthenticator extends AbstractAuthenticator implements StatelessInter
3637
'tokenPrefix' => null,
3738
];
3839

40+
/**
41+
* Gets the identifier, loading a default Token identifier if none configured.
42+
*
43+
* This is done lazily to allow loadIdentifier() to be called after loadAuthenticator().
44+
*
45+
* @return \Authentication\Identifier\IdentifierInterface
46+
*/
47+
public function getIdentifier(): IdentifierInterface
48+
{
49+
if ($this->_identifier instanceof IdentifierCollection && $this->_identifier->isEmpty()) {
50+
$this->_identifier->load('Authentication.Token');
51+
}
52+
53+
return $this->_identifier;
54+
}
55+
3956
/**
4057
* Checks if the token is in the headers or a request parameter
4158
*
@@ -120,23 +137,18 @@ protected function getTokenFromQuery(ServerRequestInterface $request, ?string $q
120137
*/
121138
public function authenticate(ServerRequestInterface $request): ResultInterface
122139
{
123-
// If no identifier is configured, set up a default Token identifier
124-
// This is done lazily to allow loadIdentifier() to be called after loadAuthenticator()
125-
if ($this->_identifier instanceof IdentifierCollection && $this->_identifier->isEmpty()) {
126-
$this->_identifier->load('Authentication.Token');
127-
}
128-
129140
$token = $this->getToken($request);
130141
if ($token === null) {
131142
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING);
132143
}
133144

134-
$user = $this->_identifier->identify([
145+
$identifier = $this->getIdentifier();
146+
$user = $identifier->identify([
135147
TokenIdentifier::CREDENTIAL_TOKEN => $token,
136148
]);
137149

138150
if (!$user) {
139-
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $this->_identifier->getErrors());
151+
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND, $identifier->getErrors());
140152
}
141153

142154
return new Result($user, Result::SUCCESS);

0 commit comments

Comments
 (0)