Skip to content

Commit

Permalink
Merge pull request #11 from dinosaure/digestif
Browse files Browse the repository at this point in the history
Use `digestif` instead of `cryptokit`
  • Loading branch information
sporto committed Oct 1, 2021
2 parents 7d9e277 + ffb0e6e commit 862fd43
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
6 changes: 3 additions & 3 deletions jwto.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ build: [

depends: [
"ocaml" {>= "4.05"}
"dune" {build}
"dune" {>= "1.7"}
"alcotest" {with-test}
"cryptokit" {>= "1.10"}
"digestif" {>= "1.0.0"}
"fmt" {>= "0.8"}
"yojson" {>= "1.6"}
"base64" {>= "3.1"}
Expand All @@ -28,4 +28,4 @@ depends: [
]

synopsis: "JWT encoding, decoding and verification"
description: "JWT encoding, decoding and verification"
description: "JWT encoding, decoding and verification"
2 changes: 1 addition & 1 deletion lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
(public_name jwto)
(preprocess
(pps ppx_deriving.std))
(libraries ppx_deriving.std base64 cryptokit re.str yojson fmt))
(libraries ppx_deriving.std base64 digestif re.str yojson fmt))
16 changes: 5 additions & 11 deletions lib/jwto.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ type algorithm =
| Unknown
[@@deriving show, eq]

let fn_for_algorithm = function
| HS256 -> Cryptokit.MAC.hmac_sha256
| HS512 -> Cryptokit.MAC.hmac_sha512
| Unknown -> Cryptokit.MAC.hmac_sha256
let fn_for_algorithm alg ~secret str = match alg with
| HS256 -> Digestif.SHA256.hmac_string ~key:secret str |> Digestif.SHA256.to_raw_string
| HS512 -> Digestif.SHA512.hmac_string ~key:secret str |> Digestif.SHA512.to_raw_string
| Unknown -> Digestif.SHA256.hmac_string ~key:secret str |> Digestif.SHA256.to_raw_string

let algorithm_to_string (alg : algorithm) : string =
match alg with
Expand Down Expand Up @@ -161,13 +161,7 @@ let sign (secret : string) (unsigned_token : unsigned_token) : (string, string)
fn_for_algorithm unsigned_token.header.alg
in
encode_unsigned unsigned_token
|> map_result (fun encoded_token ->
(
Cryptokit.hash_string
(algo_fn secret)
encoded_token
)
)
|> map_result (fun encoded_token -> algo_fn ~secret encoded_token)

let make_signed_token (secret : string) (unsigned_token : unsigned_token) : (t, string) result =
sign secret unsigned_token
Expand Down

0 comments on commit 862fd43

Please sign in to comment.