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

ClientData::getRawData()   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 0
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 ClientData
19
{
20
    /**
21
     * @var string
22
     */
23
    private $rawData;
24
25
    /**
26
     * @var string
27
     */
28
    private $typ;
29
30
    /**
31
     * @var string
32
     */
33
    private $challenge;
34
35
    /**
36
     * @var string
37
     */
38
    private $origin;
39
40
    /**
41
     * @var string
42
     */
43
    private $cid_pubkey;
44
45
    /**
46
     * ClientData constructor.
47
     *
48
     * @param string $rawData
49
     * @param array  $clientData
50
     */
51
    private function __construct(string $rawData, array $clientData)
52
    {
53
        $this->rawData = $rawData;
54
        foreach ($clientData as $k => $v) {
55
            $this->$k = $v;
56
        }
57
    }
58
59
    /**
60
     * @param string $clientData
61
     *
62
     * @return ClientData
63
     */
64
    public static function create(string $clientData): self
65
    {
66
        $rawData = Base64Url::decode($clientData);
67
        $clientData = json_decode($rawData, true);
68
        if (!is_array($clientData)) {
69
            throw new \InvalidArgumentException('Invalid client data.');
70
        }
71
72
        $diff = array_diff_key(get_class_vars(self::class), $clientData);
73
        unset($diff['rawData']);
74
        if (!empty($diff)) {
75
            throw new \InvalidArgumentException('Invalid client data.');
76
        }
77
78
        return new self($rawData, $clientData);
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getRawData(): string
85
    {
86
        return $this->rawData;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getType(): string
93
    {
94
        return $this->typ;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getChallenge(): string
101
    {
102
        return Base64Url::decode($this->challenge);
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getOrigin(): string
109
    {
110
        return $this->origin;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getChannelIdPublicKey(): string
117
    {
118
        return $this->cid_pubkey;
119
    }
120
}
121