Completed
Push — master ( b97427...e235cc )
by Raffael
30:35 queued 26:08
created

DeviceFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
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) {
0 ignored issues
show
Bug introduced by
The class Webauthn\AuthenticatorAttestationResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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