firefly-iii/app/Http/Middleware/AuthenticateTwoFactor.php

78 lines
2.3 KiB
PHP
Raw Normal View History

<?php
/**
* AuthenticateTwoFactor.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
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
2017-02-17 13:15:17 -06:00
use Log;
use Preferences;
use Session;
/**
2017-11-15 05:25:49 -06:00
* Class AuthenticateTwoFactor.
*/
class AuthenticateTwoFactor
{
/**
* Handle an incoming request.
*
2017-11-15 05:25:49 -06:00
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
*
* @return mixed
*/
public function handle(Request $request, Closure $next, $guard = null)
{
// do the usual auth, again:
if (Auth::guard($guard)->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
}
return redirect()->guest('login');
}
2017-11-15 05:25:49 -06:00
if (1 === intval(auth()->user()->blocked)) {
Auth::guard($guard)->logout();
Session::flash('logoutMessage', trans('firefly.block_account_logout'));
return redirect()->guest('login');
}
2016-03-20 10:46:26 -05:00
$is2faEnabled = Preferences::get('twoFactorAuthEnabled', false)->data;
2017-11-15 05:25:49 -06:00
$has2faSecret = null !== Preferences::get('twoFactorAuthSecret');
2017-02-17 13:15:17 -06:00
2017-11-18 09:30:45 -06:00
// grab 2auth information from session.
2017-11-22 13:20:57 -06:00
$is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated');
2017-02-17 13:15:17 -06:00
2016-03-20 10:46:26 -05:00
if ($is2faEnabled && $has2faSecret && !$is2faAuthed) {
2017-02-17 13:15:17 -06:00
Log::debug('Does not seem to be 2 factor authed, redirect.');
2016-12-05 23:52:17 -06:00
return redirect(route('two-factor.index'));
}
return $next($request);
}
}