Manuscript Documentation
v2026.12.0

@std/data/jwt.ms#

Import each required symbol with import Name from "@std/data/jwt.ms".

JwtAlgorithm Enum#

Selects how a token is signed. A verifier names the one it accepts; it never trusts the algorithm a token claims.

Manuscript
enum JwtAlgorithm {
    Hs256
    Hs384
    Hs512
    Rs256
    Es256
    EdDsa
}

JwtAlgorithm.EdDsa#

Ed25519. Signing takes the 32-byte private seed; verifying takes the 32-byte public key.

Manuscript
EdDsa

JwtAlgorithm.Es256#

ECDSA over P-256 with SHA-256. Signing takes a PKCS#8 DER private key; verifying takes a PKIX DER public key.

Manuscript
Es256

JwtAlgorithm.Hs256#

HMAC with SHA-256 and a shared secret of any length (use at least 32 random bytes).

Manuscript
Hs256

JwtAlgorithm.Hs384#

HMAC with SHA-384 and a shared secret.

Manuscript
Hs384

JwtAlgorithm.Hs512#

HMAC with SHA-512 and a shared secret.

Manuscript
Hs512

JwtAlgorithm.Rs256#

RSASSA-PKCS1-v1_5 with SHA-256. Signing takes a PKCS#8 DER private key; verifying takes a PKIX DER public key.

Manuscript
Rs256

JwtCheck Type#

What verify_token requires of a token beyond its signature.

Manuscript
type JwtCheck {
    algorithm JwtAlgorithm = JwtAlgorithm.Hs256
    issuer String? = null
    audience String? = null
    leeway Int = 0
    at Int? = null
    max_bytes Int? = null
}

JwtCheck.algorithm#

The only algorithm accepted; a token whose header names another returns an Error with kind EBADMSG.

Manuscript
algorithm JwtAlgorithm = JwtAlgorithm.Hs256

JwtCheck.at#

The Unix time in seconds to judge exp and nbf against; null uses the current time.

Manuscript
at Int? = null

JwtCheck.audience#

When set, the aud claim, a string or a list of strings, must include it.

Manuscript
audience String? = null

JwtCheck.issuer#

When set, the iss claim must equal it.

Manuscript
issuer String? = null

JwtCheck.leeway#

Seconds of clock skew tolerated when checking exp and nbf.

Manuscript
leeway Int = 0

JwtCheck.max_bytes#

Optional maximum token length in bytes accepted before any decoding; when set, larger tokens return an Error with kind EBADMSG. null imposes no limit.

Manuscript
max_bytes Int? = null

sign_token Function#

Creates a signed token from claims, a record or JsonValue object such as Session(sub: "42", exp: 1900000000). The result is the compact header.claims.signature form, ready for an Authorization: Bearer header or a cookie. key is the secret for the HMAC algorithms and the private key for the others, in the form JwtAlgorithm describes. Registered claims (exp, iat, iss, aud) are ordinary fields; the library adds none, so set exp yourself. A key that does not suit the algorithm, or claims that cannot be encoded as JSON (a NumberValue whose text is not a JSON number, say), return an Error with kind EINVAL; the message says which.

Manuscript
import Bytes from "@std/bytes.ms"
import sign_token, verify_token from "@std/data/jwt.ms"

type Session {
    sub String
    exp Int
}

token := sign_token(Session(sub: "42", exp: 1900000000), secret)?
session := verify_token[Session](token, secret)?
print(session.sub)
Manuscript
fn sign_token[T Data](claims T, key Bytes, algorithm JwtAlgorithm = JwtAlgorithm.Hs256) Result[String, Error] { ... }

verify_token Function#

Checks a token’s signature and claims, and returns its claims decoded as T (a record, or JsonValue for the raw object). The signature is verified before the claims are read. check.algorithm is the only algorithm accepted, so a token cannot choose a weaker one or null. The payload must be a JSON object; any other JSON value returns EBADMSG. Critical header extensions and unencoded payloads are unsupported and rejected. When present, exp and nbf (seconds since the Unix epoch, fractions allowed) are compared exactly, allowing check.leeway whole seconds of clock skew. iss and aud are enforced only when check names them. key is the secret for the HMAC algorithms and the public key for the others, in the form JwtAlgorithm describes. A failure returns an Error that says which check failed. Show callers one generic message, because the specific cause helps an attacker.

Manuscript
fn verify_token[T Data = JsonValue](token String, key Bytes, check JwtCheck = JwtCheck()) Result[T, Error] { ... }