- WIP on 2.x auth. method

This commit is contained in:
Dave Mc Nicoll 2025-03-28 13:38:28 +00:00
parent 23079e6040
commit 6582378b6e
5 changed files with 70 additions and 9 deletions

View File

@ -42,7 +42,6 @@ class HeaderAuthentication implements AuthorizeMethodInterface
return isset($methodObj) && $methodObj->execute($request);
}
public function catchRequest(ServerRequestInterface $request) : bool
{
foreach(array_merge($request->getHeader('Accept'), $request->getHeader('Content-Type')) as $accept) {

View File

@ -120,7 +120,7 @@ class Authenticate {
return $this;
}
public function logUser(?int $id) : ? UserInterface
public function loadUser(?int $id) : ? UserInterface
{
try {
if ($id === null || null === ($entity = $this->user::repository()->loadFromPk($id))) {
@ -133,8 +133,17 @@ class Authenticate {
$this->user->fromArray($entity);
$this->user->loggedIn(true);
return $this->user;
}
public function logUser(?int $id) : ? UserInterface
{
if ( null !== $this->loadUser($id) ) {
$this->user->loggedIn(true);
return $this->user;
}
return null;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace Ulmus\User\Lib;
enum AuthenticationMethodEnum
{
case ForceLogin;
case UsernamePassword;
}

View File

@ -0,0 +1,46 @@
<?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);
}
}

View File

@ -13,15 +13,13 @@ use Ulmus\User\Authorize\HeaderAuthentication;
class HeaderAuthenticationMiddleware implements MiddlewareInterface
{
protected HeaderAuthentication $authenticator;
#protected HeaderAuthentication $authenticator;
public function __construct(
protected UserInterface $entity,
protected \Closure $loginFailedResponse,
HeaderAuthentication $authenticator = null,
) {
$this->authenticator = $authenticator ?: new HeaderAuthentication();
}
protected HeaderAuthentication $authenticator,
) { }
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{