How can i use jwto?

Hi. I am really OCaml beginner and i try to use Jwto.

I read and try it.
But it does not work…
I want to simply encode and decode.

(* Encode *)
utop # let payload=[("user","koji")];;
val payload : (string * string) list = [("user", "koji")]  

utop # let encoded = Jwto.encode Jwto.HS256 "secret" payload;;
val encoded : (string, string) result =

(* Decode *)
utop # let decoded = Jwto.decode "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoia29qaSJ9.iTB59lHphOi66_84L_oinHlUm95nwOJ-NJuth8MBE3c";;
val decoded : (Jwto.t, string) result = Ok <abstr>

utop # let Ok decoded = Jwto.decode "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoia29qaSJ9.iTB59lHphOi66_84L_oinHlUm95nwOJ-NJuth8MBE3c";;
val decoded : Jwto.t = <abstr>

Above codes seems to work correctly.
And “decoded”'s type is “Jwto.t”.

this is declaration of “Jwto.t” from

type t =
    header: header; 
    payload: payload; 
    signature: string;
  } [@@deriving show, eq]

From the my current understanding of Ocaml, i think this is a “record”.
Therefore i try to access to “signature” attribute.
But somehow this “decoded” variable does’nt seem to have the attribute…

utop # decoded.signature;;
Line 1, characters 8-17:
Error: Unbound record field signature

Why “decode” has not the attribute?
I still can’t tell if this is a Jwto problem or an OCaml problem.
OCaml version is 4.09.0, utop is 2.4.3.

It looks like the type Jwto.t is abstract in the signature, so you cannot access the record fields: rather than looking at the ml file, you should look at the mli file.

1 Like

I made this lib because I couldn’t find a decent JWT lib when experimenting with oCaml and as learning exercise. I’m not surprised if this is a problem with the lib. PRs are very welcome. Thanks

1 Like

I did not right understand OCaml specification…(abstract, mli etc)
But! I know now what i have to learn into!
And i was complicating this problem.
This is JWT. so i can split the JWT token with “.” and can decode each splited tokens with “Base64.decode”.
Your answer was really helpful.
Thank you!

Your Jwto library is really great!
And this is good a material to learn the OCaml code for me. :slight_smile:
Thank you for your effort!

By the way, maybe i found a problem by “is_valid” function.
I try to solve this and create PR.
But my OCaml skill is not yet so goold…
I am going to write about that on Issue by Github for now.