ulmus-user/src/Authorize/Header/BasicMethod.php

36 lines
1.1 KiB
PHP

<?php
namespace Ulmus\User\Authorize\Header;
use Psr\Http\Message\ServerRequestInterface;
use Ulmus\User\Entity\BasicAuthUserInterface;
use Ulmus\User\Entity\UserInterface;
use Ulmus\User\Lib\Authorize;
class BasicMethod implements MethodInterface
{
public function __construct(
protected BasicAuthUserInterface $user,
protected string|array $arguments
) {}
public function execute(ServerRequestInterface $request) : bool
{
if ( false === $decoded = base64_decode($this->arguments) ) {
throw new \RuntimeException("Base64 decoding of given username:password failed");
}
list($userName, $password) = explode(':', $decoded) + [ null, null ];
if ( empty($userName) ) {
throw new \RuntimeException("A username must be provided");
}
elseif ( empty($password) ) {
throw new \RuntimeException("A password must be provided");
}
( new Authorize($this->user) )->authenticate([ $this->user->usernameField() => $userName ], $password);
return $this->user->loggedIn();
}
}