2016-03-19 10:23:09 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AuthenticateTwoFactor.php
|
2016-04-01 09:44:46 -05:00
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
2016-03-19 10:23:09 -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:23:09 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Middleware;
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Preferences;
|
|
|
|
use Session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AuthenticateTwoFactor
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Middleware
|
|
|
|
*/
|
|
|
|
class AuthenticateTwoFactor
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2016-05-20 10:53:03 -05:00
|
|
|
return redirect()->guest('login');
|
|
|
|
}
|
2016-03-19 10:23:09 -05:00
|
|
|
|
2016-09-16 05:15:58 -05:00
|
|
|
if (intval(auth()->user()->blocked) === 1) {
|
2016-05-20 10:53:03 -05:00
|
|
|
Auth::guard($guard)->logout();
|
|
|
|
Session::flash('logoutMessage', trans('firefly.block_account_logout'));
|
|
|
|
|
|
|
|
return redirect()->guest('login');
|
2016-03-19 10:23:09 -05:00
|
|
|
}
|
2016-03-20 10:46:26 -05:00
|
|
|
$is2faEnabled = Preferences::get('twoFactorAuthEnabled', false)->data;
|
|
|
|
$has2faSecret = !is_null(Preferences::get('twoFactorAuthSecret'));
|
|
|
|
$is2faAuthed = Session::get('twofactor-authenticated');
|
|
|
|
if ($is2faEnabled && $has2faSecret && !$is2faAuthed) {
|
2016-12-05 23:52:17 -06:00
|
|
|
return redirect(route('two-factor.index'));
|
2016-03-19 10:23:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|