- WIP on user auth
This commit is contained in:
1 parent
7769b1a1ca
commit
9ae3e2d6f9
6 files changed
+38
-37
No files matched your search
@@ -7,7 +7,7 @@ use Ulmus\User\Entity\UserInterface;
|
||||
|
||||
interface AuthorizeMethodInterface
|
||||
{
|
||||
public function connect(ServerRequestInterface $request, UserInterface $user) : UserInterface|false;
|
||||
public function connect(ServerRequestInterface $request, UserInterface $user) : bool;
|
||||
|
||||
public function catchRequest(ServerRequestInterface $request) : bool;
|
||||
}
|
||||
@@ -4,13 +4,12 @@ namespace Ulmus\User\Authorize;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Ulmus\User\Common\AuthorizeContentTypeEnum;
|
||||
use Ulmus\User\Entity\User;
|
||||
use Ulmus\User\Entity\UserInterface;
|
||||
use Ulmus\User\Lib\Authenticate;
|
||||
use Ulmus\User\Lib\Authorize;
|
||||
|
||||
class HeaderAuthentication implements AuthorizeMethodInterface
|
||||
{
|
||||
public function connect(ServerRequestInterface $request, UserInterface $user): UserInterface|false
|
||||
public function connect(ServerRequestInterface $request, UserInterface $user): bool
|
||||
{
|
||||
if ( null !== ( $auth = $request->getHeaderLine('Authorization') ) ) {
|
||||
|
||||
@@ -18,7 +17,7 @@ class HeaderAuthentication implements AuthorizeMethodInterface
|
||||
|
||||
switch(strtolower(strtolower($method))) {
|
||||
case "basic":
|
||||
return $this->basicMethod($userPass);
|
||||
return $this->basicMethod($user, $userPass);
|
||||
|
||||
default:
|
||||
throw new \InvalidArgumentException("An authentication method must be provided");
|
||||
@@ -28,25 +27,24 @@ class HeaderAuthentication implements AuthorizeMethodInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function basicMethod(string $userPassword) : UserInterface|false
|
||||
protected function basicMethod(UserInterface $user, string $userPassword) : bool
|
||||
{
|
||||
if ( false === $decoded = base64_decode($userPassword) ) {
|
||||
throw new \RuntimeException("Base64 decoding of given username:password failed");
|
||||
}
|
||||
|
||||
list($user, $password) = explode(':', $decoded) + [ null, null ];
|
||||
list($userName, $password) = explode(':', $decoded) + [ null, null ];
|
||||
|
||||
if ( empty($user) ) {
|
||||
if ( empty($userName) ) {
|
||||
throw new \RuntimeException("A username must be provided");
|
||||
}
|
||||
elseif ( empty($password) ) {
|
||||
throw new \RuntimeException("A password must be provided");
|
||||
}
|
||||
|
||||
$authenticate = new Authenticate();
|
||||
( new Authorize($user) )->authenticate([ 'email' => $userName, 'username' => $userName ], $password);
|
||||
|
||||
|
||||
return false;
|
||||
return $user->isLoaded();
|
||||
}
|
||||
|
||||
public function catchRequest(ServerRequestInterface $request) : bool
|
||||
|
||||
@@ -9,17 +9,17 @@ use Ulmus\User\Lib\Authenticate;
|
||||
class PostRequestAuthentication implements AuthorizeMethodInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected Authenticate $authenticate,
|
||||
protected string $fieldUser = "username",
|
||||
protected string $postFieldUser = "username",
|
||||
public Authenticate $authenticate,
|
||||
protected string $fieldUser = "email",
|
||||
protected string $postFieldUser = "email",
|
||||
protected string $postFieldPassword = "password",
|
||||
) {}
|
||||
|
||||
public function connect(ServerRequestInterface $request, UserInterface $user): UserInterface|false
|
||||
public function connect(ServerRequestInterface $request, UserInterface $user): bool
|
||||
{
|
||||
$post = $request->getParsedBody();
|
||||
|
||||
return $this->authenticate->authenticate($user::repository(), [ $this->fieldUser => $post[$this->postFieldUser] ], $post[$this->postFieldPassword]);
|
||||
return $this->authenticate->authenticate([ $this->fieldUser => $post[$this->postFieldUser] ], $post[$this->postFieldPassword]);
|
||||
}
|
||||
|
||||
public function catchRequest(ServerRequestInterface $request): bool
|
||||
|
||||
Reference in new issue
Block a user