Skip to content

feat(auth): allow-list token clients - #364

Merged
uniqueg merged 11 commits into
devfrom
feat/auth-clients
Oct 21, 2026
Merged

uniqueg merged 11 commits into
devfrom
feat/auth-clients

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #339

Some apps must accept tokens only from specific clients, not by audience: TES backends behind the proTES gateway should accept only tokens obtained by proTES, whose audience is proTES's client ID. LS Login identifies the client in the client_id claim (RFC 9068); other identity providers use azp.

  • New security.auth.clients (default: null, not restricted): IDs of the clients whose tokens the app accepts. After signature verification, the token's client_id claim or, if it has none, its azp claim must be listed (exact match); tokens of other clients and tokens with neither claim are rejected with 401 (WWW-Authenticate: Bearer error="invalid_token"). A configured list must be non-empty with non-blank IDs.
  • clients can be used in addition to audience or instead of it with verify_audience: false: the startup error for trusted issuers without audience now names clients as the alternative, and the warning about disabled audience validation is not logged when clients are restricted.
  • Docs: security.auth options, "How tokens are validated" (step 6), a tip on the gateway use case (including that client IDs are matched for all trusted issuers), "Migrating to 0.16.0" (new options), config template and examples.

Verification

Locally (Python 3.13, MongoDB 7.0 on port 12345):

  • pytest: 1086 passed (full suite); auth and config tests re-run after the last test additions (226 passed)
  • coverage report: foca/security/auth.py and foca/models/config.py at 100%
  • ruff check, ruff format --check, mypy: clean
  • mkdocs build --strict: OK

Reviews: adversarial and quality review found no high or medium findings. Low findings fixed: client_id now takes precedence over azp (fallback only if the token has no client_id, instead of accepting either claim); the after-signature test asserts that the client check is not reached for badly signed tokens; stricter match in the config test; the warning text is asserted; the multi-issuer caveat and the verify_audience guidance are documented consistently (docstrings, docs, template, examples). A second adversarial round on the logic change found no high or medium findings; its low finding (no test for an invalid client_id with an accepted azp) is fixed.

CI: all checks passed, also after the review fixes and the merge of dev.

Declined review findings

  • Reject client IDs with surrounding whitespace at config time (quality review, optional): client IDs are compared exactly, like audience; the docstring now says so.

Open questions

None.

🤖 Generated with Claude Code

Summary by Sourcery

Restrict accepted bearer tokens to explicitly configured clients while preserving audience validation as an independent option.

New Features:

  • Add optional authentication client allow-listing using the token's client_id claim, falling back to azp when absent.
  • Allow client restrictions to provide a safer alternative when audience validation is disabled.

Enhancements:

  • Validate configured client lists and enforce client checks after token signature verification.
  • Suppress the disabled-audience warning when client restrictions are configured.

Documentation:

  • Document client-based token validation, gateway use cases, configuration options, examples, and migration guidance.

Tests:

  • Add coverage for accepted and rejected clients, claim precedence, signature-validation ordering, configuration validation, and warning behavior.

uniqueg and others added 2 commits October 22, 2026 00:46
Accept only OAuth 2.0 access tokens as per RFC 9068, i.e., JWTs whose
`typ` header is `at+jwt` or `application/at+jwt` (compared
case-insensitively), so that other JWTs of trusted issuers, such as ID
tokens, cannot be used as access tokens. Tokens of other types or
without `typ` header are rejected with 401 before any request to the
identity provider.

New options `security.auth.token_types` (accepted `typ` values) and
`security.auth.verify_token_type` (opt-out for identity providers that
do not set `typ`, with a startup warning as for `verify_audience`).

BREAKING CHANGE: tokens without an accepted `typ` header are rejected;
set `security.auth.verify_token_type: false` for identity providers
that do not set it. Documented in "Migrating to 0.16.0".

Closes #338

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Add `security.auth.clients`: IDs of the clients whose tokens the app
accepts, checked against the verified `client_id` claim (RFC 9068) or,
with identity providers that use it, the `azp` claim (OpenID Connect).
Tokens of other clients, and tokens with neither claim, are rejected
with 401. Use case: TES backends behind the proTES gateway accept only
tokens obtained by proTES.

`clients` can be used in addition to `audience` or instead of it (with
`verify_audience: false`): the startup error for trusted issuers
without audience names it as the alternative, and the warning about
disabled audience validation is not logged when clients are
restricted. By default, clients are not restricted.

Closes #339

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Adds an optional, validated client allow-list for bearer tokens, enforcing exact client_id/fallback azp matching after signature verification while integrating it with audience validation, warnings, configuration, tests, and documentation.

Sequence diagram for bearer token client allow-list validation

sequenceDiagram
    participant Client
    participant Auth as Auth.validate_token
    participant Signature as _verify_signature
    participant ClientCheck as _verify_client
    participant UserInfo as _get_userinfo

    Client->>Auth: validate_token(token)
    Auth->>Signature: _verify_signature(token, ...)
    Signature-->>Auth: verified claims
    opt clients is configured
        Auth->>ClientCheck: _verify_client(claims, conf)
        alt client_id claim exists
            ClientCheck-->>Auth: exact client_id allow-list match
        else no client_id claim
            ClientCheck-->>Auth: exact azp allow-list match
        end
        ClientCheck-->>Auth: unauthorized if client is missing or not accepted
    end
    opt user info validation or missing claims
        Auth->>UserInfo: _get_userinfo(token, claims, ...)
    end
    Auth-->>Client: validated claims or 401 invalid_token
Loading

File-Level Changes

Change Details Files
Adds optional allow-list enforcement for the OAuth client that obtained a token.
  • Introduces nullable security.auth.clients configuration with validation for non-empty, non-blank IDs.
  • Checks client_id after signature verification, falling back to azp only when client_id is absent.
  • Rejects unlisted clients and tokens without a usable client claim with the existing unauthorized Bearer response.
  • Supports client restrictions alongside audience validation or as the alternative when verify_audience is disabled.
  • Suppresses the disabled-audience warning when clients are restricted and updates related startup validation and warning text.
foca/models/config.py
foca/security/auth.py
tests/security/test_auth.py
Documents client-based token restriction and the gateway deployment use case across configuration and migration guidance.
  • Adds the client check to the token-validation sequence and explains claim precedence and multi-issuer considerations.
  • Updates auth configuration reference, template, examples, and 0.16 migration notes.
  • Clarifies when disabling audience validation is safe with a client allow-list.
docs/authentication.md
docs/configuration/auth.md
docs/migration-0.16.md
templates/config.yaml
examples/petstore-access-control/config.yaml
examples/petstore/config.yaml

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 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (4aca562) to head (2d2334d).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #364   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        24           
  Lines         1810      1822   +12     
=========================================
+ Hits          1810      1822   +12     
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 and others added 9 commits October 22, 2026 01:06
Ignore surrounding whitespace in configured token types, so that a
padded entry does not silently reject every token; test non-string
`typ` headers and mixed-case `application/at+jwt`. Say in the migration
notes that apps using LS Login need no changes.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Link the 0.16 migration notes from the README and the docs index, add
the token types to the example configurations, mention the token type
check in the `validate_token` docstring, and note that a generic type
such as `JWT` does not tell access tokens apart from ID tokens.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Identify the client by the `client_id` claim (RFC 9068) and fall back
to the `azp` claim only if the token has no `client_id`, instead of
accepting either claim, so that the decision rests on the authoritative
claim. Assert in the test that the client check runs after signature
verification. Document that client IDs are matched for all trusted
issuers and that restricting clients is the sanctioned reason to
disable audience validation.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Python 3.10 support was dropped in #360 without a migration note.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
An invalid or null `client_id` claim is not replaced by the `azp`
claim; state the precedence in the config error and migration notes.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
# Conflicts:
#	docs/authentication.md
#	docs/configuration/auth.md
#	docs/migration-0.16.md
#	foca/models/config.py
#	foca/security/auth.py
#	templates/config.yaml
#	tests/security/test_auth.py
@uniqueg
uniqueg marked this pull request as ready for review October 21, 2026 23:34

@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 reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. When configured, this changes which signed tokens can access the app: a matching client_id or azp can satisfy the client restriction, including when audience validation is disabled. If that check or the configured IDs admit an unintended client, reverting stops future acceptance but cannot undo access already granted.


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

@uniqueg
uniqueg merged commit 3256f35 into dev Oct 21, 2026
14 checks passed
@uniqueg
uniqueg deleted the feat/auth-clients branch October 21, 2026 23:36
@uniqueg uniqueg mentioned this pull request Oct 21, 2026
2 tasks
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.

1 participant