-
Notifications
You must be signed in to change notification settings - Fork 136
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: Add guard assignment helper middleware #362
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f9f51bd
feature: Guard assignment helper middleware
evansims f5dab7d
Update tests
evansims 56c3467
Update README.md
evansims 7713c59
Merge branch 'main' into feat/add-guard-assignment-helper-middleware
evansims 28bc2f8
Update README.md
evansims File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Auth0\Laravel\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use function is_string; | ||
|
||
/** | ||
* This helper middleware assigns a specific guard for use in a routing group. | ||
* Note that this middleware is not required for the Auth0 Laravel SDK to function, | ||
* but can be used to simplify configuration of multiple guards. | ||
*/ | ||
final class Guard | ||
{ | ||
private string $defaultGuard = ''; | ||
|
||
public function __construct() | ||
{ | ||
$guard = config('auth.defaults.guard'); | ||
|
||
if (is_string($guard)) { | ||
$this->defaultGuard = $guard; | ||
} | ||
} | ||
|
||
public function handle( | ||
Request $request, | ||
Closure $next, | ||
?string $guard = null, | ||
): Response { | ||
$guard = trim($guard ?? ''); | ||
|
||
if ('' === $guard) { | ||
auth()->shouldUse($this->defaultGuard); | ||
|
||
return $next($request); | ||
} | ||
|
||
auth()->shouldUse($guard); | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Facades\Route; | ||
|
||
uses()->group('middleware', 'middleware.guard'); | ||
|
||
beforeEach(function (): void { | ||
$this->laravel = app('auth0'); | ||
}); | ||
|
||
it('assigns the guard for route handling', function (): void { | ||
$routeNonexistentGuard = '/' . uniqid(); | ||
$routeMiddlewareAssignedGuard = '/' . uniqid(); | ||
$routeMiddlewareUnassignedGuard = '/' . uniqid(); | ||
$routeUnspecifiedGuard = '/' . uniqid(); | ||
|
||
$defaultGuardClass = 'Illuminate\Auth\SessionGuard'; | ||
$sdkGuardClass = 'Auth0\Laravel\Auth\Guard'; | ||
|
||
config(['auth.defaults.guard' => 'web']); | ||
|
||
Route::get($routeUnspecifiedGuard, function (): string { | ||
return get_class(auth()->guard()); | ||
}); | ||
|
||
Route::middleware('guard:nonexistent')->get($routeNonexistentGuard, function (): string { | ||
return get_class(auth()->guard()); | ||
}); | ||
|
||
Route::middleware('guard:testGuard')->get($routeMiddlewareAssignedGuard, function (): string { | ||
return get_class(auth()->guard()); | ||
}); | ||
|
||
Route::middleware('guard')->get($routeMiddlewareUnassignedGuard, function (): string { | ||
return get_class(auth()->guard()); | ||
}); | ||
|
||
$this->get($routeUnspecifiedGuard) | ||
->assertStatus(Response::HTTP_OK) | ||
->assertSee($defaultGuardClass); | ||
|
||
$this->get($routeNonexistentGuard) | ||
->assertStatus(Response::HTTP_INTERNAL_SERVER_ERROR); | ||
|
||
$this->get($routeMiddlewareAssignedGuard) | ||
->assertStatus(Response::HTTP_OK) | ||
->assertSee($sdkGuardClass); | ||
|
||
$this->get($routeUnspecifiedGuard) | ||
->assertStatus(Response::HTTP_OK) | ||
->assertSee($sdkGuardClass); | ||
|
||
$this->get($routeMiddlewareUnassignedGuard) | ||
->assertStatus(Response::HTTP_OK) | ||
->assertSee($defaultGuardClass); | ||
|
||
$this->get($routeUnspecifiedGuard) | ||
->assertStatus(Response::HTTP_OK) | ||
->assertSee($defaultGuardClass); | ||
}); | ||
|
||
it('assigns the guard for route group handling', function (): void { | ||
$routeMiddlewareUnassignedGuard = '/' . uniqid(); | ||
$routeUnspecifiedGuard = '/' . uniqid(); | ||
|
||
$defaultGuardClass = 'Illuminate\Auth\SessionGuard'; | ||
$sdkGuardClass = 'Auth0\Laravel\Auth\Guard'; | ||
|
||
config(['auth.defaults.guard' => 'web']); | ||
|
||
Route::middleware('guard:testGuard')->group(function () use ($routeUnspecifiedGuard, $routeMiddlewareUnassignedGuard) { | ||
Route::get($routeUnspecifiedGuard, function (): string { | ||
return get_class(auth()->guard()); | ||
}); | ||
|
||
Route::middleware('guard')->get($routeMiddlewareUnassignedGuard, function (): string { | ||
return get_class(auth()->guard()); | ||
}); | ||
}); | ||
|
||
$this->get($routeUnspecifiedGuard) | ||
->assertStatus(Response::HTTP_OK) | ||
->assertSee($sdkGuardClass); | ||
|
||
$this->get($routeMiddlewareUnassignedGuard) | ||
->assertStatus(Response::HTTP_OK) | ||
->assertSee($defaultGuardClass); | ||
|
||
$this->get($routeUnspecifiedGuard) | ||
->assertStatus(Response::HTTP_OK) | ||
->assertSee($sdkGuardClass); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this assertion done above? On L43.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies if this is intentional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Widcket 👋 No worries! It's repeated a few times to ensure the guard changes are properly persisted between requests. Here's the breakdown of what it's doing:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification!