lumoauth / symfony-bundle
LumoAuth for Symfony: configuration, an autowired LumoAuth client, and an access-token handler for the security component.
Package info
github.com/LumoAuth/symfony-bundle
Type:symfony-bundle
pkg:composer/lumoauth/symfony-bundle
Requires
- php: ^8.1
- lumoauth/lumoauth: ^1.0
- symfony/config: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/http-kernel: ^6.4 || ^7.0
- symfony/security-http: ^6.4 || ^7.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The LumoAuth bundle for Symfony 6.4 and 7. It does three things:
- It registers the
LumoAuth\LumoAuthclient as a service that you can autowire. - It provides
LumoAuthAccessTokenHandler, which lets the Symfony security component accept bearer tokens from LumoAuth on your API. - It provides the
#[RequirePoppySession]attribute, which verifies the requests that personal agents send to your API.
The bundle builds on the core SDK, lumoauth/lumoauth. That
package documents the namespaces, credentials, scopes and errors.
Install
composer require lumoauth/symfony-bundle
The command installs the latest version. Packagist lists the versions.
The package type is symfony-bundle, so Symfony Flex adds it to config/bundles.php.
Without Flex, add it yourself:
// config/bundles.php return [ // … LumoAuth\Symfony\LumoAuthBundle::class => ['all' => true], ];
Configuration
The bundle reads the environment by default, so .env is often enough:
LUMOAUTH_URL=https://app.lumoauth.dev LUMOAUTH_ORG_ID=acme-corp LUMOAUTH_API_KEY=lmk_…
To set the values in YAML, create config/packages/lumoauth.yaml:
lumoauth: url: '%env(LUMOAUTH_URL)%' org_id: '%env(LUMOAUTH_ORG_ID)%' api_key: '%env(LUMOAUTH_API_KEY)%'
| Key | Default | Meaning |
|---|---|---|
url |
LUMOAUTH_URL, else https://app.lumoauth.dev |
LumoAuth URL for your region (EU: https://eu.app.lumoauth.dev) |
org_id |
LUMOAUTH_ORG_ID |
Organization slug. Not needed with custom_domain. |
custom_domain |
LUMOAUTH_CUSTOM_DOMAIN |
The organization's own domain, such as auth.acme.com. It replaces url. See Use a custom domain. |
api_key |
LUMOAUTH_API_KEY |
Organization API key, sent as X-API-Key |
http_client |
null |
Service ID of a PSR-18 client. When it is null, the SDK finds one. |
user_identifier_claim |
sub |
The userinfo claim that becomes the user identifier in Symfony |
client_id |
LUMOAUTH_CLIENT_ID |
Client ID of a confidential application whose allowed scopes include poppy:verify |
client_secret |
LUMOAUTH_CLIENT_SECRET |
Client secret of that application |
poppy.public_origin |
LUMOAUTH_POPPY_PUBLIC_ORIGIN |
The address that personal agents call, such as https://shop.example.com. #[RequirePoppySession] needs it. |
Use a custom domain
An organization can serve LumoAuth on its own domain, for example
https://auth.acme.com. Set custom_domain instead of url and org_id:
# config/packages/lumoauth.yaml lumoauth: custom_domain: 'auth.acme.com' api_key: '%env(LUMOAUTH_API_KEY)%'
The bundle also reads LUMOAUTH_CUSTOM_DOMAIN when the key is not set. 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. LumoAuthAccessTokenHandlerand#[RequirePoppySession]call the domain too.
A value with a path, a query, a fragment or credentials throws LumoAuthConfigError
when Symfony creates the client.
Use the client
Autowire LumoAuth\LumoAuth. The service is also available under the ID lumoauth.
use LumoAuth\LumoAuth; final class DocumentController { public function __construct(private readonly LumoAuth $lumo) {} public function edit(): Response { if (!$this->lumo->permissions->check('document.edit')) { throw new AccessDeniedHttpException(); } // … } }
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:. With the default user_identifier_claim of sub, that ID is the
Symfony user identifier:
$lumo->permissions->check('document.edit', userId: $this->getUser()->getUserIdentifier());
The API key needs the authz:check scope for this. Without it, LumoAuth answers 403
INSUFFICIENT_PERMISSIONS. If you set user_identifier_claim to another claim, such as
email, the identifier is no longer a LumoAuth user ID. In that case, call LumoAuth with
the user's own access token instead:
$asUser = new LumoAuth(orgId: 'acme-corp', tokenProvider: fn () => $accessToken); $asUser->permissions->check('document.edit');
Accept bearer tokens from LumoAuth
LumoAuthAccessTokenHandler works with the security component's access_token
authenticator. For each request it calls the LumoAuth userinfo endpoint with the bearer
token. It then returns a user badge whose identifier is the sub claim, or the claim set
in user_identifier_claim.
# config/packages/security.yaml security: firewalls: api: pattern: ^/api stateless: true access_token: token_handler: LumoAuth\Symfony\Security\LumoAuthAccessTokenHandler
- Your firewall's user provider loads the user by that identifier.
- The badge also carries every userinfo claim as attributes.
- An invalid or expired token causes a
BadCredentialsException, and Symfony answers 401. - When LumoAuth cannot be reached, the request also fails with 401.
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.
#[RequirePoppySession] asks LumoAuth to verify that header before the action runs. The
action then gets the personal agent, the user, the Session and the account of the request.
The bundle 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:
# config/packages/lumoauth.yaml lumoauth: org_id: '%env(LUMOAUTH_ORG_ID)%' client_id: '%env(LUMOAUTH_CLIENT_ID)%' client_secret: '%env(LUMOAUTH_CLIENT_SECRET)%' poppy: public_origin: 'https://shop.example.com'
Put the attribute on an action, or on a controller class to cover every action:
<?php // src/Controller/ShopApiController.php namespace App\Controller; use LumoAuth\Resource\PoppyVerification; use LumoAuth\Symfony\Poppy\RequirePoppySession; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Attribute\Route; final class ShopApiController extends AbstractController { // A signed-out operation: every verified personal agent may call it. #[Route('/api/catalog', methods: ['GET'])] #[RequirePoppySession] public function catalog(PoppyVerification $who): JsonResponse { return $this->json([ 'products' => ['stormline-shell-m', 'trail-socks-3'], 'session' => $who->session->id, // store a cart under this ID 'signed_in' => $who->signedIn, ]); } // An account operation: the access token must carry the scope. #[Route('/api/orders', methods: ['GET'])] #[RequirePoppySession('orders:read')] public function orders(PoppyVerification $who): JsonResponse { return $this->json([ 'account' => $who->account->sub, // the user in your LumoAuth organization 'agent' => $who->clientId, ]); } }
| Argument of the attribute | Meaning |
|---|---|
requiredScope |
The scopes that the operation needs, as a string or a list. Leave it out for a signed-out operation. |
resource |
The resource identifier of your service, when it takes only tokens that were issued for it |
allowReplay |
true accepts a Session header that was verified before. The default is false. |
An attribute on an action replaces the attribute on its class.
| Situation | What happens |
|---|---|
| LumoAuth verifies the request | The action runs. It takes the result as a PoppyVerification argument. The result is also in the request attribute lumoauth.poppy (PoppyGuard::REQUEST_ATTRIBUTE). |
| LumoAuth rejects the request | The action does not run. The personal agent gets the response that LumoAuth gave: its status, every header and its body. Symfony's error page is not used. |
| The SDK throws an error | The action does not run, and nothing is relayed. Symfony 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 |
A protected action throws LumoAuthConfigError, and Symfony answers 500. A malformed value fails every request. |
- The public origin comes from configuration. The bundle 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 the base URL when the application runs under a path prefix. - Only the protocol's headers are sent to LumoAuth:
Poppy-Session,Poppy-Version,Poppy-Extensions,AuthorizationandDPoP. The bundle 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\LumoAuth; use LumoAuth\Symfony\Poppy\PoppyRefusalResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; public function returnOrder(Request $request, LumoAuth $lumo): Response { $who = $lumo->poppy->guard('https://shop.example.com')->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.
More
- Core SDK guide:
lumoauth/lumoauth - Full guide and troubleshooting: sdk-php README
Licence
MIT