Repository navigation
feat: stateless Bearer authenticators, issuer option and organizations (4.2.0) - #70
Merged
Merged
Conversation
…entationDTO
UserRepresentationDTO gets new trailing fields, all defaulting to null so
the 4.1 positional constructor keeps working:
- authorizedParty and authorizedPartyRoles, from `azp` and
`resource_access[azp]`;
- organizations, read by the new OrganizationRepresentationDTO from the
`organization` claim in every format Keycloak emits (list of aliases,
single alias, map alias => {id, attributes});
- issuer, expiresAt and the raw claims.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The decoders only check that `iss` contains the realm name. The new `keycloak.issuer` option, when set, makes verifyToken() require an exact match. It is needed when the application reaches Keycloak on a different URL (e.g. an internal Docker hostname) than the public one that issues the tokens, so `base_url` cannot be used for the comparison. Without the option the behaviour is unchanged, and TokenDecoderInterface is untouched for custom decoders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
KeycloakBearerAuthenticator reads the `Authorization: Bearer` header and verifies the access token locally with verifyToken(), without calling Keycloak on each request; failures get a 401 JSON response. KeycloakOptionalBearerAuthenticator lets the request continue as anonymous instead, for public routes. They are registered only when the new `bearer` option is enabled: - `roles_source`: `all` (default, as KeycloakResourceOwner always did) or `authorized_party`, only the roles of the client that requested the token; - `allowed_clients`: accepted `azp` values, empty means any; - `client_roles`: extra roles granted according to `azp`. KeycloakResourceOwner gains getAuthorizedParty() and getOrganizations(). symfony/yaml is added to require-dev to test the extension. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
verifyToken() logged every decoded claim at info level twice. With the Bearer authenticator it runs on each API request, so the claims now go to the debug level. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TokenAuthListener calls the userinfo endpoint on each request and works outside the Symfony security system. It now triggers a deprecation and will be removed in 5.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- client_roles apply only with roles_source `authorized_party`, so the default `all` keeps the 4.1 roles even when client_roles is configured; - getRoles($client_id) always returns realm roles plus that client's roles, whatever the source; - roles_source becomes RolesSourceEnum instead of string constants; - malformed `roles` claims and an empty `azp` are ignored instead of raising warnings; - the two authenticators share their arguments through a YAML anchor; - README: client_roles scope, debug log level and raw claims in the DTO. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds what an API needs to authenticate Keycloak access tokens through the Symfony security system, verifying them locally. Everything is opt-in, so it targets a minor release (4.2.0): with the configuration unchanged, behaviour is the same as 4.1.
Release notes (4.2.0)
Added
KeycloakBearerAuthenticator: stateless authenticator forAuthorization: Bearertokens, verified locally withverifyToken()(signature, expiration, issuer) without calling Keycloak on each request. Failures get a401JSON response.KeycloakOptionalBearerAuthenticator: same verification, but an invalid token lets the request continue as anonymous (public routes).bearerconfiguration node (disabled by default) withroles_source(all|authorized_party, seeRolesSourceEnum),allowed_clientsandclient_roles(applied only withauthorized_party).keycloak.issueroption: when set,verifyToken()requires an exactissmatch. Useful when the app reaches Keycloak on an internal URL that differs from the public issuer.UserRepresentationDTO:authorizedParty,authorizedPartyRoles,organizations,issuer,expiresAtand rawclaims(trailing, nullable, positional constructor of 4.1 still works).OrganizationRepresentationDTO: reads theorganizationclaim in every format Keycloak emits (list of aliases, single alias, map with id and attributes).KeycloakResourceOwner::getAuthorizedParty()andgetOrganizations().Changed
verifyToken()logs the decoded token atdebuglevel instead ofinfo.symfony/yamladded torequire-dev(used by the new extension test).Deprecated
TokenAuthListener: use the Bearer authenticator. It will be removed in 5.0.Fixed
Removed
Compatibility
beareris disabled by default: the authenticators are not registered.roles_sourcedefaults toall, soKeycloakResourceOwner::getRoles()keeps returning realm roles plus every client's roles;client_rolesis ignored in that mode.getRoles($clientId)with an explicit client keeps its 4.1 meaning.UserRepresentationDTO::$claimscarries the raw token claims: avoid serializing the DTO into responses or logs as a whole.TokenDecoderInterfaceis unchanged: the issuer check lives inKeycloakClient::verifyToken().email/emailVerified(UserRepresentationDTO:emailandemailVerifiedshould be nullable #69) and the removal ofTokenAuthListener.Tests
composer test: 157 tests, all green.composer lint: clean.main; no new errors in the touched files.🤖 Generated with Claude Code