mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-20 11:19:16 +00:00
Better 2fa handling
This commit is contained in:
@@ -19,7 +19,6 @@ use FireflyIII\Repositories\User\UserRepositoryInterface;
|
|||||||
use Illuminate\Mail\Message;
|
use Illuminate\Mail\Message;
|
||||||
use Log;
|
use Log;
|
||||||
use Mail;
|
use Mail;
|
||||||
use Session;
|
|
||||||
use Swift_TransportException;
|
use Swift_TransportException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -54,20 +53,6 @@ class UserEventHandler
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle user logout events.
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function logoutUser(): bool
|
|
||||||
{
|
|
||||||
// dump stuff from the session:
|
|
||||||
Session::forget('twoFactorAuthenticated');
|
|
||||||
Session::forget('twoFactorAuthenticatedDate');
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param RequestedNewPassword $event
|
* @param RequestedNewPassword $event
|
||||||
*
|
*
|
||||||
|
@@ -16,6 +16,7 @@ use Config;
|
|||||||
use FireflyConfig;
|
use FireflyConfig;
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
|
use Illuminate\Cookie\CookieJar;
|
||||||
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Lang;
|
use Lang;
|
||||||
@@ -75,22 +76,25 @@ class LoginController extends Controller
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
|
* @param CookieJar $cookieJar
|
||||||
*
|
*
|
||||||
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function logout(Request $request)
|
public function logout(Request $request, CookieJar $cookieJar)
|
||||||
{
|
{
|
||||||
if (intval(getenv('SANDSTORM')) === 1) {
|
if (intval(getenv('SANDSTORM')) === 1) {
|
||||||
return view('error')->with('message', strval(trans('firefly.sandstorm_not_available')));
|
return view('error')->with('message', strval(trans('firefly.sandstorm_not_available')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$cookie = $cookieJar->forever('twoFactorAuthenticated', 'false');
|
||||||
|
|
||||||
$this->guard()->logout();
|
$this->guard()->logout();
|
||||||
|
|
||||||
$request->session()->flush();
|
$request->session()->flush();
|
||||||
|
|
||||||
$request->session()->regenerate();
|
$request->session()->regenerate();
|
||||||
|
|
||||||
return redirect('/');
|
return redirect('/')->withCookie($cookie);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -13,14 +13,13 @@ declare(strict_types = 1);
|
|||||||
|
|
||||||
namespace FireflyIII\Http\Controllers\Auth;
|
namespace FireflyIII\Http\Controllers\Auth;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Http\Requests\TokenFormRequest;
|
use FireflyIII\Http\Requests\TokenFormRequest;
|
||||||
|
use Illuminate\Cookie\CookieJar;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Log;
|
use Log;
|
||||||
use Preferences;
|
use Preferences;
|
||||||
use Session;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class TwoFactorController
|
* Class TwoFactorController
|
||||||
@@ -84,12 +83,12 @@ class TwoFactorController extends Controller
|
|||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function postIndex(TokenFormRequest $request)
|
public function postIndex(TokenFormRequest $request, CookieJar $cookieJar)
|
||||||
{
|
{
|
||||||
Session::put('twoFactorAuthenticated', true);
|
// set cookie!
|
||||||
Session::put('twoFactorAuthenticatedDate', new Carbon);
|
$cookie = $cookieJar->forever('twoFactorAuthenticated', 'true');
|
||||||
|
|
||||||
return redirect(route('home'));
|
return redirect(route('home'))->withCookie($cookie);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -14,8 +14,10 @@ declare(strict_types = 1);
|
|||||||
namespace FireflyIII\Http\Middleware;
|
namespace FireflyIII\Http\Middleware;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
|
use Cookie;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Log;
|
||||||
use Preferences;
|
use Preferences;
|
||||||
use Session;
|
use Session;
|
||||||
|
|
||||||
@@ -55,8 +57,13 @@ class AuthenticateTwoFactor
|
|||||||
}
|
}
|
||||||
$is2faEnabled = Preferences::get('twoFactorAuthEnabled', false)->data;
|
$is2faEnabled = Preferences::get('twoFactorAuthEnabled', false)->data;
|
||||||
$has2faSecret = !is_null(Preferences::get('twoFactorAuthSecret'));
|
$has2faSecret = !is_null(Preferences::get('twoFactorAuthSecret'));
|
||||||
$is2faAuthed = Session::get('twofactor-authenticated');
|
|
||||||
|
// grab 2auth information from cookie, not from session.
|
||||||
|
$is2faAuthed = Cookie::get('twoFactorAuthenticated') === 'true';
|
||||||
|
|
||||||
if ($is2faEnabled && $has2faSecret && !$is2faAuthed) {
|
if ($is2faEnabled && $has2faSecret && !$is2faAuthed) {
|
||||||
|
Log::debug('Does not seem to be 2 factor authed, redirect.');
|
||||||
|
|
||||||
return redirect(route('two-factor.index'));
|
return redirect(route('two-factor.index'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -17,7 +17,8 @@ use Closure;
|
|||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Preferences;
|
use Preferences;
|
||||||
use Session;
|
use Session;
|
||||||
|
use Log;
|
||||||
|
use Cookie;
|
||||||
/**
|
/**
|
||||||
* Class RedirectIfTwoFactorAuthenticated
|
* Class RedirectIfTwoFactorAuthenticated
|
||||||
*
|
*
|
||||||
@@ -40,7 +41,10 @@ class RedirectIfTwoFactorAuthenticated
|
|||||||
|
|
||||||
$is2faEnabled = Preferences::get('twoFactorAuthEnabled', false)->data;
|
$is2faEnabled = Preferences::get('twoFactorAuthEnabled', false)->data;
|
||||||
$has2faSecret = !is_null(Preferences::get('twoFactorAuthSecret'));
|
$has2faSecret = !is_null(Preferences::get('twoFactorAuthSecret'));
|
||||||
$is2faAuthed = Session::get('twoFactorAuthenticated');
|
|
||||||
|
// grab 2auth information from cookie
|
||||||
|
$is2faAuthed = Cookie::get('twoFactorAuthenticated') === 'true';
|
||||||
|
|
||||||
if ($is2faEnabled && $has2faSecret && $is2faAuthed) {
|
if ($is2faEnabled && $has2faSecret && $is2faAuthed) {
|
||||||
return redirect('/');
|
return redirect('/');
|
||||||
}
|
}
|
||||||
|
@@ -56,12 +56,6 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
'FireflyIII\Handlers\Events\UpdatedJournalEventHandler@scanBills',
|
'FireflyIII\Handlers\Events\UpdatedJournalEventHandler@scanBills',
|
||||||
'FireflyIII\Handlers\Events\UpdatedJournalEventHandler@processRules',
|
'FireflyIII\Handlers\Events\UpdatedJournalEventHandler@processRules',
|
||||||
],
|
],
|
||||||
|
|
||||||
// LARAVEL EVENTS:
|
|
||||||
'Illuminate\Auth\Events\Logout' =>
|
|
||||||
[
|
|
||||||
'FireflyIII\Handlers\Events\UserEventHandler@logoutUser',
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user