feat: configurable claim locations on DomainConfig - #15
Merged
Merged
Conversation
`permission_extractor` is the only way to reach a claim that is not top
level, and it has two limits. It writes to exactly one field,
`permissions`, so a second nested claim cannot be surfaced at all. And it
is a callable, so every consumer hand-writes the same path walk.
The shipped `extract_keycloak_permissions` keys on `azp`, which is right
for a token issued straight to a client. A token exchanged for a specific
audience has `azp` naming the broker and the roles filed under the
audience instead, so that extractor misses and each consumer writes its
own.
`claim_locations` maps a target field to a path of claim keys, with
`{audience}` and `{azp}` substituted per token. Both of the above become
configuration:
claim_locations={
"permissions": ("resource_access", "{audience}", "permission_roles"),
"application_roles": ("resource_access", "{audience}", "application_roles"),
}
An unresolved path leaves the field unset so `TokenPayload` applies its
own default, because a claim the provider does not issue is an answer
rather than an error. Naming the field in `required_claims` inverts that
and raises `PayloadMappingError`, matching what a `permission_extractor`
that does not match the token has always done.
Applied before `permission_extractor`, which still wins on `permissions`,
so the two coexist while a caller migrates.
Co-Authored-By: Claude Opus 5 (1M context) <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.
What
Adds
claim_locationsandrequired_claimstoDomainConfig: a declarative way to read a claim that is not top level, as a mapping of target field to a path of claim keys.{audience}is substituted from the audience passed todecode,{azp}from the token's own claim.Why
permission_extractoris the only existing hook and it has two limits: it writes to exactly one field,permissions, so a second nested claim cannot be surfaced at all; and it is a callable, so every consumer hand-writes the same path walk.The shipped
extract_keycloak_permissionskeys onazp, correct for a token issued straight to a client. A token exchanged for a specific audience names the broker inazpand files the roles under the audience, so that extractor misses and each consumer writes its own. That is the case driving this.Behaviour
TokenPayloadapplies its own default. A claim the provider does not issue is an answer, not an error.required_claimsinverts that: a miss raisesPayloadMappingError, a 500, matching what apermission_extractorthat does not match the token has always done.permission_extractor, which still wins onpermissions, so the two coexist while a caller migrates.Tests
Five new cases in
tests/unit/test_token_decoder.py: the{audience}and{azp}placeholders, an unresolved path falling back to the default, a required miss raisingPayloadMappingError, and the extractor winning over a location. Full suite 379 passed;just lintandjust typecheckclean.🤖 Generated with Claude Code