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

PublicKeyCredentialEntity::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
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\Db;
26
27
use JsonSerializable;
28
use OCP\AppFramework\Db\Entity;
29
use Webauthn\PublicKeyCredentialSource;
30
use Webauthn\TrustPath\TrustPathLoader;
31
32
/**
33
 * @since 19.0.0
34
 *
35
 * @method string getUid();
36
 * @method void setUid(string $uid)
37
 * @method string getName();
38
 * @method void setName(string $name);
39
 * @method string getPublicKeyCredentialId();
40
 * @method void setPublicKeyCredentialId(string $id);
41
 * @method string getData();
42
 * @method void setData(string $data);
43
 */
44
class PublicKeyCredentialEntity extends Entity implements JsonSerializable {
45
46
	/** @var string */
47
	protected $name;
48
49
	/** @var string */
50
	protected $uid;
51
52
	/** @var string */
53
	protected $publicKeyCredentialId;
54
55
	/** @var string */
56
	protected $data;
57
58
	public function __construct() {
59
		$this->addType('name', 'string');
60
		$this->addType('uid', 'string');
61
		$this->addType('publicKeyCredentialId', 'string');
62
		$this->addType('data', 'string');
63
	}
64
65
	static function fromPublicKeyCrendentialSource(string $name, PublicKeyCredentialSource $publicKeyCredentialSource): PublicKeyCredentialEntity {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
		$publicKeyCredentialEntity = new self();
67
68
		$publicKeyCredentialEntity->setName($name);
69
		$publicKeyCredentialEntity->setUid($publicKeyCredentialSource->getUserHandle());
70
		$publicKeyCredentialEntity->setPublicKeyCredentialId(base64_encode($publicKeyCredentialSource->getPublicKeyCredentialId()));
71
		$publicKeyCredentialEntity->setData(json_encode($publicKeyCredentialSource));
72
73
		return $publicKeyCredentialEntity;
74
	}
75
76
	function toPublicKeyCredentialSource(): PublicKeyCredentialSource {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
77
		return PublicKeyCredentialSource::createFromArray(
78
			json_decode($this->getData(), true)
79
		);
80
	}
81
82
	/**
83
	 * @inheritDoc
84
	 */
85
	public function jsonSerialize(): array {
86
		return [
87
			'id' => $this->getId(),
88
			'name' => $this->getName(),
89
		];
90
	}
91
92
}
93