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
26 changes: 21 additions & 5 deletions src/Controller/Component/AuthenticationComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\Routing\Router;
use Cake\Utility\Hash;
use Exception;
Expand Down Expand Up @@ -311,7 +312,7 @@ public function setIdentity(ArrayAccess|array $identity)
$identity,
);

$controller->setRequest($result['request']);
$this->updateRequest($result['request']);
$controller->setResponse($result['response']);

return $this;
Expand Down Expand Up @@ -339,7 +340,7 @@ public function replaceIdentity(ArrayAccess|array $identity)

$identity = $service->buildIdentity($identity);

$controller->setRequest(
$this->updateRequest(
$controller->getRequest()->withAttribute(
$service->getIdentityAttribute(),
$identity,
Expand All @@ -365,7 +366,7 @@ public function logout(): ?string
$controller->getResponse(),
);

$controller->setRequest($result['request']);
$this->updateRequest($result['request']);
$controller->setResponse($result['response']);

$this->dispatchEvent('Authentication.logout', [], $controller);
Expand Down Expand Up @@ -462,7 +463,7 @@ public function impersonate(ArrayAccess $impersonated)
throw new UnexpectedValueException('An error has occurred impersonating user.');
}

$controller->setRequest($result['request']);
$this->updateRequest($result['request']);
$controller->setResponse($result['response']);

return $this;
Expand Down Expand Up @@ -491,7 +492,7 @@ public function stopImpersonating()
throw new UnexpectedValueException('An error has occurred stopping impersonation.');
}

$controller->setRequest($result['request']);
$this->updateRequest($result['request']);
$controller->setResponse($result['response']);

return $this;
Expand Down Expand Up @@ -537,4 +538,19 @@ protected function getImpersonationAuthenticationService(): ImpersonationInterfa

return $service;
}

/**
* Set the request on the controller and on the Router.
*
* Keeping Router in sync ensures that errors raised later in the request
* are rendered with the current identity.
*
* @param \Cake\Http\ServerRequest $request The request.
* @return void
*/
protected function updateRequest(ServerRequest $request): void
{
$this->getController()->setRequest($request);
Router::setRequest($request);
}
}
7 changes: 7 additions & 0 deletions src/Middleware/AuthenticationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Authentication\Authenticator\UnauthenticatedException;
use Cake\Core\ContainerApplicationInterface;
use Cake\Core\ContainerInterface;
use Cake\Http\ServerRequest;
use Cake\Routing\Router;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\RedirectResponse;
use Laminas\Diactoros\Stream;
Expand Down Expand Up @@ -98,6 +100,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$request = $request->withAttribute($service->getIdentityAttribute(), $service->getIdentity());
$request = $request->withAttribute('authentication', $service);
$request = $request->withAttribute('authenticationResult', $result);
if ($request instanceof ServerRequest && class_exists(Router::class)) {
// Keep Router in sync so that errors raised by later middleware
// are rendered for the authenticated request.
Router::setRequest($request);
}

try {
$response = $handler->handle($request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public function testSetIdentity(): void
$component->setIdentity($identityData);
$result = $component->getIdentity();
$this->assertSame($identityData, $result->getOriginalData());
$this->assertSame($controller->getRequest(), Router::getRequest());
}

/**
Expand Down Expand Up @@ -303,6 +304,7 @@ public function testReplaceIdentity(): void
$controller->getRequest()->getSession()->read('Auth'),
'Session must not be written by replaceIdentity().',
);
$this->assertSame($controller->getRequest(), Router::getRequest());
}

/**
Expand Down Expand Up @@ -479,6 +481,7 @@ public function testLogout(): void
$this->assertSame('florian', $controller->getRequest()->getAttribute('identity')->username);
$component->logout();
$this->assertNull($controller->getRequest()->getAttribute('identity'));
$this->assertSame($controller->getRequest(), Router::getRequest());
$this->assertInstanceOf(Event::class, $result);
$this->assertSame('Authentication.logout', $result->getName());
}
Expand Down
25 changes: 25 additions & 0 deletions tests/TestCase/Middleware/AuthenticationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Cake\Core\TestSuite\ContainerStubTrait;
use Cake\Http\Response;
use Cake\Http\ServerRequestFactory;
use Cake\Routing\Router;
use Firebase\JWT\JWT;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use TestApp\Application;
Expand Down Expand Up @@ -91,6 +92,30 @@ public function testApplicationAuthentication(): void
$this->assertTrue($service->authenticators()->has('Form'));
}

/**
* Test that the authenticated request is stored in Router so that errors
* thrown by later middleware are rendered with the identity.
*/
public function testRouterRequestIsUpdated(): void
{
$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/testpath'],
[],
['username' => 'mariano', 'password' => 'password'],
);
Router::setRequest($request);
$handler = new TestRequestHandler();

$middleware = new AuthenticationMiddleware($this->service);
$middleware->process($request, $handler);

$routerRequest = Router::getRequest();
$this->assertSame($handler->request, $routerRequest);
$this->assertSame($this->service, $routerRequest->getAttribute('authentication'));
$this->assertSame('mariano', $routerRequest->getAttribute('identity')->username);
$this->assertNotNull($routerRequest->getAttribute('authenticationResult'));
}

public function testProviderAuthentication(): void
{
$request = ServerRequestFactory::fromGlobals(
Expand Down
Loading