-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumb_password.ml
61 lines (50 loc) · 1.62 KB
/
dumb_password.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
module DumbPassword (App: App.APP) = struct
open Lwt
open Config
open Ocsigen_messages
open Eliom_lib
open Eliom_content
open Html5.D
open Html5.F
open Eliom_tools.F
open Ocsipersist
exception BadPassword
let send_error str =
Ocsigen_messages.errlog str;
Eliom_registration.Html5.send
(html ~title:"error" (body [pcdata ("Error: " ^ str)]))
let service_path = ["login"; "dumb_password"]
let password_table = (open_table "users":string table)
(*let _ = add password_table "root" "root"*)
let main_service =
let get_service = Eliom_service.App.service
~path:service_path
~get_params:Eliom_parameter.(unit) () in
let post_service = Eliom_service.App.post_service
~fallback:get_service
~post_params:Eliom_parameter.((string "user") ** (string "password")) ()
in
let _ = Eliom_registration.Any.register post_service
(fun () (user,password) ->
try%lwt
let%lwt real_password = find password_table user in
if real_password = password then
let%lwt () = User.perform_login user in
Eliom_registration.Redirection.send App.welcome_service
else raise BadPassword
with
| Not_found | BadPassword -> send_error "bad password"
) in
let _ = App.register get_service
(fun () () ->
let login_form = Form.post_form ~service:post_service (fun (user, password) ->
[p [ pcdata "User";
Form.input ~input_type:`Text ~name:user Form.string;
pcdata "Password";
Form.input ~input_type:`Password ~name:password Form.string;
Form.input ~input_type:`Submit Form.string;
]]) () in
return (Template.make_page [login_form]))
in
get_service
end