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

RegistrationRequest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A jsonSerialize() 0 8 1
A getChallenge() 0 3 1
A __construct() 0 9 3
A getApplicationId() 0 3 1
A getRegisteredKeys() 0 3 1
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 RegistrationRequest implements \JsonSerializable
19
{
20
    private const PROTOCOL_VERSION = 'U2F_V2';
21
22
    /**
23
     * @var string
24
     */
25
    private $applicationId;
26
27
    /**
28
     * @var string
29
     */
30
    private $challenge;
31
32
    /**
33
     * @var RegisteredKey[]
34
     */
35
    private $registeredKeys = [];
36
37
    /**
38
     * RegistrationRequest constructor.
39
     *
40
     * @param string          $applicationId
41
     * @param RegisteredKey[] $registeredKeys
42
     *
43
     * @throws \Exception
44
     */
45
    private function __construct(string $applicationId, array $registeredKeys)
46
    {
47
        $this->applicationId = $applicationId;
48
        $this->challenge = random_bytes(32);
49
        foreach ($registeredKeys as $registeredKey) {
50
            if (!$registeredKey instanceof RegisteredKey) {
51
                throw new \InvalidArgumentException('Invalid registered keys list.');
52
            }
53
            $this->registeredKeys[Base64Url::encode($registeredKey->getKeyHandler())] = $registeredKey;
54
        }
55
    }
56
57
    /**
58
     * @param string          $applicationId
59
     * @param RegisteredKey[] $registeredKeys
60
     *
61
     * @throws \Exception
62
     *
63
     * @return RegistrationRequest
64
     */
65
    public static function create(string $applicationId, array $registeredKeys = []): self
66
    {
67
        return new self($applicationId, $registeredKeys);
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getApplicationId(): string
74
    {
75
        return $this->applicationId;
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getChallenge(): string
82
    {
83
        return $this->challenge;
84
    }
85
86
    /**
87
     * @return RegisteredKey[]
88
     */
89
    public function getRegisteredKeys(): array
90
    {
91
        return $this->registeredKeys;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function jsonSerialize(): array
98
    {
99
        return [
100
            'appId'            => $this->applicationId,
101
            'registerRequests' => [
102
                ['version'   => self::PROTOCOL_VERSION, 'challenge' => Base64Url::encode($this->challenge)],
103
            ],
104
            'registeredKeys' => array_values($this->registeredKeys),
105
        ];
106
    }
107
}
108