2020-02-09 20:06:08 +01:00
|
|
|
<?php
|
2020-04-09 11:50:14 +02:00
|
|
|
|
2020-02-09 20:06:08 +01:00
|
|
|
declare(strict_types=1);
|
2020-04-29 11:57:22 +02:00
|
|
|
|
2020-02-09 20:06:08 +01:00
|
|
|
/**
|
2024-05-23 09:26:56 +02:00
|
|
|
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2020-02-09 20:06:08 +01:00
|
|
|
*/
|
|
|
|
namespace OC\Authentication\WebAuthn;
|
|
|
|
|
|
|
|
use OC\Authentication\WebAuthn\Db\PublicKeyCredentialEntity;
|
|
|
|
use OC\Authentication\WebAuthn\Db\PublicKeyCredentialMapper;
|
|
|
|
use OCP\AppFramework\Db\IMapperException;
|
|
|
|
use Webauthn\PublicKeyCredentialSource;
|
|
|
|
use Webauthn\PublicKeyCredentialSourceRepository;
|
|
|
|
use Webauthn\PublicKeyCredentialUserEntity;
|
|
|
|
|
|
|
|
class CredentialRepository implements PublicKeyCredentialSourceRepository {
|
|
|
|
/** @var PublicKeyCredentialMapper */
|
|
|
|
private $credentialMapper;
|
|
|
|
|
|
|
|
public function __construct(PublicKeyCredentialMapper $credentialMapper) {
|
|
|
|
$this->credentialMapper = $credentialMapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function findOneByCredentialId(string $publicKeyCredentialId): ?PublicKeyCredentialSource {
|
|
|
|
try {
|
|
|
|
$entity = $this->credentialMapper->findOneByCredentialId($publicKeyCredentialId);
|
|
|
|
return $entity->toPublicKeyCredentialSource();
|
|
|
|
} catch (IMapperException $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return PublicKeyCredentialSource[]
|
|
|
|
*/
|
|
|
|
public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity): array {
|
|
|
|
$uid = $publicKeyCredentialUserEntity->getId();
|
|
|
|
$entities = $this->credentialMapper->findAllForUid($uid);
|
|
|
|
|
|
|
|
return array_map(function (PublicKeyCredentialEntity $entity) {
|
|
|
|
return $entity->toPublicKeyCredentialSource();
|
|
|
|
}, $entities);
|
|
|
|
}
|
|
|
|
|
2024-03-24 12:17:10 +01:00
|
|
|
public function saveAndReturnCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, ?string $name = null, bool $userVerification = false): PublicKeyCredentialEntity {
|
2020-02-09 20:06:08 +01:00
|
|
|
$oldEntity = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$oldEntity = $this->credentialMapper->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId());
|
|
|
|
} catch (IMapperException $e) {
|
|
|
|
}
|
|
|
|
|
2020-05-07 16:13:19 +02:00
|
|
|
$defaultName = false;
|
2020-02-09 20:06:08 +01:00
|
|
|
if ($name === null) {
|
2020-05-07 16:13:19 +02:00
|
|
|
$defaultName = true;
|
2020-02-09 20:06:08 +01:00
|
|
|
$name = 'default';
|
|
|
|
}
|
|
|
|
|
2024-03-24 12:17:10 +01:00
|
|
|
$entity = PublicKeyCredentialEntity::fromPublicKeyCrendentialSource($name, $publicKeyCredentialSource, $userVerification);
|
2020-02-09 20:06:08 +01:00
|
|
|
|
|
|
|
if ($oldEntity) {
|
|
|
|
$entity->setId($oldEntity->getId());
|
2020-05-07 16:13:19 +02:00
|
|
|
if ($defaultName) {
|
2020-02-09 20:06:08 +01:00
|
|
|
$entity->setName($oldEntity->getName());
|
|
|
|
}
|
2024-03-24 12:17:10 +01:00
|
|
|
|
|
|
|
// Don't downgrade UV just because it was skipped during a login due to another key
|
|
|
|
if ($oldEntity->getUserVerification()) {
|
|
|
|
$entity->setUserVerification(true);
|
|
|
|
}
|
2020-02-09 20:06:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->credentialMapper->insertOrUpdate($entity);
|
|
|
|
}
|
|
|
|
|
2024-03-28 16:13:19 +01:00
|
|
|
public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, ?string $name = null): void {
|
2020-02-09 20:06:08 +01:00
|
|
|
$this->saveAndReturnCredentialSource($publicKeyCredentialSource, $name);
|
|
|
|
}
|
|
|
|
}
|