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