-
Notifications
You must be signed in to change notification settings - Fork 20
/
site_admin.ml
2127 lines (1992 loc) · 81.8 KB
/
site_admin.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* BELENIOS *)
(* *)
(* Copyright © 2012-2023 Inria *)
(* *)
(* This program is free software: you can redistribute it and/or modify *)
(* it under the terms of the GNU Affero General Public License as *)
(* published by the Free Software Foundation, either version 3 of the *)
(* License, or (at your option) any later version, with the additional *)
(* exemption that compiling, linking, and/or using OpenSSL is allowed. *)
(* *)
(* This program is distributed in the hope that it will be useful, but *)
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)
(* Affero General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU Affero General Public *)
(* License along with this program. If not, see *)
(* <http://www.gnu.org/licenses/>. *)
(**************************************************************************)
open Lwt
open Lwt.Syntax
open Belenios
open Belenios_server_core
open Web_common
module Pages_admin_root = Pages_admin
module Make
(X : Pages_sig.S)
(Site_common : Site_common_sig.S)
(Web_cont : Web_cont_sig.S)
(Web_auth : Web_auth_sig.S) =
struct
open X
open Web_services
open Site_common
open Eliom_service
open Eliom_registration
let get_preferred_gettext () = Web_i18n.get_preferred_gettext "admin"
let () =
Any.register ~service:home (fun () () ->
Redirection.send (Redirection admin))
let get_elections_by_owner_sorted u =
let* elections = Storage.get_elections_by_owner u in
let filter f =
let open Belenios_api.Serializable_t in
List.filter (fun ({ state; _ } : summary) -> f state) elections
in
let draft = filter (fun x -> x = `Draft) in
let elections =
filter (function
| `Open | `Closed | `Shuffling | `EncryptedTally -> true
| _ -> false)
in
let tallied = filter (fun x -> x = `Tallied) in
let archived = filter (fun x -> x = `Archived) in
let sort l =
let open Belenios_api.Serializable_t in
List.sort
(fun ({ date = x; _ } : summary) { date = y; _ } -> Float.compare x y)
l
in
return (sort draft, sort elections, sort tallied, sort archived)
let () =
let@ a = Accounts.add_update_hook in
let* user = Eliom_reference.get Web_state.site_user in
match user with
| Some (u, b, t) when a.id = b.id ->
Eliom_reference.set Web_state.site_user (Some (u, a, t))
| _ -> Lwt.return_unit
let with_site_user f =
let* user = Eliom_reference.get Web_state.site_user in
match user with Some u -> f u | None -> forbidden ()
let with_metadata_check_owner s uuid f =
let* user = Eliom_reference.get Web_state.site_user in
let* metadata = Web_persist.get_election_metadata s uuid in
match user with
| Some (_, a, _) when Accounts.check a metadata.e_owners -> f metadata
| _ -> forbidden ()
let without_site_user ?fallback () f =
let* l = get_preferred_gettext () in
let open (val l) in
let* user = Eliom_reference.get Web_state.site_user in
match user with
| None -> f ()
| Some u -> (
match fallback with
| Some g -> g u
| None ->
Pages_common.generic_page ~title:(s_ "Error")
(s_
"This page is not accessible to authenticated administrators, \
because it is meant to be used by third parties.")
()
>>= Html.send)
let () =
Redirection.register ~service:privacy_notice_accept (fun () cont ->
let () = Web_state.set_consent_cookie () in
let cont =
match cont with
| ContAdmin -> Redirection admin
| ContSignup service ->
Redirection (preapply ~service:signup_captcha service)
in
return cont)
let () =
Any.register ~service:sealing (fun () () ->
let* site_user = Eliom_reference.get Web_state.site_user in
match site_user with
| None -> forbidden ()
| Some _ -> (
match !Web_config.sealing with
| Some (file, content_type) -> File.send ~content_type file
| None -> fail_http `Not_found))
let () =
Html.register ~service:admin (fun () () ->
let* site_user = Eliom_reference.get Web_state.site_user in
match site_user with
| None -> Pages_admin.admin_login Web_auth.get_site_login_handler
| Some (_, a, _) ->
let* show =
match a.consent with
| Some x
when Datetime.to_unixfloat x >= !Web_config.tos_last_update ->
return_false
| _ -> (
let@ s = Storage.with_transaction in
let* x = Accounts.update_account_by_id s a.id in
match x with
| None -> return_true
| Some (a, set) ->
let current_consent =
match a.consent with
| None -> 0.
| Some x -> Datetime.to_unixfloat x
in
if
current_consent < !Web_config.tos_last_update
&& Web_state.get_consent_cookie ()
then return_true
else
let consent = Some (Datetime.now ()) in
let* () = set { a with consent } in
return_false)
in
if show then Pages_admin.privacy_notice ContAdmin
else if a.email = None then Pages_admin.set_email ()
else
let* elections = get_elections_by_owner_sorted a.id in
Pages_admin.admin ~elections)
module SetEmailSender = struct
type payload = unit
type context = unit
let send ~context:() ~address ~code =
let* l = get_preferred_gettext () in
let subject, body = Pages_admin_root.mail_set_email l address code in
send_email ~subject ~recipient:address ~body MailSetEmail
end
module SetEmailOtp = Otp.Make (SetEmailSender) ()
let () =
Any.register ~service:set_email_post (fun () address ->
let@ _ = with_site_user in
if is_email ~blacklist:!Web_config.blacklisted_domains address then
let* () =
Eliom_reference.set Web_state.set_email_env (Some address)
in
let* () = SetEmailOtp.generate ~context:() ~address ~payload:() in
Pages_admin.set_email_confirm ~address >>= Html.send
else
let* l = get_preferred_gettext () in
let open (val l) in
let msg = s_ "This e-mail address is invalid!" in
let title = s_ "Error" in
Pages_common.generic_page ~title msg () >>= Html.send ~code:400)
let () =
Any.register ~service:set_email_confirm (fun () code ->
let* u = Eliom_reference.get Web_state.site_user in
let* x = Eliom_reference.get Web_state.set_email_env in
match (x, u) with
| None, _ | _, None -> forbidden ()
| Some address, Some (_, a, _) -> (
match SetEmailOtp.check ~address ~code with
| Some () ->
let@ s = Storage.with_transaction in
let@ a, set =
fun cont ->
let* x = Accounts.update_account_by_id s a.id in
match x with
| None -> Lwt.fail @@ Failure "set_email_confirm"
| Some x -> cont x
in
let* () = set { a with email = Some address } in
let* () = Web_state.discard () in
Redirection.send (Redirection admin)
| None ->
let* l = get_preferred_gettext () in
let open (val l) in
let msg =
s_
"The provided code is incorrect. Please go back and try \
again."
in
let title = s_ "Incorrect code" in
Pages_common.generic_page ~title msg () >>= Html.send ~code:403))
let create_new_election (account : account) cred draft_authentication =
let open Belenios_api.Serializable_t in
let (Version v) = List.hd Belenios.Election.supported_crypto_versions in
let open (val Belenios.Election.get_serializers v) in
let draft_questions =
{
t_description = Defaults.description;
t_name = Defaults.name;
t_questions = Array.map of_concrete Defaults.questions;
t_administrator = Some account.name;
t_credential_authority =
Some (match cred with `Automatic -> "server" | `Manual -> "");
}
in
let address =
match account.email with None -> "" | Some x -> Printf.sprintf " <%s>" x
in
let draft =
{
draft_version = Defaults.version;
draft_owners = [ account.id ];
draft_questions;
draft_languages = [ "en"; "fr" ];
draft_contact = Some (account.name ^ address);
draft_booth = 2;
draft_authentication;
draft_group = !Web_config.default_group;
}
in
let* uuid =
let@ s = Storage.with_transaction in
Api_drafts.post_drafts account s (Belenios_api.Common.Draft (v, draft))
in
match uuid with
| Some uuid -> redir_preapply election_draft uuid ()
| None ->
let* l = get_preferred_gettext () in
let open (val l) in
Pages_common.generic_page ~title:(s_ "Error")
(s_ "Creating new elections is forbidden on this server!")
()
>>= Html.send
let () =
Any.register ~service:election_draft_pre (fun () () ->
let@ _ = with_site_user in
Pages_admin.election_draft_pre () >>= Html.send)
let http_rex = "^https?://[a-z0-9/.-]+$"
let is_http_url =
let rex = Re.Pcre.regexp ~flags:[ `CASELESS ] http_rex in
fun x -> match re_exec_opt ~rex x with Some _ -> true | None -> false
let () =
Any.register ~service:election_draft_new
(fun () (credmgmt, (auth, cas_server)) ->
let* l = get_preferred_gettext () in
let open (val l) in
let@ _, a, _ = with_site_user in
let* credmgmt =
match credmgmt with
| Some "auto" -> return `Automatic
| Some "manual" -> return `Manual
| _ -> fail_http `Bad_request
in
let* auth_parsed =
match auth with
| Some "password" -> return `Password
| Some "cas" -> (
match cas_server with
| None -> fail_http `Bad_request
| Some cas_server ->
return @@ `CAS (Stdlib.String.trim cas_server))
| Some x ->
let n = Stdlib.String.length x in
if n > 1 && Stdlib.String.get x 0 = '%' then
let name = Stdlib.String.sub x 1 (n - 1) in
return @@ `Configured name
else fail_http `Bad_request
| _ -> fail_http `Bad_request
in
let has_cas_server =
match cas_server with None | Some "" -> false | Some _ -> true
in
match auth_parsed with
| `CAS cas_server when not (is_http_url cas_server) ->
Pages_common.generic_page ~title:(s_ "Error") (s_ "Bad CAS server!")
()
>>= Html.send
| _ when has_cas_server && auth <> Some "cas" ->
Pages_common.generic_page ~title:(s_ "Error")
(s_ "Non-empty CAS server, but CAS authentication not selected!")
()
>>= Html.send
| _ -> create_new_election a credmgmt auth_parsed)
let with_draft_public s uuid f =
let* election = Spool.get s uuid Spool.draft in
match election with None -> fail_http `Not_found | Some x -> f x
let with_draft_public_update s uuid f =
let* election = Spool.update s uuid Spool.draft in
match election with None -> fail_http `Not_found | Some x -> f x
let with_draft ?(save = true) s uuid f =
let@ _, a, _ = with_site_user in
let@ (Draft (_, se) as x), set =
fun cont ->
if save then
let* x = Spool.update s uuid Spool.draft in
match x with
| None -> fail_http `Not_found
| Some (x, set) -> cont (x, Some set)
else
let* x = Spool.get s uuid Spool.draft in
match x with None -> fail_http `Not_found | Some x -> cont (x, None)
in
if Accounts.check a se.se_owners then
Lwt.catch
(fun () ->
let* r = f x in
let* () =
match set with None -> Lwt.return_unit | Some set -> set x
in
return r)
(fun e ->
let msg = match e with Failure s -> s | _ -> Printexc.to_string e in
let service = preapply ~service:election_draft uuid in
let* l = get_preferred_gettext () in
let open (val l) in
Pages_common.generic_page ~title:(s_ "Error") ~service msg ()
>>= Html.send)
else forbidden ()
let with_draft_update ?(save = true) s uuid f =
let@ _, a, _ = with_site_user in
let@ (((Draft (_, se) as x), set) as d) =
fun cont ->
let* x = Spool.update s uuid Spool.draft in
match x with None -> fail_http `Not_found | Some x -> cont x
in
if Accounts.check a se.se_owners then
Lwt.catch
(fun () ->
let* r = f d in
let* () = if save then set x else return_unit in
return r)
(fun e ->
let msg = match e with Failure s -> s | _ -> Printexc.to_string e in
let service = preapply ~service:election_draft uuid in
let* l = get_preferred_gettext () in
let open (val l) in
Pages_common.generic_page ~title:(s_ "Error") ~service msg ()
>>= Html.send)
else forbidden ()
let () =
Any.register ~service:election_draft (fun uuid () ->
let@ s = Storage.with_transaction in
let@ se = with_draft ~save:false s uuid in
Pages_admin.election_draft uuid se () >>= Html.send)
let () =
Any.register ~service:election_draft_trustees (fun uuid () ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se) as x) = with_draft ~save:false s uuid in
match se.se_trustees with
| `Basic _ ->
Pages_admin.election_draft_trustees uuid x () >>= Html.send
| `Threshold _ ->
redir_preapply election_draft_threshold_trustees uuid ())
let () =
Any.register ~service:election_draft_threshold_trustees (fun uuid () ->
let@ s = Storage.with_transaction in
let@ se = with_draft ~save:false s uuid in
Pages_admin.election_draft_threshold_trustees uuid se () >>= Html.send)
let () =
Any.register ~service:election_draft_credential_authority (fun uuid () ->
let@ s = Storage.with_transaction in
let@ se = with_draft ~save:false s uuid in
Pages_admin.election_draft_credential_authority uuid se () >>= Html.send)
let () =
Any.register ~service:election_draft_set_credential_authority
(fun uuid name ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se)) = with_draft s uuid in
let* l = get_preferred_gettext () in
let open (val l) in
let service =
Eliom_service.preapply ~service:election_draft_credential_authority
uuid
in
match
if se.se_metadata.e_cred_authority = Some "server" then
Error
(s_ "You cannot set the credential authority for this election!")
else
match name with
| "" -> Ok None
| "server" ->
Error (s_ "Invalid public name for credential authority!")
| x -> Ok (Some x)
with
| Ok e_cred_authority ->
se.se_metadata <- { se.se_metadata with e_cred_authority };
let msg =
s_
"The public name of the credential authority has been set \
successfully!"
in
Pages_common.generic_page ~title:(s_ "Success") ~service msg ()
>>= Html.send
| Error msg ->
Pages_common.generic_page ~title:(s_ "Error") ~service msg ()
>>= Html.send)
let () =
Any.register ~service:election_draft_languages (fun uuid languages ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se)) = with_draft s uuid in
let* l = get_preferred_gettext () in
let open (val l) in
let langs = languages_of_string languages in
match langs with
| [] ->
let service = preapply ~service:election_draft uuid in
Pages_common.generic_page ~title:(s_ "Error") ~service
(s_ "You must select at least one language!")
()
>>= Html.send
| _ :: _ -> (
let available_languages =
List.map fst Belenios_ui.Languages.available
in
let unavailable =
List.filter (fun x -> not (List.mem x available_languages)) langs
in
match unavailable with
| [] ->
se.se_metadata <-
{ se.se_metadata with e_languages = Some langs };
redir_preapply election_draft uuid ()
| l :: _ ->
let service = preapply ~service:election_draft uuid in
Pages_common.generic_page ~title:(s_ "Error") ~service
(Printf.sprintf (f_ "No such language: %s") l)
()
>>= Html.send))
let () =
Any.register ~service:election_draft_contact (fun uuid contact ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se)) = with_draft s uuid in
let contact =
if contact = "" || contact = Defaults.contact then None
else Some contact
in
se.se_metadata <- { se.se_metadata with e_contact = contact };
redir_preapply election_draft uuid ())
let () =
Any.register ~service:election_draft_admin_name (fun uuid name ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se)) = with_draft s uuid in
let administrator = if name = "" then None else Some name in
se.se_administrator <- administrator;
redir_preapply election_draft uuid ())
let () =
Any.register ~service:election_draft_description
(fun uuid (name, description) ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se)) = with_draft s uuid in
let* l = get_preferred_gettext () in
let open (val l) in
if Stdlib.String.length name > Defaults.max_election_name_size then
let msg =
Printf.sprintf
(f_ "The election name must be %d characters or less!")
Defaults.max_election_name_size
in
Pages_common.generic_page ~title:(s_ "Error") msg () >>= Html.send
else (
se.se_questions <-
{ se.se_questions with t_name = name; t_description = description };
redir_preapply election_draft uuid ()))
let handle_password account se uuid ~force voters =
let* l = get_preferred_gettext () in
let open (val l) in
let max_voters = Accounts.max_voters account in
if List.length voters > max_voters then
Lwt.fail
(Failure
(Printf.sprintf
(f_ "Cannot send passwords, there are too many voters (max is %d)")
max_voters))
else if se.se_questions.t_name = Defaults.name then
Lwt.fail (Failure (s_ "The election name has not been edited!"))
else
let title = se.se_questions.t_name in
let langs = get_languages se.se_metadata.e_languages in
let show_weight = has_explicit_weights voters in
let* jobs =
Lwt_list.fold_left_s
(fun jobs id ->
match id.sv_password with
| Some _ when not force -> Lwt.return jobs
| None | Some _ ->
let* email, x =
Mails_voter.generate_password_email se.se_metadata langs title
uuid id.sv_id show_weight
in
id.sv_password <- Some x;
Lwt.return (email :: jobs))
[] voters
in
let* () = Mails_voter.submit_bulk_emails jobs in
let service = preapply ~service:election_draft uuid in
Pages_common.generic_page ~title:(s_ "Success") ~service
(s_ "Passwords have been generated and mailed!")
()
>>= Html.send
let () =
Any.register ~service:election_draft_auth_genpwd (fun uuid () ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se)) = with_draft s uuid in
let@ _, account, _ = with_site_user in
handle_password account se uuid ~force:false se.se_voters)
let () =
Any.register ~service:election_regenpwd (fun uuid () ->
Pages_admin.regenpwd uuid () >>= Html.send)
let () =
Any.register ~service:election_regenpwd_post (fun uuid user ->
let@ s = Storage.with_transaction in
let@ metadata = with_metadata_check_owner s uuid in
let* l = get_preferred_gettext () in
let open (val l) in
let service = preapply ~service:election_admin uuid in
let* b = Web_persist.regen_password s uuid metadata user in
if b then
Pages_common.generic_page ~title:(s_ "Success") ~service
(Printf.sprintf (f_ "A new password has been mailed to %s.") user)
()
>>= Html.send
else
Pages_common.generic_page ~title:(s_ "Error") ~service
(Printf.sprintf
(f_ "%s is not a registered user for this election.")
user)
()
>>= Html.send)
let () =
Any.register ~service:election_draft_questions (fun uuid () ->
let@ s = Storage.with_transaction in
let@ se = with_draft ~save:false s uuid in
Pages_admin.election_draft_questions uuid se () >>= Html.send)
let () =
Any.register ~service:election_draft_questions_post
(fun uuid (template, booth_version) ->
let@ s = Storage.with_transaction in
let@ (Draft (v, se) as x) = with_draft s uuid in
let* l = get_preferred_gettext () in
let open (val l) in
let open (val Election.get_serializers v) in
let template = template_of_string read_question template in
let fixed_group = Web_persist.is_group_fixed uuid x in
(match
( Election.has_nh_questions (Template (v, se.se_questions)),
Election.has_nh_questions (Template (v, template)) )
with
| true, true | false, false -> ()
| true, false when fixed_group -> ()
| true, false -> se.se_group <- !Web_config.default_group
| false, true when fixed_group ->
failwith (s_ "This kind of change is not allowed now!")
| false, true -> se.se_group <- !Web_config.nh_group);
se.se_questions <- template;
let e_booth_version =
match booth_version with 1 -> None | x -> Some x
in
se.se_metadata <- { se.se_metadata with e_booth_version };
redir_preapply election_draft uuid ())
let () =
Any.register ~service:election_draft_preview (fun (uuid, ()) () ->
let@ s = Storage.with_transaction in
let@ (Draft (v, se)) = with_draft ~save:false s uuid in
let version = se.se_version in
let group = se.se_group in
let module G = (val Group.of_string ~version group : GROUP) in
let public_key = G.to_string G.g in
let raw_election =
Election.make_raw_election ~version
(Template (v, se.se_questions))
~uuid ~group ~public_key
in
let* x = String.send (raw_election, "application/json") in
return @@ Eliom_registration.cast_unknown_content_kind x)
let () =
Any.register ~service:election_draft_voters (fun uuid () ->
let@ s = Storage.with_transaction in
let@ se = with_draft ~save:false s uuid in
let@ _, account, _ = with_site_user in
let max_voters = Accounts.max_voters account in
Pages_admin.election_draft_voters uuid se max_voters () >>= Html.send)
let check_consistency voters =
let get_shape voter =
match voter.sv_id with
| `Plain, { login; weight; _ } -> `Plain (login <> None, weight <> None)
| `Json, _ -> `Json
in
match voters with
| [] -> true
| voter :: voters ->
let shape = get_shape voter in
let rec loop = function
| [] -> true
| voter :: voters -> get_shape voter = shape && loop voters
in
loop voters
let () =
Any.register ~service:election_draft_voters_add (fun uuid voters ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se) as x) = with_draft s uuid in
let@ _, account, _ = with_site_user in
let* l = get_preferred_gettext () in
let open (val l) in
match Web_persist.get_credentials_status uuid x with
| `Done | `Pending _ -> forbidden ()
| `None -> (
let voters =
split_lines voters
|> List.map (fun x ->
match Voter.of_string x with
| exception _ ->
Printf.ksprintf failwith
(f_ "%S is not a valid identity")
x
| voter ->
if not (Voter.validate voter) then
Printf.ksprintf failwith
(f_ "%S is not a valid identity")
x;
voter)
in
match
Api_drafts.merge_voters se.se_voters voters (fun _ -> None)
with
| Error x ->
let _, x, _ = Voter.get x in
Printf.ksprintf failwith
(f_
"Duplicate voter: %s. This is not allowed. If two voters \
have the same address, use different logins.")
x
| Ok (voters, total_weight) ->
let max_voters = Accounts.max_voters account in
let () =
let expanded =
Weight.expand ~total:total_weight total_weight
in
if Z.compare expanded Weight.max_expanded_weight > 0 then
Printf.ksprintf failwith
(f_
"The total weight (%s) cannot be handled. Its \
expanded value must be less than %s.")
Weight.(to_string total_weight)
(Z.to_string Weight.max_expanded_weight)
in
if not (check_consistency voters) then
failwith
(s_
"The voter list is not consistent (a login or a weight \
is missing).");
let uses_password_auth =
match se.se_metadata.e_auth_config with
| Some configs ->
List.exists
(fun { auth_system; _ } -> auth_system = "password")
configs
| None -> false
in
let cred_auth_is_server =
se.se_metadata.e_cred_authority = Some "server"
in
if
(uses_password_auth || cred_auth_is_server)
&& List.length voters > max_voters
then
Lwt.fail
(Failure
(Printf.sprintf
(f_ "There are too many voters (max is %d)")
max_voters))
else (
se.se_voters <- voters;
redir_preapply election_draft_voters uuid ())))
let () =
Any.register ~service:election_draft_voters_remove (fun uuid voter ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se) as x) = with_draft s uuid in
match Web_persist.get_credentials_status uuid x with
| `Done | `Pending _ -> forbidden ()
| `None ->
let filter v =
let _, login, _ = Voter.get v.sv_id in
login <> voter
in
se.se_voters <- List.filter filter se.se_voters;
redir_preapply election_draft_voters uuid ())
let () =
Any.register ~service:election_draft_voters_remove_all (fun uuid () ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se) as x) = with_draft s uuid in
match Web_persist.get_credentials_status uuid x with
| `Done | `Pending _ -> forbidden ()
| `None ->
se.se_voters <- [];
redir_preapply election_draft_voters uuid ())
let () =
Any.register ~service:election_draft_voters_passwd (fun uuid voter ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se)) = with_draft s uuid in
let@ _, account, _ = with_site_user in
let filter v =
let _, login, _ = Voter.get v.sv_id in
login = voter
in
let voter = List.filter filter se.se_voters in
handle_password account se uuid ~force:true voter)
let ensure_trustees_mode s uuid ((Draft (_, se) as x), set) mode =
match (se.se_trustees, mode) with
| `Basic _, `Basic | `Threshold _, `Threshold _ -> Lwt.return x
| `Threshold _, `Basic | `Basic _, `Threshold _ -> (
let* () = Api_drafts.put_draft_trustees_mode (x, set) mode in
let* x = Spool.get s uuid Spool.draft in
match x with
| Some se -> Lwt.return se
| None -> Lwt.fail (Failure "inconsistency in ensure_trustees_mode"))
let handle_trustee_add mode uuid (trustee_address, trustee_name) =
let@ s = Storage.with_transaction in
let@ se, set = with_draft_update ~save:false s uuid in
let* l = get_preferred_gettext () in
let open (val l) in
if is_email trustee_address then
let* se = ensure_trustees_mode s uuid (se, set) mode in
let open Belenios_api.Serializable_t in
let trustee =
{
trustee_address = Some trustee_address;
trustee_name;
trustee_token = None;
trustee_state = None;
trustee_key = None;
}
in
let* () = Api_drafts.post_draft_trustees (se, set) trustee in
redir_preapply election_draft_trustees uuid ()
else
let msg =
Printf.sprintf (f_ "%s is not a valid e-mail address!") trustee_address
in
let service = preapply ~service:election_draft_trustees uuid in
Pages_common.generic_page ~title:(s_ "Error") ~service msg ()
>>= Html.send
let handle_trustee_del service uuid address =
let@ s = Storage.with_transaction in
let@ se = with_draft_update ~save:false s uuid in
let* _ = Api_drafts.delete_draft_trustee se address in
redir_preapply service uuid ()
let () =
Any.register ~service:election_draft_trustee_add (handle_trustee_add `Basic)
let () =
Any.register ~service:election_draft_trustee_del
(handle_trustee_del election_draft_trustees)
let () =
Any.register ~service:election_draft_credentials (fun (uuid, token) () ->
let@ () = without_site_user () in
let@ s = Storage.with_transaction in
let@ se = with_draft_public s uuid in
match Web_persist.get_credentials_status uuid se with
| `Done | `Pending _ ->
Pages_admin.election_draft_credentials_already_generated ()
>>= Html.send
| `None ->
Printf.sprintf "%s/draft/credentials.html#%s-%s" !Web_config.prefix
(Uuid.unwrap uuid) token
|> String_redirection.send)
let () =
Html.register ~service:election_draft_credentials_static (fun () () ->
Pages_admin.election_draft_credentials_static ())
let handle_credentials_post uuid token creds =
let@ s = Storage.with_transaction in
let@ (Draft (_, se) as x), set = with_draft_public_update s uuid in
if se.se_public_creds <> token then forbidden ()
else
match Web_persist.get_credentials_status uuid x with
| `Done | `Pending _ -> forbidden ()
| `None ->
let creds = public_credentials_of_string creds in
let* () =
Api_drafts.submit_public_credentials s uuid (x, set) creds
in
Pages_admin.election_draft_credentials_done x () >>= Html.send
let () =
Any.register ~service:election_draft_credentials_post
(fun (uuid, token) creds ->
let@ () = without_site_user () in
wrap_handler (fun () -> handle_credentials_post uuid token creds))
let () =
Any.register ~service:election_draft_credentials_post_file
(fun (uuid, token) creds ->
let@ () = without_site_user () in
let* creds = exhaust_file creds in
wrap_handler (fun () -> handle_credentials_post uuid token creds))
let () =
Any.register ~service:election_draft_credentials_server (fun uuid () ->
let@ s = Storage.with_transaction in
let@ (Draft (_, se) as x) = with_draft s uuid in
let@ _, account, _ = with_site_user in
let max_voters = Accounts.max_voters account in
let* l = get_preferred_gettext () in
let open (val l) in
if se.se_questions.t_name = Defaults.name then
Lwt.fail (Failure (s_ "The election name has not been edited!"))
else
let* x = Api_drafts.generate_credentials_on_server account uuid x in
match x with
| Ok () ->
let service = preapply ~service:election_draft uuid in
Pages_common.generic_page ~title:(s_ "Success") ~service
(s_
"Credentials have been generated! They will be e-mailed \
when the election is validated. You should download \
private credentials (and store them in a secure location), \
in case someone loses their credential.")
()
>>= Html.send
| Error `Already ->
Lwt.fail (Failure (s_ "The credentials were already sent"))
| Error `NoVoters -> Lwt.fail (Failure (s_ "No voters"))
| Error `TooManyVoters ->
Lwt.fail
(Failure
(Printf.sprintf
(f_
"Cannot send credentials, there are too many voters \
(max is %d)")
max_voters))
| Error `NoServer ->
Lwt.fail (Failure (s_ "The authority is not the server")))
let () =
Any.register ~service:election_draft_credentials_get (fun uuid () ->
let@ s = Storage.with_transaction in
let@ _ = with_draft ~save:false s uuid in
let* () = Web_persist.set_private_creds_downloaded s uuid in
let* filename =
let module S = (val s) in
S.get_as_file (Election (uuid, Private_creds))
in
File.send ~content_type:"text/plain" filename)
let () =
Any.register ~service:election_draft_trustee (fun (uuid, token) () ->
let* l = get_preferred_gettext () in
let open (val l) in
let@ s = Storage.with_transaction in
let@ () =
without_site_user
~fallback:(fun (_, a, _) ->
let@ (Draft (_, se) as x) = with_draft_public s uuid in
if Accounts.check a se.se_owners then
Pages_admin.election_draft_trustees ~token uuid x ()
>>= Html.send
else forbidden ())
()
in
let@ (Draft (_, se)) = with_draft_public s uuid in
let ts =
match se.se_trustees with
| `Basic x -> x.dbp_trustees
| `Threshold _ -> []
in
match List.find_opt (fun t -> t.st_token = token) ts with
| None -> forbidden ()
| Some t ->
if t.st_public_key <> "" then
let msg = s_ "Your public key has already been received!" in
let title = s_ "Error" in
Pages_common.generic_page ~title msg () >>= Html.send ~code:403
else
Printf.sprintf "%s/draft/trustee.html#%s-%s" !Web_config.prefix
(Uuid.unwrap uuid) token
|> String_redirection.send)
let () =
Html.register ~service:election_draft_trustee_static (fun () () ->
Pages_admin.election_draft_trustee_static ())
let () =
Any.register ~service:election_draft_trustee_post
(fun (uuid, token) public_key ->
let@ () = without_site_user () in
let* l = get_preferred_gettext () in
let open (val l) in
if token = "" then forbidden ()
else
let* x =
let@ s = Storage.with_transaction in
let@ (Draft (_, se) as fse), set =
with_draft_public_update s uuid
in
let ts =
match se.se_trustees with
| `Basic x -> x.dbp_trustees
| `Threshold _ -> []
in
let&* t = List.find_opt (fun x -> token = x.st_token) ts in
if t.st_public_key <> "" then
let msg =
s_
"A public key already existed, the key you've just uploaded \
has been ignored!"
in
let title = s_ "Error" in
return_some (title, msg, 400)
else
let version = se.se_version in
let module G = (val Group.of_string ~version se.se_group : GROUP)
in
let module Trustees = (val Trustees.get_by_version version) in
let pk =
trustee_public_key_of_string (sread G.of_string)
(sread G.Zq.of_string) public_key
in
let module K = Trustees.MakeCombinator (G) in
if not (K.check [ `Single pk ]) then
let msg = s_ "Invalid public key!" in
let title = s_ "Error" in
return_some (title, msg, 400)
else (
(* we keep pk as a string because of G.t *)
t.st_public_key <- public_key;
let* () = set fse in
let msg = s_ "Your key has been received and checked!" in
let title = s_ "Success" in
return_some (title, msg, 200))
in
match x with
| None -> forbidden ()
| Some (title, msg, code) ->