Issues JWT tokens in an OIDC-compliant-ish way. As it name suggests this is for testing purposes only!
  • Go 85.9%
  • HTML 13.6%
  • Dockerfile 0.5%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Sophie 7c0dac9245
All checks were successful
Build Docker Image / build (push) Successful in 1m28s
Merge pull request 'feat: serve Prometheus metrics on a port of its own' (#12) from pixel/300-add-a-metrics-endpoint-on-a-port-of-its into main
Reviewed-on: #12
Reviewed-by: pixel-board <pixel-board@m.ac.nz>
2026-11-30 21:19:45 +11:00
.forgejo/workflows ci: also tag docker image with branch name (main) 2026-11-11 23:02:35 +11:00
config feat: serve Prometheus metrics on a port of its own 2026-11-30 21:03:50 +11:00
handlers feat: serve Prometheus metrics on a port of its own 2026-11-30 21:03:50 +11:00
jwks feat: add browser-based HTML frontend for token generation 2026-04-11 22:58:04 +10:00
keys initial commit 2026-04-11 22:14:51 +10:00
metrics feat: serve Prometheus metrics on a port of its own 2026-11-30 21:03:50 +11:00
.gitignore initial commit 2026-04-11 22:14:51 +10:00
Dockerfile chore(deps): update golang docker tag to v1.27 2026-10-01 13:02:55 +00:00
go.mod feat: serve Prometheus metrics on a port of its own 2026-11-30 21:03:50 +11:00
go.sum feat: serve Prometheus metrics on a port of its own 2026-11-30 21:03:50 +11:00
main.go feat: serve Prometheus metrics on a port of its own 2026-11-30 21:03:50 +11:00
main_test.go feat: serve Prometheus metrics on a port of its own 2026-11-30 21:03:50 +11:00
README.md feat: serve Prometheus metrics on a port of its own 2026-11-30 21:03:50 +11:00
renovate.json Update renovate.json 2026-08-08 10:55:08 +10:00

test-token-service

A local OIDC JWT token service for development and testing. Issues signed JWTs and exposes standard OIDC discovery endpoints so your app can validate tokens against a real JWKS.

Quick start

go run .

This generates fresh RSA-2048 and EC P-384 signing keys on startup and begins serving on port 8080.

Configuration

All options can be set via flags or environment variables.

Flag Env var Default Description
-port TOKEN_SERVICE_PORT 8080 Port to listen on
-issuer TOKEN_SERVICE_ISSUER http://localhost:<port> Issuer URL embedded in tokens
-default-ttl TOKEN_SERVICE_DEFAULT_TTL 3600 Default token lifetime (seconds)
-max-ttl TOKEN_SERVICE_MAX_TTL 86400 Maximum token lifetime (seconds)
-metrics-port TOKEN_SERVICE_METRICS_PORT 0 (off) Port to serve Prometheus metrics on

Endpoints

POST /token

Issues a signed JWT. Requires Content-Type: application/json.

Request body:

{
  "alg": "RS256",
  "sub": "user-123",
  "aud": "my-service",
  "expires_in": 3600,
  "claims": {
    "email": "user@example.com",
    "role": "admin"
  }
}
Field Type Required Description
alg string yes Signing algorithm: RS256 or ES384
sub string no Subject claim
aud string or array no Audience — single string or array of up to 10 strings
expires_in integer no Token lifetime in seconds (capped at max-ttl)
claims object no Additional claims to embed in the token

Response:

{
  "access_token": "<jwt>",
  "token_type": "Bearer",
  "expires_in": 3600
}

Constraints:

  • alg=none and all other algorithms are rejected
  • Reserved claims (iss, iat, exp, nbf, jti, sub, aud, alg, kid, typ) cannot appear in claims
  • claims object must be ≤ 4 KB when JSON-encoded
  • sub must be ≤ 255 characters
  • Each aud entry must be ≤ 255 characters; max 10 entries
  • Request body is capped at 64 KB

GET /.well-known/openid-configuration

Returns the OIDC discovery document pointing at this service.

GET /.well-known/jwks.json

Returns the public keys as a JSON Web Key Set. Use this URI when configuring your app's token verifier.

Example

# Issue a token
TOKEN=$(curl -s -X POST http://localhost:8080/token \
  -H 'Content-Type: application/json' \
  -d '{"alg":"RS256","sub":"alice","aud":"my-api","claims":{"role":"admin"}}' \
  | jq -r .access_token)

# Inspect the token (header + payload, no verification)
echo $TOKEN | cut -d. -f1,2 | tr '.' '\n' | base64 -d 2>/dev/null | jq

Metrics

Prometheus metrics are off by default. Set a metrics port to serve them at GET /metrics on that port. They are never served on the main port, and the metrics port serves nothing else. The service refuses to start if the metrics port can't be bound or equals the main port.

TOKEN_SERVICE_METRICS_PORT=9090 go run .
curl localhost:9090/metrics
Metric Labels Description
test_token_service_tokens_issued_total alg: RS256, ES384 Tokens issued
test_token_service_token_requests_refused_total reason: content_type, body, alg, sub, aud, expires_in, claims, server_error POST /token requests answered with an error
test_token_service_jwks_requests_total Fetches of /.well-known/jwks.json
test_token_service_discovery_requests_total Fetches of /.well-known/openid-configuration

body covers invalid JSON and bodies over 64 KB; server_error means signing failed. All label values are reported from startup, at 0. The standard Go runtime (go_*) and process (process_*) metrics are included too.

Docker

Build and run standalone:

docker build -t test-token-service .
docker run --rm -p 8080:8080 test-token-service

To use in another project's docker-compose.yml:

services:
  token-service:
    build:
      context: ../test-token-service   # adjust path as needed
    ports:
      - "8080:8080"
    environment:
      TOKEN_SERVICE_ISSUER: http://token-service:8080

Set TOKEN_SERVICE_ISSUER to the container's service name so tokens embed the correct iss claim — your app's JWKS verifier needs to reach that same URL to fetch /.well-known/jwks.json.

Development

go test ./...

Keys are ephemeral — regenerated on every restart. This service is intended for local development only and should not be used in production.