Skip to content

A free open source IT asset/license management system

fix: try oidc guard before Passport (auth:oidc,api)

Rod Christiansen1ba7bb4210a5
diff --git a/app/Auth/OidcGuard.php b/app/Auth/OidcGuard.php
index 8ad770c24a..7570eeb9fc 100644
--- a/app/Auth/OidcGuard.php
+++ b/app/Auth/OidcGuard.php
@@ -15,7 +15,7 @@ use Illuminate\Http\Request;
* null on any failure so the framework produces a normal 401.
*
* Registered as the `oidc` driver in config/auth.php and layered alongside
- * Passport via the `auth:api,oidc` multi-guard, so a route accepts either an
+ * Passport via the `auth:oidc,api` multi-guard, so a route accepts either an
* OIDC token or a Passport token. Inert until config('oidc.enabled') is true.
*/
class OidcGuard implements Guard
diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php
index 82b9419801..a294a17a37 100644
--- a/app/Http/Kernel.php
+++ b/app/Http/Kernel.php
@@ -82,7 +82,11 @@ class Kernel extends HttpKernel
],
'api' => [
- 'auth:api,oidc',
+ // oidc first: Passport's TokenGuard clears the Authorization header
+ // when it fails, so it must run AFTER the OIDC guard (which never
+ // clears it) -- otherwise an OIDC bearer would be wiped before the
+ // oidc guard sees it. A Passport token falls through cleanly.
+ 'auth:oidc,api',
CheckUserIsActivated::class,
EnforceApiTwoFactorEnrollment::class,
EnforceApiUserAgent::class,
diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php
index 595b3d2174..2c660cdb9b 100644
--- a/app/Providers/AuthServiceProvider.php
+++ b/app/Providers/AuthServiceProvider.php
@@ -105,7 +105,7 @@ class AuthServiceProvider extends ServiceProvider
Passport::cookie(config('passport.cookie_name'));
// Federated identity: a provider-agnostic OIDC bearer guard, layered
- // alongside Passport via the `auth:api,oidc` multi-guard. Inert until
+ // alongside Passport via the `auth:oidc,api` multi-guard. Inert until
// config('oidc.enabled') is true, so this is purely additive.
Auth::extend('oidc', function ($app, $name, array $config) {
return new \App\Auth\OidcGuard(
diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php
index 758068b268..5264d1c3da 100644
--- a/app/Providers/RouteServiceProvider.php
+++ b/app/Providers/RouteServiceProvider.php
@@ -65,7 +65,9 @@ class RouteServiceProvider extends ServiceProvider
protected function mapApiRoutes()
{
Route::group([
- 'middleware' => 'auth:api,oidc',
+ // oidc before api: Passport clears the Authorization header on
+ // failure, so it must run after the OIDC guard. See Http/Kernel.
+ 'middleware' => 'auth:oidc,api',
// 'namespace' => $this->namespace, // this might also be a problem? I don't really know :/
'prefix' => 'api',
], function ($router) {
diff --git a/config/auth.php b/config/auth.php
index 8971c8eace..d69b52cf2b 100644
--- a/config/auth.php
+++ b/config/auth.php
@@ -50,7 +50,7 @@ return [
],
// Federated OIDC bearer guard (see config/oidc.php). Layered with
- // Passport via `auth:api,oidc`; inert until OIDC is configured.
+ // Passport via `auth:oidc,api`; inert until OIDC is configured.
'oidc' => [
'driver' => 'oidc',
'provider' => 'users',
diff --git a/tests/Feature/Authentication/OidcApiAuthTest.php b/tests/Feature/Authentication/OidcApiAuthTest.php
index 31dbf0eea1..192c9f87d8 100644
--- a/tests/Feature/Authentication/OidcApiAuthTest.php
+++ b/tests/Feature/Authentication/OidcApiAuthTest.php
@@ -12,7 +12,7 @@ use Tests\TestCase;
/**
* Provider-agnostic OIDC bearer authentication for the API. Exercised through a
- * throwaway route under the real `auth:api,oidc` multi-guard, with JWTs signed
+ * throwaway route under the real `auth:oidc,api` multi-guard, with JWTs signed
* by a local RSA keypair. The validator's JWKS retrieval is stubbed with that
* keypair's public key (the network/discovery path is unchanged in production),
* so these tests never touch a network or a real IdP.
@@ -65,7 +65,7 @@ class OidcApiAuthTest extends TestCase
}
});
- Route::middleware('auth:api,oidc')->get('/_test/oidc-whoami', function () {
+ Route::middleware('auth:oidc,api')->get('/_test/oidc-whoami', function () {
return response()->json(['username' => auth()->user()->username]);
});
}
@@ -179,7 +179,7 @@ class OidcApiAuthTest extends TestCase
public function test_passport_token_still_authenticates_via_multiguard()
{
// The oidc guard must not break the existing Passport path: a route
- // under auth:api,oidc still authenticates a Passport-acting user.
+ // under auth:oidc,api still authenticates a Passport-acting user.
$user = User::factory()->create(['activated' => 1]);
Passport::actingAs($user);