65c1b5fb75
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
49 lines
1.7 KiB
PHP
49 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace KTXC\Http\Middleware;
|
|
|
|
use KTXC\Http\Request\Request;
|
|
use KTXC\Http\Response\Response;
|
|
use KTXC\Service\SecurityService;
|
|
use KTXC\Context\IdentityContext;
|
|
use KTXF\Cache\BlobCacheInterface;
|
|
use KTXF\Cache\EphemeralCacheInterface;
|
|
use KTXF\Cache\PersistentCacheInterface;
|
|
|
|
/**
|
|
* Authentication middleware
|
|
* Authenticates the request and initializes session identity
|
|
*
|
|
* Note: This middleware does NOT enforce authentication.
|
|
* It only attempts to authenticate if credentials are present.
|
|
* Route-level authentication is enforced by RouterMiddleware.
|
|
*/
|
|
class AuthenticationMiddleware implements MiddlewareInterface
|
|
{
|
|
public function __construct(
|
|
private readonly SecurityService $securityService,
|
|
private readonly IdentityContext $identityContext,
|
|
private readonly EphemeralCacheInterface $ephemeralCache,
|
|
private readonly PersistentCacheInterface $persistentCache,
|
|
private readonly BlobCacheInterface $blobCache,
|
|
) {}
|
|
|
|
public function process(Request $request, RequestHandlerInterface $handler): Response
|
|
{
|
|
// Attempt to authenticate the request
|
|
$identity = $this->securityService->authenticate($request);
|
|
|
|
// Initialize session identity if authentication succeeded
|
|
if ($identity) {
|
|
$this->identityContext->initialize($identity);
|
|
$identityId = $this->identityContext->identifier();
|
|
$this->ephemeralCache->setUserContext($identityId);
|
|
$this->persistentCache->setUserContext($identityId);
|
|
$this->blobCache->setUserContext($identityId);
|
|
}
|
|
|
|
// Continue to next middleware (authentication is optional at this stage)
|
|
return $handler->handle($request);
|
|
}
|
|
}
|