Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webauthn supports also username #1999

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/Http/Controllers/WebAuthn/WebAuthnLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ class WebAuthnLoginController
*/
public function options(AssertionRequest $request): Responsable
{
return $request->toVerify($request->validate(['user_id' => 'sometimes|int'])['user_id'] ?? null);
$fields = $request->validate([
'user_id' => 'sometimes|int',
'username' => 'sometimes|string',
]);

$username = $fields['username'] ?? null;
$authenticatable = $fields['user_id'] ?? ($username !== null ? ['username' => $username] : null);

return $request->toVerify($authenticatable);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/WebAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,34 @@ public function testWebAuthLoginOptions(): void
]);
}

/**
* Testing the Login options.
*
* @return void
*/
public function testWebAuthLoginOptionsUsername(): void
{
$this->createCredentials();

// Generate a challenge for username = admin
$response = $this->postJson('/api/WebAuthn::login/options', ['username' => 'admin']);
$this->assertOk($response);

$challengeRetrieved = Session::get(config('webauthn.challenge.key'));
$clg = $challengeRetrieved->data->toBase64Url();

$response->assertJson([
'timeout' => 60000,
'challenge' => $clg,
'allowCredentials' => [
0 => [
'id' => '_Xlz-khgFhDdkvOWyy_YqC54ExkYyp1o6HAQiybqLST-9RGBndpgI06TQygIYI7ZL2dayCMYm6J1-bXyl72obA',
'type' => 'public-key',
],
],
]);
}

/**
* Testing the Login interface.
*
Expand Down