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 pathWebAuthn.php
154 lines (139 loc) · 4.64 KB
/
WebAuthn.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace DarkGhostHunter\Larapass\Facades;
use Closure;
use DarkGhostHunter\Larapass\Auth\CredentialBroker;
use DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable;
use DarkGhostHunter\Larapass\WebAuthn\WebAuthnAssertValidator;
use DarkGhostHunter\Larapass\WebAuthn\WebAuthnAttestCreator;
use DarkGhostHunter\Larapass\WebAuthn\WebAuthnAttestValidator;
use Illuminate\Support\Facades\Facade;
use Webauthn\PublicKeyCredentialCreationOptions;
use Webauthn\PublicKeyCredentialRequestOptions;
class WebAuthn extends Facade
{
/**
* Constant representing a successfully sent recovery.
*
* @var string
*/
public const RECOVERY_SENT = CredentialBroker::RESET_LINK_SENT;
/**
* Constant representing a successfully reset recovery.
*
* @var string
*/
public const RECOVERY_ATTACHED = CredentialBroker::PASSWORD_RESET;
/**
* Constant representing the user not found response.
*
* @var string
*/
public const INVALID_USER = CredentialBroker::INVALID_USER;
/**
* Constant representing an invalid token.
*
* @var string
*/
public const INVALID_TOKEN = CredentialBroker::INVALID_TOKEN;
/**
* Constant representing a throttled reset attempt.
*
* @var string
*/
public const RECOVERY_THROTTLED = CredentialBroker::RESET_THROTTLED;
/**
* Creates a new attestation (registration) request.
*
* @param \DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable $user
*
* @return \Webauthn\PublicKeyCredentialCreationOptions
*/
public static function generateAttestation(WebAuthnAuthenticatable $user): PublicKeyCredentialCreationOptions
{
return static::$app[WebAuthnAttestCreator::class]->generateAttestation($user);
}
/**
* Validates the attestation response, and returns the validated credentials.
*
* It returns `false` when the validation fails.
*
* @param array $data
* @param \DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable $user
*
* @return bool|\Webauthn\PublicKeyCredentialSource
*/
public static function validateAttestation(array $data, WebAuthnAuthenticatable $user)
{
return static::$app[WebAuthnAttestValidator::class]->validate($data, $user);
}
/**
* Creates a new assertion request for a given user, or blank if there is no user given.
*
* @param \DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable|null $user
*
* @return \Webauthn\PublicKeyCredentialRequestOptions
*/
public static function generateAssertion(?WebAuthnAuthenticatable $user = null): PublicKeyCredentialRequestOptions
{
return static::$app[WebAuthnAssertValidator::class]->generateAssertion($user);
}
/**
* Validates the attestation response, and returns the used credentials.
*
* It returns `false` when the validation fails.
*
* @param array $data
*
* @return bool
*/
public static function validateAssertion(array $data): bool
{
return (bool)static::$app[WebAuthnAssertValidator::class]->validate($data);
}
/**
* Sends an account recovery email to an user by the credentials.
*
* @param array $credentials
*
* @return string
*/
public static function sendRecoveryLink(array $credentials): string
{
return static::$app[CredentialBroker::class]->sendResetLink($credentials);
}
/**
* Recover the account for the given token.
*
* @param array $credentials
* @param \Closure $callback
*
* @return \Illuminate\Contracts\Auth\CanResetPassword|mixed|string
*/
public static function recover(array $credentials, Closure $callback)
{
return static::$app[CredentialBroker::class]->reset($credentials, $callback);
}
/**
* Get the user for the given credentials.
*
* @param array $credentials
*
* @return null|\DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable|\Illuminate\Contracts\Auth\CanResetPassword
*/
public static function getUser(array $credentials)
{
return static::$app[CredentialBroker::class]->getUser($credentials);
}
/**
* Validate the given account recovery token.
*
* @param \DarkGhostHunter\Larapass\Contracts\WebAuthnAuthenticatable|\Illuminate\Contracts\Auth\CanResetPassword|null $user
* @param string $token
*
* @return bool
*/
public static function tokenExists($user, string $token): bool
{
return $user ? static::$app[CredentialBroker::class]->tokenExists($user, $token) : false;
}
}