Files
firefly-iii/app/Http/Controllers/Auth/TwoFactorController.php

257 lines
7.9 KiB
PHP
Raw Normal View History

<?php
/**
* TwoFactorController.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Auth;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\TokenFormRequest;
2018-07-09 19:24:08 +02:00
use FireflyIII\User;
2017-02-17 20:15:17 +01:00
use Illuminate\Cookie\CookieJar;
2017-02-16 21:01:22 +01:00
use Illuminate\Http\Request;
use Log;
2019-08-04 07:00:47 +02:00
use PragmaRX\Google2FALaravel\Support\Authenticator;
use Preferences;
/**
2017-11-15 12:25:49 +01:00
* Class TwoFactorController.
*/
class TwoFactorController extends Controller
{
2019-08-04 07:00:47 +02:00
/**
* @param Request $request
*/
public function submitMFA(Request $request)
{
/** @var array $mfaHistory */
$mfaHistory = Preferences::get('mfa_history', [])->data;
$mfaCode = $request->get('one_time_password');
// is in history? then refuse to use it.
if ($this->inMFAHistory($mfaCode, $mfaHistory)) {
$this->filterMFAHistory();
session()->flash('error', trans('firefly.wrong_mfa_code'));
return redirect(route('home'));
}
/** @var Authenticator $authenticator */
$authenticator = app(Authenticator::class)->boot($request);
if ($authenticator->isAuthenticated()) {
// save MFA in preferences
$this->addToMFAHistory($mfaCode);
// otp auth success!
return redirect(route('home'));
}
2019-08-04 07:10:18 +02:00
// 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'));
}
2019-08-04 07:00:47 +02:00
session()->flash('error', trans('firefly.wrong_mfa_code'));
return redirect(route('home'));
}
/**
* @param string $mfaCode
*/
private function addToMFAHistory(string $mfaCode): void
{
/** @var array $mfaHistory */
$mfaHistory = Preferences::get('mfa_history', [])->data;
$entry = [
'time' => time(),
'code' => $mfaCode,
];
$mfaHistory[] = $entry;
Preferences::set('mfa_history', $mfaHistory);
$this->filterMFAHistory();
}
/**
* Remove old entries from the preferences array.
*/
private function filterMFAHistory(): void
{
/** @var array $mfaHistory */
$mfaHistory = Preferences::get('mfa_history', [])->data;
$newHistory = [];
$now = time();
foreach ($mfaHistory as $entry) {
$time = $entry['time'];
$code = $entry['code'];
if ($now - $time <= 300) {
$newHistory[] = [
'time' => $time,
'code' => $code,
];
}
}
Preferences::set('mfa_history', $newHistory);
}
/**
2018-07-21 08:06:24 +02:00
* Show 2FA screen.
*
2017-02-16 21:01:22 +01:00
* @param Request $request
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
2017-11-15 12:25:49 +01:00
*
* @throws FireflyException
2018-07-14 11:45:05 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
2017-02-16 21:01:22 +01:00
public function index(Request $request)
{
die('this auth controller must be refactored.');
2016-09-16 12:15:58 +02:00
$user = auth()->user();
// to make sure the validator in the next step gets the secret, we push it in session
$secretPreference = app('preferences')->get('twoFactorAuthSecret', null);
2017-11-15 12:25:49 +01:00
$secret = null === $secretPreference ? null : $secretPreference->data;
2018-04-02 15:10:40 +02:00
$title = (string)trans('firefly.two_factor_title');
2016-12-06 06:52:17 +01:00
// make sure the user has two factor configured:
$has2FA = app('preferences')->get('twoFactorAuthEnabled', false)->data;
2017-11-15 12:25:49 +01:00
if (null === $has2FA || false === $has2FA) {
2016-12-06 06:52:17 +01:00
return redirect(route('index'));
}
2018-07-08 07:59:58 +02:00
if ('' === (string)$secret) {
2016-03-22 17:22:48 +01:00
throw new FireflyException('Your two factor authentication secret is empty, which it cannot be at this point. Please check the log files.');
}
2017-02-16 21:01:22 +01:00
$request->session()->flash('two-factor-secret', $secret);
2016-04-03 10:34:42 +02:00
return view('auth.two-factor', compact('user', 'title'));
}
/**
2018-07-21 08:06:24 +02:00
* What to do if 2FA lost?
*
* @return mixed
*/
public function lostTwoFactor()
{
2018-07-09 19:24:08 +02:00
/** @var User $user */
2017-11-22 21:12:27 +01:00
$user = auth()->user();
$siteOwner = config('firefly.site_owner');
2018-04-02 15:10:40 +02:00
$title = (string)trans('firefly.two_factor_forgot_title');
Log::info(
'To reset the two factor authentication for user #' . $user->id .
' (' . $user->email . '), simply open the "preferences" table and delete the entries with the names "twoFactorAuthEnabled" and' .
' "twoFactorAuthSecret" for user_id ' . $user->id . '. That will take care of it.'
);
2016-04-03 10:34:42 +02:00
return view('auth.lost-two-factor', compact('user', 'siteOwner', 'title'));
}
/**
2018-07-21 08:06:24 +02:00
* Submit 2FA code.
*
* @param TokenFormRequest $request
2017-02-25 05:57:01 +01:00
* @param CookieJar $cookieJar
*
* @return mixed
*/
2017-02-17 20:15:17 +01:00
public function postIndex(TokenFormRequest $request, CookieJar $cookieJar)
{
2017-11-22 20:20:57 +01:00
// wants to remember session?
$remember = $request->session()->get('remember_login') ?? false;
2017-11-22 21:12:27 +01:00
$minutes = config('session.lifetime');
if (true === $remember) {
2017-11-22 20:20:57 +01:00
// set cookie with a long lifetime (30 days)
$minutes = 43200;
}
$cookie = $cookieJar->make(
'twoFactorAuthenticated', 'true', $minutes, config('session.path'), config('session.domain'), config('session.secure'), config('session.http_only')
);
// whatever the case, forget about it:
$request->session()->forget('remember_login');
2017-11-22 20:20:57 +01:00
return redirect(route('home'))->withCookie($cookie);
}
2019-08-04 07:00:47 +02:00
/**
* Each MFA history has a timestamp and a code, saving the MFA entries for 5 minutes. So if the
* submitted MFA code has been submitted in the last 5 minutes, it won't work despite being valid.
*
* @param string $mfaCode
* @param array $mfaHistory
*
* @return bool
*/
private function inMFAHistory(string $mfaCode, array $mfaHistory): bool
{
$now = time();
foreach ($mfaHistory as $entry) {
$time = $entry['time'];
$code = $entry['code'];
if ($code === $mfaCode && $now - $time <= 300) {
return true;
}
}
return false;
}
2019-08-04 07:10:18 +02:00
/**
* 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);
}
2016-03-29 16:17:06 +02:00
}