Repository navigation
feat(auth): allow-list token clients - #364
Conversation
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]>
Reviewer's GuideAdds an optional, validated client allow-list for bearer tokens, enforcing exact Sequence diagram for bearer token client allow-list validationsequenceDiagram
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
File-Level Changes
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 #364 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 24 24
Lines 1810 1822 +12
=========================================
+ Hits 1810 1822 +12
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:
|
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]>
# Conflicts: # foca/security/auth.py
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
There was a problem hiding this comment.
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.
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_idclaim (RFC 9068); other identity providers useazp.security.auth.clients(default:null, not restricted): IDs of the clients whose tokens the app accepts. After signature verification, the token'sclient_idclaim or, if it has none, itsazpclaim must be listed (exact match); tokens of other clients and tokens with neither claim are rejected with401(WWW-Authenticate: Bearer error="invalid_token"). A configured list must be non-empty with non-blank IDs.clientscan be used in addition toaudienceor instead of it withverify_audience: false: the startup error for trusted issuers without audience now namesclientsas the alternative, and the warning about disabled audience validation is not logged when clients are restricted.security.authoptions, "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.pyandfoca/models/config.pyat 100%ruff check,ruff format --check,mypy: cleanmkdocs build --strict: OKReviews: adversarial and quality review found no high or medium findings. Low findings fixed:
client_idnow takes precedence overazp(fallback only if the token has noclient_id, instead of accepting either claim); the after-signature test asserts that the client check is not reached for badly signed tokens; strictermatchin the config test; the warning text is asserted; the multi-issuer caveat and theverify_audienceguidance 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 invalidclient_idwith an acceptedazp) is fixed.CI: all checks passed, also after the review fixes and the merge of
dev.Declined review findings
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:
client_idclaim, falling back toazpwhen absent.Enhancements:
Documentation:
Tests: