Repository navigation
feat(auth): add per-issuer settings - #514
Conversation
Identity providers differ in the audiences of their tokens, their group claims and the claims they release only via user info. Issuers in security.auth.issuers can now have an audience, a claim_groups and userinfo_claims of their own, which replace the global settings for their tokens; unset ones fall back to them, so apps without such settings behave as before. Tokens are validated with their issuer's audience, user info claims are added as configured for it, and get_claim_groups() picks the group claim by the token's issuer. With an audience for each issuer, no global audience is required; the startup group checks count group claims of issuers. Closes #510 Co-Authored-By: Claude Opus 5.5 <[email protected]>
- An empty claim_groups of an issuer takes no groups from its tokens, e.g., for issuers trusted for login only, whose group claim values could collide with those of others. - Startup messages name the group claims and user info claims of issuers; the audience error names issuers without an audience. - Tests of two issuers with different audiences, of non-string issuers, and of empty group mappings with issuers; get_claim_groups() takes a required issuer. - Docs: wording, rewrapping, empty lists and values. Refs #510 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The startup warning about colliding group claim values now applies only if several trusted issuers have a group claim, so that taking no groups from the other issuers (claim_groups: "") silences it. Also rewrap docs, describe get_claim_groups() and clarify docstrings. Refs #510 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideIntroduces issuer-specific audiences, group claims, and user-info claims with backward-compatible global fallbacks, propagates issuer-aware group handling through authentication and access control, adds startup validation and diagnostics, and updates the documentation and tests. Sequence diagram for issuer-specific token validationsequenceDiagram
participant Token
participant Auth as AuthConfig
participant JWT as JWTValidator
participant UserInfo
Token->>Auth: get_audience(issuer)
Auth-->>JWT: issuer audience or global audience
JWT->>JWT: _verify_signature(token, audience)
JWT-->>Auth: validated claims
Auth->>Auth: get_userinfo_claims(issuer)
Auth->>UserInfo: fetch missing configured claims
UserInfo-->>Auth: userinfo claims
Sequence diagram for issuer-aware group extractionsequenceDiagram
participant Request
participant Enforcement
participant AuthConfig
participant Claims
Request->>Enforcement: get_current_group_values()
Enforcement->>Claims: read iss and token claims
Enforcement->>AuthConfig: get_claim_groups(issuer)
AuthConfig-->>Enforcement: issuer claim_groups or global fallback
Enforcement->>Claims: read configured group claim
Claims-->>Request: group values
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #514 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 55 55
Lines 6558 6609 +51
=========================================
+ Hits 6558 6609 +51
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/security/access_control/register_access_control.py" line_range="81" />
<code_context>
- _check_group_config(access_control_config)
- _check_admin_role(access_control_config)
+ auth_config = get_app_config(cnx_app.app).security.auth
+ _check_group_config(access_control_config, auth_config)
+ _check_admin_role(access_control_config, auth_config)
</code_context>
<issue_to_address>
**App rejects unused global claim**
When the global `claim_groups` has surrounding whitespace and every trusted issuer sets its own `claim_groups`, `_check_group_config` rejects the global value before checking the effective per-issuer claims, so startup fails even though no issuer uses that value.
Only validate the global `claim_groups` value when at least one trusted issuer falls back to it.
Also at `foca/security/access_control/register_access_control.py:247`.
</issue_to_address>
### Comment 2
<location path="foca/security/access_control/register_access_control.py" line_range="363" />
<code_context>
"""
conf = access_control_config
- keeps_group_subjects = bool(conf.claim_groups) and (
+ keeps_group_subjects = any(_get_group_claims(conf, auth_config)) and (
conf.group_mapping is None or conf.keep_group_subjects
)
</code_context>
<issue_to_address>
**Admin error points to global claim**
When `require_admin` is enabled, no admin grant is found, and group claims are configured only per issuer, `_check_admin_role` now accepts issuer-specific group claims when deciding whether group subjects can satisfy admin rules, but its `require_admin` error still says group mapping needs the global `claim_groups` setting. Operators using only issuer-specific claims are directed toward an unnecessary global setting, which can make those claims apply to more issuers than intended.
Update the error guidance to mention issuer-specific `claim_groups` as an alternative to the global setting.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 1 finding to address first, and a wrong per-issuer audience or group-claim selection could accept a token for the wrong app or grant access based on the wrong groups, exposing protected data or actions. Reverting removes the new selection behavior, but any access or exposure that occurred while it was active cannot be undone.
Blocking findings: foca/security/access_control/register_access_control.py:81
The startup message on missing admins names the group claims of issuers next to the global one (Sourcery). Refs #510 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Identity providers differ: they issue tokens for different audiences, put groups in different claims, and some release claims only via user info. Issuers in
security.auth.issuerscan now have settings of their own, which replace the global ones for their tokens:audience: tokens are validated with the audience of their issuer. If every issuer has one, the globalaudienceis not required. The error for missing audiences names the issuers that have none.claim_groups: groups are taken from the group claim of the token's issuer.""to take no groups from that issuer's tokens, e.g., for an issuer trusted for login only, whose group claim values could collide with those of the others.get_claim_groups(issuer)returns the claim that applies.get_current_group_values(): enforcement, mapped and opt-in roles, ownership and plugin principals.userinfo_claims: claims are added from user info as configured for the token's issuer. An empty list adds none.client_idandazpare refused, as globally.auth.audience,access_control.claim_groups,auth.userinfo_claims). Apps without per-issuer settings behave as before.group_mappingwith only per-issuer claims is fine).Completes #503 together with #509 (PR #512).
Closes #510
Closes #503
Verification
Run locally on a300c51:
ruff checkandruff format --check: pass.mypy: pass.pytest -m "not mongodb": 2788 passed, 1 skipped, with 100% coverage offoca.mkdocs build --strict: pass.pre-commit: pass.The MongoDB tests and semgrep did not run locally (CI runs them). CI on a300c51: 27 checks pass, and 3 are skipped as intended.
Review: two rounds of adversarial and quality review.
claim_groups: "");get_claim_groups();get_claim_groups(), docstring wording.Sourcery: 2 findings.
Declined review findings
Sourcery: accept a global
claim_groupswith whitespace if all issuers have claims of their own: declined. A malformed setting is refused wherever it is set; otherwise the typo would surface only later (e.g., when an issuer without a claim is added), as users silently having no groups.Cache the issuer settings: declined, as in fix(auth): namespace subjects of several issuers #512.
get_issuer_configs()builds a few small models per call (about 7 µs for 3 issuers), and the config models don't validate assignment, so a cache could go stale. Conflicting duplicate entries are refused at config load.Share the audience and group claim validators between
IssuerConfigand the global models: declined. The checks are one line each, and the global audience check also depends onissuersandverify_audience.🤖 Generated with Claude Code