feat(auth): split backup code out of the main 2FA challenge view
Mount now redirects key-only users without a secure context straight to the dedicated backup view. The main challenge form renders only for TOTP users; a subordinate button links to two-factor.challenge.backup instead of embedding the field inline. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>feat/v1-foundation
parent
4e32f76d1b
commit
67ce2de538
|
|
@ -23,6 +23,12 @@ class TwoFactorChallenge extends Component
|
|||
if (! session()->has('2fa.user')) {
|
||||
return $this->redirect(route('login'), navigate: true);
|
||||
}
|
||||
|
||||
// No TOTP and no usable security key (a key-only user over http + bare IP, where WebAuthn
|
||||
// has no secure context) → the backup code is the only path: go straight to its own view.
|
||||
if (! $this->pendingHasTotp && ! $this->webauthnAvailable()) {
|
||||
return $this->redirect(route('two-factor.challenge.backup'), navigate: true);
|
||||
}
|
||||
}
|
||||
|
||||
public function verify()
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
@php
|
||||
$field = 'h-14 w-full rounded-md border border-line bg-inset text-center font-mono text-2xl font-semibold tracking-[0.5em] text-ink caret-accent placeholder:text-ink-4 focus:border-accent/40 focus:outline-none';
|
||||
$fieldText = 'h-12 w-full rounded-md border border-line bg-inset px-3 text-center font-mono text-sm text-ink placeholder:text-ink-4 focus:border-accent/40 focus:outline-none';
|
||||
$label = 'mb-1.5 block font-mono text-[11px] uppercase tracking-wider text-ink-3';
|
||||
$err = 'mt-1.5 flex items-center gap-1.5 font-mono text-[11px] text-offline';
|
||||
@endphp
|
||||
|
|
@ -17,21 +16,15 @@
|
|||
<x-btn variant="primary" size="lg" class="w-full" x-data x-on:click="window.clusevWebauthn?.login($wire)">
|
||||
<x-icon name="shield" class="h-4 w-4" /> {{ __('auth.webauthn_login') }}
|
||||
</x-btn>
|
||||
<div class="flex items-center gap-3">
|
||||
<span class="h-px flex-1 bg-line"></span>
|
||||
<span class="font-mono text-[10px] uppercase tracking-wider text-ink-4">{{ __('common.or') }}</span>
|
||||
<span class="h-px flex-1 bg-line"></span>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
@if ($this->pendingHasTotp)
|
||||
<form wire:submit="verify" class="space-y-4">
|
||||
<div>
|
||||
<label for="code" class="{{ $label }}">{{ $this->pendingHasTotp ? __('auth.code') : __('auth.challenge_backup_label') }}</label>
|
||||
<label for="code" class="{{ $label }}">{{ __('auth.code') }}</label>
|
||||
<input wire:model="code" id="code" inputmode="text" autocomplete="one-time-code" autofocus
|
||||
class="{{ $this->pendingHasTotp ? $field : $fieldText }}"
|
||||
placeholder="{{ $this->pendingHasTotp ? '000000' : __('auth.challenge_backup_placeholder') }}" />
|
||||
class="{{ $field }}" placeholder="000000" />
|
||||
@error('code') <p class="{{ $err }}"><x-icon name="alert" class="h-3.5 w-3.5 shrink-0" />{{ $message }}</p> @enderror
|
||||
<p class="mt-2 text-center font-mono text-[11px] text-ink-4">{{ $this->pendingHasTotp ? __('auth.challenge_recovery_hint') : __('auth.challenge_backup_only_hint') }}</p>
|
||||
</div>
|
||||
|
||||
<x-btn variant="primary" size="lg" type="submit" class="w-full" wire:loading.attr="disabled" wire:target="verify">
|
||||
|
|
@ -41,7 +34,8 @@
|
|||
</x-btn>
|
||||
</form>
|
||||
|
||||
@if ($this->pendingHasTotp && $this->webauthnAvailable())
|
||||
@if ($this->webauthnAvailable())
|
||||
{{-- TOTP + key: the security key is the alternate path. --}}
|
||||
<div class="flex items-center gap-3">
|
||||
<span class="h-px flex-1 bg-line"></span>
|
||||
<span class="font-mono text-[10px] uppercase tracking-wider text-ink-4">{{ __('common.or') }}</span>
|
||||
|
|
@ -51,6 +45,12 @@
|
|||
<x-icon name="shield" class="h-4 w-4" /> {{ __('auth.webauthn_login') }}
|
||||
</x-btn>
|
||||
@endif
|
||||
@endif
|
||||
|
||||
{{-- Backup code lives on its own view — a deliberate, subordinate step. --}}
|
||||
<x-btn variant="secondary" size="lg" class="w-full" :href="route('two-factor.challenge.backup')" wire:navigate>
|
||||
{{ __('auth.challenge_use_backup') }}
|
||||
</x-btn>
|
||||
|
||||
<div class="flex justify-center">
|
||||
<a href="{{ route('login') }}" wire:navigate class="inline-flex items-center gap-1.5 font-mono text-[11px] text-ink-4 transition-colors hover:text-ink-2">
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace Tests\Feature;
|
|||
use App\Livewire\Auth\TwoFactorChallenge;
|
||||
use App\Models\User;
|
||||
use App\Models\WebauthnCredential;
|
||||
use App\Services\WebauthnService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
|
@ -21,23 +22,6 @@ class ChallengeFactorAdaptTest extends TestCase
|
|||
return $u->fresh();
|
||||
}
|
||||
|
||||
public function test_key_only_user_has_no_totp_field_but_a_backup_code_works(): void
|
||||
{
|
||||
$user = $this->keyOnlyUser();
|
||||
$codes = $user->replaceRecoveryCodes();
|
||||
session(['2fa.user' => $user->id, '2fa.remember' => false]);
|
||||
|
||||
Livewire::test(TwoFactorChallenge::class)
|
||||
->assertSet('pendingHasTotp', false)
|
||||
->assertDontSee('000000')
|
||||
->assertSee(__('auth.challenge_backup_label'))
|
||||
->set('code', $codes[0])
|
||||
->call('verify')
|
||||
->assertRedirect(route('dashboard'));
|
||||
|
||||
$this->assertAuthenticatedAs($user);
|
||||
}
|
||||
|
||||
public function test_totp_user_sees_the_field(): void
|
||||
{
|
||||
$user = User::factory()->create(['two_factor_secret' => 'S', 'two_factor_confirmed_at' => now()]);
|
||||
|
|
@ -45,4 +29,42 @@ class ChallengeFactorAdaptTest extends TestCase
|
|||
|
||||
Livewire::test(TwoFactorChallenge::class)->assertSet('pendingHasTotp', true);
|
||||
}
|
||||
|
||||
public function test_totp_user_sees_the_backup_link_but_not_the_backup_field(): void
|
||||
{
|
||||
$user = User::factory()->create(['two_factor_secret' => 'S', 'two_factor_confirmed_at' => now()]);
|
||||
session(['2fa.user' => $user->id, '2fa.remember' => false]);
|
||||
|
||||
Livewire::test(TwoFactorChallenge::class)
|
||||
->assertSee('000000') // TOTP field present
|
||||
->assertSee(__('auth.challenge_use_backup')) // backup link present
|
||||
->assertDontSee(__('auth.challenge_backup_placeholder')); // no backup field here
|
||||
}
|
||||
|
||||
public function test_key_only_user_without_secure_context_is_redirected_to_the_backup_view(): void
|
||||
{
|
||||
// No domain configured → webauthnAvailable() false, pendingHasTotp false: the backup code
|
||||
// is the only usable path, so mount() sends the user straight to the dedicated view.
|
||||
$user = $this->keyOnlyUser();
|
||||
session(['2fa.user' => $user->id, '2fa.remember' => false]);
|
||||
|
||||
Livewire::test(TwoFactorChallenge::class)
|
||||
->assertRedirect(route('two-factor.challenge.backup'));
|
||||
}
|
||||
|
||||
public function test_key_only_user_with_secure_context_stays_and_shows_key_plus_backup(): void
|
||||
{
|
||||
$svc = $this->mock(WebauthnService::class);
|
||||
$svc->shouldReceive('available')->andReturn(true);
|
||||
|
||||
$user = $this->keyOnlyUser();
|
||||
session(['2fa.user' => $user->id, '2fa.remember' => false]);
|
||||
|
||||
Livewire::test(TwoFactorChallenge::class)
|
||||
->assertNoRedirect()
|
||||
->assertSee(__('auth.webauthn_login')) // security-key button
|
||||
->assertSee(__('auth.challenge_use_backup')) // backup link
|
||||
->assertDontSee('000000') // no TOTP field
|
||||
->assertDontSee(__('auth.challenge_backup_placeholder')); // no inline backup field
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue