Skip to content
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
13 changes: 3 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,22 @@ jobs:
fail-fast: true
matrix:
php: [8.5, 8.4, 8.3, 8.2]
laravel: [13.*, 12.*, 11.*]
laravel: [13.*, 12.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 13.*
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
- laravel: 11.*
testbench: 9.*
carbon: 3.*
collision: 8.*
filament: ^5.3.5
filament: ^5.10
exclude:
- laravel: 13.*
php: 8.2
- laravel: 11.*
php: 8.5

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }}

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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
],
"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"
},
"require-dev": {
"larastan/larastan": "^3.0",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^8.0",
"orchestra/testbench": "^9.0|^10.0|^11.0",
"orchestra/testbench": "^10.0|^11.0",
"pestphp/pest": "^3.7|^4.0",
"pestphp/pest-plugin-arch": "^3.0|^4.0",
"pestphp/pest-plugin-laravel": "^3.0|^4.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;
}
}
94 changes: 94 additions & 0 deletions tests/Feature/LoginChallengeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?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 Laravel\Passkeys\Actions\VerifyPasskey;
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 after the passkey is verified through the confirmation endpoint', function () {
$component = loginUpToPasskeyChallenge();
$state = $component->get('userUndertakingMultiFactorAuthentication');

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

$this->mock(VerifyPasskey::class, function ($mock) {
$mock->shouldReceive('__invoke')->once()->andReturn($this->user->passkeys()->first());
});

$this->postJson('/admin/passkeys/confirm', ['credential' => fakeAssertionCredential()])
->assertOk();

expect(Filament::auth()->check())->toBeFalse();

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

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

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);
});
23 changes: 0 additions & 23 deletions tests/Feature/PasskeyLoginControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,6 @@ function createUserWithPasskey(string $email): array
return [$user, $passkey];
}

/**
* Minimal valid WebAuthn assertion credential:
* - id/rawId: base64url/base64 of the same raw bytes
* - clientDataJSON: base64url of JSON with type, challenge, origin
* - authenticatorData: base64 of rpIdHash(32) + flags(1) + counter(4)
* - signature: any base64-encoded bytes
*
* @return array<string, mixed>
*/
function fakeAssertionCredential(): array
{
return [
'id' => 'dGVzdGNyZWRlbnRpYWxpZA',
'rawId' => 'dGVzdGNyZWRlbnRpYWxpZA==',
'type' => 'public-key',
'response' => [
'clientDataJSON' => 'eyJ0eXBlIjoid2ViYXV0aG4uZ2V0IiwiY2hhbGxlbmdlIjoiZEdWemRBIiwib3JpZ2luIjoiaHR0cDpcL1wvbG9jYWxob3N0In0',
'authenticatorData' => 'SZYN5YgOjGh0NBcPZHZgW4/krrmihjLHmVzzuoMdl2MBAAAAAA==',
'signature' => 'ZmFrZS1zaWc=',
],
];
}

it('returns passwordless verification options without allowCredentials', function () {
$response = $this->getJson('/admin/passkeys/login/options');

Expand Down
24 changes: 24 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,27 @@
use AdriaanZon\FilamentPasskeys\Tests\TestCase;

uses(TestCase::class)->in(__DIR__);

/**
* Minimal valid WebAuthn assertion credential, with every binary member
* base64url-encoded without padding as the browser API emits it:
* - id/rawId: the same raw bytes
* - clientDataJSON: JSON with type, challenge, origin
* - authenticatorData: rpIdHash(32) + flags(1) + counter(4)
* - signature: any bytes
*
* @return array<string, mixed>
*/
function fakeAssertionCredential(): array
{
return [
'id' => 'dGVzdGNyZWRlbnRpYWxpZA',
'rawId' => 'dGVzdGNyZWRlbnRpYWxpZA',
'type' => 'public-key',
'response' => [
'clientDataJSON' => 'eyJ0eXBlIjoid2ViYXV0aG4uZ2V0IiwiY2hhbGxlbmdlIjoiZEdWemRBIiwib3JpZ2luIjoiaHR0cDpcL1wvbG9jYWxob3N0In0',
'authenticatorData' => 'SZYN5YgOjGh0NBcPZHZgW4_krrmihjLHmVzzuoMdl2MBAAAAAA',
'signature' => 'ZmFrZS1zaWc',
],
];
}
Loading