Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Authenticator/CookieAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,14 @@ protected function _createPlainToken(ArrayAccess|array $identity): string
$usernameField = $this->getConfig('fields.username');
$passwordField = $this->getConfig('fields.password');

$salt = $this->getConfig('salt', '');
if ($identity[$usernameField] === null || $identity[$passwordField] === null) {
throw new InvalidArgumentException(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this create a 500 for the application? Could we use an exception that will render a 40x response instead?

Copy link
Copy Markdown
Member Author

@dereuromark dereuromark Apr 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should, it is a developer error.
It can only run into this if you misconfigured them afaik.

As otherwise it shouldnt even go into this authenticator that far.
They would at bare minimum be empty strings, never null. Null means you set them to impossible values IMO.

sprintf('Fields %s cannot be found in entity', '`' . $usernameField . '`/`' . $passwordField . '`'),
);
}

$value = $identity[$usernameField] . $identity[$passwordField];
$salt = $this->getConfig('salt', '');

if ($salt === false) {
return $value;
Expand Down
31 changes: 31 additions & 0 deletions tests/TestCase/Authenticator/CookieAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,37 @@ public function testPersistIdentityLoginUrlMismatch()
);
}

/**
* @return void
*/
public function testPersistIdentityInvalidConfig()
{
$identifiers = new IdentifierCollection([
'Authentication.Password',
]);

$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/users/login'],
);
$request = $request->withParsedBody([
'remember_me' => 1,
]);
$response = new Response();

$authenticator = new CookieAuthenticator($identifiers, [
'loginUrl' => '/users/login',
]);

$identity = new ArrayObject([
'username' => null,
'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO',
]);

$this->expectException(InvalidArgumentException::class);

$authenticator->persistIdentity($request, $response, $identity);
}

/**
* testClearIdentity
*
Expand Down