Skip to content

Commit 242b12a

Browse files
committed
Fix up identifier defaulting.
1 parent 5b62daf commit 242b12a

10 files changed

Lines changed: 54 additions & 15 deletions

src/Authenticator/AbstractAuthenticator.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Authentication\Identifier\PasswordIdentifier;
2121
use Cake\Core\InstanceConfigTrait;
2222
use Psr\Http\Message\ServerRequestInterface;
23+
use RuntimeException;
2324

2425
abstract class AbstractAuthenticator implements AuthenticatorInterface
2526
{
@@ -41,28 +42,38 @@ abstract class AbstractAuthenticator implements AuthenticatorInterface
4142
/**
4243
* Identifier instance.
4344
*
44-
* @var \Authentication\Identifier\IdentifierInterface|null
45+
* @var \Authentication\Identifier\IdentifierInterface
4546
*/
46-
protected ?IdentifierInterface $_identifier = null;
47+
protected IdentifierInterface $_identifier;
4748

4849
/**
4950
* Constructor
5051
*
5152
* @param \Authentication\Identifier\IdentifierInterface|null $identifier Identifier instance.
5253
* @param array<string, mixed> $config Configuration settings.
54+
* @throws \RuntimeException When identifier is null and the authenticator doesn't provide a default.
5355
*/
5456
public function __construct(?IdentifierInterface $identifier, array $config = [])
5557
{
58+
if ($identifier === null) {
59+
throw new RuntimeException(
60+
sprintf(
61+
'Identifier is required for `%s`. Please provide an identifier instance.',
62+
static::class,
63+
),
64+
);
65+
}
66+
5667
$this->_identifier = $identifier;
5768
$this->setConfig($config);
5869
}
5970

6071
/**
6172
* Gets the identifier.
6273
*
63-
* @return \Authentication\Identifier\IdentifierInterface|null
74+
* @return \Authentication\Identifier\IdentifierInterface
6475
*/
65-
public function getIdentifier(): ?IdentifierInterface
76+
public function getIdentifier(): IdentifierInterface
6677
{
6778
return $this->_identifier;
6879
}

src/Authenticator/CookieAuthenticator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
105105

106106
[$username, $tokenHash] = $token;
107107

108-
assert($this->_identifier !== null);
109108
$identity = $this->_identifier->identify(compact('username'));
110109

111110
if (!$identity) {

src/Authenticator/EnvironmentAuthenticator.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
namespace Authentication\Authenticator;
1818

19+
use Authentication\Identifier\IdentifierFactory;
20+
use Authentication\Identifier\IdentifierInterface;
1921
use Authentication\UrlChecker\UrlCheckerTrait;
2022
use Cake\Routing\Router;
2123
use Psr\Http\Message\ServerRequestInterface;
@@ -45,6 +47,21 @@ class EnvironmentAuthenticator extends AbstractAuthenticator
4547
'optionalFields' => [],
4648
];
4749

50+
/**
51+
* Constructor
52+
*
53+
* @param \Authentication\Identifier\IdentifierInterface|null $identifier Identifier instance.
54+
* @param array<string, mixed> $config Configuration settings.
55+
*/
56+
public function __construct(?IdentifierInterface $identifier, array $config = [])
57+
{
58+
if ($identifier === null) {
59+
$identifier = IdentifierFactory::create('Authentication.Callback');
60+
}
61+
62+
parent::__construct($identifier, $config);
63+
}
64+
4865
/**
4966
* Get values from the environment variables configured by `fields`.
5067
*
@@ -153,7 +170,6 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
153170

154171
$data = array_merge($this->_getOptionalData($request), $data);
155172

156-
assert($this->_identifier !== null);
157173
$user = $this->_identifier->identify($data);
158174

159175
if (!$user) {

src/Authenticator/FormAuthenticator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
159159
]);
160160
}
161161

162-
assert($this->_identifier !== null);
163162
$user = $this->_identifier->identify($data);
164163

165164
if (!$user) {

src/Authenticator/HttpBasicAuthenticator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
8181
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING);
8282
}
8383

84-
assert($this->_identifier !== null);
8584
$user = $this->_identifier->identify([
8685
PasswordIdentifier::CREDENTIAL_USERNAME => $username,
8786
PasswordIdentifier::CREDENTIAL_PASSWORD => $password,

src/Authenticator/HttpDigestAuthenticator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
9494
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING);
9595
}
9696

97-
assert($this->_identifier !== null);
9897
$user = $this->_identifier->identify([
9998
PasswordIdentifier::CREDENTIAL_USERNAME => $digest['username'],
10099
]);

src/Authenticator/JwtAuthenticator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
9999
return new Result(null, Result::FAILURE_CREDENTIALS_INVALID);
100100
}
101101

102-
/** @phpstan-ignore-next-line */
103-
$result = json_decode(json_encode($result), true);
102+
$result = json_decode((string)json_encode($result), true);
104103

105104
$subjectKey = $this->getConfig('subjectKey');
106105
if (empty($result[$subjectKey])) {
@@ -113,7 +112,6 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
113112
return new Result($user, Result::SUCCESS);
114113
}
115114

116-
assert($this->_identifier !== null);
117115
$user = $this->_identifier->identify([
118116
$subjectKey => $result[$subjectKey],
119117
]);

src/Authenticator/PrimaryKeySessionAuthenticator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
4545
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND);
4646
}
4747

48-
assert($this->_identifier !== null);
4948
$user = $this->_identifier->identify([$this->getConfig('identifierKey') => $userId]);
5049
if (!$user) {
5150
return new Result(null, Result::FAILURE_IDENTITY_NOT_FOUND);

src/Authenticator/SessionAuthenticator.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
use ArrayAccess;
1919
use ArrayObject;
20+
use Authentication\Identifier\IdentifierFactory;
21+
use Authentication\Identifier\IdentifierInterface;
2022
use Authentication\Identifier\PasswordIdentifier;
2123
use Cake\Http\Exception\UnauthorizedException;
2224
use Psr\Http\Message\ResponseInterface;
@@ -47,6 +49,25 @@ class SessionAuthenticator extends AbstractAuthenticator implements PersistenceI
4749
'identityAttribute' => 'identity',
4850
];
4951

52+
/**
53+
* Constructor
54+
*
55+
* @param \Authentication\Identifier\IdentifierInterface|null $identifier Identifier instance.
56+
* @param array<string, mixed> $config Configuration settings.
57+
*/
58+
public function __construct(?IdentifierInterface $identifier, array $config = [])
59+
{
60+
if ($identifier === null) {
61+
$identifierConfig = [];
62+
if (isset($config['fields'])) {
63+
$identifierConfig['fields'] = $config['fields'];
64+
}
65+
$identifier = IdentifierFactory::create('Authentication.Password', $identifierConfig);
66+
}
67+
68+
parent::__construct($identifier, $config);
69+
}
70+
5071
/**
5172
* Authenticate a user using session data.
5273
*
@@ -69,7 +90,6 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
6990
foreach ($this->getConfig('fields') as $key => $field) {
7091
$credentials[$key] = $user[$field];
7192
}
72-
assert($this->_identifier !== null);
7393
$user = $this->_identifier->identify($credentials);
7494

7595
if (!$user) {

src/Authenticator/TokenAuthenticator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ public function authenticate(ServerRequestInterface $request): ResultInterface
142142
return new Result(null, Result::FAILURE_CREDENTIALS_MISSING);
143143
}
144144

145-
assert($this->_identifier !== null);
146145
$user = $this->_identifier->identify([
147146
TokenIdentifier::CREDENTIAL_TOKEN => $token,
148147
]);

0 commit comments

Comments
 (0)