Compare commits
2
Commits
0271aec31b
...
v2.x
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a24bcad76 | ||
|
|
0eaaf79ad8 |
No files matched your search
@@ -70,12 +70,12 @@ class JsonWebTokenDecoder
|
|||||||
public function isJWT() : bool
|
public function isJWT() : bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return $this->parse();
|
return strlen($this->encoded) > 23 && count(explode('.', $this->encoded)) === 3;
|
||||||
}
|
}
|
||||||
catch(\Throwable $t) {}
|
catch(\Throwable $t) {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function getPayload() : array
|
public function getPayload() : array
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,30 +6,32 @@ use Psr\Http\Message\ServerRequestInterface;
|
|||||||
use Ulmus\User\Authorize\Bearer\JsonWebTokenDecoder;
|
use Ulmus\User\Authorize\Bearer\JsonWebTokenDecoder;
|
||||||
use Ulmus\User\Entity\UserInterface;
|
use Ulmus\User\Entity\UserInterface;
|
||||||
use Ulmus\User\Lib\Authenticate;
|
use Ulmus\User\Lib\Authenticate;
|
||||||
|
use Ulmus\User\Lib\AuthenticationMethodEnum;
|
||||||
|
|
||||||
class BearerMethod implements MethodInterface
|
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();
|
||||||
|
|
||||||
$payload = $this->jwt->getPayload();
|
$payload = $this->jwt->getPayload();
|
||||||
|
|
||||||
if ( $payload['sub'] ?? false) {
|
if ( $payload['sub'] ?? false) {
|
||||||
$request = $request->withAttribute('authentication_middleware:user_id', $payload['sub']);
|
$request = $request
|
||||||
|
->withAttribute('authentication_middleware:method', AuthenticationMethodEnum::ForceLogin)
|
||||||
|
->withAttribute('authentication_middleware:jwt', $payload)
|
||||||
|
->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;
|
||||||
@@ -37,6 +39,8 @@ class BearerMethod implements MethodInterface
|
|||||||
case BearerTokenTypeEnum::UniqueKey:
|
case BearerTokenTypeEnum::UniqueKey:
|
||||||
# @TODO
|
# @TODO
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
return $request;
|
return $request;
|
||||||
@@ -44,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;
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -48,12 +48,12 @@ abstract class User implements UserInterface {
|
|||||||
#[Field]
|
#[Field]
|
||||||
public string $password;
|
public string $password;
|
||||||
|
|
||||||
#[Field\UpdatedAt(name: "updated_at", readonly: true)]
|
|
||||||
public ? Datetime $updatedAt;
|
|
||||||
|
|
||||||
#[Field\CreatedAt(name: "created_at", readonly: true)]
|
#[Field\CreatedAt(name: "created_at", readonly: true)]
|
||||||
public Datetime $createdAt;
|
public Datetime $createdAt;
|
||||||
|
|
||||||
|
#[Field\UpdatedAt(name: "updated_at", readonly: true)]
|
||||||
|
public ? Datetime $updatedAt;
|
||||||
|
|
||||||
public bool $logged = false;
|
public bool $logged = false;
|
||||||
|
|
||||||
public function __toString() : string
|
public function __toString() : string
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\User\Exception;
|
||||||
|
|
||||||
|
class InvalidUserException extends \Exception {}
|
||||||
@@ -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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ use Psr\Http\{
|
|||||||
};
|
};
|
||||||
use Ulmus\User\Entity\UserInterface;
|
use Ulmus\User\Entity\UserInterface;
|
||||||
use Ulmus\User\Authorize\PostRequestAuthentication;
|
use Ulmus\User\Authorize\PostRequestAuthentication;
|
||||||
|
use Ulmus\User\Exception\InvalidUserException;
|
||||||
use Ulmus\User\Lib\Authenticate;
|
use Ulmus\User\Lib\Authenticate;
|
||||||
use Ulmus\User\Lib\AuthenticationMethodEnum;
|
use Ulmus\User\Lib\AuthenticationMethodEnum;
|
||||||
|
|
||||||
@@ -23,11 +24,23 @@ class AuthenticationMiddleware implements MiddlewareInterface
|
|||||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
$this->launchAuthentication($request);
|
||||||
|
}
|
||||||
|
catch(InvalidUserException $e) {
|
||||||
|
return call_user_func($this->loginFailedResponse, [ 'api.error_message' => $e->getMessage() ]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $handler->handle($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function launchAuthentication(ServerRequestInterface $request) : void
|
||||||
|
{
|
||||||
if (null !== $id = $request->getAttribute('authentication_middleware:user_id')) {
|
if (null !== $id = $request->getAttribute('authentication_middleware:user_id')) {
|
||||||
|
|
||||||
$this->authenticator->loadUser($id);
|
$this->authenticator->loadUser($id);
|
||||||
|
|
||||||
if ( ! $this->authenticator->user->isLoaded() ) {
|
if ( ! $this->authenticator->user->isLoaded() ) {
|
||||||
throw new \Exception("Given user id do not match with an existing/active user");
|
throw new InvalidUserException("Given user id do not match with an existing/active user");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,10 +54,4 @@ class AuthenticationMiddleware implements MiddlewareInterface
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(\Exception $e) {
|
|
||||||
return call_user_func($this->loginFailedResponse, [ 'api.error_message' => $e->getMessage() ]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $handler->handle($request);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user