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

120 lines
4.0 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;
/**
2017-11-15 12:25:49 +01:00
* Class TwoFactorController.
*/
class TwoFactorController extends Controller
{
/**
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)
{
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);
}
2016-03-29 16:17:06 +02:00
}