Can use backup codes to login.

This commit is contained in:
James Cole
2019-08-04 07:10:18 +02:00
parent 0b6c3efe8d
commit e41211bed7
3 changed files with 51 additions and 10 deletions

View File

@@ -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);
}
}