12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- declare(strict_types=1);
- /**
- * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- 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);
- }
- public function saveAndReturnCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, ?string $name = null): PublicKeyCredentialEntity {
- $oldEntity = null;
- try {
- $oldEntity = $this->credentialMapper->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId());
- } catch (IMapperException $e) {
- }
- $defaultName = false;
- if ($name === null) {
- $defaultName = true;
- $name = 'default';
- }
- $entity = PublicKeyCredentialEntity::fromPublicKeyCrendentialSource($name, $publicKeyCredentialSource);
- if ($oldEntity) {
- $entity->setId($oldEntity->getId());
- if ($defaultName) {
- $entity->setName($oldEntity->getName());
- }
- }
- return $this->credentialMapper->insertOrUpdate($entity);
- }
- public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, ?string $name = null): void {
- $this->saveAndReturnCredentialSource($publicKeyCredentialSource, $name);
- }
- }
|