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

PublicKeyCredentialLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 29 6
A createAuthenticatorResponse() 0 13 3
A __construct() 0 4 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
use CBOR\Decoder;
18
use U2FAuthentication\Fido2\AttestationStatement\AttestationObjectLoader;
19
20
class PublicKeyCredentialLoader
21
{
22
    /**
23
     * @var Decoder
24
     */
25
    private $decoder;
26
27
    /**
28
     * @var AttestationObjectLoader
29
     */
30
    private $attestationObjectLoader;
31
32
    public function __construct(Decoder $decoder, AttestationObjectLoader $attestationObjectLoader)
33
    {
34
        $this->decoder = $decoder;
35
        $this->attestationObjectLoader = $attestationObjectLoader;
36
    }
37
38
    public function load(string $data): PublicKeyCredential
39
    {
40
        $json = json_decode($data, true);
41
        if (!array_key_exists('id', $json)) {
42
            throw new \InvalidArgumentException();
43
        }
44
        $id = Base64Url::decode($json['id']);
45
        if (!array_key_exists('rawId', $json)) {
46
            throw new \InvalidArgumentException();
47
        }
48
        $rawId = Base64Url::decode($json['rawId']);
49
        if (!array_key_exists('type', $json)) {
50
            throw new \InvalidArgumentException();
51
        }
52
        if (!hash_equals($id, $rawId)) {
53
            throw new \InvalidArgumentException();
54
        }
55
        if (!array_key_exists('response', $json)) {
56
            throw new \InvalidArgumentException();
57
        }
58
59
        $publicKeyCredential = new PublicKeyCredential(
60
            $json['id'],
61
            $json['type'],
62
            $rawId,
63
            $this->createAuthenticatorResponse($json['response'])
64
        );
65
66
        return $publicKeyCredential;
67
    }
68
69
    private function createAuthenticatorResponse(array $response): AuthenticatorAttestationResponse
70
    {
71
        if (!array_key_exists('clientDataJSON', $response)) {
72
            throw new \InvalidArgumentException();
73
        }
74
        if (!array_key_exists('attestationObject', $response)) {
75
            throw new \InvalidArgumentException();
76
        }
77
        $attestationObject = $this->attestationObjectLoader->load($response['attestationObject']);
78
79
        return new AuthenticatorAttestationResponse(
80
            CollectedClientData::createFormJson($response['clientDataJSON']),
81
            $attestationObject
82
        );
83
    }
84
}
85