JSON Web Token (JWT) - RFC 7519
This module implements JSON Web Tokens as specified in RFC 7519.
JWTs are compact, URL-safe means of representing claims to be transferred between two parties. The claims are encoded as a JSON object that is used as the payload of a JSON Web Signature (JWS) structure, enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC).
References
- RFC 7519 - JSON Web Token (JWT)
- RFC 7515 - JSON Web Signature (JWS)
- RFC 7517 - JSON Web Key (JWK)
- RFC 7518 - JSON Web Algorithms (JWA)
Error Handling
type error = | Invalid_json of string(*JSON parsing failed
*)| Invalid_base64url of string(*Base64url decoding failed
*)| Invalid_structure of string(*Wrong number of parts or malformed structure
*)| Invalid_header of string(*Header validation failed
*)| Invalid_claims of string(*Claims validation failed
*)| Invalid_uri of string| Duplicate_claim of string| Unsupported_algorithm of string(*Unknown algorithm identifier
*)| Algorithm_not_allowed of string(*Algorithm rejected by allowed_algs policy
*)| Signature_mismatch(*Signature verification failed
*)| Token_expired| Token_not_yet_valid| Invalid_issuer| Invalid_audience| Key_type_mismatch of string(*Key doesn't match algorithm
*)| Unsecured_not_allowed| Nesting_too_deep(*Nested JWT exceeds max_depth
*)
val pp_error : Format.formatter -> error -> unitPretty-print an error.
val error_to_string : error -> stringConvert error to human-readable string.
Algorithms
Signature and MAC algorithms for JWT. See RFC 7518 Section 3.
module Algorithm : sig ... endJSON Web Key
Key representation for JWT signature verification. See RFC 7517.
module Jwk : sig ... endJOSE Header
The JOSE (JSON Object Signing and Encryption) Header. See RFC 7519 Section 5.
module Header : sig ... endClaims
JWT Claims Set. See RFC 7519 Section 4.
module Claims : sig ... endJWT Token
type t = {header : Header.t;(*JOSE header
*)claims : Claims.t;(*Claims set
*)signature : string;(*Raw signature bytes
*)raw : string;(*Original compact serialization
*)
}A parsed JWT token.
Parsing
See RFC 7519 Section 7.2.
parse ?strict token_string parses a JWT from its compact serialization.
This parses the token structure but does NOT verify the signature. Use verify to validate the signature after parsing.
parse_unsafe token_string parses a JWT without strict validation. Equivalent to parse ~strict:false.
Nested JWTs
See RFC 7519 Section 7.2 step 8 and Appendix A.2.
parse_nested ?strict ?max_depth token parses a potentially nested JWT. Returns a list of JWTs from outermost to innermost.
val is_nested : t -> boolis_nested t returns true if the JWT has cty: "JWT" header, indicating it contains a nested JWT.
Accessors
val signature : t -> stringsignature t returns the raw signature bytes.
val raw : t -> stringraw t returns the original token string.
Verification
See RFC 7519 Section 7.2.
val verify :
key:Jwk.t ->
?allow_none:bool ->
?allowed_algs:Algorithm.t list ->
t ->
(unit, error) resultverify ~key ?allow_none ?allowed_algs t verifies the JWT signature.
val validate :
now:Ptime.t ->
?iss:string ->
?aud:string ->
?leeway:Ptime.Span.t ->
t ->
(unit, error) resultvalidate ~now ?iss ?aud ?leeway t validates JWT claims.
val verify_and_validate :
key:Jwk.t ->
now:Ptime.t ->
?allow_none:bool ->
?allowed_algs:Algorithm.t list ->
?iss:string ->
?aud:string ->
?leeway:Ptime.Span.t ->
t ->
(unit, error) resultverify_and_validate ~key ~now ... verifies signature and validates claims.
Creation
See RFC 7519 Section 7.1.
create ~header ~claims ~key creates and signs a new JWT.
The key must be appropriate for the algorithm specified in header. For alg:none, pass any key (it will be ignored).
val encode : t -> stringencode t returns the compact serialization of the JWT.
Utilities
val is_expired : now:Ptime.t -> ?leeway:Ptime.Span.t -> t -> boolis_expired ~now ?leeway t checks if the token has expired. Returns false if no exp claim present.
val time_to_expiry : now:Ptime.t -> t -> Ptime.Span.t optiontime_to_expiry ~now t returns time until expiration, or None if no expiration claim or already expired.
Base64url Utilities
Exposed for testing with RFC test vectors.
Base64url encode without padding per RFC 7515 Appendix C.
CBOR Web Token (CWT)
See RFC 8392.
module Cwt : sig ... endCBOR Web Token (CWT) - RFC 8392