package jws
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=eea7319ae84c3649b4c72cd59f6308fa62f23bb51b41efb8af4a3a097dd71c3d
sha512=d27b80a04bd67641ca6ceff37d145491c6783cf1b6edac6f04dbc3bce1081e9378d8f07bc7412eb136e788c8ecdc2fa0a2f27e259c32f94fe90eb0aa88a5fb0f
doc/jws.jwt/Jwt/index.html
Module JwtSource
JWT - JSON Web Token (RFC 7519)
A thin layer on top of Jws Compact Serialization that interprets the payload as a JSON claims set.
Encoding a JWT
let claims =
Jwt.Claims.empty
|> Jwt.Claims.sub "1234567890"
|> Jwt.Claims.iss "https://example.com"
|> Jwt.Claims.iat 1516239022.
|> Jwt.Claims.add "admin" Jsont.bool true
in
let token = Jwt.encode pk claimsDecoding and validating a JWT
let now = Unix.gettimeofday () in
match Jwt.decode ~now ~aud:"https://api.example.com" ~public token with
| Ok jwt ->
let sub = Jwt.sub jwt in
let admin = Jwt.claim jwt ~key:"admin" Jsont.bool in
...
| Error (`Msg e) -> ...Claims
Claims are built as Jsont.json string maps, the same representation used for protected header members in Jws. The Claims module provides helpers for the registered claim names defined by RFC 7519, 4.1.
Decoded JWT values
A decoded and verified JWT.
header jwt is the underlying JWS value. Use Jws.value to read header fields such as "kid".
aud jwt is the "aud" claim, normalized to a list (a single-string audience is returned as a singleton list).
value jwt ~key codec reads a custom claim via a Jsont.t codec. Returns None when the claim is absent or cannot be decoded.
Encoding
encode pk claims produces a signed JWT in Compact Serialization. The algorithm is derived from pk and a "typ":"JWT" header is added.
Decoding
val decode :
?now:float ->
?aud:string ->
?public:Jws.Jwk.t ->
string ->
(t, [> `Msg of string ]) resultdecode ?now ?aud ?public token decodes and verifies a JWT.
- If
nowis provided, the"exp"and"nbf"claims are validated against it. Ifnowis omitted, time-based validation is skipped. - If
audis provided, the"aud"claim must be present and containaud. publicis the verification key (seeJws.Compact.decode).