lumoauth / laravel
LumoAuth for Laravel: service provider, LumoAuth facade and a permission middleware.
Requires
- php: ^8.1
- illuminate/contracts: ^11.0 || ^12.0
- illuminate/http: ^11.0 || ^12.0
- illuminate/support: ^11.0 || ^12.0
- lumoauth/lumoauth: ^1.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The LumoAuth package for Laravel 11 and 12. It adds four things:
- A service provider that registers
LumoAuth\LumoAuthas a singleton. - The
LumoAuthfacade. - The
lumoauth.permissionroute middleware. - The
lumoauth.poppyroute middleware, which verifies the requests that personal agents send to your API.
The package builds on the core SDK, lumoauth/lumoauth. That
package documents the namespaces, credentials, scopes and errors.
Laravel 11 and 12 need PHP 8.2 or later. The package does not support Laravel 10: Composer refuses to install Laravel 10 because of its security advisories.
Install
composer require lumoauth/laravel
php artisan vendor:publish --tag=lumoauth-config # optional; the environment variables work without it
The first command installs the latest version. Packagist lists the versions.
Package discovery registers the service provider and the facade alias.
Configuration
LUMOAUTH_URL=https://app.lumoauth.dev LUMOAUTH_ORG_ID=acme-corp LUMOAUTH_API_KEY=lmk_…
Key in config/lumoauth.php |
Environment variable | Default |
|---|---|---|
url |
LUMOAUTH_URL |
https://app.lumoauth.dev (EU: https://eu.app.lumoauth.dev) |
org_id |
LUMOAUTH_ORG_ID |
none; not needed with custom_domain |
custom_domain |
LUMOAUTH_CUSTOM_DOMAIN |
none; the organization's own domain, such as auth.acme.com. It replaces url. See Use a custom domain. |
api_key |
LUMOAUTH_API_KEY |
none; sent as X-API-Key |
user_subject_attribute |
LUMOAUTH_USER_SUBJECT_ATTRIBUTE |
lumoauth_sub; the user model attribute that holds the LumoAuth user ID |
client_id |
LUMOAUTH_CLIENT_ID |
none; the client ID of a confidential application whose allowed scopes include poppy:verify |
client_secret |
LUMOAUTH_CLIENT_SECRET |
none; the client secret of that application |
poppy.public_origin |
LUMOAUTH_POPPY_PUBLIC_ORIGIN |
none; the address that personal agents call, such as https://shop.example.com. The lumoauth.poppy middleware needs it. |
Use a custom domain
An organization can serve LumoAuth on its own domain, for example
https://auth.acme.com. Set the domain instead of LUMOAUTH_URL and LUMOAUTH_ORG_ID:
LUMOAUTH_CUSTOM_DOMAIN=auth.acme.com LUMOAUTH_API_KEY=lmk_…
With a custom domain:
- The client sends every request to the domain, and
urlis not used. - The organization ID is not needed, and no path has an
/orgs/...segment. - The issuer of OAuth and OpenID Connect is the bare domain,
https://auth.acme.com.$lumo->issuer()returns it. - The
lumoauth.permissionandlumoauth.poppymiddleware call the domain too.
A value with a path, a query, a fragment or credentials throws LumoAuthConfigError
when Laravel creates the client. If you published config/lumoauth.php before this
option existed, the package still reads LUMOAUTH_CUSTOM_DOMAIN.
Use the client
Inject LumoAuth\LumoAuth anywhere that the container resolves classes. The container
also binds it as lumoauth.
use LumoAuth\LumoAuth; final class DocumentController { public function __construct(private readonly LumoAuth $lumo) {} public function update(Request $request, string $id) { abort_unless($this->lumo->permissions->check('document.edit'), 403); // … } }
The SDK exposes its namespaces as properties, and a facade forwards only method calls. To reach a namespace through the facade, get the client first:
use LumoAuth\Laravel\Facades\LumoAuth; LumoAuth::getFacadeRoot()->permissions->check('document.edit');
The configured client uses the organization API key. A permission check with that key is
about the user who created the key. To check a signed-in user, pass the user's LumoAuth
user ID as userId:. The API key needs the authz:check scope for this:
$this->lumo->permissions->check('document.edit', userId: $request->user()->lumoauth_sub);
The LumoAuth user ID is a UUID: the sub claim of the user's LumoAuth tokens. Your app's
own user ID does not work. Store the sub on the user model when the user signs in with
LumoAuth, for example in a lumoauth_sub column. You can also call LumoAuth with the
user's own access token:
$asUser = new \LumoAuth\LumoAuth(orgId: 'acme-corp', tokenProvider: fn () => session('lumoauth_access_token')); $asUser->permissions->check('document.edit');
Route middleware
Route::put('/documents/{id}', UpdateDocument::class) ->middleware('lumoauth.permission:document.edit,document.view');
The middleware requires every listed permission. It checks the signed-in user's
LumoAuth user ID, and it never uses your app's own user ID. It reads that ID from the user
model attribute that user_subject_attribute names, lumoauth_sub by default.
| Situation | Response |
|---|---|
| Nobody is signed in | 401 |
| The user has no LumoAuth user ID | 403 |
LumoAuth does not know the ID (SUBJECT_NOT_FOUND) |
403 |
| A listed permission is missing | 403 |
| Every listed permission is granted | The request continues |
The configured API key needs the authz:check scope. Without it, the SDK throws
LumoAuthPermissionDeniedError, which Laravel reports as a server error. Treat that as a
configuration error.
When the ID lives somewhere else, set a resolver in a service provider's boot() method:
use LumoAuth\Laravel\Http\Middleware\RequirePermission; RequirePermission::resolveSubjectUsing(fn ($user) => $user->identities()->where('provider', 'lumoauth')->value('subject'));
Verify requests from personal agents
A personal agent is an AI assistant that acts for one person, and it sends a
Poppy-Session header to your API, as the Personal Agent Protocol defines. The
lumoauth.poppy middleware asks LumoAuth to verify that header before the route runs. The
route then gets the personal agent, the user, the Session and the account of the request.
The package authenticates these calls as one of your organization's confidential
applications. The application needs the poppy:verify scope among its allowed scopes.
Set its credentials and the public origin of your API:
LUMOAUTH_ORG_ID=acme-corp LUMOAUTH_CLIENT_ID=client_… LUMOAUTH_CLIENT_SECRET=… LUMOAUTH_POPPY_PUBLIC_ORIGIN=https://shop.example.com
If you published config/lumoauth.php before this feature, the package still reads these
variables. Publish the file again to see the new keys.
<?php // routes/api.php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; use LumoAuth\Laravel\Http\Middleware\RequirePoppySession; // A signed-out operation: every verified personal agent may call it. Route::get('/catalog', function (Request $request) { $who = RequirePoppySession::verification($request); return [ 'products' => ['stormline-shell-m', 'trail-socks-3'], 'session' => $who->session->id, // store a cart under this ID 'signed_in' => $who->signedIn, ]; })->middleware('lumoauth.poppy'); // An account operation: the access token must carry the scope. Route::get('/orders', function (Request $request) { $who = RequirePoppySession::verification($request); return ['account' => $who->account->sub, 'agent' => $who->clientId]; })->middleware('lumoauth.poppy:orders:read');
Parameters. Every middleware parameter is a scope that the operation needs. Two
parameters have another meaning: resource=<identifier> and allow_replay. Separate
parameters with commas. RequirePoppySession::using() builds the same string from named
arguments:
Route::post('/orders/{id}/return', ReturnOrder::class) ->middleware('lumoauth.poppy:orders:read,orders:write'); Route::post('/mcp', McpEndpoint::class)->middleware(RequirePoppySession::using( requiredScope: 'orders:write', resource: 'https://mcp.shop.example.com/mcp', // only tokens that were issued for this service allowReplay: true, // accept a Session header that was verified before ));
| Situation | What happens |
|---|---|
| LumoAuth verifies the request | The route runs. RequirePoppySession::verification($request) returns the PoppyVerification. The result is also in the request attribute lumoauth.poppy. |
| LumoAuth rejects the request | The route does not run. The personal agent gets the response that LumoAuth gave: its status, every header and its body. |
| The SDK throws an error | The route does not run, and nothing is relayed. Laravel answers 500. This happens when LumoAuth cannot be reached, and when it rejects your own application with 401, 403, 404 or 429. |
poppy.public_origin is missing or malformed |
The middleware throws LumoAuthConfigError when Laravel creates it, and Laravel answers 500 |
- The public origin comes from configuration. The middleware builds the URL from
poppy.public_originand the path of the request as the client sent it (getRequestUri()). It never uses theHostheader or theX-Forwarded-*headers. The path includes every route prefix. - Only the protocol's headers are sent to LumoAuth:
Poppy-Session,Poppy-Version,Poppy-Extensions,AuthorizationandDPoP. The middleware never reads the request body. - One verification for each request. A Session header is good for one verification.
Pass the
PoppyVerificationon to your own services.
To verify a request yourself, call the core SDK and relay a rejection with
PoppyRefusalResponse:
use LumoAuth\Laravel\Http\PoppyRefusalResponse; use LumoAuth\LumoAuth; Route::post('/orders/{id}/return', function (Request $request, LumoAuth $lumo, string $id) { $who = $lumo->poppy->guard(config('lumoauth.poppy.public_origin'))->verify( $request->getRealMethod(), $request->getRequestUri(), $request->headers->all(), requiredScope: 'orders:write', ); if (!$who->active) { return PoppyRefusalResponse::from($who); } // … });
The core SDK also looks up and ends Sessions: $lumo->poppy->getSessionStatus() and
$lumo->poppy->endSession(). See the
full guide and the LumoAuth
documentation: Session verification.
Upgrading from 1.0.x
The lumoauth.permission middleware now checks the user's LumoAuth user ID. Version
1.0.x sent your app's own user ID (getAuthIdentifier()) as userId:. LumoAuth does not
know that ID. The check then failed with SUBJECT_NOT_FOUND, or it asked about a
different subject.
What changed:
- The middleware reads the LumoAuth user ID from the user model attribute that
user_subject_attributenames. The default islumoauth_sub. SetLUMOAUTH_USER_SUBJECT_ATTRIBUTEto use another attribute. - When that attribute is empty, the middleware answers 403. It never falls back to your app's user ID.
- When the ID lives somewhere else, call
RequirePermission::resolveSubjectUsing(). See Route middleware.
A user passes the middleware only after your app has stored the user's LumoAuth user ID.
To upgrade, add a column, and store the sub claim when the user signs in.
1. Add the column.
<?php // database/migrations/2026_10_26_000000_add_lumoauth_sub_to_users_table.php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::table('users', function (Blueprint $table) { $table->string('lumoauth_sub')->nullable()->unique(); }); } public function down(): void { Schema::table('users', function (Blueprint $table) { $table->dropUnique(['lumoauth_sub']); $table->dropColumn('lumoauth_sub'); }); } };
2. Store the sub claim in the sign-in callback. The example uses the SDK's
authorization code flow with PKCE. It reads sub from the userinfo response. Put your
OAuth client's ID and secret in config/services.php under lumoauth.
// routes/web.php use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Route; use Illuminate\Support\Str; use LumoAuth\LumoAuth; Route::get('/auth/lumoauth', function (LumoAuth $lumo) { $state = Str::random(40); $verifier = Str::random(64); session(['lumoauth_state' => $state, 'lumoauth_verifier' => $verifier]); return redirect()->away($lumo->auth->authorizationUrl( clientId: config('services.lumoauth.client_id'), redirectUri: route('lumoauth.callback'), scope: 'openid profile email', state: $state, codeChallenge: rtrim(strtr(base64_encode(hash('sha256', $verifier, true)), '+/', '-_'), '='), )); }); Route::get('/auth/lumoauth/callback', function (Request $request, LumoAuth $lumo) { abort_unless(hash_equals((string) session()->pull('lumoauth_state'), (string) $request->query('state')), 400); $tokens = $lumo->auth->exchangeCode( code: (string) $request->query('code'), redirectUri: route('lumoauth.callback'), clientId: config('services.lumoauth.client_id'), clientSecret: config('services.lumoauth.client_secret'), codeVerifier: (string) session()->pull('lumoauth_verifier'), ); $claims = $lumo->auth->userinfo($tokens['access_token']); // Find the user by the LumoAuth user ID first. Match an existing account by email // only when LumoAuth has verified that email. $user = User::firstWhere('lumoauth_sub', $claims['sub']); if ($user === null && ($claims['email_verified'] ?? false)) { $user = User::firstWhere('email', $claims['email']); } abort_if($user === null, 403, 'No account for this LumoAuth user.'); $user->forceFill(['lumoauth_sub' => $claims['sub']])->save(); Auth::login($user); session(['lumoauth_access_token' => $tokens['access_token']]); return redirect()->intended('/'); })->name('lumoauth.callback');
Existing users get their LumoAuth user ID on their next sign-in. Until then, the middleware answers 403 for them.
More
- Core SDK guide:
lumoauth/lumoauth - Full guide and troubleshooting: sdk-php README
Licence
MIT