diff --git a/src/Authorize/Bearer/JsonWebTokenDecoder.php b/src/Authorize/Bearer/JsonWebTokenDecoder.php index 404804d..5604684 100644 --- a/src/Authorize/Bearer/JsonWebTokenDecoder.php +++ b/src/Authorize/Bearer/JsonWebTokenDecoder.php @@ -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; diff --git a/src/Authorize/Bearer/JsonWebTokenValidate.php b/src/Authorize/Bearer/JsonWebTokenValidate.php index 0fb2c7c..25a1bba 100644 --- a/src/Authorize/Bearer/JsonWebTokenValidate.php +++ b/src/Authorize/Bearer/JsonWebTokenValidate.php @@ -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) ); } } diff --git a/src/Authorize/CookieAuthentication.php b/src/Authorize/CookieAuthentication.php new file mode 100644 index 0000000..24952f2 --- /dev/null +++ b/src/Authorize/CookieAuthentication.php @@ -0,0 +1,33 @@ +cookie->get('jwt')); + + if (isset($methodObj)) { + $request = $methodObj->execute($request); + } + + return $request; + } + + public function catchRequest(ServerRequestInterface $request) : bool + { + return $this->cookie->has('jwt'); + } +} \ No newline at end of file diff --git a/src/Authorize/Header/BearerMethod.php b/src/Authorize/Header/BearerMethod.php index 98a890e..004e053 100644 --- a/src/Authorize/Header/BearerMethod.php +++ b/src/Authorize/Header/BearerMethod.php @@ -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; } diff --git a/src/Lib/Authenticate.php b/src/Lib/Authenticate.php index 5dc1ebd..99cd7ad 100644 --- a/src/Lib/Authenticate.php +++ b/src/Lib/Authenticate.php @@ -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"); } } diff --git a/src/Middleware/CookieAuthenticationMiddleware.php b/src/Middleware/CookieAuthenticationMiddleware.php new file mode 100644 index 0000000..eeb974c --- /dev/null +++ b/src/Middleware/CookieAuthenticationMiddleware.php @@ -0,0 +1,30 @@ +authenticator->catchRequest($request) ) { + $request = $this->authenticator->connect($request); + } + + return $handler->handle($request); + } +}