Skip to content

Commit

Permalink
webauthn supports also username
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Sep 3, 2023
1 parent cc9a6d4 commit 1a81f28
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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

0 comments on commit 1a81f28

Please sign in to comment.