- WIP on authentication portal
This commit is contained in:
1 parent
1559ceb248
commit
7769b1a1ca
15 files changed
+220
-134
No files matched your search
@@ -7,5 +7,7 @@ use Ulmus\User\Entity\UserInterface;
|
||||
|
||||
interface AuthorizeMethodInterface
|
||||
{
|
||||
public function connect(ServerRequestInterface $request) : UserInterface|false;
|
||||
public function connect(ServerRequestInterface $request, UserInterface $user) : UserInterface|false;
|
||||
|
||||
public function catchRequest(ServerRequestInterface $request) : bool;
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\User\Authorize;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Ulmus\User\Entity\UserInterface;
|
||||
|
||||
class BasicAuthentication implements AuthorizeMethodInterface
|
||||
{
|
||||
public function connect(ServerRequestInterface $request): UserInterface|false
|
||||
{
|
||||
if ( null === $auth = $request->getHeader('Authorization') ) {
|
||||
list($method, $userPass) = explode(' ', $auth, 2) + [ null, null ];
|
||||
|
||||
if (! $method ) {
|
||||
throw new \InvalidArgumentException("An authentication method must be provided");
|
||||
}
|
||||
elseif (! $userPass ) {
|
||||
throw new \InvalidArgumentException("A base64-encoded 'user:password' value must be provided");
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function basicMethod(string $header) : UserInterface|false
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
|
||||
class HeaderAuthentication implements AuthorizeMethodInterface
|
||||
{
|
||||
public function connect(ServerRequestInterface $request, UserInterface $user): UserInterface|false
|
||||
{
|
||||
if ( null !== ( $auth = $request->getHeaderLine('Authorization') ) ) {
|
||||
|
||||
list($method, $userPass) = explode(' ', $auth, 2) + [ null, null ];
|
||||
|
||||
switch(strtolower(strtolower($method))) {
|
||||
case "basic":
|
||||
return $this->basicMethod($userPass);
|
||||
|
||||
default:
|
||||
throw new \InvalidArgumentException("An authentication method must be provided");
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function basicMethod(string $userPassword) : UserInterface|false
|
||||
{
|
||||
if ( false === $decoded = base64_decode($userPassword) ) {
|
||||
throw new \RuntimeException("Base64 decoding of given username:password failed");
|
||||
}
|
||||
|
||||
list($user, $password) = explode(':', $decoded) + [ null, null ];
|
||||
|
||||
if ( empty($user) ) {
|
||||
throw new \RuntimeException("A username must be provided");
|
||||
}
|
||||
elseif ( empty($password) ) {
|
||||
throw new \RuntimeException("A password must be provided");
|
||||
}
|
||||
|
||||
$authenticate = new Authenticate();
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function catchRequest(ServerRequestInterface $request) : bool
|
||||
{
|
||||
foreach(array_merge($request->getHeader('Accept'), $request->getHeader('Content-Type')) as $accept) {
|
||||
foreach(explode(',', $accept) as $contentType) {
|
||||
if ( AuthorizeContentTypeEnum::tryFrom(strtolower($contentType)) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\User\Authorize;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Ulmus\User\Entity\UserInterface;
|
||||
use Ulmus\User\Lib\Authenticate;
|
||||
|
||||
class PostRequestAuthentication implements AuthorizeMethodInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected Authenticate $authenticate,
|
||||
protected string $fieldUser = "username",
|
||||
protected string $postFieldUser = "username",
|
||||
protected string $postFieldPassword = "password",
|
||||
) {}
|
||||
|
||||
public function connect(ServerRequestInterface $request, UserInterface $user): UserInterface|false
|
||||
{
|
||||
$post = $request->getParsedBody();
|
||||
|
||||
return $this->authenticate->authenticate($user::repository(), [ $this->fieldUser => $post[$this->postFieldUser] ], $post[$this->postFieldPassword]);
|
||||
}
|
||||
|
||||
public function catchRequest(ServerRequestInterface $request): bool
|
||||
{
|
||||
$post = $request->getParsedBody();
|
||||
|
||||
return strtoupper( $request->getMethod() ) === "POST" && isset($post[$this->postFieldUser], $post[$this->postFieldPassword]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user