generated from Nodarx/template
3a68d03114
Signed-off-by: Sebastian <krupinski01@gmail.com>
46 lines
1.2 KiB
PHP
46 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXM\ServiceDav\Auth;
|
|
|
|
use KTXC\Service\SecurityService;
|
|
use KTXC\Context\IdentityContext;
|
|
use Sabre\DAV\Auth\Backend\AbstractBasic;
|
|
use Sabre\HTTP\RequestInterface;
|
|
use Sabre\HTTP\ResponseInterface;
|
|
|
|
class DavAuthBackend extends AbstractBasic
|
|
{
|
|
public const PRINCIPAL_PREFIX = 'principals/';
|
|
|
|
public function __construct(
|
|
private readonly IdentityContext $identityContext,
|
|
private readonly SecurityService $securityService,
|
|
string $realm = 'KTXC DAV',
|
|
) {
|
|
$this->realm = $realm;
|
|
}
|
|
|
|
public function check(RequestInterface $request, ResponseInterface $response): array
|
|
{
|
|
if ($this->identityContext->identity() !== null) {
|
|
$userId = $this->identityContext->identifier();
|
|
return [true, self::PRINCIPAL_PREFIX . $userId];
|
|
}
|
|
|
|
return parent::check($request, $response);
|
|
}
|
|
|
|
protected function validateUserPass($username, $password): bool
|
|
{
|
|
$user = $this->securityService->authenticatePassword($username, $password);
|
|
if ($user === null) {
|
|
return false;
|
|
}
|
|
|
|
$this->identityContext->initialize($user);
|
|
return true;
|
|
}
|
|
}
|