Skip to content

feat(auth): add JWT Bearer token authentication support#74

Merged
arne-aignx merged 1 commit into
mainfrom
feat/bearer-auth
May 28, 2026
Merged

feat(auth): add JWT Bearer token authentication support#74
arne-aignx merged 1 commit into
mainfrom
feat/bearer-auth

Conversation

@arne-aignx
Copy link
Copy Markdown
Contributor

@arne-aignx arne-aignx commented May 27, 2026

Summary

  • Extends api.auth to accept Auth0 JWT Bearer tokens alongside existing cookie-based sessions
  • Bearer auth is opt-in via AUTH_JWT_ENABLED=true and AUTH_JWT_AUDIENCE=<audience>; each require_* dependency tries Bearer first, then falls back to cookie
  • Adds JWKS fetching with per-domain in-memory TTL cache (AUTH0_JWKS_CACHE_TTL = 3600s)
  • New AuthSettings fields: cookie_enabled (primary name, replaces deprecated enabled), jwt_enabled, jwt_audience
  • New Bearer HTTPBearer security schemes for all four OpenAPI dependency variants
  • New deps: PyJWT[cryptography]>=2.10,<3, httpx>=0.28,<1

Test plan

  • mise run lint passes (ruff + pyright)
  • mise run test_unit passes (all existing + new AuthSettings and _validate_jwt unit tests)
  • mise run test_integration passes
  • AUTH_JWT_ENABLED=false (default): Bearer tokens are ignored, cookie path unchanged
  • AUTH_JWT_ENABLED=true with valid Bearer token: JWT validated, cookie path skipped
  • AUTH_JWT_ENABLED=true with invalid/expired Bearer token: falls back to cookie

🤖 Generated with Claude Code

@arne-aignx arne-aignx requested a review from a team as a code owner May 27, 2026 15:03
@codecov
Copy link
Copy Markdown

codecov Bot commented May 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

Files with missing lines Coverage Δ
src/aignostics_foundry_core/api/auth.py 100.00% <100.00%> (ø)

@arne-aignx arne-aignx force-pushed the feat/bearer-auth branch 2 times, most recently from 31d1397 to 7d84586 Compare May 27, 2026 15:21
Comment thread src/aignostics_foundry_core/api/auth.py
Comment thread src/aignostics_foundry_core/api/auth.py
Comment thread pyproject.toml Outdated
Comment thread src/aignostics_foundry_core/api/auth.py Outdated
if jwt_user:
set_sentry_user(jwt_user, role_claim=auth_settings.role_claim)
return jwt_user
logger.debug("Bearer token present but JWT validation failed; falling back to cookie")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I am unsure about this logging - debug will likely not be visible in production anyways, but we don't have any context to this log - how would this be useful if we cannot associate this situation to a particular user or service? Should we either remove this line, or add some context to it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not the best. But here we cannot get much more information here, since we cannot decode the user from the token
I updated some other log entries though, to enrich the logs with additional infos.

Information about the service, request etc. can be added by the consumer API services using middleware. I would push back that is a concern of the auth module in foundry core.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could we add the trace id automatically perhaps?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Let's include this in a middleware and automatically add it to the routers we use here.
But I think it's out of scope for this change

@melifaro fyi - since you looked into porting the telemetry package. This could align well with the OTEL instrumentation. And I need to take a step back how to disentangle this from the existing code and it's sentry integration

@arne-aignx arne-aignx force-pushed the feat/bearer-auth branch 2 times, most recently from 8cddf2d to 0b46839 Compare May 28, 2026 09:41
Extends api.auth to accept Auth0 JWT Bearer tokens alongside existing
cookie-based sessions. Bearer auth is opt-in via AUTH_JWT_ENABLED and
AUTH_JWT_AUDIENCE; each require_* dependency tries Bearer first, then
falls back to cookie. Adds JWKS fetching with per-domain TTL cache.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sonarqubecloud
Copy link
Copy Markdown

@arne-aignx arne-aignx merged commit 8a959f4 into main May 28, 2026
12 checks passed
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.

2 participants