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

DeviceFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 10
dl 0
loc 71
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 34 3
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\Device;
13
14
use Balloon\App\Webauthn\CredentialRepository;
15
use Micro\Http\Response;
16
use Symfony\Component\HttpFoundation\Request;
17
use Webauthn\AuthenticatorAttestationResponse;
18
use Webauthn\AuthenticatorAttestationResponseValidator;
19
use Webauthn\PublicKeyCredentialCreationOptions;
20
use Webauthn\PublicKeyCredentialLoader;
21
22
class DeviceFactory
23
{
24
    /**
25
     * CredentialRepository.
26
     *
27
     * @var CredentialRepository
28
     */
29
    protected $repository;
30
31
    /**
32
     * PublicKeyCredentialLoader.
33
     *
34
     * @var PublicKeyCredentialLoader
35
     */
36
    protected $loader;
37
38
    /**
39
     * AuthenticatorAttestationResponseValidator.
40
     *
41
     * @var AuthenticatorAttestationResponseValidator
42
     */
43
    protected $validator;
44
45
    /**
46
     * Initialize.
47
     */
48
    public function __construct(CredentialRepository $repository, PublicKeyCredentialLoader $loader, AuthenticatorAttestationResponseValidator $validator)
49
    {
50
        $this->repository = $repository;
51
        $this->validator = $validator;
52
        $this->loader = $loader;
53
    }
54
55
    /**
56
     * Create device.
57
     */
58
    public function create(PublicKeyCredentialCreationOptions $publicKeyCredentialCreationOptions, array $data)
59
    {
60
        // We init the PSR7 Request object
61
        $psr7Request = \Zend\Diactoros\ServerRequestFactory::fromGlobals();
62
63
        // Load the data
64
        $publicKeyCredential = $this->loader->load(json_encode($data));
65
        $response = $publicKeyCredential->getResponse();
66
67
        // Check if the response is an Authenticator Attestation Response
68
        if (!$response instanceof AuthenticatorAttestationResponse) {
69
            throw new \RuntimeException('Not an authenticator attestation response');
70
        }
71
72
        // Check the response against the request
73
        $this->validator->check($response, $publicKeyCredentialCreationOptions, $psr7Request);
74
75
        // You can get the Public Key Credential Source. This object should be persisted using the Public Key Credential Source repository
76
        $publicKeyCredentialSource = \Webauthn\PublicKeyCredentialSource::createFromPublicKeyCredential(
77
            $publicKeyCredential,
78
            $publicKeyCredentialCreationOptions->getUser()->getId()
79
        );
80
81
        $this->repository->saveCredentialSource($publicKeyCredentialSource);
82
83
        //You can also get the PublicKeyCredentialDescriptor.
84
        $publicKeyCredentialDescriptor = $publicKeyCredential->getPublicKeyCredentialDescriptor();
0 ignored issues
show
Unused Code introduced by
$publicKeyCredentialDescriptor is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
86
        // Normally this condition should be true. Just make sure you received the credential data
87
        $attestedCredentialData = null;
0 ignored issues
show
Unused Code introduced by
$attestedCredentialData is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
        if ($response->getAttestationObject()->getAuthData()->hasAttestedCredentialData()) {
89
            $attestedCredentialData = $response->getAttestationObject()->getAuthData()->getAttestedCredentialData();
0 ignored issues
show
Unused Code introduced by
$attestedCredentialData is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
        }
91
    }
92
}
93