Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ jobs:
testbench: 11.*
carbon: 3.*
collision: 8.*
filament: ^5.4.4
filament: ^5.10
- laravel: 12.*
testbench: 10.*
carbon: 3.*
collision: 8.*
filament: ^5.3.5
filament: ^5.10
- laravel: 11.*
testbench: 9.*
carbon: 3.*
collision: 8.*
filament: ^5.3.5
filament: ^5.10
exclude:
- laravel: 13.*
php: 8.2
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ A Filament v5 panel plugin for passkey/WebAuthn authentication. Use passkeys as

## Installation

Requires Filament 5.10 or later.

1. Install the package via Composer:
```bash
composer require adriaanzon/filament-passkeys
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],
"require": {
"php": "^8.2",
"filament/filament": "^5.3.5",
"filament/filament": "^5.10",
"laravel/passkeys": "^0.2.1",
"spatie/laravel-package-tools": "^1.15.0"
},
Expand Down
21 changes: 19 additions & 2 deletions src/Http/Controllers/PasskeyConfirmationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AdriaanZon\FilamentPasskeys\Http\Controllers;

use Filament\Facades\Filament;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\JsonResponse;
Expand Down Expand Up @@ -73,25 +74,41 @@ protected function resolveUserFromSession(Request $request): ?Authenticatable
return $this->findUserByEncryptedId($request->session()->get('passkey.mfa_user'));
}

/**
* Resolves the user from the login page's encrypted MFA state, which holds
* the auth identifier and a key scoped to the panel's guard and user model.
*/
protected function findUserByEncryptedId(mixed $encrypted): ?Authenticatable
{
if (! is_string($encrypted) || blank($encrypted)) {
return null;
}

try {
$userId = decrypt($encrypted);
$data = decrypt($encrypted);
} catch (DecryptException) {
return null;
}

if (! is_array($data) || ! is_string($data['userKey'] ?? null)) {
return null;
}

$userId = $data['identifier'] ?? null;

if (! is_string($userId) && ! is_int($userId)) {
return null;
}

/** @var class-string<Authenticatable> $userModel */
$userModel = Passkeys::userModel();

return $userModel::find($userId);
$user = $userModel::find($userId);

if (! $user || ! hash_equals($data['userKey'], Filament::getUserScopedAuthIdentifier($user))) {
return null;
}

return $user;
}
}
69 changes: 69 additions & 0 deletions tests/Feature/LoginChallengeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

use AdriaanZon\FilamentPasskeys\Tests\Fixtures\User;
use Filament\Auth\Pages\Login;
use Filament\Facades\Filament;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Livewire\Features\SupportTesting\Testable;
use Livewire\Livewire;

beforeEach(function () {
config(['auth.providers.users.model' => User::class]);
Auth::forgetGuards();

Filament::setCurrentPanel('admin');

$this->user = User::create([
'name' => 'Test',
'email' => 'test@example.com',
'password' => Hash::make('password'),
]);

$this->user->passkeys()->create([
'name' => 'Test Key',
'credential_id' => 'dGVzdC1jcmVkZW50aWFsLWlk',
'credential' => ['id' => 'test'],
]);
});

function loginUpToPasskeyChallenge(): Testable
{
return Livewire::test(Login::class)
->fillForm(['email' => 'test@example.com', 'password' => 'password'])
->call('authenticate')
->assertHasNoFormErrors()
->assertSet('userUndertakingMultiFactorAuthentication', fn (mixed $state): bool => filled($state));
}

it('accepts the login page MFA state on the confirmation endpoint', function () {
$state = loginUpToPasskeyChallenge()->get('userUndertakingMultiFactorAuthentication');

$this->getJson('/admin/passkeys/confirm/options?user=' . urlencode($state))
->assertOk()
->assertJsonStructure(['options']);
});

it('signs the user in once the passkey has been verified', function () {
$component = loginUpToPasskeyChallenge();

session()->put('filament-passkeys.mfa-verified.' . $this->user->getKey(), true);

$component
->set('data.multiFactor.passkey.credential', 'verified')
->call('authenticate')
->assertHasNoFormErrors();

expect(Filament::auth()->id())->toBe($this->user->getKey());
});

it('keeps the user signed out until the passkey has been verified', function () {
loginUpToPasskeyChallenge()
->set('data.multiFactor.passkey.credential', 'verified')
->call('authenticate')
->assertHasFormErrors(['multiFactor.passkey.credential']);

expect(Filament::auth()->check())->toBeFalse();
});
55 changes: 48 additions & 7 deletions tests/Feature/PasskeyConfirmationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,37 @@
declare(strict_types=1);

use AdriaanZon\FilamentPasskeys\Tests\Fixtures\User;
use Filament\Facades\Filament;
use Illuminate\Testing\TestResponse;

it('returns confirmation options for an MFA-pending user', function () {
$user = User::create([
beforeEach(function () {
$this->user = User::create([
'name' => 'Test',
'email' => 'test@example.com',
'password' => 'password',
]);
});

$encryptedUserId = encrypt($user->getKey());
function confirmationOptions(string $token): TestResponse
{
return test()->getJson('/admin/passkeys/confirm/options?user=' . urlencode($token));
}

$response = $this->getJson('/admin/passkeys/confirm/options?user=' . urlencode($encryptedUserId));
/**
* Mirrors the state Filament's login page stores for the user undertaking MFA.
*/
function loginPageMfaState(User $user, ?string $userKey = null): string
{
return encrypt([
'identifier' => $user->getAuthIdentifier(),
'userKey' => $userKey ?? Filament::getUserScopedAuthIdentifier($user),
]);
}

$response->assertOk();
$response->assertJsonStructure(['options']);
it('returns confirmation options for an MFA-pending user', function () {
confirmationOptions(loginPageMfaState($this->user))
->assertOk()
->assertJsonStructure(['options']);
});

it('rejects confirmation options request without user parameter', function () {
Expand All @@ -25,6 +42,30 @@
});

it('rejects confirmation options request with invalid encrypted user', function () {
$this->getJson('/admin/passkeys/confirm/options?user=invalid-garbage')
confirmationOptions('invalid-garbage')
->assertStatus(403);
});

it('rejects a bare encrypted user id', function () {
confirmationOptions(encrypt($this->user->getKey()))
->assertStatus(403);
});

it('rejects a user key scoped to another principal', function () {
confirmationOptions(loginPageMfaState($this->user, hash('sha256', 'other')))
->assertStatus(403);
});

it('rejects a state without a user key', function () {
confirmationOptions(encrypt(['identifier' => $this->user->getKey()]))
->assertStatus(403);
});

it('rejects a state for an unknown user', function () {
$state = loginPageMfaState($this->user);

$this->user->delete();

confirmationOptions($state)
->assertStatus(403);
});
Loading