Webs_authenticatableAuthenticatable data.
This module defines a simple US-ASCII compatible encoding scheme to publish non-encrypted, expirable data bytes that can be authentified with a private key. Human readability and secrecy is a non-goal, storing state in non-trusted environments is.
The data is not encrypted.
Note. The encoding scheme and module is designed with extensibility in mind. But for now only an HMAC-SHA-256 based scheme is defined.
The type for some notion of time to expire data.
The semantics is left to the client. One can use a logical notion of time or the number of seconds since the Unix epoch.
module Private_key : sig ... endPrivate keys.
The type for authenticatable bytes. The encoding scheme for bytes data and an optional expiration timestamp expire and private key private_key is defined by:
exp = match expire with None -> "" | Some e -> string_of_int e
msg = exp ^ ":" ^ data
hs256 = "HS256:" ^ (hmac_sha_256 private_key msg)
auth = base64url_unpadded (hs256 ^ msg)with base64url_unpadded being base64url without padding.
val encode : private_key:Private_key.t -> expire:time option -> string -> tencode ~private_key ~expire data makes data data expire at expire (if any) and authenticatable via the private key and scheme defined by private_key.
type format_error = [ | `Base64url of Webs_base64.errorbase64url decode error.
| `Scheme of string optionScheme name, if one was found.
*) ]The type for decode format errors.
val format_error_message : format_error -> stringformat_error_message e is an english error message for e.
type error = [ | `AuthenticationAuthentication error.
*)| `Expired of timeExpiration time.
*)| `Missing_now_for of timeExpiration time.
*)| `Format of format_errorDecode format error
*) ]The type for decode and authentication errors. See decode.
val error_message : error -> stringerror_message e is an english error message for e.
val error_string : ('a, error) Stdlib.result -> ('a, string) Stdlib.resulterror_string r is Result.map_error error_message r.
val decode :
private_key:Private_key.t ->
now:time option ->
t ->
(time option * string, error) Stdlib.resultdecode ~private_key ~now s authenticates data s with the private key and scheme defined by private_key and expires it (if applicable) according to now. If now is None and s has an expiration timestamp, the result errors. More precisely the result is:
Ok (expire, data) with data the authenticated bytes and expire the expiration timestamp iff s is authenticated by private_key and either:
expire is Noneexpire is Some t and now is Some now with now < t.Error `Authentication if s cannot be authenticated by private_key.Error (`Expired t), if s is authenticated by private_key and expires at t but now is Some now with now >= t.Error (`Missing_now_for t), if s is authenticated by private_key and expires at t but now is None.Error (`Format _) if any other decoding error occurs.The type for untrusted decode results.
val untrusted_decode : t -> (untrusted, format_error) Stdlib.resultuntrusted_decode s decodes the encoding structure of s but neither authenticates nor expires the data.