-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathAuthenticateAdapter.php
More file actions
98 lines (85 loc) · 3.49 KB
/
Copy pathAuthenticateAdapter.php
File metadata and controls
98 lines (85 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Webauthn;
use Cake\Http\Exception\BadRequestException;
use Webauthn\AuthenticationExtensions\AuthenticationExtensionsClientInputs;
use Webauthn\AuthenticatorAssertionResponse;
use Webauthn\AuthenticatorAssertionResponseValidator;
use Webauthn\PublicKeyCredentialRequestOptions;
use Webauthn\PublicKeyCredentialSource;
class AuthenticateAdapter extends BaseAdapter
{
/**
* @return \Webauthn\PublicKeyCredentialRequestOptions
*/
public function getOptions(): PublicKeyCredentialRequestOptions
{
$userEntity = $this->getUserEntity();
$allowedCredentials = array_map(function (PublicKeyCredentialSource $credential) {
return $credential->getPublicKeyCredentialDescriptor();
}, $this->repository->findAllForUserEntity($userEntity));
$options = (new PublicKeyCredentialRequestOptions(random_bytes(32)))
->setRpId($this->rpEntity->getId())
->setUserVerification(PublicKeyCredentialRequestOptions::USER_VERIFICATION_REQUIREMENT_PREFERRED)
->allowCredentials(...$allowedCredentials)
->setExtensions(new AuthenticationExtensionsClientInputs());
$this->request->getSession()->write(
'Webauthn2fa.authenticateOptions',
json_encode($options),
);
return $options;
}
/**
* Verify the registration response
*
* @return \Webauthn\PublicKeyCredentialSource
* @throws \Throwable
*/
public function verifyResponse(): \Webauthn\PublicKeyCredentialSource
{
/** @var \Webauthn\PublicKeyCredentialRequestOptions $options */
$options = PublicKeyCredentialRequestOptions::createFromString(
(string)$this->request->getSession()->read('Webauthn2fa.authenticateOptions'),
);
$publicKeyCredentialLoader = $this->createPublicKeyCredentialLoader();
$publicKeyCredential = $publicKeyCredentialLoader->loadArray($this->request->getData());
$authenticatorAssertionResponse = $publicKeyCredential->getResponse();
if ($authenticatorAssertionResponse instanceof AuthenticatorAssertionResponse) {
$authenticatorAssertionResponseValidator = $this->createAssertionResponseValidator();
return $authenticatorAssertionResponseValidator->check(
$publicKeyCredential->getRawId(),
$authenticatorAssertionResponse,
$options,
$this->request,
$this->getUserEntity()->getId(),
);
}
throw new BadRequestException(
__d(
'cake_d_c/users',
'Could not validate credential response for authentication',
),
);
}
/**
* @return \Webauthn\AuthenticatorAssertionResponseValidator
*/
protected function createAssertionResponseValidator(): AuthenticatorAssertionResponseValidator
{
return new AuthenticatorAssertionResponseValidator(
$this->repository,
null,
$this->createExtensionOutputCheckerHandler(),
$this->getAlgorithmManager(),
);
}
}