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

feature/SGRD-9 | Support regex realm matching #8

Merged

Conversation

amrography
Copy link
Member

@amrography amrography commented Dec 24, 2024

We can pass the realm names like following:

Route::middleware('auth:jwt(root|foo),web, ... guards')

The Authenticate middleware will first attempt to auth user with JWT guard, use the given realms. If it fails, it will continue to auth all the other guards.

Changes:

for Laravel 10, update app/Http/Kernel.php:

protected $middlewareAliases = [
- 'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
+ 'auth' => \Supaapps\Guard\Http\Middleware\Authenticate::class,

For Laravel 11, update bootstrap/app.php see

+->withMiddleware(function (Middleware $middleware) {
+    $middleware->alias([
+        'auth' => \Supaapps\Guard\Http\Middleware\Authenticate::class,
+    ]);
+})

Also, I can apply a totally different approach. Here in middleware aliases, I can separate the logic from default Authenticate middleware:

protected $middlewareAliases = [
  'auth' => \App\Http\Middleware\Authenticate::class,
  'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
  'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
+ 'auth.jwt' => **MIDDLEWARE THAT SETS REALM NAMES THEN AUTH**
...

And the usage will be like:

Route::middleware('auth.jwt:root,foo') // or auth.jwt:regex pattern

The idea is simple here, let's assume we have .env like:

SUPAAPPS_GUARD_AUTH_REALM_NAME=root,foo,bar

Then it matches JWT aud of all of the following:

- root
- foo
- bar

@amrography amrography self-assigned this Dec 24, 2024
@amrography amrography requested a review from SirNarsh December 24, 2024 08:27
@SirNarsh
Copy link
Member

jwt:.* any realm
jwt:root|tggy allows audience root & tggy
jwt realm from env var

@amrography
Copy link
Member Author

jwt:.* any realm jwt:root|tggy allows audience root & tggy jwt realm from env var

We don't have jwt middleware. We protect routes by adding middleware('auth:jwt')

@SirNarsh SirNarsh merged commit 7ab686b into main Dec 27, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants