diff --git a/src/Authorize/MacAddressAuthentication.php b/src/Authorize/MacAddressAuthentication.php new file mode 100644 index 0000000..41727da --- /dev/null +++ b/src/Authorize/MacAddressAuthentication.php @@ -0,0 +1,55 @@ +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; + } +} \ No newline at end of file diff --git a/src/Lib/Authenticate.php b/src/Lib/Authenticate.php index 2692bd3..ab2455d 100644 --- a/src/Lib/Authenticate.php +++ b/src/Lib/Authenticate.php @@ -120,15 +120,19 @@ class Authenticate { return $this; } - public function logUser(?int $id) : ? UserInterface + public function logUser(null|int|object $id) : ? UserInterface { - try { - if ($id === null || null === ($entity = $this->user::repository()->loadFromPk($id))) { + if (! is_object($id)) { + try { + if ($id === null || null === ($entity = $this->user::repository()->loadFromPk($id))) { + return null; + } + } catch (\Exception $ex) { return null; } } - catch(\Exception $ex) { - return null; + else { + $entity = $id; } $this->user->fromArray($entity); diff --git a/src/Middleware/LocalMacAddressMiddleware.php b/src/Middleware/LocalMacAddressMiddleware.php new file mode 100644 index 0000000..0abf272 --- /dev/null +++ b/src/Middleware/LocalMacAddressMiddleware.php @@ -0,0 +1,40 @@ +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); + } +} diff --git a/src/Middleware/PostRequestAuthenticationMiddleware.php b/src/Middleware/PostRequestAuthenticationMiddleware.php index e7c3a45..b35a45e 100644 --- a/src/Middleware/PostRequestAuthenticationMiddleware.php +++ b/src/Middleware/PostRequestAuthenticationMiddleware.php @@ -30,6 +30,9 @@ class PostRequestAuthenticationMiddleware implements MiddlewareInterface if ( $this->authenticator->catchRequest($request) ) { 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"); } }