Compare commits
No commits in common. "master" and "v1.1.1" have entirely different histories.
@ -10,7 +10,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"mcnd/ulmus": "^1.0 || ^2.0"
|
"mcnd/ulmus": "dev-master"
|
||||||
},
|
},
|
||||||
"repositories": [
|
"repositories": [
|
||||||
{
|
{
|
||||||
|
|||||||
@ -5,13 +5,11 @@ namespace Ulmus\User\Authorize\Header;
|
|||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Ulmus\User\Entity\BasicAuthUserInterface;
|
use Ulmus\User\Entity\BasicAuthUserInterface;
|
||||||
use Ulmus\User\Entity\UserInterface;
|
use Ulmus\User\Entity\UserInterface;
|
||||||
use Ulmus\User\Lib\Authenticate;
|
|
||||||
use Ulmus\User\Lib\Authorize;
|
use Ulmus\User\Lib\Authorize;
|
||||||
|
|
||||||
class BasicMethod implements MethodInterface
|
class BasicMethod implements MethodInterface
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected Authenticate $authorize,
|
|
||||||
protected BasicAuthUserInterface $user,
|
protected BasicAuthUserInterface $user,
|
||||||
protected string|array $arguments
|
protected string|array $arguments
|
||||||
) {}
|
) {}
|
||||||
@ -31,8 +29,7 @@ class BasicMethod implements MethodInterface
|
|||||||
throw new \RuntimeException("A password must be provided");
|
throw new \RuntimeException("A password must be provided");
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->authorize->user = $this->user;
|
( new Authorize($this->user) )->authenticate([ $this->user->usernameField() => $userName ], $password);
|
||||||
$this->authorize->authenticate([ $this->user->usernameField() => $userName ], $password);
|
|
||||||
|
|
||||||
return $this->user->loggedIn();
|
return $this->user->loggedIn();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,6 @@ use Psr\Http\Message\ServerRequestInterface;
|
|||||||
use Ulmus\User\Authorize\Bearer\JsonWebToken;
|
use Ulmus\User\Authorize\Bearer\JsonWebToken;
|
||||||
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\Authorize;
|
use Ulmus\User\Lib\Authorize;
|
||||||
|
|
||||||
class BearerMethod implements MethodInterface
|
class BearerMethod implements MethodInterface
|
||||||
@ -14,21 +13,20 @@ class BearerMethod implements MethodInterface
|
|||||||
protected JsonWebTokenDecoder $jwt;
|
protected JsonWebTokenDecoder $jwt;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected Authenticate $authorize,
|
|
||||||
protected UserInterface $user,
|
protected UserInterface $user,
|
||||||
protected string $token
|
protected string $token
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function execute(ServerRequestInterface $request) : bool
|
public function execute(ServerRequestInterface $request) : bool
|
||||||
{
|
{
|
||||||
$this->authorize->user = $this->user;
|
|
||||||
|
|
||||||
switch($this->autodetectTokenType()) {
|
switch($this->autodetectTokenType()) {
|
||||||
case BearerTokenTypeEnum::JsonWebToken:
|
case BearerTokenTypeEnum::JsonWebToken:
|
||||||
|
$authorize = new Authorize($this->user);
|
||||||
|
|
||||||
$payload = $this->jwt->getPayload();
|
$payload = $this->jwt->getPayload();
|
||||||
|
|
||||||
if ($payload['sub'] ?? false) {
|
if ($payload['sub'] ?? false) {
|
||||||
if ( ! $this->authorize->logUser($payload['sub']) ) {
|
if ( ! $authorize->logUser((int) $payload['sub']) ) {
|
||||||
throw new \Exception("Given user id do not match with an existing/active user");
|
throw new \Exception("Given user id do not match with an existing/active user");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,12 +4,10 @@ namespace Ulmus\User\Authorize\Header;
|
|||||||
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Ulmus\User\Entity\DigestAuthUserInterface;
|
use Ulmus\User\Entity\DigestAuthUserInterface;
|
||||||
use Ulmus\User\Lib\Authenticate;
|
|
||||||
|
|
||||||
class DigestMethod implements MethodInterface
|
class DigestMethod implements MethodInterface
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected Authenticate $authorize,
|
|
||||||
protected DigestAuthUserInterface $user,
|
protected DigestAuthUserInterface $user,
|
||||||
protected string|array $arguments
|
protected string|array $arguments
|
||||||
) {}
|
) {}
|
||||||
|
|||||||
@ -5,14 +5,9 @@ namespace Ulmus\User\Authorize;
|
|||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Ulmus\User\Common\AuthorizeContentTypeEnum;
|
use Ulmus\User\Common\AuthorizeContentTypeEnum;
|
||||||
use Ulmus\User\Entity\{ DigestAuthUserInterface, UserInterface };
|
use Ulmus\User\Entity\{ DigestAuthUserInterface, UserInterface };
|
||||||
use Ulmus\User\Lib\Authenticate;
|
|
||||||
|
|
||||||
class HeaderAuthentication implements AuthorizeMethodInterface
|
class HeaderAuthentication implements AuthorizeMethodInterface
|
||||||
{
|
{
|
||||||
public function __construct(
|
|
||||||
protected Authenticate $authorize,
|
|
||||||
) { }
|
|
||||||
|
|
||||||
public function connect(ServerRequestInterface $request, UserInterface $user): bool
|
public function connect(ServerRequestInterface $request, UserInterface $user): bool
|
||||||
{
|
{
|
||||||
if (null !== ( $auth = $request->getHeaderLine('Authorization') )) {
|
if (null !== ( $auth = $request->getHeaderLine('Authorization') )) {
|
||||||
@ -20,7 +15,7 @@ class HeaderAuthentication implements AuthorizeMethodInterface
|
|||||||
|
|
||||||
switch(strtolower($method)) {
|
switch(strtolower($method)) {
|
||||||
case "basic":
|
case "basic":
|
||||||
$methodObj = new Header\BasicMethod($this->authorize, $user, $value);
|
$methodObj = new Header\BasicMethod($user, $value);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "digest":
|
case "digest":
|
||||||
@ -28,11 +23,11 @@ class HeaderAuthentication implements AuthorizeMethodInterface
|
|||||||
throw new \RuntimeException("Your user entity must provide a valid hash of `user:realm:password` ");
|
throw new \RuntimeException("Your user entity must provide a valid hash of `user:realm:password` ");
|
||||||
}
|
}
|
||||||
|
|
||||||
$methodObj = new Header\DigestMethod($this->authorize, $user, $value);
|
$methodObj = new Header\DigestMethod($user, $value);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "bearer":
|
case "bearer":
|
||||||
$methodObj = new Header\BearerMethod($this->authorize, $user, $value);
|
$methodObj = new Header\BearerMethod($user, $value);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "token":
|
case "token":
|
||||||
|
|||||||
@ -1,55 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ulmus\User\Authorize;
|
|
||||||
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
|
||||||
use Ulmus\User\Entity\UserInterface;
|
|
||||||
use Ulmus\User\Lib\Authenticate;
|
|
||||||
|
|
||||||
class MacAddressAuthentication implements AuthorizeMethodInterface
|
|
||||||
{
|
|
||||||
public function __construct(
|
|
||||||
public Authenticate $authenticate,
|
|
||||||
protected string $fieldUser = "mac",
|
|
||||||
) {}
|
|
||||||
|
|
||||||
public function connect(ServerRequestInterface $request, UserInterface $user): bool
|
|
||||||
{
|
|
||||||
$server = $request->getServerParams();
|
|
||||||
|
|
||||||
# var_dump($server, $this->getArpMacAddress($server['REMOTE_ADDR']));die();
|
|
||||||
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return $this->authenticate->authenticate([ $this->fieldUser => $post[$this->postFieldUser] ], $post[$this->postFieldPassword]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function catchRequest(ServerRequestInterface $request): bool
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
|
|
||||||
$post = $request->getParsedBody();
|
|
||||||
|
|
||||||
return strtoupper( $request->getMethod() ) === "POST" && isset($post[$this->postFieldUser], $post[$this->postFieldPassword]);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getArpMacAddress(string $ip) : ?string
|
|
||||||
{
|
|
||||||
#run the external command, break output into lines
|
|
||||||
$arp = shell_exec(sprintf("arp -a %s", escapeshellarg($ip)));
|
|
||||||
$lines = explode("\n", $arp);
|
|
||||||
|
|
||||||
#look for the output line describing our IP address
|
|
||||||
foreach($lines as $line)
|
|
||||||
{
|
|
||||||
$cols = preg_split('/\s+/', trim($line));
|
|
||||||
|
|
||||||
if ($cols[0] === $ip)
|
|
||||||
{
|
|
||||||
return $cols[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -121,19 +121,15 @@ class Authenticate {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function logUser(null|int|object $id) : ? UserInterface
|
public function logUser(?int $id) : ? UserInterface
|
||||||
{
|
{
|
||||||
if (! is_object($id)) {
|
try {
|
||||||
try {
|
if ($id === null || null === ($entity = $this->user::repository()->loadFromPk($id))) {
|
||||||
if ($id === null || null === ($entity = $this->user::repository()->loadFromPk($id))) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} catch (\Exception $ex) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
catch(\Exception $ex) {
|
||||||
$entity = $id;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->user->fromArray($entity);
|
$this->user->fromArray($entity);
|
||||||
|
|||||||
@ -10,19 +10,17 @@ use Psr\Http\{
|
|||||||
};
|
};
|
||||||
use Ulmus\User\Entity\UserInterface;
|
use Ulmus\User\Entity\UserInterface;
|
||||||
use Ulmus\User\Authorize\HeaderAuthentication;
|
use Ulmus\User\Authorize\HeaderAuthentication;
|
||||||
use Ulmus\User\Lib\Authenticate;
|
|
||||||
|
|
||||||
class HeaderAuthenticationMiddleware implements MiddlewareInterface
|
class HeaderAuthenticationMiddleware implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
protected HeaderAuthentication $authenticator;
|
protected HeaderAuthentication $authenticator;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected Authenticate $authorize,
|
|
||||||
protected UserInterface $entity,
|
protected UserInterface $entity,
|
||||||
protected \Closure $loginFailedResponse,
|
protected \Closure $loginFailedResponse,
|
||||||
? HeaderAuthentication $authenticator = null,
|
HeaderAuthentication $authenticator = null,
|
||||||
) {
|
) {
|
||||||
$this->authenticator = $authenticator ?: new HeaderAuthentication($authorize);
|
$this->authenticator = $authenticator ?: new HeaderAuthentication();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||||
|
|||||||
@ -1,40 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Ulmus\User\Middleware;
|
|
||||||
|
|
||||||
use Psr\Http\{
|
|
||||||
Message\ResponseInterface,
|
|
||||||
Message\ServerRequestInterface,
|
|
||||||
Server\MiddlewareInterface,
|
|
||||||
Server\RequestHandlerInterface
|
|
||||||
};
|
|
||||||
use Ulmus\User\Authorize\MacAddressAuthentication;
|
|
||||||
use Ulmus\User\Entity\UserInterface;
|
|
||||||
use Ulmus\User\Authorize\PostRequestAuthentication;
|
|
||||||
use Ulmus\User\Lib\Authenticate;
|
|
||||||
|
|
||||||
class LocalMacAddressMiddleware implements MiddlewareInterface
|
|
||||||
{
|
|
||||||
protected MacAddressAuthentication $authenticator;
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
protected UserInterface $entity,
|
|
||||||
protected \Closure $loginFailedResponse,
|
|
||||||
? MacAddressAuthentication $authenticator = null,
|
|
||||||
) {
|
|
||||||
$this->authenticator = $authenticator ?: new MacAddressAuthentication(new Authenticate($entity));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
||||||
{
|
|
||||||
$this->authenticator->authenticate->rememberMe();
|
|
||||||
|
|
||||||
if ( $this->authenticator->catchRequest($request) ) {
|
|
||||||
if ( ! $this->authenticator->connect($request, $this->entity) ) {
|
|
||||||
return call_user_func($this->loginFailedResponse, "Login failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $handler->handle($request);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -19,7 +19,7 @@ class PostRequestAuthenticationMiddleware implements MiddlewareInterface
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
protected UserInterface $entity,
|
protected UserInterface $entity,
|
||||||
protected \Closure $loginFailedResponse,
|
protected \Closure $loginFailedResponse,
|
||||||
? PostRequestAuthentication $authenticator = null,
|
PostRequestAuthentication $authenticator = null,
|
||||||
) {
|
) {
|
||||||
$this->authenticator = $authenticator ?: new PostRequestAuthentication(new Authenticate($entity));
|
$this->authenticator = $authenticator ?: new PostRequestAuthentication(new Authenticate($entity));
|
||||||
}
|
}
|
||||||
@ -30,9 +30,6 @@ class PostRequestAuthenticationMiddleware implements MiddlewareInterface
|
|||||||
|
|
||||||
if ( $this->authenticator->catchRequest($request) ) {
|
if ( $this->authenticator->catchRequest($request) ) {
|
||||||
if ( ! $this->authenticator->connect($request, $this->entity) ) {
|
if ( ! $this->authenticator->connect($request, $this->entity) ) {
|
||||||
# Adds random sleep after a bad login attempts
|
|
||||||
sleep(random_int(2,8));
|
|
||||||
|
|
||||||
return call_user_func($this->loginFailedResponse, "Login failed");
|
return call_user_func($this->loginFailedResponse, "Login failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user