Skip to content

Implement EdDSA JWTs#12

Merged
markmnl merged 2 commits intomainfrom
jwt-EdDSA
Apr 27, 2026
Merged

Implement EdDSA JWTs#12
markmnl merged 2 commits intomainfrom
jwt-EdDSA

Conversation

@markmnl
Copy link
Copy Markdown
Owner

@markmnl markmnl commented Apr 27, 2026

Closes #11

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds production-grade JWT verification using EdDSA (Ed25519) with JWKS key discovery, while retaining an HMAC (HS256) fallback for development. This replaces the previous gin-jwt-based middleware with a custom Gin handler that performs explicit claim validation, IdP/JWKS verification, and (in EdDSA mode) in-process replay detection.

Changes:

  • Replace gin-jwt middleware with a custom JWT verifier supporting HS256 (dev) and EdDSA+JWKS (prod).
  • Add EdDSA-focused tests (issuer, kid, alg downgrade, expiry, replay) and HMAC regression tests.
  • Add runtime configuration for JWKS/issuer/audience selection and update documentation/dependencies accordingly.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/middleware/jwt.go New JWT middleware implementation supporting HS256 + EdDSA/JWKS, plus header parsing and error responses.
src/middleware/jwt_test.go New middleware-level tests for both modes, including replay and clock-skew scenarios.
src/middleware/jti_cache.go Adds an in-process JTI replay cache with periodic sweeping.
src/main.go Adds env-driven JWT mode selection and JWKS wiring via keyfunc.
src/go.mod Drops gin-jwt, upgrades to golang-jwt/jwt/v5, adds keyfunc/v3.
src/go.sum Dependency checksum updates consistent with module changes.
README.md Documents new JWT modes/env vars and token requirements.

Comment thread src/middleware/jwt.go Outdated
Comment thread README.md
### HMAC (development)

Active when `FMSG_JWT_JWKS_URL` is unset. Tokens must be HS256-signed with the
shared secret in `FMSG_API_JWT_SECRET`. Required claims are `sub` and `exp`;
Comment thread src/middleware/jwt.go
Comment thread src/middleware/jwt.go
Comment on lines +51 to +57
if len(c.entries) >= jtiCacheMaxEntries {
c.sweepLocked(now)
if len(c.entries) >= jtiCacheMaxEntries {
// Cache full of unexpired entries; refuse to grow but do not
// falsely flag the token as a replay.
return false
}
Comment thread README.md
fmsg IdP and signed with Ed25519. The JWKS endpoint is polled on a schedule;
the IdP can rotate keys by adding a new JWK with a fresh `kid`.

Required token header: `alg: EdDSA`, `kid: <known to JWKS>`, `typ: JWT`.
Comment on lines +73 to +80
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodGet, "/fmsg", nil)
if token != "" {
c.Request.Header.Set("Authorization", "Bearer "+token)
}
called := false
c.Set("__test_next__", &called)
mw(c)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@markmnl markmnl merged commit ac2eefb into main Apr 27, 2026
1 check passed
@markmnl markmnl deleted the jwt-EdDSA branch April 27, 2026 09:33
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.

JWT EdDSA Support

2 participants