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

117 lines
4.0 KiB
PHP
Raw Normal View History

<?php
/**
* TwoFactorController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05: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
* 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;
2017-02-17 13:15:17 -06:00
use Illuminate\Cookie\CookieJar;
2017-02-16 14:01:22 -06:00
use Illuminate\Http\Request;
use Log;
use Preferences;
/**
2017-11-15 05:25:49 -06:00
* Class TwoFactorController.
*/
class TwoFactorController extends Controller
{
/**
2017-02-16 14:01:22 -06:00
* @param Request $request
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
2017-11-15 05:25:49 -06:00
*
* @throws FireflyException
2017-09-16 02:24:48 -05:00
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
2017-02-16 14:01:22 -06:00
public function index(Request $request)
{
2016-09-16 05:15:58 -05:00
$user = auth()->user();
// to make sure the validator in the next step gets the secret, we push it in session
2017-03-25 07:41:17 -05:00
$secretPreference = Preferences::get('twoFactorAuthSecret', null);
2017-11-15 05:25:49 -06:00
$secret = null === $secretPreference ? null : $secretPreference->data;
$title = strval(trans('firefly.two_factor_title'));
2016-12-05 23:52:17 -06:00
// make sure the user has two factor configured:
2017-03-25 07:41:17 -05:00
$has2FA = Preferences::get('twoFactorAuthEnabled', false)->data;
2017-11-15 05:25:49 -06:00
if (null === $has2FA || false === $has2FA) {
2016-12-05 23:52:17 -06:00
return redirect(route('index'));
}
2017-11-15 05:25:49 -06:00
if (0 === strlen(strval($secret))) {
2016-03-22 11:22:48 -05: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 14:01:22 -06:00
$request->session()->flash('two-factor-secret', $secret);
2016-04-03 03:34:42 -05:00
return view('auth.two-factor', compact('user', 'title'));
}
/**
* @return mixed
2017-11-15 05:25:49 -06:00
*
* @throws FireflyException
*/
public function lostTwoFactor()
{
2017-11-22 14:12:27 -06:00
$user = auth()->user();
$siteOwner = env('SITE_OWNER', '');
2017-11-22 14:12:27 -06:00
$title = strval(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 03:34:42 -05:00
return view('auth.lost-two-factor', compact('user', 'siteOwner', 'title'));
}
/**
* @param TokenFormRequest $request
2017-02-24 22:57:01 -06:00
* @param CookieJar $cookieJar
*
* @return mixed
2017-02-24 22:57:01 -06:00
* @SuppressWarnings(PHPMD.UnusedFormalParameter) // it's unused but the class does some validation.
*/
2017-02-17 13:15:17 -06:00
public function postIndex(TokenFormRequest $request, CookieJar $cookieJar)
{
2017-11-22 13:20:57 -06:00
// wants to remember session?
$remember = $request->session()->get('remember_login') ?? false;
2017-11-22 14:12:27 -06:00
$minutes = config('session.lifetime');
if (true === $remember) {
2017-11-22 13:20:57 -06: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 13:20:57 -06:00
return redirect(route('home'))->withCookie($cookie);
}
2016-03-29 09:17:06 -05:00
}