. */ declare(strict_types=1); namespace FireflyIII\Http\Middleware; use Closure; use Illuminate\Contracts\Auth\Factory as Auth; use Log; /** * Class AuthenticateTwoFactor. */ class AuthenticateTwoFactor { /** * The authentication factory instance. * * @var \Illuminate\Contracts\Auth\Factory */ protected $auth; /** * Create a new middleware instance. * * @param \Illuminate\Contracts\Auth\Factory $auth * * @return void */ public function __construct(Auth $auth) { $this->auth = $auth; } /** * @param $request * @param Closure $next * @param array ...$guards * * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|mixed * @throws \Psr\Container\NotFoundExceptionInterface * @throws \Psr\Container\ContainerExceptionInterface */ public function handle($request, Closure $next, ...$guards) { if ($this->auth->guest()) { return response()->redirectTo(route('login')); } $is2faEnabled = app('preferences')->get('twoFactorAuthEnabled', false)->data; $has2faSecret = null !== app('preferences')->get('twoFactorAuthSecret'); $is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated'); if ($is2faEnabled && $has2faSecret && !$is2faAuthed) { Log::debug('Does not seem to be 2 factor authed, redirect.'); return response()->redirectTo(route('two-factor.index')); } return $next($request); } }