Skip to content
Jennifer Programming Language

jwt - JSON Web Tokens (RFC 7519)

Import with import "jwt.j" as jwt;. Sign, verify, and decode compact JWTs. Claims are a json.Value object; a token is the usual header.payload.signature of base64url segments.

Thirteen algorithms across five families:

FamilyAlgorithmsKey (bytes)crypto.sign name
HMACHS256 / HS384 / HS512a shared secret of at least 32 / 48 / 64 bytes- (hash.hmac)
RSARS256 / RS384 / RS512a PEM RSA key (PKCS#1 / PKCS#8 / PKIX)"rsa-pkcs1-sha256" ...
RSA-PSSPS256 / PS384 / PS512a PEM RSA key (PKCS#1 / PKCS#8 / PKIX)"rsa-pss-sha256" ...
ECDSAES256 / ES384 / ES512a PEM EC key (SEC1 / PKCS#8 / PKIX)"ecdsa-sha256" ...
EdDSAEdDSAa raw Ed25519 key, crypto.keypair("ed25519")"ed25519"

The key is always bytes: a secret, a PEM blob read from disk, or a raw Ed25519 key, depending on the algorithm family. Read a key file with fs.readSecretBytes, which refuses one that others could read or replace (see fs).

jennifer
use json;
use crypto;
import "jwt.j" as jwt;

def secret as bytes init crypto.randBytes(32);          # HS256 needs >= 32 bytes
def claims as json.Value init json.decode('{"sub":"ada","exp":9999999999}');

def token as string init jwt.sign("HS256", $claims, $secret);
def back as json.Value init jwt.verify("HS256", $token, $secret);   # throws if invalid

Runnable: examples/modules/jwt_demo.j.

Surface

CallReturnsNotes
jwt.sign(alg, claims, key)stringSign claims (a json.Value) into a compact JWT.
jwt.signWithKid(alg, claims, key, kid)stringLike sign, with a kid (key id) in the header so the verifier can select the key; an empty kid is the same as sign.
jwt.verify(alg, token, key)json.ValueVerify the signature, the header algorithm, and exp / nbf; return the claims. Throws on any failure.
jwt.verifyLeeway(alg, token, key, leeway)json.ValueLike verify, but widen the exp / nbf checks by leeway seconds each way for clock skew. verify is verifyLeeway(..., 0).
jwt.Policystruct{iss, aud, requireExp, leeway}: the expected issuer and audience ("" skips a check), whether exp is required, and the clock-skew leeway in seconds.
jwt.verifyWith(alg, token, key, policy)json.ValueLike verifyLeeway with policy.leeway, then additionally enforce the jwt.Policy claim checks (issuer / audience / required exp). Throws on any failure.
jwt.verifyWithKeys(alg, token, keysByKid)json.ValueSelect the verification key by the header's kid from a map of string to string (secret, PEM text, or a JWK), then verify.
jwt.verifyJwks(alg, token, jwksJson)json.ValueVerify against a JWKS ({"keys":[...]}): select the JWK by the header's kid, check it is meant for this alg, convert it with crypto.jwkToPem, then verify. RS* / PS* / ES* only; needs the default binary.
jwt.jwksKeys(alg, jwksJson)map of string to stringConvert a JWKS once into kid -> PEM for the keys usable with alg, for verifyWithKeys - no per-token parse. RS* / PS* / ES* only; needs the default binary.
jwt.verifyWithKeysWith(alg, token, keysByKid, policy)json.ValueverifyWithKeys, then the same jwt.Policy checks as verifyWith.
jwt.verifyJwksWith(alg, token, jwksJson, policy)json.ValueverifyJwks, then the same jwt.Policy checks as verifyWith - the form to use for an OIDC issuer's tokens.
jwt.decode(token)json.ValueThe payload claims without verifying - for inspection only.
jwt.header(token)json.ValueThe token header (its alg / kid), also without verifying.

Verifying safely

jwt.verify takes the algorithm you expect as its first argument:

  • Algorithm confusion is rejected. A token whose header alg does not equal the alg you pass is refused. This closes the classic attack where an attacker re-signs a token as HS256 using your RSA public key as the HMAC secret: you asked for RS256, so the HS256 token never gets that far. Never derive the verification algorithm from the token's own header.
  • "none" is not an algorithm. It is not in the supported set, so a token claiming alg: none cannot be verified (only decoded).
  • HMAC keys must be long enough. An HS256 / HS384 / HS512 key shorter than the hash output (32 / 48 / 64 bytes) is refused by sign and by every verifier, as RFC 7518 3.2 requires. A short secret is guessable, and an empty one - an unset environment variable, say - would let anyone forge tokens; this way a misconfigured service rejects every token instead.
  • Header and claims must be JSON objects. A token whose header or claims set is any other JSON value (an array, a string) is malformed, and sign refuses claims that are not an object.
  • Time claims are enforced. When present, exp (expiry) rejects an expired token and nbf (not-before) rejects one that is not yet valid, both against the current time. NumericDate claims are Unix seconds, an integer or a fractional number (1700000000.5); any other type is malformed.
  • Clock skew is a leeway. jwt.verifyLeeway($alg, $token, $key, $leeway) widens each time check by leeway seconds: a token is accepted while now < exp + leeway and now >= nbf - leeway. jwt.verify is verifyLeeway(..., 0); use a small leeway (30-60s is typical) only where clock drift is real. leeway must be 0..86400 (one day); anything else is an Error of kind "value". The ...With verifiers take it from jwt.Policy.leeway.
  • HMAC comparison is constant-time (hash.equal): a wrong MAC leaks nothing through timing.
  • Segments must be canonical base64url. A segment with stray = padding or non-zero trailing bits decodes to the same bytes as the canonical spelling - a second token string that verifies as the same token, which breaks anything keyed on the token string (replay caches, denylists). Such spellings are rejected as malformed, matching strict JWS implementations.
  • Every rejection is an Error of kind "value", whatever the cause - a bad signature, an expired token, a malformed segment, a member of the wrong type - so one catch that answers 401 covers them all.
  • Issuer / audience are application policy. jwt.verify checks the signature, algorithm, and time claims; it does not check iss or aud, because the expected values are the application's, not the library's. Use jwt.verifyWith($alg, $token, $key, jwt.Policy{iss: "https://issuer.example", aud: "my-api", requireExp: true, leeway: 30}) to additionally require them. An empty iss (or aud) skips that check; a non-empty one must match. aud may be a JSON string or an array of strings (RFC 7519), and the check passes when the expected audience is present.
  • A missing exp is accepted unless the policy requires it. RFC 7519 makes exp optional, so jwt.verify accepts a token that never expires. A service issuing short-lived tokens should set requireExp: true on its jwt.Policy: verifyWith then refuses a token without exp (a value error), so one minted by a bug or a leaked key cannot stay valid forever.

jwt.decode and jwt.header do not verify anything. Use them only to read a token you have not trusted yet (for example, to read kid before fetching the matching key). Never authorize on their output.

Building claims

Claims are a json.Value: decode a literal, or assemble one with the json write surface:

jennifer
use json;
use time;
use crypto;
import "jwt.j" as jwt;

def secret as bytes init crypto.randBytes(32);
def claims as json.Value init json.map();
$claims = json.set($claims, "/sub", "ada");
$claims = json.set($claims, "/role", "admin");
$claims = json.set($claims, "/exp", time.unix(time.now()) + 3600);   # one hour
def token as string init jwt.sign("HS256", $claims, $secret);

Keys by family

  • HS*: a bytes secret at least as long as the hash - 32 bytes for HS256, 48 for HS384, 64 for HS512; shorter keys are refused. Use a random one (crypto.randBytes(32)), not a password.
  • RS* / PS* / ES*: a PEM key as bytes. Signing needs the private key (read it with fs.readSecretBytes), verifying needs the public key (fs.readBytes). Standard PEM encodings are accepted (RSA PKCS#1 or PKCS#8; EC SEC1 or PKCS#8; public keys in PKIX / -----BEGIN PUBLIC KEY-----). An EC key must be on the curve of the algorithm (P-256 for ES256, P-384 for ES384, P-521 for ES512, RFC 7518 3.4), when signing and when verifying: crypto takes the curve from the key, so a P-384 key would otherwise sign and verify "ES256" tokens. Another curve, or a PEM that is no EC key, is an Error of kind "value".
  • EdDSA: the public / private bytes from crypto.keypair("ed25519").

jwt does not generate keys itself. Bring them from your identity provider or openssl, or make them with crypto.keypair: "rsa2048" / "p256" (and the other sizes and curves) return a crypto.Keypair whose private signs and whose public PEM verifies; "ed25519" returns the raw EdDSA pair.

Selecting a key by kid

When an issuer rotates keys, each token's header carries a kid (key id) naming which key signed it. jwt.verifyWithKeys reads that kid, looks it up in a caller-supplied map of string to string, and verifies with the matching value

  • an HMAC secret for HS*, or PEM key text or a JWK for RS* / PS* / ES* (each is decoded to bytes for you). A token with no kid, or a kid absent from the map, is an error.
jennifer
def keys as map of string to string init {
    "2024-key": "-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----\n",
    "2025-key": "-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----\n"
};
def claims as json.Value init jwt.verifyWithKeys("RS256", $token, $keys);

alg is still required and enforced against the header: selecting the key by kid does not select the algorithm, so the algorithm-confusion protection above stays in force. Pin alg to what the issuer uses.

For a raw JWKS endpoint ({"keys":[{"kty":..,"kid":..,"n":..,"e":..}, ...]}), jwt.verifyJwks finds the JWK whose kid matches the header, converts it to a PEM public key with crypto.jwkToPem, and verifies. It is for RS* / PS* / ES* only, since a JWKS carries public keys: for HMAC use verifyWithKeys with the shared secret, and for EdDSA pass the key to verify.

jennifer
def jwks as string init http.get("https://issuer.example.com/.well-known/jwks.json", {}).body;
def claims as json.Value init jwt.verifyJwks("RS256", $token, $jwks);

A JWK must be meant for the job. The selected key is refused when its use is not "sig", its alg is not the one you asked for, or its kty is the wrong family (RSA for RS* / PS*, EC for ES*) - an issuer binds each key to one algorithm (RFC 8725 3.1). An EC key must also be on the curve of the algorithm (crv P-256 for ES256, P-384 for ES384, P-521 for ES512, RFC 7518 3.4). Otherwise members the key does not carry are not checked. The same rule applies to a JWK held in a verifyWithKeys map.

Convert the set once when verifying many tokens. verifyJwks parses the JWKS and converts the key on every call. A service checking each request should convert the set when it fetches it, with jwt.jwksKeys, and verify against the resulting kid -> PEM map; keys unusable for alg are left out of the map:

jennifer
def keys as map of string to string init jwt.jwksKeys("RS256", $jwks);
def claims as json.Value init jwt.verifyWithKeys("RS256", $token, $keys);

Check the issuer and audience on key-set tokens. verifyWithKeys and verifyJwks verify the signature and the exp / nbf times, and nothing more: they accept a token with no exp (it never expires), and a token minted for any audience. A JWKS is what an OIDC issuer publishes - CI identity tokens, login ID tokens - and that issuer serves many relying parties, so without an audience check a token minted for another service replays against yours. Use the ...With forms, which apply the same jwt.Policy as verifyWith:

jennifer
def policy as jwt.Policy init jwt.Policy{
    iss: "https://token.actions.githubusercontent.com",
    aud: "https://registry.example.com",
    requireExp: true,
    leeway: 30
};
def claims as json.Value init jwt.verifyJwksWith("RS256", $token, $jwks, $policy);

JWT as web auth (jwt_auth)

There is no separate jwt_auth module; JWT authentication is this module used as a web before middleware. Register a guard that takes the bearer token (web.bearerToken), verifies it, and rejects on failure; it hands the verified subject to the route handler as a request-scoped note (httpd.setRequestValue, read back with httpd.requestValue):

jennifer
use encoding;
use os;
use json;
use httpd;
import "web.j" as web;
import "jwt.j" as jwt;

def const JWT_SECRET as bytes init encoding.encode("utf-8", os.getEnv("JWT_SECRET"));

func requireJwt(ctx as web.Context) {
    def token as string init web.bearerToken($ctx);
    if ($token == "") {
        web.respond($ctx, 401, "missing bearer token");
        return false;                         # stop the chain
    }
    try {
        def claims as json.Value init jwt.verify("HS256", $token, JWT_SECRET);
        httpd.setRequestValue($ctx.req, "user", json.asString($claims, "/sub"));
    } catch (e) {
        web.respond($ctx, 401, "invalid token");
        return false;
    }
    return true;                               # continue to the handler
}

If JWT_SECRET is unset or shorter than 32 bytes, every token is refused (the key-length check), so a missing secret fails closed instead of opening the API.

Platforms

HS* (over hash.hmac) and EdDSA (over crypto.sign / crypto.verify with "ed25519") run on both binaries. RS* / PS* / ES* need the crypto library's RSA / ECDSA surface, which is on the default jennifer binary; on jennifer-tiny they throw a "not available" error (the same split as net).

Academy talks

TalkWhat it shows
Crypto, tokens and certificates with Jennifersigning tokens and verifying them against a pinned algorithm policy

API reference

Every exported function, struct and constant of jwt, with its signature, generated from the module's doc comments: the jwt API reference.