JWT
import "std/data" (included in import "std") · namespace std.data
JSON Web Tokens (RFC 7519) signed with HMAC (HS256/384/512), RSA (RS256/384/512), ECDSA (ES256/384/512) or Ed25519 (EdDSA), and key sets (JWKS) as identity providers publish them. The tokens interoperate with other implementations (checked against PyJWT both ways).
import "std"
fn main() {
let key = JwtKey.of(CryptoKey.generate("p256")).withKid("2026-11")
let claims = Json.object()
claims.set("sub", "42")
claims.set("aud", "app")
claims.set("exp", Time.now() + 3600)
let token = Jwt.sign(claims, key)
print(Jwt.decode(token).header.toJson())
// what a verifier holds: the public key set
let keys = Jwks.parse(new Jwks().add(JwtKey.fromJwk(key.toJwk())).toJson().toJson())
let opts = new JwtOptions()
opts.audience = "app"
print(Jwt.verifyWith(token, keys, opts).getString("sub", ""))
try {
Jwt.verify(token + "x", JwtKey.fromJwk(key.toJwk()))
} catch (e: JwtException) {
print(e.message())
}
}{"alg":"ES256","typ":"JWT","kid":"2026-11"}
42
the signature does not matchVerifying checks, in order: the header's algorithm is the key's own (so none, or HS256 presented to an RSA key, is refused), the signature, exp (required unless requireExpiry is off) and nbf with leewaySeconds of slack, then iss and aud when JwtOptions names them. Every failure is a JwtException. Jwks.parse reads a provider's jwks_uri document (fetch it with HttpClient); verifyWith picks the key by the token's kid.
class JwtException extends Exception
A token was malformed, its signature did not check, or a claim refused it.
Constructors
new JwtException(msg: string)
Methods
fn kind() -> string
class JwtKey
A key and the one algorithm it signs and verifies with.
Static members
static fn hmac(secret: Bytes, alg: string = "HS256") -> JwtKey— A shared secret (at least as long as the hash: 32 bytes for HS256).static fn of(key: CryptoKey, alg: string = "") -> JwtKey— An RSA, ECDSA or Ed25519 key (private to sign, public to verify). The algorithm follows from it (RS256, ES256/384/512 by curve, EdDSA) unless given.static fn fromJwk(jwk: JsonValue) -> JwtKey— A public key from a JWK (its "alg" and "kid" kept when present).static fn hashBytes(alg: string) -> int
Fields
alg: string— "HS256", "RS256", "ES256", "EdDSA", ...kid: string = ""— The key id ("" for none): written in the header, and how a key set finds the key.secret: Bytes = nullkey: CryptoKey = null
Methods
fn withKid(kid: string) -> JwtKeyfn toJwk() -> JsonValue— The public key as a JWK with its alg, kid and use ("sig").fn hashName() -> string
class Jwks
A set of keys (JWKS), as served at an issuer's jwks_uri.
Constructors
new Jwks()
Static members
static fn parse(text: string) -> Jwks— The keys of a JWKS document ({"keys": [...]}); ones that cannot be read are skipped.
Fields
keys: Array<JwtKey> = new Array<JwtKey>()
Methods
fn add(k: JwtKey) -> Jwksfn find(kid: string) -> JwtKey— The key with this id (or, without an id, the only key); null when none fits.fn toJson() -> JsonValue
class JwtOptions
What verification also requires.
Constructors
new JwtOptions()
Fields
leewaySeconds: long = 60— Seconds of clock difference forgiven for exp and nbf.issuer: string = ""— When set, the iss claim must be this.audience: string = ""— When set, the aud claim (a string or an array) must include this.requireExpiry: bool = true— Whether a token without exp is refused.
class JwtParts
A token's parts, read without checking anything (Jwt.decode).
Constructors
new JwtParts(header: JsonValue, claims: JsonValue, signed: string, signature: Bytes)
Fields
header: JsonValueclaims: JsonValuesigned: stringsignature: Bytes
class Jwt
Static members
static fn sign(claims: JsonValue, key: JwtKey) -> string—claimssigned withkey: header.payload.signature.static fn decode(token: string) -> JwtParts— The header and claims, unchecked (to see which key a token names; never trust them).static fn verify(token: string, key: JwtKey, options: JwtOptions = null) -> JsonValue— The claims of a token signed bykey; JwtException when anything is wrong.static fn verifyWith(token: string, keys: Jwks, options: JwtOptions = null) -> JsonValue— The claims of a token signed by one ofkeys(picked by the header's kid).