Skip to content

feat(auth)!: require the at+jwt token type - #361

Merged
uniqueg merged 5 commits into
devfrom
feat/auth-token-types
Oct 21, 2026
Merged

uniqueg merged 5 commits into
devfrom
feat/auth-token-types

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #338

FOCA accepted any signed JWT of a trusted issuer as an access token, including ID tokens. RFC 9068 has resource servers require the typ header at+jwt (or application/at+jwt) precisely so that other JWTs cannot be used as access tokens; LS Login issues such tokens.

  • New security.auth.token_types (default: ["at+jwt", "application/at+jwt"]): accepted typ header values, compared case-insensitively (media types, RFC 7515 §4.1.9) and ignoring surrounding whitespace in the configured values. Tokens of other types or without typ are rejected with 401 and WWW-Authenticate: Bearer error="invalid_token", before any request to the identity provider.
  • New security.auth.verify_token_type (default: true): opt-out for identity providers that do not set typ, mirroring verify_audience; a startup warning is logged when disabled, and the config fails validation if token_types is empty or blank while the check is on.
  • Docs: security.auth options, "How tokens are validated", a new "Migrating to 0.16.0" page (breaking change + how to disable; linked from README, docs index and changelog), config template and example configs.

Breaking change (feat(auth)!): tokens without an accepted typ header are rejected. Apps using LS Login need no changes; others set verify_token_type: false or list their typ in token_types.

Verification

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

  • pytest: 1064 passed (full suite, first commit); auth and config tests re-run after the review fixes (205 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 in the follow-up commits (whitespace in configured types, non-string typ test, README/index links, example configs, validate_token docstring, JWT-type caveat), except those below.

CI: all checks passed (also after the review fixes); Sourcery approved.

Declined review findings

  • Build the lowercased set of accepted types once instead of per request: the set has two entries, so the cost is negligible, and caching it on the pydantic model adds complexity.
  • Show verify_token_type as a key in the config template rather than only in a comment: the template mirrors how verify_audience is presented (comment next to audience).
  • Drop the "New options" section of the 0.16 migration notes as redundant with the breaking change: kept for consistency with the 0.15 notes; feat(auth): allow-list token clients #339 adds its option there.

Open questions

None.

🤖 Generated with Claude Code

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]>
@sourcery-ai

sourcery-ai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This breaking authentication change restricts bearer tokens to configured RFC 9068 typ values by default, performs the check before identity-provider requests, and adds opt-out configuration, validation, tests, examples, and migration documentation.

Sequence diagram for access-token type validation

sequenceDiagram
    participant Client
    participant FOCA
    participant IdentityProvider

    Client->>FOCA: validate_token(token)
    FOCA->>FOCA: get_unverified_header(token)
    alt verify_token_type and typ not accepted
        FOCA-->>Client: 401 Unauthorized
        FOCA-->>Client: WWW-Authenticate: Bearer error="invalid_token"
    else typ accepted or verify_token_type disabled
        FOCA->>IdentityProvider: _get_oidc_config(issuer, conf)
        IdentityProvider-->>FOCA: Discovery document and public keys
        FOCA-->>Client: Validated token claims
    end
Loading

File-Level Changes

Change Details Files
Require RFC 9068 access-token types before contacting identity providers.
  • Add configurable, case-insensitive accepted typ values with RFC 9068 defaults.
  • Reject missing, non-string, or unaccepted types with an invalid-token response before network access.
  • Allow an explicit opt-out and emit a security warning when type validation is disabled.
foca/security/auth.py
foca/models/config.py
tests/security/test_auth.py
Extend configuration examples and validation for token-type enforcement.
  • Add token_types and verify_token_type settings with defaults and validation for empty or blank values.
  • Document the new fields in model docstrings and configuration examples/templates.
foca/models/config.py
docs/configuration/auth.md
templates/config.yaml
examples/petstore/config.yaml
examples/petstore-access-control/config.yaml
Document the breaking authentication change and migration path.
  • Update token-validation documentation to describe type checking and ordering.
  • Add 0.16.0 migration guidance, navigation, changelog, and README links.
  • Explain opt-out behavior and risks for providers issuing generic or missing token types.
docs/authentication.md
docs/migration-0.16.md
docs/changelog.md
docs/index.md
mkdocs.yml
README.md

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 (b1998a7) to head (c211015).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #361   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        24           
  Lines         1796      1810   +14     
=========================================
+ Hits          1796      1810   +14     
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 2 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]>
@uniqueg
uniqueg marked this pull request as ready for review October 21, 2026 23:15
sourcery-ai[bot]
sourcery-ai Bot previously approved these changes Oct 21, 2026

@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

Approved.


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

uniqueg and others added 2 commits October 22, 2026 01:23
Python 3.10 support was dropped in #360 without a migration note.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@sourcery-ai
sourcery-ai Bot dismissed their stale review October 21, 2026 23:25

Sourcery withdrew this approval because the latest commits introduced blocking findings.

@uniqueg
uniqueg merged commit 4aca562 into dev Oct 21, 2026
14 checks passed
@uniqueg
uniqueg deleted the feat/auth-token-types branch October 21, 2026 23:28
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