package sihl

  1. Overview
  2. Docs
The Sihl web framework

Install

Dune Dependency

Authors

Maintainers

Sources

0.3.0.tar.gz
md5=3265ccfac470edc97a524259ec98e15b
sha512=0e6b184d5077a444a0583b65e5b99ba69e798321a34bc99bcc121eb82a3555b86ce8d1c7d3fc6afdcadf87d76f2d903702e5e6252b6b06fd7c1311b712298cc9

doc/src/sihl/web_bearer_token.ml.html

Source file web_bearer_token.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
let key : string Opium.Context.key =
  Opium.Context.Key.create ("token", Sexplib.Std.(sexp_of_string))
;;

let find req = Opium.Context.find_exn key req.Opium.Request.env
let find_opt req = Opium.Context.find key req.Opium.Request.env

let set token req =
  let env = req.Opium.Request.env in
  let env = Opium.Context.add key token env in
  { req with env }
;;

let middleware =
  let filter handler req =
    match Opium.Request.header "authorization" req with
    | Some authorization ->
      (match String.split_on_char ' ' authorization with
      | [ "Bearer"; token ] ->
        let req = set token req in
        handler req
      | [ "bearer"; token ] ->
        let req = set token req in
        handler req
      | _ -> handler req)
    | None -> handler req
  in
  Rock.Middleware.create ~name:"bearer" ~filter
;;
OCaml

Innovation. Community. Security.