-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Add guard assignment helper middleware (#362)
- Loading branch information
Showing
6 changed files
with
226 additions
and
29 deletions.
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