47 lines
1.5 KiB
PHP
47 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\User\Middleware;
|
|
|
|
use Psr\Http\{
|
|
Message\ResponseInterface,
|
|
Message\ServerRequestInterface,
|
|
Server\MiddlewareInterface,
|
|
Server\RequestHandlerInterface
|
|
};
|
|
use Ulmus\User\Entity\UserInterface;
|
|
use Ulmus\User\Authorize\PostRequestAuthentication;
|
|
use Ulmus\User\Lib\Authenticate;
|
|
use Ulmus\User\Lib\AuthenticationMethodEnum;
|
|
|
|
class AuthenticationMiddleware implements MiddlewareInterface
|
|
{
|
|
public function __construct(
|
|
protected UserInterface $user,
|
|
protected Authenticate $authenticator,
|
|
) {}
|
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
{
|
|
try {
|
|
if (null !== $id = $request->getAttribute('authentication_middleware:user_id')) {
|
|
$this->authenticator->loadUser($id);
|
|
}
|
|
|
|
switch($request->getAttribute('authentication_middleware:method')) {
|
|
case AuthenticationMethodEnum::ForceLogin:
|
|
$this->authenticator->login();
|
|
break;
|
|
|
|
case AuthenticationMethodEnum::UsernamePassword:
|
|
$this->authenticator->authenticate($request->getAttribute('authentication_middleware:username'), $request->getAttribute('authentication_middleware:password'));
|
|
break;
|
|
}
|
|
}
|
|
catch(\Exception $e) {
|
|
throw $e;
|
|
}
|
|
|
|
return $handler->handle($request);
|
|
}
|
|
}
|