Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sporto/jwto
JWTs for OCaml
https://github.com/sporto/jwto
ocaml
Last synced: about 1 month ago
JSON representation
JWTs for OCaml
- Host: GitHub
- URL: https://github.com/sporto/jwto
- Owner: sporto
- License: mit
- Created: 2018-11-06T10:12:42.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-10-01T06:40:50.000Z (about 3 years ago)
- Last Synced: 2024-05-09T07:11:39.412Z (8 months ago)
- Topics: ocaml
- Language: OCaml
- Homepage: https://sporto.github.io/jwto/
- Size: 95.7 KB
- Stars: 26
- Watchers: 4
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE
Awesome Lists containing this project
README
# Ocaml JWT
## Create and encode a signed token
A payload is a list of tuples `(string, string)`:
```ocaml
let payload =
[
("user", "sam");
("age", "17");
]
```For the signature algorithm, `Jwto` supports HMAC applied to SHA-256 or SHA-512.
We can sign and encode the payload in one go by doing:
```ocaml
Jwto.encode Jwto.HS256 "secret" payload-->
"eyJhbGciOiJIUzI1NiJ9..."
```## Decode token
To decode the token without verifying it, and get a `Jwto.t`:
```ocaml
let signed_token =
match Jwto.decode "eyJhbGciOiJIUzI1NiJ9..." with
| Ok t -> t
| Error err -> failwith err-->
{ header = ...; payload = [...]; signature = ... }
```## Verify token
```ocaml
Jwto.is_valid "secret" signed_token-->
true
```## Decode and verify
To decode and verify the token in one go:
```ocaml
Jwto.decode_and_verify "secret" "eyJhbGciOiJIUzI1NiJ9..."-->
Ok { header = ...; payload = [...]; signature = ... }
```If the verification fails, you will get an `Error`.