This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
forked from DarkGhostHunter/Larapass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegistersWebAuthn.php
63 lines (53 loc) · 1.99 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
<?php
namespace DarkGhostHunter\Larapass\Http;
use Illuminate\Http\Request;
use DarkGhostHunter\Larapass\Facades\WebAuthn;
use DarkGhostHunter\Larapass\Events\AttestationSuccessful;
use DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable;
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)
{
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)
{
// 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(
$request->validate($this->attestationRules()), $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($user, $credentials)
{
// ...
}
}