RegistrationRequest::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 7
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) 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\Fido;
15
16
use Assert\Assertion;
17
use Base64Url\Base64Url;
18
19
class RegistrationRequest implements \JsonSerializable
20
{
21
    private const PROTOCOL_VERSION = 'U2F_V2';
22
23
    private $applicationId;
24
25
    private $challenge;
26
27
    /**
28
     * @var RegisteredKey[]
29
     */
30
    private $registeredKeys = [];
31
32
    /**
33
     * @param RegisteredKey[] $registeredKeys
34
     */
35
    public function __construct(string $applicationId, array $registeredKeys)
36
    {
37
        $this->applicationId = $applicationId;
38
        $this->challenge = random_bytes(32);
39
        foreach ($registeredKeys as $registeredKey) {
40
            Assertion::isInstanceOf($registeredKey, RegisteredKey::class, 'Invalid registered keys list.');
41
            $this->registeredKeys[Base64Url::encode((string) $registeredKey->getKeyHandler())] = $registeredKey;
42
        }
43
    }
44
45
    public function getApplicationId(): string
46
    {
47
        return $this->applicationId;
48
    }
49
50
    public function getChallenge(): string
51
    {
52
        return $this->challenge;
53
    }
54
55
    /**
56
     * @return RegisteredKey[]
57
     */
58
    public function getRegisteredKeys(): array
59
    {
60
        return $this->registeredKeys;
61
    }
62
63
    public function jsonSerialize(): array
64
    {
65
        return [
66
            'appId' => $this->applicationId,
67
            'registerRequests' => [
68
                ['version' => self::PROTOCOL_VERSION, 'challenge' => Base64Url::encode($this->challenge)],
69
            ],
70
            'registeredKeys' => array_values($this->registeredKeys),
71
        ];
72
    }
73
}
74