Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt issue #64

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is never called

Copy link
Member Author

@lahirulhr lahirulhr Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Conflict resolution went wrong. line 74 should be removed. will fix soon

}
}