mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 10:53:37 +00:00
Can use backup codes to login.
This commit is contained in:
@@ -64,6 +64,17 @@ class TwoFactorController extends Controller
|
||||
// otp auth success!
|
||||
return redirect(route('home'));
|
||||
}
|
||||
|
||||
// could be user has a backup code.
|
||||
if ($this->isBackupCode($mfaCode)) {
|
||||
$this->removeFromBackupCodes($mfaCode);
|
||||
$authenticator->login();
|
||||
|
||||
session()->flash('info', trans('firefly.mfa_backup_code'));
|
||||
|
||||
return redirect(route('home'));
|
||||
}
|
||||
|
||||
session()->flash('error', trans('firefly.wrong_mfa_code'));
|
||||
|
||||
return redirect(route('home'));
|
||||
@@ -213,4 +224,33 @@ class TwoFactorController extends Controller
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if code is in users backup codes.
|
||||
*
|
||||
* @param string $mfaCode
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isBackupCode(string $mfaCode): bool
|
||||
{
|
||||
$list = Preferences::get('mfa_recovery', [])->data;
|
||||
if (in_array($mfaCode, $list, true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the used code from the list of backup codes.
|
||||
*
|
||||
* @param string $mfaCode
|
||||
*/
|
||||
private function removeFromBackupCodes(string $mfaCode): void
|
||||
{
|
||||
$list = Preferences::get('mfa_recovery', [])->data;
|
||||
$newList = array_values(array_diff($list, [$mfaCode]));
|
||||
Preferences::set('mfa_recovery', $newList);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user