- Added a new CookieAuthentication middleware
This commit is contained in:
parent
0eaaf79ad8
commit
6a24bcad76
@ -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;
|
||||
|
||||
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
33
src/Authorize/CookieAuthentication.php
Normal file
33
src/Authorize/CookieAuthentication.php
Normal 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');
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
30
src/Middleware/CookieAuthenticationMiddleware.php
Normal file
30
src/Middleware/CookieAuthenticationMiddleware.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user