Jwt
Auth
JSON Web Tokens for HTTP APIs.
**HS256** (sign / signWithExp / verify) takes an HMAC secret string. **RS256** (signRs256 / signWithExpRs256 / verifyRs256) takes a PEM key (PKCS#1 or PKCS#8 private for signing; public or private PEM for verify).
Verification is fail-closed on the protected header alg: it must be absent or match the function (HS256 / RS256). Tokens with alg: none or any other algorithm do not verify. Prefer claims as a map that includes exp (Unix seconds). RealWorld clients send the token as Authorization: Token <jwt>.
Values
sign
sign : String -> a -> StringSign claims (any JSON-encodable value) as an HS256 JWT.
Claims should be a map and should include exp (Unix seconds). Prefer signWithExp when you want the runtime to set expiry.
Examples
Jwt.sign secret { sub = "42", exp = 1735689600 }signWithExp
signWithExp : String -> Int -> a -> StringSign map claims with exp set to now + ttlSeconds (HS256).
Examples
Jwt.signWithExp secret 3600 { sub = "42", role = "user" }verify
verify : String -> String -> Maybe aVerify an HS256 JWT and return its claims.
Returns Nothing when the secret is empty, the signature is wrong, the token is malformed, the header alg is present and not HS256 (including none), an exp claim (Unix seconds) is present and in the past, or an nbf claim is present and still in the future.
Examples
Jwt.verify secret tokensignRs256
signRs256 : String -> a -> StringSign claims as an RS256 JWT with an RSA private key PEM.
Examples
Jwt.signRs256 privatePem { sub = "42" }signWithExpRs256
signWithExpRs256 : String -> Int -> a -> StringSign map claims with exp set to now + ttlSeconds (RS256).
Examples
Jwt.signWithExpRs256 privatePem 3600 { sub = "42" }verifyRs256
verifyRs256 : String -> String -> Maybe aVerify an RS256 JWT with an RSA public (or private) key PEM.
Returns Nothing when the key is invalid, the signature is wrong, the token is malformed, the header alg is present and not RS256, or exp / nbf checks fail.
Examples
Jwt.verifyRs256 publicPem token