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

PublicKeyCredentialCreationValidator::isValid()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 5
nop 2
dl 0
loc 13
rs 9.6111
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) 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 U2FAuthentication\Fido2\AttestationStatement\AttestationStatementSupportManager;
17
18
class PublicKeyCredentialCreationValidator
19
{
20
    /**
21
     * @var AttestationStatementSupportManager
22
     */
23
    private $attestationStatementSupportManager;
24
25
    public function __construct(AttestationStatementSupportManager $attestationStatementSupportManager)
26
    {
27
        $this->attestationStatementSupportManager = $attestationStatementSupportManager;
28
    }
29
30
    /**
31
     * @see https://www.w3.org/TR/webauthn/#registering-a-new-credential
32
     */
33
    public function isValid(PublicKeyCredential $publicKeyCredential, PublicKeyCredentialCreationOptions $publicKeyCredentialCreationOptions): bool
34
    {
35
        if ('webauthn.create' !== $publicKeyCredential->getResponse()->getClientDataJSON()->getType()) {
36
            throw new \InvalidArgumentException();
37
        }
38
        if (hash_equals($publicKeyCredentialCreationOptions->getChallenge(), $publicKeyCredential->getResponse()->getClientDataJSON()->getChallenge())) {
39
            throw new \InvalidArgumentException();
40
        }
41
        if ($publicKeyCredentialCreationOptions->getRp()->getId() !== $publicKeyCredential->getResponse()->getClientDataJSON()->getOrigin()) {
42
            throw new \InvalidArgumentException();
43
        }
44
        if ($publicKeyCredential->getResponse()->getClientDataJSON()->getTokenBinding()) {
45
            throw new \InvalidArgumentException();
46
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 44 is false. This is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
47
    }
48
}
49