-WIP on Bearer token

This commit is contained in:
Dave Mc Nicoll committed 2024-05-31 12:27:07 +00:00
1 parent 0657622eab
commit c4e4db7a45
13 files changed
+377 -35

No files matched your search

+45 -1
View File
@@ -3,10 +3,15 @@
namespace Ulmus\User\Authorize\Header;
use Psr\Http\Message\ServerRequestInterface;
use Ulmus\User\Authorize\Bearer\JsonWebToken;
use Ulmus\User\Authorize\Bearer\JsonWebTokenDecoder;
use Ulmus\User\Entity\UserInterface;
use Ulmus\User\Lib\Authorize;
class BearerMethod implements MethodInterface
{
protected JsonWebTokenDecoder $jwt;
public function __construct(
protected UserInterface $user,
protected string $token
@@ -14,6 +19,45 @@ class BearerMethod implements MethodInterface
public function execute(ServerRequestInterface $request) : bool
{
return false;
switch($this->autodetectTokenType()) {
case BearerTokenTypeEnum::JsonWebToken:
$authorize = new Authorize($this->user);
$payload = $this->jwt->getPayload();
if ($payload['sub'] ?? false) {
if ( ! $authorize->logUser((int) $payload['sub']) ) {
throw new \Exception("Given user id do not match with an existing/active user");
}
}
else {
throw new \InvalidArgumentException("Given JsonWebToken is missing a 'sub' key (which concords to user id)");
}
break;
case BearerTokenTypeEnum::UniqueKey:
# @TODO
break;
}
return $this->user->loggedIn();
}
public function autodetectTokenType() : BearerTokenTypeEnum
{
$this->jwt = new JsonWebTokenDecoder($this->token);
if ( $this->jwt->isJWT() ) {
return BearerTokenTypeEnum::JsonWebToken;
}
return BearerTokenTypeEnum::UniqueKey;
}
protected function userFromJWT() : UserInterface
{
}
}