- Added a new CookieAuthentication middleware

This commit is contained in:
Dave Mc Nicoll 2026-10-20 11:57:30 +00:00
parent 0eaaf79ad8
commit 6a24bcad76
6 changed files with 76 additions and 13 deletions

View File

@ -70,7 +70,7 @@ class JsonWebTokenDecoder
public function isJWT() : bool
{
try {
return count(explode('.', $this->encoded)) === 3;
return strlen($this->encoded) > 23 && count(explode('.', $this->encoded)) === 3;
}
catch(\Throwable $t) {
return false;

View File

@ -53,7 +53,7 @@ abstract class JsonWebTokenValidate
if ( ($compare ?? null) !== $decodedSignature) {
throw new JsonWebTokenDecodingError(
sprintf("Given signature (%s) do not match computed signature (%s)", $encodedSignature, JsonWebTokenEncoder::base64url_encode($compare))
sprintf("Given signature (%s) do not match computed signature", $encodedSignature)
);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace Ulmus\User\Authorize;
use DI\Attribute\Inject;
use Psr\Http\Message\ServerRequestInterface;
use Storage\Cookie;
use Ulmus\User\Lib\Authenticate;
use Ulmus\User\Lib\HeaderAuthenticationTypeEnum;
class CookieAuthentication implements AuthorizeMethodInterface
{
public function __construct(
protected Authenticate $authorize,
protected Cookie $cookie,
) { }
public function connect(ServerRequestInterface $request): ServerRequestInterface
{
$methodObj = new Header\BearerMethod($this->cookie->get('jwt'));
if (isset($methodObj)) {
$request = $methodObj->execute($request);
}
return $request;
}
public function catchRequest(ServerRequestInterface $request) : bool
{
return $this->cookie->has('jwt');
}
}

View File

@ -13,15 +13,11 @@ class BearerMethod implements MethodInterface
protected JsonWebTokenDecoder $jwt;
public function __construct(
# protected Authenticate $authorize,
# protected UserInterface $user,
protected string $token
) {}
public function execute(ServerRequestInterface $request) : ServerRequestInterface
{
# $this->authorize->user = $this->user;
switch($this->autodetectTokenType()) {
case BearerTokenTypeEnum::JsonWebToken:
$this->jwt->decode();
@ -35,7 +31,7 @@ class BearerMethod implements MethodInterface
->withAttribute('authentication_middleware:user_id', $payload['sub']);
}
else {
throw new \InvalidArgumentException("Given JsonWebToken is missing a 'sub' key (which concords to user id)");
throw new \InvalidArgumentException("Given JsonWebToken is missing a 'sub' key (which match with a user's id)");
}
break;
@ -52,11 +48,14 @@ class BearerMethod implements MethodInterface
public function autodetectTokenType() : BearerTokenTypeEnum
{
$this->jwt = new JsonWebTokenDecoder($this->token, getenv('LEAN_RANDOM'));
try {
$this->jwt = new JsonWebTokenDecoder($this->token, hash('sha512', getenv('LEAN_RANDOM')));
if ( $this->jwt->isJWT() ) {
return BearerTokenTypeEnum::JsonWebToken;
if ( $this->jwt->isJWT() ) {
return BearerTokenTypeEnum::JsonWebToken;
}
}
catch(\Throwable $ex) {/* Not a JWT */}
return BearerTokenTypeEnum::UniqueKey;
}

View File

@ -37,7 +37,8 @@ class Authenticate {
return null;
}
public function forgetMe() {
public function forgetMe() : void
{
$this->cookie->delete("user.id");
$this->session->destroy();
}
@ -104,8 +105,8 @@ class Authenticate {
$this->session->set("user.id", $this->user->id);
}
if ( $this->cookie ) {
$this->cookie->set("user.id", $this->user->id);
if ( $this->cookie && ! $this->cookie->has('user.id')) {
$this->cookie->set("user.id", $this->user->id, samesite: "none");
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Ulmus\User\Middleware;
use DI\Attribute\Inject;
use Psr\Http\{
Message\ResponseInterface,
Message\ServerRequestInterface,
Server\MiddlewareInterface,
Server\RequestHandlerInterface
};
use Ulmus\User\Authorize\CookieAuthentication;
class CookieAuthenticationMiddleware implements MiddlewareInterface
{
public function __construct(
#[Inject]
protected CookieAuthentication $authenticator,
) { }
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ( $this->authenticator->catchRequest($request) ) {
$request = $this->authenticator->connect($request);
}
return $handler->handle($request);
}
}