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..17326239 100644 --- a/tests/TestCase/Controller/Component/AuthenticationComponentTest.php +++ b/tests/TestCase/Controller/Component/AuthenticationComponentTest.php @@ -280,6 +280,73 @@ public function testSetIdentityOverwrite(): void ); } + public function testSetIdentityWithCookieAuthDoNotRememberMe(): void + { + $service = new AuthenticationService([ + 'authenticators' => [ + '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); + $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(): void + { + $service = new AuthenticationService([ + 'authenticators' => [ + '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); + $registry = new ComponentRegistry($controller); + $component = new AuthenticationComponent($registry); + + $identityData = new Entity([ + 'username' => 'florian', + 'password' => '$2y$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO', + ]); + $component->setIdentity($identityData); + $result = $component->getIdentity(); + $this->assertSame($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.