Skip to content

Commit

Permalink
Merge pull request #64 from Visanduma/prompt_issue
Browse files Browse the repository at this point in the history
Prompt issue
  • Loading branch information
lahirulhr authored Jun 19, 2024
2 parents 7d6932a + d434949 commit 3c4fec0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ return [
**/
'reauthorize_urls' => [
// 'nova/resources/users/new',
// 'nova/resources/users/*/edit',
// 'resources/users/new',
// 'resources/users/*/edit',
],
/* timeout in minutes */
Expand Down
4 changes: 2 additions & 2 deletions config/nova-two-factor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
10 changes: 6 additions & 4 deletions src/Http/Middleware/TwoFa.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'));
}
Expand Down
30 changes: 22 additions & 8 deletions src/NovaTwoFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
}

/**
Expand All @@ -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());
}
}

0 comments on commit 3c4fec0

Please sign in to comment.