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

ClientData::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ClientData::getType() 0 3 1
A ClientData::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\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
    public function __construct(string $clientData)
46
    {
47
        $this->rawData = Base64Url::decode($clientData);
48
        $clientData = json_decode($this->rawData, true);
49
        if (!\is_array($clientData)) {
50
            throw new \InvalidArgumentException('Invalid client data.');
51
        }
52
53
        $diff = array_diff_key(get_class_vars(self::class), $clientData);
54
        unset($diff['rawData'], $diff['cid_pubkey']);
55
56
        if (!empty($diff)) {
57
            throw new \InvalidArgumentException('Invalid client data.');
58
        }
59
60
        foreach ($clientData as $k => $v) {
61
            $this->$k = $v;
62
        }
63
    }
64
65
    public function getRawData(): string
66
    {
67
        return $this->rawData;
68
    }
69
70
    public function getType(): string
71
    {
72
        return $this->typ;
73
    }
74
75
    public function getChallenge(): string
76
    {
77
        return Base64Url::decode($this->challenge);
78
    }
79
80
    public function getOrigin(): string
81
    {
82
        return $this->origin;
83
    }
84
85
    public function getChannelIdPublicKey(): string
86
    {
87
        return $this->cid_pubkey;
88
    }
89
}
90