@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.
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.
EdDsaJwtAlgorithm.Es256#
ECDSA over P-256 with SHA-256. Signing takes a PKCS#8 DER private key; verifying takes a PKIX DER public key.
Es256JwtAlgorithm.Hs256#
HMAC with SHA-256 and a shared secret of any length (use at least 32 random bytes).
Hs256JwtAlgorithm.Hs384#
HMAC with SHA-384 and a shared secret.
Hs384JwtAlgorithm.Hs512#
HMAC with SHA-512 and a shared secret.
Hs512JwtAlgorithm.Rs256#
RSASSA-PKCS1-v1_5 with SHA-256. Signing takes a PKCS#8 DER private key; verifying takes a PKIX DER public key.
Rs256JwtCheck Type#
What verify_token requires of a token beyond its signature.
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.
algorithm JwtAlgorithm = JwtAlgorithm.Hs256JwtCheck.at#
The Unix time in seconds to judge exp and nbf against; null uses the current
time.
at Int? = nullJwtCheck.audience#
When set, the aud claim, a string or a list of strings, must include it.
audience String? = nullJwtCheck.issuer#
When set, the iss claim must equal it.
issuer String? = nullJwtCheck.leeway#
Seconds of clock skew tolerated when checking exp and nbf.
leeway Int = 0JwtCheck.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.
max_bytes Int? = nullsign_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.
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)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.
fn verify_token[T Data = JsonValue](token String, key Bytes, check JwtCheck = JwtCheck()) Result[T, Error] { ... }