Failed Conditions
Push — master ( 104856...f1eb38 )
by Florent
02:37
created

SignatureRequest::hasRegisteredKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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) 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\Fido;
15
16
use Base64Url\Base64Url;
17
18
class SignatureRequest implements \JsonSerializable
19
{
20
    /**
21
     * @var string
22
     */
23
    private $applicationId;
24
25
    /**
26
     * @var string
27
     */
28
    private $challenge;
29
30
    /**
31
     * @var RegisteredKey[]
32
     */
33
    private $registeredKeys = [];
34
35
    /**
36
     * SignatureRequest constructor.
37
     *
38
     * @param string          $applicationId
39
     * @param RegisteredKey[] $registeredKeys
40
     *
41
     * @throws \Exception
42
     */
43
    private function __construct(string $applicationId, array $registeredKeys)
44
    {
45
        $this->applicationId = $applicationId;
46
        foreach ($registeredKeys as $registeredKey) {
47
            if (!$registeredKey instanceof RegisteredKey) {
48
                throw new \InvalidArgumentException('Invalid registered keys list.');
49
            }
50
            $this->registeredKeys[Base64Url::encode($registeredKey->getKeyHandler())] = $registeredKey;
51
        }
52
        $this->challenge = random_bytes(32);
53
    }
54
55
    /**
56
     * @param string          $applicationId
57
     * @param RegisteredKey[] $registeredKeys
58
     *
59
     * @throws \Exception
60
     *
61
     * @return SignatureRequest
62
     */
63
    public static function create(string $applicationId, array $registeredKeys): self
64
    {
65
        return new self($applicationId, $registeredKeys);
66
    }
67
68
    /**
69
     * @param RegisteredKey $registeredKey
70
     */
71
    public function addRegisteredKey(RegisteredKey $registeredKey): void
72
    {
73
        $this->registeredKeys[Base64Url::encode($registeredKey->getKeyHandler())] = $registeredKey;
74
    }
75
76
    /**
77
     * @param KeyHandle $keyHandle
78
     *
79
     * @return bool
80
     */
81
    public function hasRegisteredKey(KeyHandle $keyHandle): bool
82
    {
83
        return array_key_exists(Base64Url::encode($keyHandle->getValue()), $this->registeredKeys);
84
    }
85
86
    /**
87
     * @param KeyHandle $keyHandle
88
     *
89
     * @return RegisteredKey
90
     */
91
    public function getRegisteredKey(KeyHandle $keyHandle): RegisteredKey
92
    {
93
        if (!$this->hasRegisteredKey($keyHandle)) {
94
            throw new \InvalidArgumentException('Unsupported key handle.');
95
        }
96
97
        return $this->registeredKeys[Base64Url::encode($keyHandle->getValue())];
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getApplicationId(): string
104
    {
105
        return $this->applicationId;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getChallenge(): string
112
    {
113
        return $this->challenge;
114
    }
115
116
    /**
117
     * @return RegisteredKey[]
118
     */
119
    public function getRegisteredKeys(): array
120
    {
121
        return $this->registeredKeys;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function jsonSerialize(): array
128
    {
129
        return [
130
            'appId'          => $this->applicationId,
131
            'challenge'      => Base64Url::encode($this->challenge),
132
            'registeredKeys' => array_values($this->registeredKeys),
133
        ];
134
    }
135
}
136