Files
service_dav/lib/Auth/DavAuthBackend.php
T
2026-07-14 18:24:31 -04:00

46 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXM\ServiceDav\Auth;
use KTXC\Service\SecurityService;
use KTXC\SessionIdentity;
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 SessionIdentity $identity,
private readonly SecurityService $securityService,
string $realm = 'KTXC DAV',
) {
$this->realm = $realm;
}
public function check(RequestInterface $request, ResponseInterface $response): array
{
if ($this->identity->identity() !== null) {
$userId = $this->identity->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->identity->initialize($user, false);
return true;
}
}