JWT signing

HS256/RS256/ES256, key management, crypto helpers.

JWT Signing & Key Verification

OneAuth supports three JWT signing algorithms. Each can be used per-app in the same deployment — HS256, RS256, and ES256 apps coexist in a shared KeyStore.

Algorithm Overview

Algorithm Type Key Type When to Use
HS256 Symmetric (HMAC-SHA256) Shared []byte secret Simple setups where all parties are trusted. App and resource server share the same secret.
RS256 Asymmetric (RSA-SHA256) *rsa.PrivateKey / *rsa.PublicKey Federated auth where apps keep private keys secret. Resource servers only get the public key. Wider ecosystem compatibility.
ES256 Asymmetric (ECDSA-P256-SHA256) *ecdsa.PrivateKey / *ecdsa.PublicKey Same as RS256 but with smaller keys and faster signatures. Preferred for new deployments.

When to Use Asymmetric (RS256/ES256)

  • Resource servers should not be able to forge tokens
  • Multiple resource servers validate tokens from the same app
  • You want to rotate keys without coordinating secret distribution
  • Compliance requires separation of signing and verification

When HS256 Is Fine

  • Single app + single resource server, both fully trusted
  • Internal services behind a VPN
  • Simplest possible setup (no key pair management)

Key Generation

Using Go (utils/crypto_helpers.go)

import "github.com/panyam/oneauth/utils"

// RSA (2048-bit minimum, 4096 for high security)
privPEM, pubPEM, err := utils.GenerateRSAKeyPair(2048)

// ECDSA P-256 (smaller keys, faster)
privPEM, pubPEM, err := utils.GenerateECDSAKeyPair()

Using OpenSSL

# RSA
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out app-private.pem
openssl pkey -in app-private.pem -pubout -out app-public.pem

# ECDSA P-256
openssl ecparam -genkey -name prime256v1 -noout -out app-private.pem
openssl pkey -in app-private.pem -pubout -out app-public.pem

PEM Encode/Decode

All key material is stored and transmitted as PEM-encoded bytes. The utils package provides round-trip helpers:

// Parse PEM → crypto key
privKey, err := utils.ParsePrivateKeyPEM(privPEM)  // → *rsa.PrivateKey or *ecdsa.PrivateKey
pubKey, err := utils.ParsePublicKeyPEM(pubPEM)      // → *rsa.PublicKey or *ecdsa.PublicKey

// Encode crypto key → PEM
privPEM = utils.EncodePrivateKeyPEM(privKey)         // PKCS8 format
pubPEM, err = utils.EncodePublicKeyPEM(pubKey)       // PKIX format

Minting Tokens

MintResourceToken is the single entry point for all three algorithms. The signing alg is auto-detected from the Go type of signingKey — no separate alg parameter needed. customClaims ride alongside the standard JWT claims; collisions with the standard set are logged and dropped.

HS256 (symmetric)

token, err := oneauth.MintResourceToken(
    "user-42", "app_abc123",
    []byte("shared-secret"),       // []byte → HS256
    map[string]any{"max_rooms": 10},
    []string{"relay:connect"},
    nil,                            // RFC 9396 authorization_details
)

RS256/ES256 (asymmetric)

privKey, _ := utils.ParsePrivateKeyPEM(privPEM)

token, err := oneauth.MintResourceToken(
    "user-42", "app_abc123",
    privKey,                       // *rsa.PrivateKey → RS256, *ecdsa.PrivateKey → ES256
    map[string]any{"max_rooms": 10},
    []string{"relay:connect"},
    nil,
)

APIAuth Configuration

Symmetric (HS256)

auth := &oneauth.APIAuth{
    JWTSecretKey: "my-secret",  // shared secret
    JWTIssuer:    "myapp",
}

Asymmetric (RS256/ES256)

auth := &oneauth.APIAuth{
    JWTSigningAlg: "RS256",
    JWTSigningKey: privKey,    // *rsa.PrivateKey for signing
    JWTVerifyKey:  pubKey,     // *rsa.PublicKey for verification
    JWTIssuer:     "myapp",
}

When JWTSigningKey/JWTVerifyKey are set, they take precedence over JWTSecretKey. The jwtKeyFunc validates that the token’s algorithm matches the key type, preventing algorithm confusion.

App Registration

Symmetric (HS256) — default

curl -X POST https://auth.example.com/apps/dcr \
  -H "X-Admin-Key: admin-key" \
  -H "Content-Type: application/json" \
  -d '{"client_name": "MyApp", "client_uri": "https://myapp.com", "signing_alg": "HS256"}'

Response includes client_secret (shown only once).

Asymmetric (RS256/ES256)

curl -X POST https://auth.example.com/apps/dcr \
  -H "X-Admin-Key: admin-key" \
  -H "Content-Type: application/json" \
  -d '{
    "client_name": "MyApp",
    "client_uri": "https://myapp.com",
    "token_endpoint_auth_method": "private_key_jwt",
    "jwks": {"keys": [{"kty": "RSA", "alg": "RS256", "n": "...", "e": "AQAB"}]}
  }'

Response omits client_secret — the app keeps its private key.

Key Rotation

Symmetric: POST /apps/{client_id}/rotate — generates a new secret, returns it.

Asymmetric: POST /apps/{client_id}/rotate with {"public_key": "..."} — replaces the stored public key.

KeyStore & DecodeVerifyKey

All KeyStore backends (InMemory, FS, GORM, GAE) store key material as []byte:

  • HS256: raw shared secret bytes
  • RS256/ES256: PEM-encoded public key bytes

No storage schema changes are needed for asymmetric keys.

At validation time, utils.DecodeVerifyKey(rawKey, alg) converts stored bytes to the appropriate type:

// In APIMiddleware.validateJWT (automatic):
rec, _ := m.KeyStore.GetKey(clientID)  // returns *KeyRecord
verifyKey, _ := utils.DecodeVerifyKey(rec.Key, rec.Algorithm)
// HS256 → returns []byte as-is
// RS256 → parses PEM → *rsa.PublicKey
// ES256 → parses PEM → *ecdsa.PublicKey

Algorithm Confusion Prevention

OneAuth prevents algorithm confusion attacks at multiple levels:

  1. KeyRecord.Algorithm: Every client has a registered algorithm (stored in KeyRecord). The middleware rejects tokens whose alg header doesn’t match.

  2. APIAuth.jwtKeyFunc(): Validates that the token’s signing method matches the configured key type (RSA method for RSA key, ECDSA method for ECDSA key, HMAC method for symmetric key).

  3. DecodeVerifyKey type checking: RS256 keys must parse to *rsa.PublicKey, ES256 to *ecdsa.PublicKey. A PEM containing the wrong key type is rejected.

This means an attacker cannot:

  • Send alg: none to skip verification
  • Send alg: HS256 with a public key as the HMAC secret
  • Send alg: RS256 for an app registered with HS256

Key ID (kid)

All minted JWTs now include a kid (Key ID) header, computed as an RFC 7638 JWK Thumbprint of the signing key. This enables resource servers to look up the correct verification key without parsing unverified claims first.

How It Works

  1. At mint time: MintResourceToken computes the JWK Thumbprint (SHA-256, base64url-encoded) of the signing key and sets it as the JWT kid header.
  2. At validation time: The middleware reads the kid header and calls KeyLookup.GetKeyByKid(kid) to find the matching key record.
  3. Thumbprint computation: Uses utils.JWKThumbprint(key) which implements RFC 7638 — canonical JSON serialization of the JWK’s required members, then SHA-256 hash.

KidStore

KidStore maintains a mapping from kid (thumbprint) to clientID. This supports key rotation grace periods where both old and new keys are valid simultaneously:

type KidStore interface {
    GetClientIDByKid(kid string) (string, error)
    SetKidMapping(kid, clientID string) error
    DeleteKidMapping(kid string) error
}

CompositeKeyLookup

CompositeKeyLookup combines multiple KeyLookup sources (e.g., a local KeyStorage and a JWKSKeyStore) into a single lookup that tries each source in order. This is useful for resource servers that need to validate tokens from both local and federated issuers:

lookup := oa.NewCompositeKeyLookup(localKeyStore, jwksKeyStore)
middleware := &oa.APIMiddleware{
    KeyStore: lookup,
}

SigningMethodForAlg / IsAsymmetricAlg

utils.SigningMethodForAlg("RS256")    // → jwt.SigningMethodRS256
utils.SigningMethodForAlg("ES256")    // → jwt.SigningMethodES256
utils.SigningMethodForAlg("HS256")    // → jwt.SigningMethodHS256 (default)

utils.IsAsymmetricAlg("RS256")       // → true
utils.IsAsymmetricAlg("ES256")       // → true
utils.IsAsymmetricAlg("HS256")       // → false

JWKS (JSON Web Key Set)

OneAuth supports standard JWKS (RFC 7517) for public key discovery. The auth server exposes asymmetric keys at /.well-known/jwks.json, and resource servers can fetch them via JWKSKeyStore.

JWK Conversion (utils/jwk.go)

import "github.com/panyam/oneauth/utils"

// Convert public key → JWK
jwk, err := utils.PublicKeyToJWK("app_abc", "RS256", rsaPublicKey)

// Convert JWK → public key
pubKey, alg, err := utils.JWKToPublicKey(jwk)

Supported key types: RSA (kty: "RSA") and ECDSA P-256 (kty: "EC", crv: "P-256").

Serving JWKS (JWKSHandler)

handler := &oa.JWKSHandler{
    KeyStore:    keyStore,  // KeyStorage (needs ListKeyIDs)
    CacheMaxAge: 3600,      // Cache-Control max-age (default: 3600)
}
mux.HandleFunc("GET /.well-known/jwks.json", handler.ServeHTTP)

Only asymmetric keys (RS256/ES256) are included. HS256 secrets are never exposed.

Fetching JWKS (JWKSKeyStore)

ks := oa.NewJWKSKeyStore("https://auth.example.com/.well-known/jwks.json",
    oa.WithRefreshInterval(30 * time.Minute),
)
ks.Start()
defer ks.Stop()

// Use as a read-only KeyLookup
rec, _ := ks.GetKey("app_abc")       // → *KeyRecord with Key and Algorithm
rec, _ = ks.GetKeyByKid(kid)          // → lookup by kid header from JWT

See FEDERATED_AUTH.md for the full JWKS guide.

Encryption at Rest (EncryptedKeyStorage)

HS256 client_secret values are stored in the KeyStore as raw []byte. To protect them at rest (e.g., against database dumps), wrap any KeyStorage with EncryptedKeyStorage:

import oa "github.com/panyam/oneauth"

inner := gormstore.NewKeyStore(db)
encrypted, err := oa.NewEncryptedKeyStorage(inner, os.Getenv("ONEAUTH_MASTER_KEY"))

Master Key

  • 64-character hex string (32 bytes): openssl rand -hex 32
  • Set via ONEAUTH_MASTER_KEY env var or keystore.master_key in YAML config
  • Never stored alongside the data it protects
  • The raw master key is not used directly — HKDF-SHA256 derives an encryption-specific key with info string "oneauth-keystore-encryption-v1"

What Gets Encrypted

Algorithm Key Type Encrypted?
HS256/HS384/HS512 Shared []byte secret Yes (AES-256-GCM)
RS256 []byte PEM (public key) No (not sensitive)
ES256 []byte PEM (public key) No (not sensitive)

Migration

If you enable encryption on an existing deployment, existing plaintext secrets remain readable. The wrapper attempts GCM decryption on read; if it fails, it returns the raw bytes as plaintext. New writes are always encrypted.

Reference Server Configuration

keystore:
  type: "gorm"
  master_key: "${ONEAUTH_MASTER_KEY}"
  gorm:
    driver: "postgres"
    dsn: "${DATABASE_URL}"

Or via environment variable (e.g., GAE):

export ONEAUTH_MASTER_KEY="$(openssl rand -hex 32)"

Shared KeyStore Requirement

When resource servers share the same KeyStore database as the auth server, they must also have the same ONEAUTH_MASTER_KEY to decrypt HS256 secrets. Resource servers using JWKS discovery are unaffected (JWKS only serves asymmetric keys).

See Also

  • FEDERATED_AUTH.md — end-to-end federated auth flow with registration, minting, and validation
  • API_AUTH.md — APIAuth and APIMiddleware configuration
  • STORES.md — KeyLookup and KeyStorage interface details