package sihl

  1. Overview
  2. Docs

Source file web_flash.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
let log_src = Logs.Src.create "sihl.middleware.flash"

module Logs = (val Logs.src_log log_src : Logs.LOG)

module Flash = struct
  type t =
    { alert : string option
    ; notice : string option
    ; custom : string option
    }

  let empty = { alert = None; notice = None; custom = None }

  let is_empty (flash : t) : bool =
    match flash.alert, flash.notice, flash.custom with
    | None, None, None -> true
    | _ -> false
  ;;

  let equals f1 f2 =
    Option.equal String.equal f1.alert f2.alert
    && Option.equal String.equal f1.notice f2.notice
    && Option.equal String.equal f1.custom f2.custom
  ;;

  let of_yojson (json : Yojson.Safe.t) : t option =
    (* In order to safe space, the empty flash cookie has the empty string as
       value *)
    let open Yojson.Safe.Util in
    let empty_string =
      try Some (to_string json) with
      | _ -> None
    in
    match empty_string with
    | Some _ -> Some empty
    | None ->
      (try
         let alert = json |> member "alert" |> to_string_option in
         let notice = json |> member "notice" |> to_string_option in
         let custom = json |> member "custom" |> to_string_option in
         Some { alert; notice; custom }
       with
      | _ -> None)
  ;;

  let to_yojson (flash : t) : Yojson.Safe.t =
    (* In order to safe space, the empty flash cookie has the empty string as
       value *)
    if is_empty flash
    then `String ""
    else (
      let { alert; notice; custom } = flash in
      let alert =
        alert
        |> Option.map (fun alert -> `String alert)
        |> Option.value ~default:`Null
      in
      let notice =
        notice
        |> Option.map (fun notice -> `String notice)
        |> Option.value ~default:`Null
      in
      let custom =
        custom
        |> Option.map (fun custom -> `String custom)
        |> Option.value ~default:`Null
      in
      `Assoc [ "alert", alert; "notice", notice; "custom", custom ])
  ;;

  let of_json (json : string) : t option =
    try of_yojson (Yojson.Safe.from_string json) with
    | _ -> None
  ;;

  let to_json (flash : t) : string = flash |> to_yojson |> Yojson.Safe.to_string

  let to_sexp (flash : t) : Sexplib0.Sexp.t =
    let open Sexplib0.Sexp_conv in
    let open Sexplib0.Sexp in
    List
      [ List [ Atom "alert"; sexp_of_option sexp_of_string flash.alert ]
      ; List [ Atom "notice"; sexp_of_option sexp_of_string flash.notice ]
      ; List [ Atom "custom"; sexp_of_option sexp_of_string flash.custom ]
      ]
  ;;
end

exception Flash_not_found

module Env = struct
  let key : Flash.t Opium.Context.key =
    Opium.Context.Key.create ("flash", Flash.to_sexp)
  ;;
end

let find req =
  (* Raising an exception is ok since we assume that before find can be called
     the middleware has been passed *)
  try Opium.Context.find_exn Env.key req.Opium.Request.env with
  | _ ->
    Logs.err (fun m -> m "No flash storage found");
    Logs.info (fun m ->
        m "Have you applied the flash middleware for this route?");
    raise Flash_not_found
;;

let find_alert req = (find req).alert
let find_notice req = (find req).notice
let find_custom req = (find req).custom

let set_alert alert resp =
  let flash = Opium.Context.find Env.key resp.Opium.Response.env in
  let flash =
    match flash with
    | None -> Flash.{ empty with alert }
    | Some flash -> Flash.{ flash with alert }
  in
  let env = resp.Opium.Response.env in
  let env = Opium.Context.add Env.key flash env in
  { resp with env }
;;

let set_notice notice resp =
  let flash = Opium.Context.find Env.key resp.Opium.Response.env in
  let flash =
    match flash with
    | None -> Flash.{ empty with notice }
    | Some flash -> Flash.{ flash with notice }
  in
  let env = resp.Opium.Response.env in
  let env = Opium.Context.add Env.key flash env in
  { resp with env }
;;

let set_custom custom resp =
  let flash = Opium.Context.find Env.key resp.Opium.Response.env in
  let flash =
    match flash with
    | None -> Flash.{ empty with custom }
    | Some flash -> Flash.{ flash with custom }
  in
  let env = resp.Opium.Response.env in
  let env = Opium.Context.add Env.key flash env in
  { resp with env }
;;

let decode_flash cookie_key req =
  match Opium.Request.cookie cookie_key req with
  | None -> Flash.empty
  | Some cookie_value ->
    (match Flash.of_json cookie_value with
    | None ->
      Logs.err (fun m ->
          m
            "Failed to parse value found in flash cookie '%s': '%s'"
            cookie_key
            cookie_value);
      Logs.info (fun m ->
          m
            "Maybe the cookie key '%s' collides with a cookie issued by \
             someone else. Try to change the cookie key."
            cookie_key);
      Flash.empty
    | Some flash -> flash)
;;

let persist_flash old_flash cookie_key resp =
  let flash = Opium.Context.find Env.key resp.Opium.Response.env in
  match flash with
  | None ->
    if Flash.is_empty old_flash
    then (* Flash was not touched, don't set cookie *)
      resp
    else (
      (* Set empty flash cookie *)
      let cookie = cookie_key, "" in
      let resp = Opium.Response.add_cookie_or_replace cookie resp in
      resp)
  | Some flash ->
    if Flash.equals old_flash flash
    then (* Flash was not touched, don't set cookie *)
      resp
    else (
      (* Flash was changed, set cookie *)
      let cookie_value = Flash.to_json flash in
      let cookie = cookie_key, cookie_value in
      let resp = Opium.Response.add_cookie_or_replace cookie resp in
      resp)
;;

let middleware ?(cookie_key = "_flash") () =
  let open Lwt.Syntax in
  let filter handler req =
    let flash = decode_flash cookie_key req in
    let env = req.Opium.Request.env in
    let env = Opium.Context.add Env.key flash env in
    let req = { req with env } in
    let* resp = handler req in
    Lwt.return @@ persist_flash flash cookie_key resp
  in
  Rock.Middleware.create ~name:"flash" ~filter
;;
OCaml

Innovation. Community. Security.