diff --git a/README.md b/README.md index 56f1870..aaadcf2 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,8 @@ return [ **/ 'reauthorize_urls' => [ - // 'nova/resources/users/new', - // 'nova/resources/users/*/edit', + // 'resources/users/new', + // 'resources/users/*/edit', ], /* timeout in minutes */ diff --git a/config/nova-two-factor.php b/config/nova-two-factor.php index d221d40..d331392 100644 --- a/config/nova-two-factor.php +++ b/config/nova-two-factor.php @@ -40,8 +40,8 @@ * you are allowed to use wildcards pattern for url matching **/ 'reauthorize_urls' => [ - // 'nova/resources/users/new', - // 'nova/resources/users/*/edit', + // 'resources/users/new', + // 'resources/users/*/edit', ], /* timeout in minutes */ diff --git a/src/Http/Middleware/TwoFa.php b/src/Http/Middleware/TwoFa.php index 1b11647..9a55a77 100644 --- a/src/Http/Middleware/TwoFa.php +++ b/src/Http/Middleware/TwoFa.php @@ -45,6 +45,11 @@ public function handle($request, Closure $next) return $next($request); } + // turn off security if 2fa is off + if(!$this->novaUser()?->twoFa?->google2fa_enable){ + return $next($request); + } + // re prompt for OTP if(NovaTwoFactor::promptEnabled($request)){ return NovaTwoFactor::prompt(); @@ -56,10 +61,7 @@ public function handle($request, Closure $next) return $next($request); } - // turn off security if 2fa is off - if(!$this->novaUser()?->twoFa?->google2fa_enable){ - return $next($request); - } + return response(view('nova-two-factor::sign-in')); } diff --git a/src/NovaTwoFactor.php b/src/NovaTwoFactor.php index 4bb25c6..ab703eb 100644 --- a/src/NovaTwoFactor.php +++ b/src/NovaTwoFactor.php @@ -3,6 +3,7 @@ namespace Visanduma\NovaTwoFactor; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Session; use Laravel\Nova\Menu\MenuSection; use Laravel\Nova\Nova; use Laravel\Nova\Tool; @@ -16,8 +17,8 @@ class NovaTwoFactor extends Tool */ public function boot() { - Nova::script('nova-two-factor', __DIR__ . '/../dist/js/tool.js'); - Nova::style('nova-two-factor', __DIR__ . '/../dist/css/tool.css'); + Nova::script('nova-two-factor', __DIR__.'/../dist/js/tool.js'); + Nova::style('nova-two-factor', __DIR__.'/../dist/css/tool.css'); } /** @@ -34,29 +35,42 @@ public function menu(Request $request) } } - public static function promptEnabled(Request $request) + public static function promptEnabled(Request $request): bool { $timeout = config('nova-two-factor.reauthorize_timeout', 5); - $promptFor = config('nova-two-factor.reauthorize_urls', []); + $promptFor = array_map(fn ($el) => trim(Nova::url($el), '/'), config('nova-two-factor.reauthorize_urls', [])); $hasUrl = $request->is($promptFor); - $lastAttempt = session()->get('2fa.prompt_at', now()->subMinutes($timeout + 1)); + $lastAttempt = self::getLastPromptTime(); + + if ($lastAttempt->diffInMinutes(now()) >= $timeout && $hasUrl) { - if ($lastAttempt->diffInMinutes(now()) > $timeout && $hasUrl) { return true; } return false; } - public static function prompt() { return inertia('NovaTwoFactor.Prompt', [ - 'referer' => request()->url() + 'referer' => request()->url(), ]); } + + public static function setLastPromptTime(): void + { + session()->put('2fa.prompt_at', now()); + } + + public static function getLastPromptTime() + { + $timeout = config('nova-two-factor.reauthorize_timeout', 5); + + return session()->get('2fa.prompt_at', now()->subMinutes($timeout + 5)); + Session::put('2fa.prompt_at', now()); + } }