Description
Add a JWT (JSON Web Token) authentication middleware plugin to secure backend APIs with token-based authentication.
Requirements
- Implement JWT plugin in
internal/plugins/jwt.go (single file, not a subdirectory)
- Support HS256, RS256, ES256 signing algorithms
- Validate token expiration, issuer, audience
- Extract claims and add to request context
- Support token from Authorization header or cookie
- Register plugin using
RegisterBuiltin() in init() function
Implementation Guide
File Structure
Create a single file: internal/plugins/jwt.go
Important: Do NOT create a subdirectory like internal/plugins/jwt/. All plugins are implemented as single .go files directly in the internal/plugins/ directory, following the same pattern as existing plugins:
internal/plugins/headers.go
internal/plugins/logging.go
internal/plugins/example_authentication.go
Plugin Registration Pattern
Your plugin must follow this pattern:
package plugins
import (
"net/http"
// ... other imports
)
func init() {
RegisterBuiltin("jwt", func(name string, cfg map[string]interface{}) (Middleware, error) {
// 1. Parse and validate configuration
secret, ok := cfg["secret"].(string)
if !ok || secret == "" {
return nil, fmt.Errorf("secret is required for jwt plugin")
}
// 2. Initialize JWT validator with config
// ... your JWT setup code
// 3. Return middleware function
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Your JWT validation logic here
// If valid: next.ServeHTTP(w, r)
// If invalid: http.Error(w, "Unauthorized", http.StatusUnauthorized)
})
}, nil
})
}
Configuration Example
plugins:
enabled: true
chain:
- name: jwt
config:
secret: "your-secret-key" # For HS256
algorithm: "HS256" # HS256, RS256, ES256
issuer: "helios"
audience: "api"
token_lookup: "header:Authorization,cookie:token"
claims_header: "X-JWT-Claims"
Acceptance Criteria
Reference Examples
Look at these existing plugins for implementation patterns:
internal/plugins/headers.go - Simple plugin with config parsing
internal/plugins/logging.go - Plugin with request/response handling
internal/plugins/example_authentication.go - Authentication pattern example
JWT Library
Use the official Go JWT library:
go get github.com/golang-jwt/jwt/v5
References
Description
Add a JWT (JSON Web Token) authentication middleware plugin to secure backend APIs with token-based authentication.
Requirements
internal/plugins/jwt.go(single file, not a subdirectory)RegisterBuiltin()ininit()functionImplementation Guide
File Structure
Create a single file:
internal/plugins/jwt.goImportant: Do NOT create a subdirectory like
internal/plugins/jwt/. All plugins are implemented as single.gofiles directly in theinternal/plugins/directory, following the same pattern as existing plugins:internal/plugins/headers.gointernal/plugins/logging.gointernal/plugins/example_authentication.goPlugin Registration Pattern
Your plugin must follow this pattern:
Configuration Example
Acceptance Criteria
internal/plugins/jwt.goRegisterBuiltin()ininit()functioninternal/plugins/jwt_test.godocs/plugin-development.mdReference Examples
Look at these existing plugins for implementation patterns:
internal/plugins/headers.go- Simple plugin with config parsinginternal/plugins/logging.go- Plugin with request/response handlinginternal/plugins/example_authentication.go- Authentication pattern exampleJWT Library
Use the official Go JWT library:
References
internal/plugins/