2016-03-19 10:22:57 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* TwoFactorController.php
|
2016-04-01 09:44:46 -05:00
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
2016-03-19 10:22:57 -05:00
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-03-19 10:22:57 -05:00
|
|
|
*/
|
|
|
|
|
2016-05-20 05:27:31 -05:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2016-03-19 10:22:57 -05:00
|
|
|
namespace FireflyIII\Http\Controllers\Auth;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2016-03-19 10:24:35 -05:00
|
|
|
use FireflyIII\Http\Requests\TokenFormRequest;
|
2016-03-19 10:22:57 -05:00
|
|
|
use Log;
|
|
|
|
use Preferences;
|
|
|
|
use Session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TwoFactorController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers\Auth
|
|
|
|
*/
|
|
|
|
class TwoFactorController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2016-09-16 05:15:58 -05:00
|
|
|
$user = auth()->user();
|
2016-03-19 10:22:57 -05:00
|
|
|
|
|
|
|
// to make sure the validator in the next step gets the secret, we push it in session
|
2016-12-05 23:52:17 -06:00
|
|
|
$secret = Preferences::get('twoFactorAuthSecret', null)->data;
|
2016-04-03 03:34:42 -05:00
|
|
|
$title = strval(trans('firefly.two_factor_title'));
|
2016-03-19 10:22:57 -05:00
|
|
|
|
2016-12-05 23:52:17 -06:00
|
|
|
// make sure the user has two factor configured:
|
|
|
|
$has2FA = Preferences::get('twoFactorAuthEnabled', null)->data;
|
|
|
|
if (is_null($has2FA) || $has2FA === false) {
|
|
|
|
return redirect(route('index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(strval($secret)) === 0) {
|
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.');
|
2016-03-19 10:22:57 -05:00
|
|
|
}
|
|
|
|
Session::flash('two-factor-secret', $secret);
|
|
|
|
|
2016-04-03 03:34:42 -05:00
|
|
|
return view('auth.two-factor', compact('user', 'title'));
|
2016-03-19 10:22:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function lostTwoFactor()
|
|
|
|
{
|
2016-09-16 05:15:58 -05:00
|
|
|
$user = auth()->user();
|
2016-03-19 10:22:57 -05:00
|
|
|
$siteOwner = env('SITE_OWNER', '');
|
2016-04-03 03:34:42 -05:00
|
|
|
$title = strval(trans('firefly.two_factor_forgot_title'));
|
2016-03-19 10:22:57 -05:00
|
|
|
|
|
|
|
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' .
|
2016-03-19 10:24:35 -05:00
|
|
|
' "twoFactorAuthSecret" for user_id ' . $user->id . '. That will take care of it.'
|
2016-03-19 10:22:57 -05:00
|
|
|
);
|
|
|
|
|
2016-04-03 03:34:42 -05:00
|
|
|
return view('auth.lost-two-factor', compact('user', 'siteOwner', 'title'));
|
2016-03-19 10:22:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-19 10:24:35 -05:00
|
|
|
* @param TokenFormRequest $request
|
2016-03-19 10:22:57 -05:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-03-19 10:24:35 -05:00
|
|
|
public function postIndex(TokenFormRequest $request)
|
2016-03-19 10:22:57 -05:00
|
|
|
{
|
|
|
|
Session::put('twofactor-authenticated', true);
|
|
|
|
Session::put('twofactor-authenticated-date', new Carbon);
|
|
|
|
|
|
|
|
return redirect(route('home'));
|
|
|
|
}
|
|
|
|
|
2016-03-29 09:17:06 -05:00
|
|
|
}
|