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

Add forced() method and middleware #28

Merged
merged 15 commits into from
Sep 15, 2024

Conversation

CodeWithDennis
Copy link
Contributor

This PR is a draft. I'm still trying to figure out how to redirect to the 2FA page.

@Baspa
Copy link
Member

Baspa commented Sep 9, 2024

I believe I already built this in a project of us previous of building this package. I can take a look at it if you want @CodeWithDennis

@CodeWithDennis
Copy link
Contributor Author

I believe I already built this in a project of us previous of building this package. I can take a look at it if you want @CodeWithDennis

Yeah, go for it! Just a heads-up, there are a few PHPSTAN issues. =(

@Baspa
Copy link
Member

Baspa commented Sep 9, 2024

In the project I have this code:

class RedirectWhen2faNotEnabled
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (!$request->user()?->hasTwoFactorEnabled() && !$request->routeIs('filament.admin.pages.two-factor')) {
            return redirect()->route('filament.admin.pages.two-factor');
        }

        return $next($request);
    }
}

And in my bootstrap/app.php:

    ->withMiddleware(function (Middleware $middleware) {
        $middleware->group('panel:admin', [
            \Illuminate\Session\Middleware\StartSession::class,
            RedirectWhen2faNotEnabled::class
        ]);
    })

I needed the StartSession middleware to be able to have the user in the request I guess.

Will take a further look tonight. @CodeWithDennis

@Baspa Baspa added the enhancement New feature or request label Sep 9, 2024
@CodeWithDennis
Copy link
Contributor Author

You should be able to edit this PR, so feel free to make any changes! Unless you prefer to rewrite it. =)

@Baspa
Copy link
Member

Baspa commented Sep 9, 2024

Already fixed redirecting in my lunch break :) @CodeWithDennis

@CodeWithDennis
Copy link
Contributor Author

Already fixed redirecting in my lunch break :) @CodeWithDennis

Dedication 🔥

@CodeWithDennis
Copy link
Contributor Author

@Baspa Does PHPStan command work for you in my branch? Seems to crash on me.

@Baspa
Copy link
Member

Baspa commented Sep 9, 2024

@Baspa Does PHPStan command work for you in my branch? Seems to crash on me.

Screenshot 2024-09-09 at 13 04 12

Seem to work for me but it was very slow, I also gave it full memory so that might be the reason why it worked for me..

@CodeWithDennis
Copy link
Contributor Author

Already fixed redirecting in my lunch break :) @CodeWithDennis

One issue with this when using multi-tenancy:

Missing required parameter for [Route: filament.customer.pages.two-factor] [URI: customer/{tenant}/two-factor] [Missing parameter: tenant].

@Baspa
Copy link
Member

Baspa commented Sep 9, 2024

Already fixed redirecting in my lunch break :) @CodeWithDennis

One issue with this when using multi-tenancy:

Missing required parameter for [Route: filament.customer.pages.two-factor] [URI: customer/{tenant}/two-factor] [Missing parameter: tenant].

Can you try again maybe? :)

@CodeWithDennis
Copy link
Contributor Author

@Baspa Does PHPStan command work for you in my branch? Seems to crash on me.

Screenshot 2024-09-09 at 13 04 12

Seem to work for me but it was very slow, I also gave it full memory so that might be the reason why it worked for me..

Scherm­afbeelding 2024-09-09 om 13 08 08

Must be something on my side.

@CodeWithDennis
Copy link
Contributor Author

CodeWithDennis commented Sep 9, 2024

Can you try again maybe? :)

Same problem, i think this is where i got stuck because filament()->getTenant() is not available in the middleware.

Shorter:

$tenant = filament()->getTenant(); // Still same issue

TwoFactor::getUrl($tenant);

But we also need to check if the panel is multi tenancy.

@Baspa
Copy link
Member

Baspa commented Sep 9, 2024

Can you try again maybe? :)

Same problem, i think this is where i got stuck because filament()->getTenant() is not available in the middleware.

Shorter:

$tenant = TwoFactor::getUrl(); // Still same issue

TwoFactor::getUrl($tenant);

But we also need to check if the panel is multi tenancy.

Hmmm good one.. I have to think about that

@Baspa
Copy link
Member

Baspa commented Sep 9, 2024

Is it not available in any middleware at all? Or just after a specific middleware? Then we could maybe load that middleware before our middleware? @CodeWithDennis

@CodeWithDennis
Copy link
Contributor Author

Is it not available in any middleware at all? Or just after a specific middleware? Then we could maybe load that middleware before our middleware? @CodeWithDennis

I mean it returns null.

@CodeWithDennis
Copy link
Contributor Author

CodeWithDennis commented Sep 9, 2024

Won't this be the same problem where we can't access the tenant parameter? I think this PR kinda done if we can figure out how we can access it. (Besides the PHPStan issues) @Baspa

Edit: Perhaps session parameter?

@CodeWithDennis
Copy link
Contributor Author

@Baspa I haven't had time to check it again yet. Did you come up with any ideas that might solve the problem?

@Baspa
Copy link
Member

Baspa commented Sep 10, 2024

@CodeWithDennis unfortunately I haven't been able to take a look at this as well.. hopefully later this week

@Baspa
Copy link
Member

Baspa commented Sep 11, 2024

It should be fixed right now, can you test it to be sure? @CodeWithDennis

@Baspa Baspa marked this pull request as ready for review September 11, 2024 11:23
@CodeWithDennis
Copy link
Contributor Author

It should be fixed right now, can you test it to be sure? @CodeWithDennis

Yes it works! Couple of questions:

  • Will it also work without tenancy?
  • Should we display a message that its forced.

@Baspa
Copy link
Member

Baspa commented Sep 11, 2024

It should be fixed right now, can you test it to be sure? @CodeWithDennis

Yes it works! Couple of questions:

  • Will it also work without tenancy?
  • Should we display a message that its forced.

Hmm, when you're not using tenancy the tenantMiddleware is not triggered. We should add another middleware when not using tenancy I guess. Displaying a message would be a idea. @CodeWithDennis

@CodeWithDennis
Copy link
Contributor Author

It should be fixed right now, can you test it to be sure? @CodeWithDennis

Yes it works! Couple of questions:

  • Will it also work without tenancy?
  • Should we display a message that its forced.

Hmm, when you're not using tenancy the tenantMiddleware is not triggered. We should add another middleware when not using tenancy I guess. Displaying a message would be a idea. @CodeWithDennis

I can imagine that if users are constantly being redirected to this page, it could be confusing, and they might not understand what's happening. It would be good to inform them that the app requires 2FA before they can use it.

I don't have time right now, but if I get a chance and you haven't added it yet, I'll take a look.

@Baspa
Copy link
Member

Baspa commented Sep 11, 2024

I can make a difference in which middleware to use now by also adding withTenancy(). Don't know if that's the best solution but it's quite clean I guess. Didn't have time to checkout the message yet. @CodeWithDennis

@CodeWithDennis
Copy link
Contributor Author

I can make a difference in which middleware to use now by also adding withTenancy(). Don't know if that's the best solution but it's quite clean I guess. Didn't have time to checkout the message yet. @CodeWithDennis

I think you can check if the Panel uses Tenancy and grab that.

@Baspa
Copy link
Member

Baspa commented Sep 12, 2024

I can make a difference in which middleware to use now by also adding withTenancy(). Don't know if that's the best solution but it's quite clean I guess. Didn't have time to checkout the message yet. @CodeWithDennis

I think you can check if the Panel uses Tenancy and grab that.

Unfortunately $panel->hasTenancy() returns false in the TwoFactorAuthPlugin class..

@@ -22,10 +22,10 @@ public function handle(Request $request, Closure $next): mixed
if ($currentPanel) {
return redirect()->to(route('filament.' . $currentPanel->getId() . '.pages.two-factor', [
'tenant' => Filament::getTenant(),
]));
]))->with('two_factor_redirect_message', __('Your administrator requires you to enable two-factor authentication.'));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we make this customizable?
Example:

->forced(message: __('Some message')

Copy link
Member

Choose a reason for hiding this comment

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

Yes maybe it's better to let the developer choose which message to show to the user. I'm also not really satisfied with showing the message yet. Weird enough I couldn't show the message in the blade view itself using the session helper..

@Baspa Baspa self-assigned this Sep 15, 2024
@Baspa Baspa merged commit fba68e3 into vormkracht10:main Sep 15, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants