Skip to content

refactor: read the token challenge from headers - #371

Merged
uniqueg merged 2 commits into
devfrom
refactor/challenge-from-headers
Nov 5, 2026
Merged

uniqueg merged 2 commits into
devfrom
refactor/challenge-from-headers

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Closes #370

Summary

_is_invalid_token() now reads the RFC 6750 challenge from the headers the exception produces (get_headers()), not from its www_authenticate attribute. It works with FOCA 0.21, which keeps the challenge in the attribute, and after elixir-cloud-aai/foca#710, which moves it to FOCA's generic headers. This unblocks foca#710. No behaviour change: FOCA sends only Bearer challenges, and only those count now.

How it was verified

  • New tests: the challenge in the attribute (Werkzeug and FOCA 0.21's own token errors) and only in extra headers, as FOCA's foca.errors.Unauthorized on dev produces it: 10 passed.
  • pytest tests/test_access_control.py tests/test_app.py tests/test_issuers.py: 408 passed. Full suite: 726 passed, 1 failed locally (test_chart.py::test_config_change_restarts_app, chart dependencies not built here; unrelated).
  • ruff check, ruff format --check (0.17.3), mypy .: passed.
  • CI: see the checks of this PR.

Declined review findings

  • Werkzeug's WWWAuthenticate.from_header reads one challenge per header value, strictly: several challenges packed into one value, or an upper-case parameter name, aren't recognised. Kept in step with FOCA's own _has_invalid_token_challenge(), which has the same limits; FOCA sends one challenge per header. A miss errs on the safe side (the read stays anonymous).

Open questions

None.

🤖 Generated with Claude Code

Summary by Sourcery

Read token validation challenges from response headers to support current and legacy FOCA error formats.

Bug Fixes:

  • Detect invalid bearer tokens from RFC 6750 challenges regardless of whether FOCA exposes them through the exception attribute or response headers.

Enhancements:

  • Restrict invalid-token detection to Bearer authentication challenges and preserve safe handling of other authentication failures.

Tests:

  • Add coverage for Werkzeug and FOCA token errors with challenges stored in attributes, generic headers, or absent.

uniqueg and others added 2 commits November 5, 2026 17:59
Tell invalid tokens of public reads from other rejections by the
WWW-Authenticate header of the exception, not its www_authenticate
attribute, so that FOCA can move the challenge to its generic
headers (foca#710). Works with FOCA 0.21 and after that change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg uniqueg added this to the v1.0.0-rc2 milestone Nov 5, 2026
@uniqueg
uniqueg marked this pull request as ready for review November 5, 2026 17:10
@sourcery-ai

sourcery-ai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The PR makes invalid-token detection representation-agnostic by reading and parsing WWW-Authenticate response headers instead of relying on the exception's www_authenticate attribute, while restricting detection to RFC 6750 Bearer challenges with invalid_token and adding compatibility-focused tests.

Sequence diagram for header-based invalid-token detection

sequenceDiagram
    participant Validation as validate_token
    participant Permissions as _is_invalid_token
    participant Error as Unauthorized
    participant Parser as WWWAuthenticate.from_header

    Validation->>Permissions: _is_invalid_token(exc)
    Permissions->>Error: get_headers()
    Error-->>Permissions: WWW-Authenticate headers
    loop each response header
        Permissions->>Parser: from_header(value)
        Parser-->>Permissions: challenge
        alt Bearer challenge with error=invalid_token
            Permissions-->>Validation: True
        end
    end
    Permissions-->>Validation: False
Loading

File-Level Changes

Change Details Files
Refactor invalid-token detection to inspect response authentication headers across FOCA error representations.
  • Iterate over get_headers() and select WWW-Authenticate fields case-insensitively.
  • Parse challenges with Werkzeug and accept only Bearer challenges carrying error="invalid_token".
  • Preserve false results for missing, non-Bearer, or otherwise unqualified challenges.
cloud_registry/ga4gh/registry/permissions.py
Add coverage for legacy exception attributes, generic response headers, and FOCA token errors.
  • Test valid and invalid Bearer challenges in exception attributes and headers.
  • Cover descriptions, other Bearer challenges, Basic challenges, and missing challenges.
  • Verify FOCA-generated unauthorized exceptions for both invalid-token states.
tests/test_access_control.py

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

@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!

Linked issues

#370 refactor: read the token challenge from headers · 1 of 1 met

# Requirement Status Notes
1 Read the challenge from exception headers, working with FOCA 0.21 and after foca#710. ✅ Met _is_invalid_token() checks get_headers() and tests cover both Werkzeug's www_authenticate attribute and FOCA token errors.

Sourcery assessment

Needs a human reviewer. The Bearer challenge determines whether a token failure is classified as an invalid credential or another validation failure; misparsing it could change how requests with rejected tokens are handled. Reverting removes the new header-based classification, but any misclassified requests handled before the revert cannot be undone.


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

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (8b69b3c) to head (9e3d48b).
⚠️ Report is 1 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #371   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines         3259      3265    +6     
=========================================
+ Hits          3259      3265    +6     

☔ 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 merged commit 4e707a9 into dev Nov 5, 2026
40 of 44 checks passed
@uniqueg
uniqueg deleted the refactor/challenge-from-headers branch November 5, 2026 17:35
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.

refactor: read the token challenge from headers

1 participant