Test Failed
Pull Request — master (#350)
by Raffael
26:39
created

CredentialRepository   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 95
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findOneByCredentialId() 0 14 2
A findAllForUserEntity() 0 4 1
A saveCredentialSource() 0 11 1
A getDescriptorsByUser() 0 10 2
A has() 0 3 1
A get() 0 3 1
A getUserHandleFor() 0 3 1
A getCounterFor() 0 3 1
A updateCounterFor() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Webauthn;
13
14
use Base64Url\Base64Url;
15
use Generator;
16
use MongoDB\BSON\ObjectId;
17
use MongoDB\BSON\ObjectIdInterface;
18
use MongoDB\BSON\UTCDateTime;
19
use MongoDB\Database;
20
use Webauthn\AttestedCredentialData;
21
use Webauthn\PublicKeyCredentialSource;
22
use Webauthn\PublicKeyCredentialSourceRepository;
23
use Webauthn\PublicKeyCredentialUserEntity;
24
25
class CredentialRepository implements PublicKeyCredentialSourceRepository
26
{
27
    /**
28
     * Database.
29
     *
30
     * @var Database
31
     */
32
    protected $db;
33
34
    /**
35
     * Initialize.
36
     */
37
    public function __construct(Database $db)
38
    {
39
        $this->db = $db;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function findOneByCredentialId(string $publicKeyCredentialId): ?PublicKeyCredentialSource
46
    {
47
        $result = $this->db->devices->findOne([
48
            'key.publicKeyCredentialId' => Base64Url::encode($publicKeyCredentialId),
49
        ]);
50
51
        if ($result === null) {
52
            return null;
53
        }
54
55
        $result['key']['credentialPublicKey'] = Base64Url::decode($result['key']['credentialPublicKey']);
56
57
        return PublicKeyCredentialSource::createFromArray($result['key']);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity): array
64
    {
65
        return [];
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource): void
72
    {
73
        $key = json_decode(json_encode($publicKeyCredentialSource));
74
        $record = [
75
            'created' => new UTCDateTime(),
76
            'owner' => new ObjectId(base64_decode($key->userHandle)),
77
            'key' => $key,
78
        ];
79
80
        $this->db->devices->insertOne($record);
81
    }
82
83
    /**
84
     * Get credentials by user.
85
     */
86
    public function getDescriptorsByUser(ObjectIdInterface $user): Generator
87
    {
88
        $result = $this->db->devices->find([
89
            'owner' => $user,
90
        ]);
91
92
        foreach ($result as $resource) {
93
            yield PublicKeyCredentialSource::createFromArray($resource['key'])->getPublicKeyCredentialDescriptor();
94
        }
95
    }
96
97
    /**
98
     * Depreacted interface methods.
99
     */
100
    public function has(string $credentialId): bool
101
    {
102
    }
103
104
    public function get(string $credentialId): AttestedCredentialData
105
    {
106
    }
107
108
    public function getUserHandleFor(string $credentialId): string
109
    {
110
    }
111
112
    public function getCounterFor(string $credentialId): int
113
    {
114
    }
115
116
    public function updateCounterFor(string $credentialId, int $newCounter): void
117
    {
118
    }
119
}
120