Passed
Push — master ( 3569aa...ab7ae0 )
by Florent
02:27
created

CollectedClientData   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenBinding() 0 3 1
A has() 0 3 1
A getOrigin() 0 3 1
A createFormJson() 0 20 5
A getChallenge() 0 3 1
A __construct() 0 4 1
A get() 0 7 2
A getType() 0 3 1
A getRawData() 0 3 1
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 Base64Url\Base64Url;
17
18
class CollectedClientData
19
{
20
    /**
21
     * @var string
22
     */
23
    private $rawData;
24
25
    /**
26
     * @var array
27
     */
28
    private $data;
29
30
    public function __construct(string $rawData, array $data)
31
    {
32
        $this->data = $data;
33
        $this->rawData = $rawData;
34
    }
35
36
    /**
37
     * @return CollectedClientData
38
     */
39
    public static function createFormJson(string $data): self
40
    {
41
        $json = json_decode(Base64Url::decode($data), true);
42
43
        if (!array_key_exists('type', $json)) {
44
            throw new \InvalidArgumentException();
45
        }
46
        if (!array_key_exists('challenge', $json)) {
47
            throw new \InvalidArgumentException();
48
        }
49
        if (!array_key_exists('origin', $json)) {
50
            throw new \InvalidArgumentException();
51
        }
52
        if (array_key_exists('tokenBinding', $json)) {
53
            $json['tokenBinding'] = TokenBinding::createFormJson($json['tokenBinding']);
54
        } else {
55
            $json['tokenBinding'] = null;
56
        }
57
58
        return new self(Base64Url::decode($data), $json);
59
    }
60
61
    public function getType(): string
62
    {
63
        return $this->data['type'];
64
    }
65
66
    public function getChallenge(): string
67
    {
68
        return $this->data['challenge'];
69
    }
70
71
    public function getOrigin(): string
72
    {
73
        return $this->data['origin'];
74
    }
75
76
    public function getTokenBinding(): ?TokenBinding
77
    {
78
        return $this->data['tokenBinding'];
79
    }
80
81
    public function getRawData(): string
82
    {
83
        return $this->rawData;
84
    }
85
86
    public function has(string $key)
87
    {
88
        return array_key_exists($key, $this->data);
89
    }
90
91
    public function get(string $key)
92
    {
93
        if (!$this->has($key)) {
94
            throw new \InvalidArgumentException(sprintf('The collected client data has no key "%s".', $key));
95
        }
96
97
        return $this->data['origin'];
98
    }
99
}
100