|
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(); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
// Normally this condition should be true. Just make sure you received the credential data |
|
87
|
|
|
$attestedCredentialData = null; |
|
|
|
|
|
|
88
|
|
|
if ($response->getAttestationObject()->getAuthData()->hasAttestedCredentialData()) { |
|
89
|
|
|
$attestedCredentialData = $response->getAttestationObject()->getAuthData()->getAttestedCredentialData(); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.