- WIP on securing PostRequestAuth and began adding some LAN auth also
This commit is contained in:
parent
d149e0a741
commit
45546fbcc0
55
src/Authorize/MacAddressAuthentication.php
Normal file
55
src/Authorize/MacAddressAuthentication.php
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -120,16 +120,20 @@ class Authenticate {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function logUser(?int $id) : ? UserInterface
|
public function logUser(null|int|object $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;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
} catch (\Exception $ex) {
|
||||||
catch(\Exception $ex) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$entity = $id;
|
||||||
|
}
|
||||||
|
|
||||||
$this->user->fromArray($entity);
|
$this->user->fromArray($entity);
|
||||||
|
|
||||||
|
|||||||
40
src/Middleware/LocalMacAddressMiddleware.php
Normal file
40
src/Middleware/LocalMacAddressMiddleware.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -30,6 +30,9 @@ 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