|
| 1 | +let log_src = Logs.Src.create ~doc:"middleware for cookie-based sessions" "opium.session" |
| 2 | + |
| 3 | +module Logs = (val Logs.src_log log_src : Logs.LOG) |
| 4 | +module Map = Map.Make (String) |
| 5 | + |
| 6 | +module Session = struct |
| 7 | + type t = |
| 8 | + { data : string Map.t |
| 9 | + ; should_set_cookie : bool |
| 10 | + } |
| 11 | + |
| 12 | + let create should_set_cookie = { data = Map.empty; should_set_cookie } |
| 13 | + |
| 14 | + let of_yojson yojson = |
| 15 | + let open Yojson.Safe.Util in |
| 16 | + let session_list = |
| 17 | + try Some (yojson |> to_assoc |> List.map (fun (k, v) -> k, to_string v)) with |
| 18 | + | _ -> None |
| 19 | + in |
| 20 | + session_list |
| 21 | + |> Option.map List.to_seq |
| 22 | + |> Option.map Map.of_seq |
| 23 | + |> Option.map (fun data -> { data; should_set_cookie = false }) |
| 24 | + ;; |
| 25 | + |
| 26 | + let to_yojson { data = session; _ } = |
| 27 | + `Assoc (session |> Map.to_seq |> List.of_seq |> List.map (fun (k, v) -> k, `String v)) |
| 28 | + ;; |
| 29 | + |
| 30 | + let of_json json = |
| 31 | + try of_yojson (Yojson.Safe.from_string json) with |
| 32 | + | _ -> None |
| 33 | + ;; |
| 34 | + |
| 35 | + let to_json session = session |> to_yojson |> Yojson.Safe.to_string |
| 36 | + |
| 37 | + let to_sexp session = |
| 38 | + let open Sexplib0.Sexp_conv in |
| 39 | + let open Sexplib0.Sexp in |
| 40 | + let data = |
| 41 | + session.data |
| 42 | + |> Map.to_seq |
| 43 | + |> List.of_seq |
| 44 | + |> sexp_of_list (sexp_of_pair sexp_of_string sexp_of_string) |
| 45 | + in |
| 46 | + List |
| 47 | + [ List [ Atom "data"; data ] |
| 48 | + ; List [ Atom "should_set_cookie"; sexp_of_bool session.should_set_cookie ] |
| 49 | + ] |
| 50 | + ;; |
| 51 | +end |
| 52 | + |
| 53 | +module SessionChange = struct |
| 54 | + type t = string option Map.t |
| 55 | + |
| 56 | + let empty = Map.empty |
| 57 | + |
| 58 | + let merge Session.{ data = session; should_set_cookie } t = |
| 59 | + let data = |
| 60 | + Map.merge |
| 61 | + (fun _ session change -> |
| 62 | + match session, change with |
| 63 | + | _, Some (Some change) -> Some change |
| 64 | + | _, Some None -> None |
| 65 | + | Some session, None -> Some session |
| 66 | + | None, None -> None) |
| 67 | + session |
| 68 | + t |
| 69 | + in |
| 70 | + Session.{ data; should_set_cookie } |
| 71 | + ;; |
| 72 | + |
| 73 | + let to_sexp t = |
| 74 | + t |
| 75 | + |> Map.to_seq |
| 76 | + |> List.of_seq |
| 77 | + |> Sexplib0.Sexp_conv.( |
| 78 | + sexp_of_list (sexp_of_pair sexp_of_string (sexp_of_option sexp_of_string))) |
| 79 | + ;; |
| 80 | +end |
| 81 | + |
| 82 | +module Env = struct |
| 83 | + let key : Session.t Context.key = Context.Key.create ("session", Session.to_sexp) |
| 84 | + |
| 85 | + let key_session_change : SessionChange.t Context.key = |
| 86 | + Context.Key.create ("session change", SessionChange.to_sexp) |
| 87 | + ;; |
| 88 | +end |
| 89 | + |
| 90 | +exception Session_not_found |
| 91 | + |
| 92 | +let find key req = |
| 93 | + let session = |
| 94 | + try Context.find_exn Env.key req.Request.env with |
| 95 | + | _ -> |
| 96 | + Logs.err (fun m -> m "No session found"); |
| 97 | + Logs.info (fun m -> m "Have you applied the session middleware?"); |
| 98 | + raise @@ Session_not_found |
| 99 | + in |
| 100 | + Map.find_opt key session.data |
| 101 | +;; |
| 102 | + |
| 103 | +let set (key, value) resp = |
| 104 | + let change = |
| 105 | + match Context.find Env.key_session_change resp.Response.env with |
| 106 | + | Some change -> Map.add key value change |
| 107 | + | None -> SessionChange.empty |> Map.add key value |
| 108 | + in |
| 109 | + let env = resp.Response.env in |
| 110 | + let env = Context.add Env.key_session_change change env in |
| 111 | + { resp with env } |
| 112 | +;; |
| 113 | + |
| 114 | +let decode_session cookie_key signed_with req = |
| 115 | + match Request.cookie ~signed_with cookie_key req with |
| 116 | + | None -> Session.create true |
| 117 | + | Some cookie_value -> |
| 118 | + (match Session.of_json cookie_value with |
| 119 | + | None -> |
| 120 | + Logs.err (fun m -> |
| 121 | + m |
| 122 | + "Failed to parse value found in session cookie '%s': '%s'" |
| 123 | + cookie_key |
| 124 | + cookie_value); |
| 125 | + Logs.info (fun m -> |
| 126 | + m |
| 127 | + "Maybe the cookie key '%s' collides with a cookie issued by someone else. \ |
| 128 | + Try to change the cookie key." |
| 129 | + cookie_key); |
| 130 | + Session.create true |
| 131 | + | Some session -> session) |
| 132 | +;; |
| 133 | + |
| 134 | +let persist_session current_session signed_with cookie_key resp = |
| 135 | + let session_change = Context.find Env.key_session_change resp.Response.env in |
| 136 | + let cookie = |
| 137 | + match current_session.Session.should_set_cookie, session_change with |
| 138 | + | true, Some session_change -> |
| 139 | + let session = SessionChange.merge current_session session_change in |
| 140 | + let cookie_value = Session.to_json session in |
| 141 | + Some (cookie_key, cookie_value) |
| 142 | + | true, None -> |
| 143 | + let cookie_value = Session.to_json (Session.create true) in |
| 144 | + Some (cookie_key, cookie_value) |
| 145 | + | false, Some session_change -> |
| 146 | + let session = SessionChange.merge current_session session_change in |
| 147 | + let cookie_value = Session.to_json session in |
| 148 | + Some (cookie_key, cookie_value) |
| 149 | + | false, None -> None |
| 150 | + in |
| 151 | + match cookie with |
| 152 | + | None -> resp |
| 153 | + | Some cookie -> Response.add_cookie_or_replace ~sign_with:signed_with cookie resp |
| 154 | +;; |
| 155 | + |
| 156 | +let m ?(cookie_key = "_session") signed_with = |
| 157 | + let open Lwt.Syntax in |
| 158 | + let filter handler req = |
| 159 | + let session = decode_session cookie_key signed_with req in |
| 160 | + let env = req.Request.env in |
| 161 | + let env = Context.add Env.key session env in |
| 162 | + let req = { req with env } in |
| 163 | + let* resp = handler req in |
| 164 | + Lwt.return @@ persist_session session signed_with cookie_key resp |
| 165 | + in |
| 166 | + Rock.Middleware.create ~name:"session" ~filter |
| 167 | +;; |
0 commit comments