PublicKeyCredentialRequestOptions::jsonSerialize()   B
last analyzed

Complexity

Conditions 7
Paths 32

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 32
nop 0
dl 0
loc 27
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace U2FAuthentication\Fido2;
15
16
use U2FAuthentication\Fido2\AuthenticationExtensions\AuthenticationExtensionsClientInputs;
17
18
class PublicKeyCredentialRequestOptions implements \JsonSerializable
19
{
20
    public const USER_VERIFICATION_REQUIREMENT_REQUIRED = 'required';
21
    public const USER_VERIFICATION_REQUIREMENT_PREFERRED = 'preferred';
22
    public const USER_VERIFICATION_REQUIREMENT_DISCOURAGED = 'discouraged';
23
24
    private $challenge;
25
26
    private $timeout;
27
28
    private $rpId;
29
30
    /**
31
     * @var PublicKeyCredentialDescriptor[]
32
     */
33
    private $allowCredentials;
34
35
    private $userVerification;
36
37
    private $extensions;
38
39
    /**
40
     * @param PublicKeyCredentialDescriptor[] $allowCredentials
41
     */
42
    public function __construct(string $challenge, ?int $timeout = null, ?string $rpId = null, array $allowCredentials = [], ?string $userVerification = null, ?AuthenticationExtensionsClientInputs $extensions = null)
43
    {
44
        $this->challenge = $challenge;
45
        $this->timeout = $timeout;
46
        $this->rpId = $rpId;
47
        $this->allowCredentials = array_values($allowCredentials);
48
        $this->userVerification = $userVerification;
49
        $this->extensions = $extensions;
50
    }
51
52
    public function getChallenge(): string
53
    {
54
        return $this->challenge;
55
    }
56
57
    public function getTimeout(): ?int
58
    {
59
        return $this->timeout;
60
    }
61
62
    public function getRpId(): ?string
63
    {
64
        return $this->rpId;
65
    }
66
67
    /**
68
     * @return PublicKeyCredentialDescriptor[]
69
     */
70
    public function getAllowCredentials(): array
71
    {
72
        return $this->allowCredentials;
73
    }
74
75
    public function getUserVerification(): ?string
76
    {
77
        return $this->userVerification;
78
    }
79
80
    public function getExtensions(): ?AuthenticationExtensionsClientInputs
81
    {
82
        return $this->extensions;
83
    }
84
85
    public function jsonSerialize(): array
86
    {
87
        $json = [
88
            'challenge' => base64_encode($this->challenge),
89
        ];
90
91
        if ($this->rpId) {
92
            $json['rpId'] = $this->rpId;
93
        }
94
95
        if ($this->userVerification) {
96
            $json['userVerification'] = $this->userVerification;
97
        }
98
99
        if (!empty($this->allowCredentials)) {
100
            $json['allowCredentials'] = $this->allowCredentials;
101
        }
102
103
        if ($this->extensions && 0 !== $this->extensions->count()) {
104
            $json['extensions'] = $this->extensions;
105
        }
106
107
        if (!\is_null($this->timeout)) {
108
            $json['timeout'] = $this->timeout;
109
        }
110
111
        return $json;
112
    }
113
}
114