Skip to content

feat(auth): add per-issuer settings - #514

Merged
uniqueg merged 5 commits into
devfrom
feat/issuer-settings
Oct 26, 2026
Merged

uniqueg merged 5 commits into
devfrom
feat/issuer-settings

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

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.issuers can 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 global audience is 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.
    • Set it to "" 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.
    • The new helper get_claim_groups(issuer) returns the claim that applies.
    • All group consumers follow, as they use 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_id and azp are refused, as globally.
  • Fallbacks: unset settings fall back to the global ones (auth.audience, access_control.claim_groups, auth.userinfo_claims). Apps without per-issuer settings behave as before.
  • Startup checks:
    • The group checks count the group claims of issuers (e.g., a group_mapping with only per-issuer claims is fine).
    • The warning about colliding group claim values between issuers now applies only if several issuers have a group claim.
  • Docs: the config page (with an example), the authentication and access control guides, the API reference, and a note in "Migrating to 0.18.0".

Completes #503 together with #509 (PR #512).

Closes #510
Closes #503

Verification

Run locally on a300c51:

  • ruff check and ruff format --check: pass.
  • mypy: pass.
  • pytest -m "not mongodb": 2788 passed, 1 skipped, with 100% coverage of foca.
  • 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.

  • Round 1 (fixed in 6c3ce72):
    • an issuer could not opt out of the global group claim (now claim_groups: "");
    • startup messages named only the global settings;
    • tests were missing: two issuers with different audiences, non-string issuers, the empty-mapping warning with issuers;
    • the loose signature of get_claim_groups();
    • docs wording and empty-list semantics.
  • Round 2 (fixed in bffc243):
    • the warning about group claims of several issuers couldn't be silenced by the new opt-out;
    • docs rewrapping, a description of get_claim_groups(), docstring wording.

Sourcery: 2 findings.

  • Fixed in a300c51: the startup message on missing admins names the group claims of issuers.
  • Declined: see below.

Declined review findings

  • Sourcery: accept a global claim_groups with 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 IssuerConfig and the global models: declined. The checks are one line each, and the global audience check also depends on issuers and verify_audience.

🤖 Generated with Claude Code

uniqueg and others added 4 commits October 26, 2026 11:52
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]>
@sourcery-ai

sourcery-ai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Introduces 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 validation

sequenceDiagram
    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
Loading

Sequence diagram for issuer-aware group extraction

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Add per-issuer authentication settings with global fallback semantics.
  • Extend issuer configuration with audience, group-claim, and user-info claim settings and validation.
  • Resolve issuer-specific audiences during JWT validation and issuer-specific user-info claims during enrichment.
  • Allow global audience omission when every issuer defines its own audience, and improve missing-audience diagnostics.
  • Reject conflicting duplicate issuer entries while preserving support for plain issuer URLs.
foca/models/config.py
foca/security/auth.py
tests/security/test_auth.py
Use the token issuer to select group claims throughout access control.
  • Add and export get_claim_groups(issuer), including opt-out behavior via an empty claim name.
  • Make current group extraction, mappings, role checks, ownership, enforcement, and plugin-related consumers use the issuer-resolved claim values.
  • Update startup validation and collision warnings to count only issuers that actually provide group claims.
foca/security/access_control/__init__.py
foca/security/access_control/enforcement.py
foca/security/access_control/register_access_control.py
tests/security/access_control/test_enforcement.py
tests/security/access_control/test_register_access_control.py
Document per-issuer settings and their migration behavior.
  • Add configuration examples and describe fallback and empty-value semantics.
  • Update authentication, access-control, API reference, deployment, and migration documentation.
  • Document audience requirements and issuer-specific group collision warnings.
docs/access-control.md
docs/authentication.md
docs/configuration/access-control.md
docs/configuration/auth.md
docs/deployment.md
docs/migration-0.18.md
docs/reference.md

Assessment against linked issues

Issue Objective Addressed Explanation
#503 Ensure subjects from different trusted issuers are namespaced distinctly, while retaining existing behavior for single-issuer deployments. ✅
#503 Document how to migrate when moving from one trusted issuer to several, including the effect on existing subject and ownership records. ❌ The migration note documents the new per-issuer settings, but does not explain how adding issuers changes subject identifiers or how to migrate existing policies and ownership records.
#503 Allow issuer-specific audience, group-claim, and optional user-info-claim settings, with global-setting fallbacks. ✅
#510 Validate each trusted issuer’s tokens against its configured audience, falling back to the global audience when the issuer has none. ✅
#510 Use each issuer’s configured group claim for its tokens, falling back to the global group claim when unset. ✅
#510 Use each issuer’s configured user-info claims, falling back to the global setting when unset, while preserving behavior for configurations without per-issuer settings. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (888fcf0) to head (a300c51).

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     
Flag Coverage Δ
test_unit 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review October 26, 2026 11:49

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

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]>
@uniqueg
uniqueg merged commit 4990e19 into dev Oct 26, 2026
30 checks passed
@uniqueg
uniqueg deleted the feat/issuer-settings branch October 26, 2026 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(auth): add per-issuer settings fix(auth): namespace subjects by issuer

1 participant