Skip to content

Commit

Permalink
Merge pull request #366 from corazzi/feature/destroy-used-recovery-codes
Browse files Browse the repository at this point in the history
Destroy a recovery code when used – fixes #284
  • Loading branch information
jeffgreco13 authored Jun 9, 2024
2 parents dd43039 + e28b477 commit 5323369
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Pages/TwoFactorPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public function authenticate()
return null;
}

// If using a recovery code, unset it so it can only be used once
if ($this->usingRecoveryCode) {
filament('filament-breezy')->auth()->user()->destroyRecoveryCode($this->code);

Check failure on line 135 in src/Pages/TwoFactorPage.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Foundation\Auth\User::destroyRecoveryCode().

Check failure on line 135 in src/Pages/TwoFactorPage.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Foundation\Auth\User::destroyRecoveryCode().
}

// If it makes it to the bottom, we're going to set the session var and send them to the dashboard.
filament('filament-breezy')->auth()->user()->setTwoFactorSession();

Expand Down
9 changes: 9 additions & 0 deletions src/Traits/TwoFactorAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ public function generateRecoveryCodes()
})->all()));
}

public function destroyRecoveryCode(string $recoveryCode): void
{
$unusedCodes = array_filter($this->two_factor_recovery_codes ?? [], fn ($code) => $code !== $recoveryCode);

$this->breezy_session->forceFill([
'two_factor_recovery_codes' => $unusedCodes ? encrypt(json_encode($unusedCodes)) : null,
])->save();
}

public function getTwoFactorQrCodeUrl()
{
return filament('filament-breezy')->getQrCodeUrl(
Expand Down

0 comments on commit 5323369

Please sign in to comment.