Passed
Push — master ( b39fb5...590849 )
by Roeland
11:59 queued 10s
created

CredentialRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2020, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC\Authentication\WebAuthn;
26
27
use OC\Authentication\WebAuthn\Db\PublicKeyCredentialEntity;
28
use OC\Authentication\WebAuthn\Db\PublicKeyCredentialMapper;
29
use OCP\AppFramework\Db\IMapperException;
30
use Webauthn\PublicKeyCredentialSource;
31
use Webauthn\PublicKeyCredentialSourceRepository;
32
use Webauthn\PublicKeyCredentialUserEntity;
33
34
class CredentialRepository implements PublicKeyCredentialSourceRepository {
35
36
	/** @var PublicKeyCredentialMapper */
37
	private $credentialMapper;
38
39
	public function __construct(PublicKeyCredentialMapper $credentialMapper) {
40
		$this->credentialMapper = $credentialMapper;
41
	}
42
43
	public function findOneByCredentialId(string $publicKeyCredentialId): ?PublicKeyCredentialSource {
44
		try {
45
			$entity = $this->credentialMapper->findOneByCredentialId($publicKeyCredentialId);
46
			return $entity->toPublicKeyCredentialSource();
47
		} catch (IMapperException $e) {
48
			return  null;
49
		}
50
	}
51
52
	/**
53
	 * @return PublicKeyCredentialSource[]
54
	 */
55
	public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity): array {
56
		$uid = $publicKeyCredentialUserEntity->getId();
57
		$entities = $this->credentialMapper->findAllForUid($uid);
58
59
		return array_map(function (PublicKeyCredentialEntity $entity) {
60
			return $entity->toPublicKeyCredentialSource();
61
		}, $entities);
62
	}
63
64
	public function saveAndReturnCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, string $name = null): PublicKeyCredentialEntity {
65
		$oldEntity = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $oldEntity is dead and can be removed.
Loading history...
66
67
		try {
68
			$oldEntity = $this->credentialMapper->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId());
69
		} catch (IMapperException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
70
71
		}
72
73
		if ($name === null) {
74
			$name = 'default';
75
		}
76
77
		$entity = PublicKeyCredentialEntity::fromPublicKeyCrendentialSource($name, $publicKeyCredentialSource);
78
79
		if ($oldEntity) {
0 ignored issues
show
introduced by
$oldEntity is of type OC\Authentication\WebAut...blicKeyCredentialEntity, thus it always evaluated to true.
Loading history...
80
			$entity->setId($oldEntity->getId());
81
			if ($name === null) {
0 ignored issues
show
introduced by
The condition $name === null is always false.
Loading history...
82
				$entity->setName($oldEntity->getName());
83
			}
84
		}
85
86
		return $this->credentialMapper->insertOrUpdate($entity);
87
	}
88
89
	public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, string $name = null): void {
90
		$this->saveAndReturnCredentialSource($publicKeyCredentialSource, $name);
91
	}
92
93
}
94