- Added a new CookieAuthentication middleware
This commit is contained in:
1 parent
0eaaf79ad8
commit
6a24bcad76
6 files changed
+76
-13
No files matched your search
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user