veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

auth/jwt

lib/auth/jwt.vk

lib/auth/jwt.vk Pure-Veko RFC 7519 / RFC 7515 JSON Web Tokens (JWT / JWS). Supports HS256 (HMAC-SHA256), EdDSA (RFC 8032 Ed25519), and ML-DSA-65 (FIPS 204 post-quantum signatures), plus exp/nbf/iss/aud claims validation.

JwtToken struct
signature
JwtToken = struct { alg str, header map, claims map, signing_input str, signature bytes }

Decoded JWT components returned by decode-unverified and verify-*.

ClaimOpts struct
signature
ClaimOpts = struct { now_sec i64 = ..., leeway_sec i64 = ..., issuer str = ..., audience str = ... }

Options for validate-claims. Zero/empty values skip the corresponding check.

signature
decode-unverified = fn [token str] JwtToken

Decodes a compact JWS string (header.payload.signature) without verifying the signature.

Executable Example
veko
use [jwt = auth/jwt]
tok = jwt/sign-hs256 (bytes/from-str 'super-secret-key-32bytes-minimum!') { sub = 'alice', role = 'admin' }
dec = jwt/decode-unverified tok
if dec.alg != 'HS256' or dec.claims.sub != 'alice' [ err 'jwt decode' ] else [ .ok ]
signature
sign-hs256 = fn [key bytes, claims map] str

Signs claims using HS256 (HMAC-SHA256).

Executable Example
veko
use [jwt = auth/jwt]
key = bytes/from-str 'test-hmac-key-0123456789abcdef01'
tok = jwt/sign-hs256 key { sub = 'u-42', iss = 'veko.dev' }
ver = jwt/verify-hs256 tok key
if ver.claims.sub != 'u-42' [ err 'hs256 verify' ] else [ .ok ]
signature
verify-hs256 = fn [token str, key bytes] JwtToken

Verifies an HS256 JWT using constant-time MAC comparison.

signature
sign-eddsa = fn [seed32 bytes, claims map] str

Signs claims using EdDSA (RFC 8032 Ed25519) with a 32-byte seed.

Executable Example
veko
use [jwt = auth/jwt, x25 = crypto/x25519]
seed = bytes/from-str '0123456789abcdef0123456789abcdef'
pk = x25/ed25519-pubkey seed
tok = jwt/sign-eddsa seed { sub = 'ed-user' }
ver = jwt/verify-eddsa tok pk
if ver.claims.sub != 'ed-user' [ err 'eddsa verify' ] else [ .ok ]
signature
verify-eddsa = fn [token str, pubkey32 bytes] JwtToken

Verifies an EdDSA (Ed25519) JWT against a 32-byte public key.

signature
sign-mldsa65 = fn [sk bytes, claims map] str

Signs claims using post-quantum ML-DSA-65 (FIPS 204).

signature
verify-mldsa65 = fn [token str, pk bytes] JwtToken

Verifies a post-quantum ML-DSA-65 JWT against an ML-DSA-65 public key.

validate-claims fn caps: .clocks
signature
validate-claims = fn [claims map, opts ClaimOpts] enum

Validates standard JWT claims (exp, nbf, iss, aud). If opts.now_sec <= 0, queries live UTC wall-clock time via time/unix.

Executable Example
veko
use [jwt = auth/jwt]
r = jwt/validate-claims { iss = 'veko', aud = 'api', nbf = 1000, exp = 2000 } (jwt/ClaimOpts { now_sec = 1500, issuer = 'veko', audience = 'api' })
if err? r [ err 'claims valid' ] else [ .ok ]
signature
parse-bearer = fn [auth_header str] str

Extracts the bearer token from an HTTP Authorization header value ('Bearer <token>').