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

82 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;
2018-02-09 12:12:46 -06:00
use Illuminate\Contracts\Auth\Factory as Auth;
2017-02-17 13:15:17 -06:00
use Log;
2018-02-09 12:12:46 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class AuthenticateTwoFactor.
*/
class AuthenticateTwoFactor
{
/**
2018-02-09 12:12:46 -06:00
* The authentication factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
/**
* Create a new middleware instance.
*
2018-02-09 12:12:46 -06:00
* @param \Illuminate\Contracts\Auth\Factory $auth
*
2018-02-09 12:12:46 -06:00
* @return void
*/
2018-02-09 12:12:46 -06:00
public function __construct(Auth $auth)
{
2018-02-09 12:12:46 -06:00
$this->auth = $auth;
}
2018-01-25 13:38:50 -06:00
2018-02-09 12:12:46 -06:00
/**
* @param $request
* @param Closure $next
* @param array ...$guards
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|mixed
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Psr\Container\ContainerExceptionInterface
2018-02-09 12:12:46 -06:00
*/
public function handle($request, Closure $next, ...$guards)
{
if ($this->auth->guest()) {
return response()->redirectTo(route('login'));
2018-01-25 13:38:50 -06:00
}
2018-02-09 12:12:46 -06:00
$is2faEnabled = app('preferences')->get('twoFactorAuthEnabled', false)->data;
$has2faSecret = null !== app('preferences')->get('twoFactorAuthSecret');
2018-01-25 11:41:27 -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.');
return response()->redirectTo(route('two-factor.index'));
}
return $next($request);
}
2018-02-09 12:12:46 -06:00
}