This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathRegistersWebAuthn.php
69 lines (58 loc) · 2.18 KB
/
RegistersWebAuthn.php
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
<?php
namespace DarkGhostHunter\Larapass\Http;
use DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable;
use DarkGhostHunter\Larapass\Events\AttestationSuccessful;
use DarkGhostHunter\Larapass\Facades\WebAuthn;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Webauthn\PublicKeyCredentialSource;
trait RegistersWebAuthn
{
use WebAuthnRules;
/**
* Returns a challenge to be verified by the user device.
*
* @param \DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable $user
*
* @return \Illuminate\Http\JsonResponse
*/
public function options(WebAuthnAuthenticatable $user): JsonResponse
{
return response()->json(WebAuthn::generateAttestation($user));
}
/**
* Registers a device for further WebAuthn authentication.
*
* @param \Illuminate\Http\Request $request
* @param \DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable $user
*
* @return \Illuminate\Http\Response
*/
public function register(Request $request, WebAuthnAuthenticatable $user): Response
{
$input = $request->validate($this->attestationRules());
// We'll validate the challenge coming from the authenticator and instantly
// save it into the credentials store. If the data is invalid we will bail
// out and return a non-authorized response since we can't save the data.
$validCredential = WebAuthn::validateAttestation($input, $user);
if ($validCredential) {
$user->addCredential($validCredential);
event(new AttestationSuccessful($user, $validCredential));
return $this->credentialRegistered($user, $validCredential) ?? response()->noContent();
}
return response()->noContent(422);
}
/**
* The user has registered a credential.
*
* @param \DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable $user
* @param \Webauthn\PublicKeyCredentialSource $credentials
*
* @return void|mixed
*/
protected function credentialRegistered(WebAuthnAuthenticatable $user, PublicKeyCredentialSource $credentials)
{
// ...
}
}