Passed
Push — master ( f215e2...ea45dc )
by Florent
02:22
created

PublicKeyCredential   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B createFromJson() 0 20 5
A getRawId() 0 3 1
A createFromReceivedData() 0 5 1
A getResponse() 0 3 1
A getPublicKeyCredentialDescriptor() 0 3 1
A jsonSerialize() 0 7 1
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\Fido2;
15
16
class PublicKeyCredential extends Credential
17
{
18
    /**
19
     * @var string
20
     */
21
    private $rawId;
22
23
    /**
24
     * @var AuthenticatorAttestationResponse
25
     */
26
    private $response;
27
28
    /**
29
     * PublicKeyCredential constructor.
30
     *
31
     * @param string                           $id
32
     * @param string                           $type
33
     * @param string                           $rawId
34
     * @param AuthenticatorAttestationResponse $response
35
     */
36
    public function __construct(string $id, string $type, string $rawId, AuthenticatorAttestationResponse $response)
37
    {
38
        parent::__construct($id, $type);
39
        $this->rawId = $rawId;
40
        $this->response = $response;
41
    }
42
43
    /**
44
     * @param array $json
45
     *
46
     * @return PublicKeyCredential
47
     */
48
    public static function createFromJson(array $json): self
49
    {
50
        if (!array_key_exists('id', $json)) {
51
            throw new \InvalidArgumentException();
52
        }
53
        if (!array_key_exists('rawId', $json)) {
54
            throw new \InvalidArgumentException();
55
        }
56
        if (!array_key_exists('type', $json)) {
57
            throw new \InvalidArgumentException();
58
        }
59
        if (!array_key_exists('response', $json)) {
60
            throw new \InvalidArgumentException();
61
        }
62
63
        return new self(
64
            $json['id'],
65
            $json['type'],
66
            $json['rawId'],
67
            AuthenticatorAttestationResponse::createFromJson($json['response'])
68
        );
69
    }
70
71
    /**
72
     * @param string $data
73
     *
74
     * @return PublicKeyCredential
75
     */
76
    public static function createFromReceivedData(string $data): self
77
    {
78
        $json = json_decode($data, true);
79
80
        return self::createFromJson($json);
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function getRawId(): string
87
    {
88
        return $this->rawId;
89
    }
90
91
    /**
92
     * @return AuthenticatorAttestationResponse
93
     */
94
    public function getResponse(): AuthenticatorAttestationResponse
95
    {
96
        return $this->response;
97
    }
98
99
    /**
100
     * @param string[] $transport
101
     *
102
     * @return PublicKeyCredentialDescriptor
103
     */
104
    public function getPublicKeyCredentialDescriptor(array $transport = []): PublicKeyCredentialDescriptor
105
    {
106
        return new PublicKeyCredentialDescriptor($this->getType(), $this->getId(), $transport);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function jsonSerialize(): array
113
    {
114
        return [
115
            'id'       => $this->getId(),
116
            'type'     => $this->getType(),
117
            'rawId'    => $this->getRawId(),
118
            'response' => $this->response,
119
        ];
120
    }
121
}
122