Skip to content

Commit a079006

Browse files
authored
feat: improve userless connection mode (#488)
1 parent bb5070b commit a079006

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

src/Events/WebauthnLoginData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WebauthnLoginData
1818
* @param PublicKeyCredentialRequestOptions $publicKey The authentication data.
1919
*/
2020
public function __construct(
21-
public User $user,
21+
public ?User $user,
2222
public PublicKeyCredentialRequestOptions $publicKey
2323
) {}
2424
}

src/Services/Webauthn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static function forgetAuthenticate(): void
102102
/**
103103
* Get publicKey data to prepare Webauthn login.
104104
*/
105-
public static function prepareAssertion(User $user): PublicKeyCredentialRequestOptions
105+
public static function prepareAssertion(?User $user): PublicKeyCredentialRequestOptions
106106
{
107107
return tap(app(RequestOptionsFactory::class)($user), function ($publicKey) use ($user) {
108108
WebauthnLoginData::dispatch($user, $publicKey);

src/Services/Webauthn/CredentialAssertionValidator.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(
3030
*
3131
* @throws ResponseMismatchException
3232
*/
33-
public function __invoke(User $user, array $data): bool
33+
public function __invoke(?User $user, array $data): bool
3434
{
3535
// Load the data
3636
$content = json_encode($data, flags: JSON_THROW_ON_ERROR);
@@ -51,11 +51,15 @@ public function __invoke(User $user, array $data): bool
5151
/**
5252
* Get public Key credential.
5353
*/
54-
protected function pullPublicKey(User $user): PublicKeyCredentialRequestOptions
54+
protected function pullPublicKey(?User $user): PublicKeyCredentialRequestOptions
5555
{
5656
try {
5757
$value = $this->cache->pull($this->cacheKey($user));
5858

59+
if ($value === null && in_array(config('webauthn.userless'), ['required', 'preferred'], true)) {
60+
$value = $this->cache->pull($this->cacheKey(null));
61+
}
62+
5963
return $this->loader->deserialize($value, PublicKeyCredentialRequestOptions::class, 'json');
6064
} catch (\Exception $e) {
6165
app('webauthn.log')->debug('Webauthn publickKey deserialize error', ['exception' => $e]);
@@ -79,14 +83,16 @@ protected function getResponse(PublicKeyCredential $publicKeyCredential): Authen
7983
/**
8084
* Get credential source from user and public key.
8185
*/
82-
protected function getCredentialSource(User $user, PublicKeyCredential $publicKeyCredential)
86+
protected function getCredentialSource(?User $user, PublicKeyCredential $publicKeyCredential)
8387
{
8488
$credentialId = $publicKeyCredential->rawId;
8589

86-
return (Webauthn::model())::where('user_id', $user->getAuthIdentifier())
87-
->where(fn ($query) => $query->where('credentialId', Base64UrlSafe::encode($credentialId))
90+
return (Webauthn::model())::where(
91+
fn ($query) => $query->where('credentialId', Base64UrlSafe::encode($credentialId))
8892
->orWhere('credentialId', Base64UrlSafe::encodeUnpadded($credentialId))
89-
)
93+
)->where(
94+
fn ($query) => $user !== null ? $query->where('user_id', $user->getAuthIdentifier()) : $query
95+
)
9096
->firstOrFail()
9197
->publicKeyCredentialSource;
9298
}

src/Services/Webauthn/CredentialValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public function __construct(
2121
/**
2222
* Returns the cache key to remember the challenge for the user.
2323
*/
24-
protected function cacheKey(User $user): string
24+
protected function cacheKey(?User $user): string
2525
{
2626
return implode(
2727
'|',
2828
[
2929
self::CACHE_PUBLICKEY_REQUEST,
30-
get_class($user).':'.$user->getAuthIdentifier(),
30+
$user !== null ? get_class($user).':'.$user->getAuthIdentifier() : '',
3131
hash('sha512', $this->request->host().'|'.$this->request->ip()),
3232
]
3333
);

src/Services/Webauthn/RequestOptionsFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(
3131
/**
3232
* Create a new PublicKeyCredentialCreationOptions object.
3333
*/
34-
public function __invoke(User $user): PublicKeyCredentialRequestOptions
34+
public function __invoke(?User $user): PublicKeyCredentialRequestOptions
3535
{
3636
$publicKey = new PublicKeyCredentialRequestOptions(
3737
$this->getChallenge(),
@@ -63,9 +63,9 @@ private static function getUserVerification(Config $config): ?string
6363
*
6464
* @return array<array-key,PublicKeyCredentialDescriptor>
6565
*/
66-
private function getAllowedCredentials(User $user): array
66+
private function getAllowedCredentials(?User $user): array
6767
{
68-
return CredentialRepository::getRegisteredKeys($user);
68+
return $user !== null ? CredentialRepository::getRegisteredKeys($user) : [];
6969
}
7070

7171
/**

0 commit comments

Comments
 (0)