Skip to content

feat: JWT authentication with PyJWT and Ory Hydra - #5

Merged
HenriBlacksmith merged 17 commits into
mainfrom
feat/jwt-authentication
Nov 17, 2026
Merged

HenriBlacksmith merged 17 commits into
mainfrom
feat/jwt-authentication

Conversation

@HenriBlacksmith

Copy link
Copy Markdown
Owner

Summary

  • Add JWT bearer authentication on all /users routes, validated against Ory Hydra (local/test environment) using PyJWT.
  • New dependency app/api/dependencies/auth.py:
    • get_current_user: verifies the access token (RS256) against Hydra's JWKS endpoint, checks the iss claim, returns 401 (problem+json, WWW-Authenticate: Bearer) on missing/invalid/expired token.
    • require_scopes(*scopes): factory returning a dependency that checks the token's scp/scope claim and returns 403 when scopes are insufficient.
  • Scopes: users:read for GET routes, users:write for POST/PUT/DELETE. /health and /user_events remain public.
  • OpenAPI now exposes the OAuth2 authorization-code flow (Hydra URLs from settings).
  • New AuthenticationSettings (issuer, jwks_url) wired into config.yaml and tests/config.yaml.
  • compose.yml: add hydra + hydra-migrate services (Ory Hydra v2.2.0, ports 4444/4445).
  • pyproject.toml: add pyjwt[crypto]>=2.10.1.

Tests

  • New tests/api/users/test_authentication.py: for each of the 5 /users routes — 401 (no token, malformed token, expired token), 403 (read-only token on write route and vice versa), plus a 201 happy path with a valid token.
  • conftest.py: rsa_private_key, make_token (RS256 tokens with Hydra-like claims) and auth_headers fixtures; JWKS client is mocked, tests never call Hydra.
  • All existing /users tests updated to send valid Authorization headers.

⚠️ Before merging

uv.lock must be regenerated locally (uv lock) and pushed, since CI and the Dockerfile use uv sync --locked and pyjwt is a new dependency.

@HenriBlacksmith
HenriBlacksmith merged commit 510d24a into main Nov 17, 2026
2 checks passed
@HenriBlacksmith
HenriBlacksmith deleted the feat/jwt-authentication branch November 17, 2026 22:31
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