From 7c27013c4e8863b0dd2977361807d1852010f9fe Mon Sep 17 00:00:00 2001 From: bancer Date: Mon, 9 Nov 2026 10:18:44 +0100 Subject: [PATCH 1/3] Forward port of #802 - fix AuthenticationComponent::setIdentity() * This ensures that remember_me cookie is cleared after the user that already has this cookie logs in without remember_me checked. * Keeps the request in Router up to date, so that e.g. the error page rendered after an exception thrown later in the same request knows the new identity. --- .../Component/AuthenticationComponent.php | 9 ++- .../Component/AuthenticationComponentTest.php | 57 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/Controller/Component/AuthenticationComponent.php b/src/Controller/Component/AuthenticationComponent.php index 71a24008..9518877f 100644 --- a/src/Controller/Component/AuthenticationComponent.php +++ b/src/Controller/Component/AuthenticationComponent.php @@ -302,7 +302,11 @@ public function setIdentity(ArrayAccess|array $identity) $controller = $this->getController(); $service = $this->getAuthenticationService(); - $service->clearIdentity($controller->getRequest(), $controller->getResponse()); + /** @psalm-var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $result */ + $result = $service->clearIdentity($controller->getRequest(), $controller->getResponse()); + + $controller->setRequest($result['request']); + $controller->setResponse($result['response']); /** @var array{request: \Cake\Http\ServerRequest, response: \Cake\Http\Response} $result */ $result = $service->persistIdentity( @@ -313,6 +317,9 @@ public function setIdentity(ArrayAccess|array $identity) $controller->setRequest($result['request']); $controller->setResponse($result['response']); + // Keep the request in Router up to date, so that e.g. the error page rendered after an exception thrown + // later in the same request knows the new identity. + Router::setRequest($result['request']); return $this; } diff --git a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php index 9e42a170..5403451e 100644 --- a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php +++ b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php @@ -280,6 +280,63 @@ public function testSetIdentityOverwrite(): void ); } + public function testSetIdentityWithCookieAuthDoNotRememberMe() + { + $service = new AuthenticationService([ + 'identifiers' => [ + 'Authentication.Password', + ], + 'authenticators' => [ + 'Authentication.Session', + 'Authentication.Form', + 'Authentication.Cookie', + ], + ]); + $request = $this->request->withAttribute('authentication', $service) + ->withData('remember_me', 0); + + $controller = new Controller($request, $this->response); + $registry = new ComponentRegistry($controller); + $component = new AuthenticationComponent($registry); + + $component->setIdentity($this->identityData); + $result = $component->getIdentity(); + $this->assertSame($this->identityData, $result->getOriginalData()); + $expectedCookieHeader = [ + 'CookieAuth=; expires=Thu, 01-Jan-1970 00:00:01 GMT+0000; path=/', + ]; + $actualCookieHeader = $controller->getResponse()->getHeader('Set-Cookie'); + $this->assertSame($expectedCookieHeader, $actualCookieHeader); + } + + public function testSetIdentityWithCookieAuthRememberMe() + { + $service = new AuthenticationService([ + 'identifiers' => [ + 'Authentication.Password', + ], + 'authenticators' => [ + 'Authentication.Session', + 'Authentication.Form', + 'Authentication.Cookie', + ], + ]); + $request = $this->request->withAttribute('authentication', $service) + ->withData('remember_me', 1); + + $controller = new Controller($request, $this->response); + $registry = new ComponentRegistry($controller); + $component = new AuthenticationComponent($registry); + + $component->setIdentity($this->identityData); + $result = $component->getIdentity(); + $this->assertSame($this->identityData, $result->getOriginalData()); + $actualCookieHeader = $controller->getResponse()->getHeader('Set-Cookie'); + $this->assertCount(2, $actualCookieHeader); + $this->assertStringContainsString('CookieAuth=', $actualCookieHeader[1]); + $this->assertStringNotContainsString('expires=Thu, 01-Jan-1970 00:00:01 GMT+0000;', $actualCookieHeader[1]); + } + /** * Ensure replaceIdentity() swaps the request attribute without * touching the session. From 73c20bcdaafc7d8a8605bea9b40702358bf7c799 Mon Sep 17 00:00:00 2001 From: bancer Date: Mon, 9 Nov 2026 13:51:50 +0100 Subject: [PATCH 2/3] Fix failed tests --- .../Component/AuthenticationComponentTest.php | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php index 5403451e..6f5ad5e4 100644 --- a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php +++ b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php @@ -280,22 +280,25 @@ public function testSetIdentityOverwrite(): void ); } - public function testSetIdentityWithCookieAuthDoNotRememberMe() + public function testSetIdentityWithCookieAuthDoNotRememberMe(): void { $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Session', - 'Authentication.Form', - 'Authentication.Cookie', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], + 'Authentication.Cookie' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); $request = $this->request->withAttribute('authentication', $service) ->withData('remember_me', 0); - $controller = new Controller($request, $this->response); + $controller = new Controller($request); $registry = new ComponentRegistry($controller); $component = new AuthenticationComponent($registry); @@ -309,28 +312,35 @@ public function testSetIdentityWithCookieAuthDoNotRememberMe() $this->assertSame($expectedCookieHeader, $actualCookieHeader); } - public function testSetIdentityWithCookieAuthRememberMe() + public function testSetIdentityWithCookieAuthRememberMe(): void { $service = new AuthenticationService([ - 'identifiers' => [ - 'Authentication.Password', - ], 'authenticators' => [ - 'Authentication.Session', - 'Authentication.Form', - 'Authentication.Cookie', + 'Authentication.Session' => [ + 'identifier' => 'Authentication.Password', + ], + 'Authentication.Form' => [ + 'identifier' => 'Authentication.Password', + ], + 'Authentication.Cookie' => [ + 'identifier' => 'Authentication.Password', + ], ], ]); $request = $this->request->withAttribute('authentication', $service) ->withData('remember_me', 1); - $controller = new Controller($request, $this->response); + $controller = new Controller($request); $registry = new ComponentRegistry($controller); $component = new AuthenticationComponent($registry); - $component->setIdentity($this->identityData); + $identityData = new Entity([ + 'username' => 'florian', + 'password' => '$2y$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO', + ]); + $component->setIdentity($identityData); $result = $component->getIdentity(); - $this->assertSame($this->identityData, $result->getOriginalData()); + $this->assertSame($identityData, $result->getOriginalData()); $actualCookieHeader = $controller->getResponse()->getHeader('Set-Cookie'); $this->assertCount(2, $actualCookieHeader); $this->assertStringContainsString('CookieAuth=', $actualCookieHeader[1]); From 241e1f83c52c13488e5d1e2d86c38baae3ec3a0b Mon Sep 17 00:00:00 2001 From: bancer Date: Mon, 9 Nov 2026 14:03:52 +0100 Subject: [PATCH 3/3] Remove empty line --- .../Controller/Component/AuthenticationComponentTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php index 6f5ad5e4..17326239 100644 --- a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php +++ b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php @@ -301,8 +301,8 @@ public function testSetIdentityWithCookieAuthDoNotRememberMe(): void $controller = new Controller($request); $registry = new ComponentRegistry($controller); $component = new AuthenticationComponent($registry); - $component->setIdentity($this->identityData); + $result = $component->getIdentity(); $this->assertSame($this->identityData, $result->getOriginalData()); $expectedCookieHeader = [