- Added a new CookieAuthentication middleware

This commit is contained in:
Dave Mc Nicoll committed 2026-10-20 11:57:30 +00:00
1 parent 0eaaf79ad8
commit 6a24bcad76
6 files changed
+74 -11

No files matched your search

+1 -1
View File
@@ -70,7 +70,7 @@ class JsonWebTokenDecoder
public function isJWT() : bool public function isJWT() : bool
{ {
try { try {
return count(explode('.', $this->encoded)) === 3; return strlen($this->encoded) > 23 && count(explode('.', $this->encoded)) === 3;
} }
catch(\Throwable $t) { catch(\Throwable $t) {
return false; return false;
@@ -53,7 +53,7 @@ abstract class JsonWebTokenValidate
if ( ($compare ?? null) !== $decodedSignature) { if ( ($compare ?? null) !== $decodedSignature) {
throw new JsonWebTokenDecodingError( 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)
); );
} }
} }
+33
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');
}
}
+5 -6
View File
@@ -13,15 +13,11 @@ class BearerMethod implements MethodInterface
protected JsonWebTokenDecoder $jwt; protected JsonWebTokenDecoder $jwt;
public function __construct( public function __construct(
# protected Authenticate $authorize,
# protected UserInterface $user,
protected string $token protected string $token
) {} ) {}
public function execute(ServerRequestInterface $request) : ServerRequestInterface public function execute(ServerRequestInterface $request) : ServerRequestInterface
{ {
# $this->authorize->user = $this->user;
switch($this->autodetectTokenType()) { switch($this->autodetectTokenType()) {
case BearerTokenTypeEnum::JsonWebToken: case BearerTokenTypeEnum::JsonWebToken:
$this->jwt->decode(); $this->jwt->decode();
@@ -35,7 +31,7 @@ class BearerMethod implements MethodInterface
->withAttribute('authentication_middleware:user_id', $payload['sub']); ->withAttribute('authentication_middleware:user_id', $payload['sub']);
} }
else { 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; break;
@@ -52,11 +48,14 @@ class BearerMethod implements MethodInterface
public function autodetectTokenType() : BearerTokenTypeEnum 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() ) { if ( $this->jwt->isJWT() ) {
return BearerTokenTypeEnum::JsonWebToken; return BearerTokenTypeEnum::JsonWebToken;
} }
}
catch(\Throwable $ex) {/* Not a JWT */}
return BearerTokenTypeEnum::UniqueKey; return BearerTokenTypeEnum::UniqueKey;
} }
+4 -3
View File
@@ -37,7 +37,8 @@ class Authenticate {
return null; return null;
} }
public function forgetMe() { public function forgetMe() : void
{
$this->cookie->delete("user.id"); $this->cookie->delete("user.id");
$this->session->destroy(); $this->session->destroy();
} }
@@ -104,8 +105,8 @@ class Authenticate {
$this->session->set("user.id", $this->user->id); $this->session->set("user.id", $this->user->id);
} }
if ( $this->cookie ) { if ( $this->cookie && ! $this->cookie->has('user.id')) {
$this->cookie->set("user.id", $this->user->id); $this->cookie->set("user.id", $this->user->id, samesite: "none");
} }
} }
@@ -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);
}
}