Passed
Push — master ( 1f909f...68b106 )
by Thomas
07:19
created

AuthenticationOptions::createForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MadWizard\WebAuthn\Server\Authentication;
4
5
use MadWizard\WebAuthn\Credential\CredentialId;
6
use MadWizard\WebAuthn\Credential\UserHandle;
7
use MadWizard\WebAuthn\Dom\UserVerificationRequirement;
8
use MadWizard\WebAuthn\Exception\WebAuthnException;
9
use MadWizard\WebAuthn\Extension\ExtensionInputInterface;
10
11
final class AuthenticationOptions
12
{
13
    /**
14
     * @var CredentialId[]
15
     */
16
    private $allowCredentials = [];
17
18
    /**
19
     * @var string|null
20
     */
21
    private $userVerification;
22
23
    /**
24
     * @var int|null
25
     */
26
    private $timeout;
27
28
    /**
29
     * @var ExtensionInputInterface[]|null
30
     */
31
    private $extensions;
32
33
    /**
34
     * User handle to load credentials from, or null for client side discoverable.
35
     *
36
     * @var UserHandle|null
37
     */
38
    private $userHandle;
39
40 1
    private function __construct(?UserHandle $userHandle)
41
    {
42 1
        $this->userHandle = $userHandle;
43 1
    }
44
45
    public static function createForUser(UserHandle $userHandle): self
46
    {
47
        return new self($userHandle);
48
    }
49
50 1
    public static function createForAnyUser(): self
51
    {
52 1
        return new self(null);
53
    }
54
55 1
    public function getUserHandle(): ?UserHandle
56
    {
57 1
        return $this->userHandle;
58
    }
59
60 1
    public function addAllowCredential(CredentialId $credential)
61
    {
62 1
        $this->allowCredentials[] = $credential;
63 1
    }
64
65
    /**
66
     * @return CredentialId[]
67
     */
68 1
    public function getAllowCredentials(): array
69
    {
70 1
        return $this->allowCredentials;
71
    }
72
73 1
    public function getUserVerification(): ?string
74
    {
75 1
        return $this->userVerification;
76
    }
77
78
    public function setUserVerification(?string $value): void
79
    {
80
        if ($value !== null && !UserVerificationRequirement::isValidValue($value)) {
81
            throw new WebAuthnException(sprintf('Value %s is not a valid UserVerificationRequirement', $value));
82
        }
83
84
        $this->userVerification = $value;
85
    }
86
87 1
    public function getTimeout(): ?int
88
    {
89 1
        return $this->timeout;
90
    }
91
92
    public function setTimeout(?int $timeout): void
93
    {
94
        $this->timeout = $timeout;
95
    }
96
97
    public function addExtensionInput(ExtensionInputInterface $input) // TODO move to trait? shared with Registration
98
    {
99
        if ($this->extensions === null) {
100
            $this->extensions = [];
101
        }
102
        $this->extensions[] = $input;
103
    }
104
105
    /**
106
     * @return ExtensionInputInterface[]|null
107
     */
108 1
    public function getExtensionInputs(): ?array
109
    {
110 1
        return $this->extensions;
111
    }
112
}
113