jwt API reference
JSON Web Tokens (RFC 7519): sign, verify, and decode compact JWTs. Claims are a json.Value object; the token is the usual header.payload.signature of base64url segments. Thirteen algorithms across five families: HMAC (HS256 / HS384 / HS512, over a shared secret), RSA PKCS#1 v1.5 (RS256 / RS384 / RS512), RSA-PSS (PS256 / PS384 / PS512), ECDSA (ES256 / ES384 / ES512), and Ed25519 (EdDSA). The key is always bytes: an HMAC secret (at least as long as the hash: 32 / 48 / 64 bytes, RFC 7518 3.2), a PEM-encoded RSA / EC key, or a raw Ed25519 key from crypto.keypair("ed25519").
verify takes the algorithm you expect and rejects a token whose header disagrees - this closes the classic JWT algorithm-confusion attack (a forged HS256 token verified with an RSA public key as the HMAC secret). It also enforces the exp (expiry) and nbf (not-before) time claims when present. Every rejection is an Error of kind "value".
RS* / PS* / ES* need the crypto library's RSA / ECDSA surface, which is on the default jennifer binary; HS* and EdDSA run on both binaries.
Import with import "jwt.j" as jwt;. See the jwt guide for prose and examples.
Contents
| Name | What it is |
|---|---|
decode | Decode a JWT's claims without verifying the signature or time claims - for inspecting a token you do no... |
header | Read a JWT's header (also without verifying) - useful for the alg and kid fields when selecting a verif... |
jwksKeys | Convert a JWKS once into the kid -> PEM map verifyWithKeys / verifyWithKeysWith take, keeping only th... |
sign | Sign claims into a compact JWT with the given algorithm. |
signWithKid | Sign claims like sign, adding a kid (key id) to the header so the verifier can pick the key:... |
verify | Verify a JWT and return its claims. |
verifyJwks | Verify a JWT against a JWKS (a JSON Web Key Set), selecting the key by the header's kid. |
verifyJwksWith | verifyJwks plus the claim policy verifyWith enforces. |
verifyLeeway | Verify a JWT exactly like verify, but widen the exp / nbf time-claim checks by leeway seconds on ea... |
verifyWith | Verify a JWT like verify, then additionally enforce the application-level claim policy: when policy.iss... |
verifyWithKeys | Verify a JWT, selecting the verification key by the header's kid (key id). |
verifyWithKeysWith | verifyWithKeys plus the claim policy verifyWith enforces: the key is chosen by the header's kid from ... |
Policy | struct: The application-level checks the ...With verifiers enforce on top of the cryptographic verification. |
Functions
decode
jwt.decode(token as string)Decode a JWT's claims without verifying the signature or time claims - for inspecting a token you do not trust yet (e.g. to read its kid / issuer before fetching a key). Never trust these claims for authorization; use verify for that.
token- the compact JWT
- Returns
json.Value - the payload claims, unverified
- Throws
Error - on a malformed token (including claims that are not a JSON object)
header
jwt.header(token as string)Read a JWT's header (also without verifying) - useful for the alg and kid fields when selecting a verification key.
token- the compact JWT
- Returns
json.Value - the token header
- Throws
Error - on a malformed token (including a header that is not a JSON object)
jwksKeys
jwt.jwksKeys(alg as string, jwksJson as string)Convert a JWKS once into the kid -> PEM map verifyWithKeys / verifyWithKeysWith take, keeping only the keys usable for alg: a key with a kid, a use of "sig" (or none), an alg of alg (or none), and the kty of its family. Fetch and convert the set when it changes, then verify each token against the map; this skips the per-token parse and conversion verifyJwks does. For RS* / PS* / ES* only, and needs the default jennifer binary, as verifyJwks.
alg- the algorithm the keys will verify (e.g. "RS256", "ES256")
jwksJson- the JWKS JSON (
{"keys":[{...}, ...]}) - Returns
map of string to string kid-> PEM public key, for the usable keys- Throws
Error - on an HMAC or EdDSA
alg, a set with nokeysarray, or a malformed usable key
sign
jwt.sign(alg as string, claims as json.Value, key as bytes)Sign claims into a compact JWT with the given algorithm. The header is {"alg": alg, "typ": "JWT"}; the payload is the encoded claims. key is the HMAC secret, a PEM RSA / EC private key, or an Ed25519 private key, by family.
alg- the JOSE algorithm (e.g. "HS256", "RS256", "ES256", "EdDSA")
claims- the claims object (the token payload)
key- the signing key (HMAC secret / PEM / Ed25519 private)
- Returns
string - the signed
header.payload.signaturetoken - Throws
Error - on an unsupported algorithm or a key the algorithm rejects
signWithKid
jwt.signWithKid(
alg as string,
claims as json.Value,
key as bytes,
kid as string)Sign claims like sign, adding a kid (key id) to the header so the verifier can pick the key: {"alg": alg, "kid": kid, "typ": "JWT"}. An empty kid is left out, which makes this the same as sign.
alg- the JOSE algorithm (e.g. "HS256", "RS256", "ES256", "EdDSA")
claims- the claims object (the token payload)
key- the signing key (HMAC secret / PEM / Ed25519 private)
kid- the key id for the header ("" for none)
- Returns
string - the signed
header.payload.signaturetoken - Throws
Error - on an unsupported algorithm or a key the algorithm rejects
verify
jwt.verify(alg as string, token as string, key as bytes)Verify a JWT and return its claims. Checks that the token's header algorithm equals alg (rejecting algorithm-confusion), that the signature is valid for key, and - when present - that the exp (expiry) and nbf (not-before) NumericDate claims allow the token now.
alg- the algorithm the caller requires (e.g. "HS256", "RS256")
token- the compact JWT
key- the verification key (HMAC secret / PEM public / Ed25519 public)
- Returns
json.Value - the verified claims
- Throws
Error - on a malformed token, an algorithm mismatch, a bad signature, or an expired / not-yet-valid token
verifyJwks
jwt.verifyJwks(alg as string, token as string, jwksJson as string)Verify a JWT against a JWKS (a JSON Web Key Set), selecting the key by the header's kid. Reads kid from the (unverified) header, finds the matching JWK in the set's keys array, converts it to a PEM public key with crypto.jwkToPem, and verifies. The JWK must be fit for the job: a use other than "sig", an alg other than the expected one, or a kty of the wrong family, or an EC key on another curve than the alg's, is refused (RFC 8725 3.1, RFC 7518 3.4). For RS* / PS* / ES* only: a JWKS carries public keys, so an HMAC alg is refused (use verifyWithKeys with the shared secret), and so is EdDSA (pass its key to verify).
alg is required and enforced against the header, exactly as verify / verifyWithKeys - selecting the key by kid does not select the algorithm, so the algorithm-confusion protection stays. This call parses the set and converts the key on every call; to verify many tokens against one set, convert it once with jwksKeys and use verifyWithKeys. Needs the default jennifer binary (crypto.jwkToPem uses Go's crypto/x509, which is not in the TinyGo build).
alg- the algorithm the caller requires (e.g. "RS256", "ES256")
token- the compact JWT
jwksJson- the JWKS JSON (
{"keys":[{...}, ...]}) - Returns
json.Value - the verified claims
- Throws
Error - on a missing / unknown
kid, an HMAC or EdDSAalg, an unusable or malformed JWK, or any verify failure
verifyJwksWith
jwt.verifyJwksWith(
alg as string,
token as string,
jwksJson as string,
policy as Policy)verifyJwks plus the claim policy verifyWith enforces. This is the form to use for an OIDC issuer's tokens (CI identity tokens, login ID tokens): the key comes from the issuer's JWKS by kid, then the Policy checks the issuer, the audience - without which a token minted for another service would be accepted by yours - and, with requireExp, that the token expires at all.
alg- the algorithm the caller requires (e.g. "RS256", "ES256")
token- the compact JWT
jwksJson- the JWKS JSON (
{"keys":[{...}, ...]}) policy- the expected issuer / audience, the
exprequirement, and the leeway - Returns
json.Value - the verified claims
- Throws
Error - on anything
verifyJwksrejects, a mismatched issuer / audience, or a missingexpthe policy requires
verifyLeeway
jwt.verifyLeeway(
alg as string,
token as string,
key as bytes,
leeway as int)Verify a JWT exactly like verify, but widen the exp / nbf time-claim checks by leeway seconds on each side, tolerating a small clock difference between the token's issuer and this verifier. A token is accepted while now < exp + leeway and now >= nbf - leeway; the signature, the header algorithm, and the crit check are enforced exactly as in verify. leeway must be 0..86400 (one day; a clock skew is seconds to minutes, and a larger window is a configuration error). verify(alg, token, key) is verifyLeeway(alg, token, key, 0).
alg- the algorithm the caller requires (e.g. "HS256", "RS256")
token- the compact JWT
key- the verification key (HMAC secret / PEM public / Ed25519 public)
leeway- clock-skew tolerance in seconds (0..86400)
- Returns
json.Value - the verified claims
- Throws
Error - kind "value" on a malformed token, an algorithm mismatch, a bad signature, an out-of-window token, or a leeway outside 0..86400
verifyWith
jwt.verifyWith(
alg as string,
token as string,
key as bytes,
policy as Policy)Verify a JWT like verify, then additionally enforce the application-level claim policy: when policy.iss is non-empty the token's iss must equal it, when policy.aud is non-empty the token's aud (a string or an array of strings) must include it, and when policy.requireExp is true the token must carry an exp claim. The signature is checked exactly as in verify, and exp / nbf as in verifyLeeway with policy.leeway; issuer, audience, and a mandatory expiry are application policy the library cannot assume, hence the explicit opt-in.
alg- the algorithm the caller requires (e.g. "HS256", "RS256")
token- the compact JWT
key- the verification key (HMAC secret / PEM public / Ed25519 public)
policy- the expected issuer / audience, the
exprequirement, and the leeway - Returns
json.Value - the verified claims
- Throws
Error - on any
verifyfailure, a mismatched issuer / audience, or a missingexpthe policy requires
verifyWithKeys
jwt.verifyWithKeys(
alg as string,
token as string,
keysByKid as map of string to string)Verify a JWT, selecting the verification key by the header's kid (key id). Reads kid from the (unverified) header, looks it up in keysByKid, and verifies with the matching value - a shared secret for HS*, or, for RS* / PS* / ES*, either a PEM public key or a JWK (a JSON object with a "kty" member, checked for its use / alg / kty as in verifyJwks and converted to a PEM via crypto.jwkToPem, which needs the default jennifer binary). A token with no kid header, or a kid absent from the map, is an Error of kind "value" (never a silent skip). The map value is text (decoded UTF-8), so it holds an HS* secret, PEM, or JWK - a raw binary Ed25519 key (from crypto.keypair("ed25519")) is not text-representable, so EdDSA is not supported on this kid-based path; pass its key to verify directly.
alg is still required and enforced against the header, exactly as in verify: selecting the key by kid does not select the algorithm, so this keeps the algorithm-confusion protection (an attacker cannot swap in HS256 and have your RSA public PEM read as an HMAC secret). Pin alg to what the issuer uses.
For a raw JWKS (a JSON set of {"kty","kid","n","e"|"x","y"} keys), use verifyJwks, which resolves the kid and converts the JWK to a key via crypto.jwkToPem - or convert the set once with jwksKeys and pass the result here, so a verifier handling many tokens does not re-parse the set per token. This keysByKid form is also the one for a kid -> secret (HMAC) map.
alg- the algorithm the caller requires (e.g. "HS256", "RS256")
token- the compact JWT
keysByKidkid-> HMAC secret, PEM key text, or a JWK (RS* / PS* / ES*)- Returns
json.Value - the verified claims
- Throws
Error - on a missing / unknown
kid, an unusable JWK, or anyverifyfailure
verifyWithKeysWith
jwt.verifyWithKeysWith(
alg as string,
token as string,
keysByKid as map of string to string,
policy as Policy)verifyWithKeys plus the claim policy verifyWith enforces: the key is chosen by the header's kid from keysByKid, the signature and exp / nbf are checked, then the Policy - a required exp, the expected issuer, the expected audience. Use this whenever the token comes from an issuer that serves more than one audience, or may omit exp.
alg- the algorithm the caller requires (e.g. "HS256", "RS256")
token- the compact JWT
keysByKidkid-> HMAC secret, PEM key text, or a JWK (RS* / PS* / ES*)policy- the expected issuer / audience, the
exprequirement, and the leeway - Returns
json.Value - the verified claims
- Throws
Error - on anything
verifyWithKeysrejects, a mismatched issuer / audience, or a missingexpthe policy requires
Structs
Policy
The application-level checks the ...With verifiers enforce on top of the cryptographic verification. exp is optional in RFC 7519, so plain verify accepts a token without one; requireExp: true refuses such a token, so a token minted by a bug or a leaked key cannot stay valid forever.
def struct Policy {
iss as string,
aud as string,
requireExp as bool,
leeway as int
};iss- the expected issuer ("" skips the check)
aud- the expected audience, in a string or array
aud("" skips the check) requireExp- refuse a token that has no
expclaim leeway- clock-skew seconds for
exp/nbf, asverifyLeeway(0..86400)