Completed
Push — master ( 7acc61...cd9e85 )
by Florent
22s
created

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