Skip to content

Commit 1366eb4

Browse files
authored
Merge pull request #718 from cakephp/cookie-fix
Alert about wrong configuration.
2 parents 6e4d461 + 230dbd3 commit 1366eb4

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Authenticator/CookieAuthenticator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,14 @@ protected function _createPlainToken(ArrayAccess|array $identity): string
134134
$usernameField = $this->getConfig('fields.username');
135135
$passwordField = $this->getConfig('fields.password');
136136

137-
$salt = $this->getConfig('salt', '');
137+
if ($identity[$usernameField] === null || $identity[$passwordField] === null) {
138+
throw new InvalidArgumentException(
139+
sprintf('Fields %s cannot be found in entity', '`' . $usernameField . '`/`' . $passwordField . '`'),
140+
);
141+
}
138142

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

141146
if ($salt === false) {
142147
return $value;

tests/TestCase/Authenticator/CookieAuthenticatorTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,37 @@ public function testPersistIdentityLoginUrlMismatch()
382382
);
383383
}
384384

385+
/**
386+
* @return void
387+
*/
388+
public function testPersistIdentityInvalidConfig()
389+
{
390+
$identifiers = new IdentifierCollection([
391+
'Authentication.Password',
392+
]);
393+
394+
$request = ServerRequestFactory::fromGlobals(
395+
['REQUEST_URI' => '/users/login'],
396+
);
397+
$request = $request->withParsedBody([
398+
'remember_me' => 1,
399+
]);
400+
$response = new Response();
401+
402+
$authenticator = new CookieAuthenticator($identifiers, [
403+
'loginUrl' => '/users/login',
404+
]);
405+
406+
$identity = new ArrayObject([
407+
'username' => null,
408+
'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO',
409+
]);
410+
411+
$this->expectException(InvalidArgumentException::class);
412+
413+
$authenticator->persistIdentity($request, $response, $identity);
414+
}
415+
385416
/**
386417
* testClearIdentity
387418
*

0 commit comments

Comments
 (0)