web_templates.ml 39.3 KB
Newer Older
Stephane Glondu's avatar
Stephane Glondu committed
1 2 3
(**************************************************************************)
(*                                BELENIOS                                *)
(*                                                                        *)
Stephane Glondu's avatar
Stephane Glondu committed
4
(*  Copyright © 2012-2014 Inria                                           *)
Stephane Glondu's avatar
Stephane Glondu committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
(*                                                                        *)
(*  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/>.                                       *)
(**************************************************************************)

22
open Lwt
23
open Serializable_j
Stephane Glondu's avatar
Stephane Glondu committed
24
open Signatures
Stephane Glondu's avatar
Stephane Glondu committed
25
open Common
26
open Web_serializable_j
27
open Web_signatures
28
open Web_common
29
open Web_services
Stephane Glondu's avatar
Stephane Glondu committed
30 31
open Eliom_content.Html5.F

Stephane Glondu's avatar
Stephane Glondu committed
32
(* TODO: these pages should be redesigned *)
Stephane Glondu's avatar
Stephane Glondu committed
33

Stephane Glondu's avatar
Stephane Glondu committed
34
let site_title = "Election Server"
Stephane Glondu's avatar
Stephane Glondu committed
35
let welcome_message = "Welcome to the Belenios system!"
36
let admin_background = " background: #FF9999;"
Stephane Glondu's avatar
Stephane Glondu committed
37

Stephane Glondu's avatar
Stephane Glondu committed
38
let format_user u =
39
  em [pcdata (string_of_user u)]
40

41
let make_login_box style auth links =
42
  let style = "float: right; text-align: right;" ^ style in
43
  let module S = (val auth : AUTH_SERVICES) in
44
  let module L = (val links : AUTH_LINKS) in
45
  lwt user = S.get_user () in
46
  lwt auth_systems = S.get_auth_systems () in
47
  return @@ div ~a:[a_style style] (
48 49 50 51 52 53 54 55 56
    match user with
    | Some user ->
      [
        div [
          pcdata "Logged in as ";
          format_user user;
          pcdata ".";
        ];
        div [
57
          a ~service:L.logout [pcdata "Log out"] ();
58 59 60 61 62 63 64 65
          pcdata ".";
        ];
      ]
    | None ->
      [
        div [
          pcdata "Not logged in.";
        ];
66
        let auth_systems =
67
          auth_systems |>
68
          List.map (fun name ->
69
            a ~service:(L.login (Some name)) [pcdata name] ()
70 71
          ) |> list_join (pcdata ", ")
        in
72
        div (
73
          [pcdata "Log in: ["] @ auth_systems @ [pcdata "]"]
74 75 76 77
        );
      ]
  )

78 79
module Site_links = struct
  let login x = Eliom_service.preapply site_login x
Stephane Glondu's avatar
Stephane Glondu committed
80
  let logout = Eliom_service.preapply logout ()
81 82
end

83
module Site_auth = struct
Stephane Glondu's avatar
Stephane Glondu committed
84 85 86 87
  let get_user () = Web_auth_state.get_site_user ()
  let get_auth_systems () =
    lwt l = Web_auth_state.get_config None in
    return (List.map fst l)
88 89
end

90
let site_links = (module Site_links : AUTH_LINKS)
91
let site_auth = (module Site_auth : AUTH_SERVICES)
92

93 94
let site_login_box () =
  make_login_box admin_background site_auth site_links
95 96 97 98 99 100 101 102 103 104 105 106 107

let base ~title ~login_box ~content ?(footer = div []) () =
  Lwt.return (html ~a:[a_dir `Ltr; a_xml_lang "en"]
    (head (Eliom_content.Html5.F.title (pcdata title)) [
      script (pcdata "window.onbeforeunload = function () {};");
      link ~rel:[`Stylesheet] ~href:(uri_of_string (fun () -> "/static/site.css")) ();
    ])
    (body [
      div ~a:[a_id "wrapper"] [
      div ~a:[a_id "header"] [
        div [
          div ~a:[a_style "float: left;"] [
            a ~service:home [pcdata site_title] ();
Stephane Glondu's avatar
Stephane Glondu committed
108
          ];
109 110
          login_box;
          div ~a:[a_style "clear: both;"] [];
Stephane Glondu's avatar
Stephane Glondu committed
111
        ];
112
        h1 ~a:[a_style "text-align: center;"] [pcdata title];
Stephane Glondu's avatar
Stephane Glondu committed
113
      ];
114 115 116 117 118 119 120 121 122
      div ~a:[a_id "main"] content;
      div ~a:[a_id "footer"; a_style "text-align: center;" ] [
        div ~a:[a_id "bottom"] [
          footer;
          pcdata "Powered by ";
          a ~service:source_code [pcdata "Belenios"] ();
          pcdata ". ";
          a ~service:admin [pcdata "Administer elections"] ();
          pcdata ".";
123
        ]
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
      ]]
     ]))

let format_election kind election =
  let module W = (val election : WEB_ELECTION) in
  let e = W.election.e_params in
  let service =
    match kind with
    | `Home -> election_home
    | `Admin -> election_admin
  in
  li [
    h3 [
      a ~service [pcdata e.e_name] (e.e_uuid, ());
    ];
    p [pcdata e.e_description];
  ]

let home ~featured () =
143 144 145 146 147 148 149 150 151 152 153
  let loria = Eliom_service.Http.external_service
    ~prefix:"http://www.loria.fr"
    ~path:[]
    ~get_params:Eliom_parameter.unit
    ()
  in
  let belenios = Eliom_service.Http.external_service
    ~prefix:"http://belenios.gforge.inria.fr"
    ~path:[]
    ~get_params:Eliom_parameter.unit
    ()
154 155 156
  in
  let content = [
    div [
Stephane Glondu's avatar
Stephane Glondu committed
157 158
      h2 ~a:[a_style "text-align:center;"] [pcdata welcome_message];
      h3 [a ~service:admin [pcdata "Administer elections"] ()];
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
      div [br ()];
      div [
        pcdata "Belenios is an electronic voting system developed at ";
        a ~service:loria [pcdata "LORIA"] ();
        pcdata ". It provides both confidentiality of the votes and ";
        pcdata "end-to-end verifiability of the result. Verifiability ";
        pcdata "relies in particular on the fact that the ballots are ";
        pcdata "stored on a public ballot box (on a webpage), so that ";
        pcdata "voters can check the presence of their ballots. Similarly, ";
        pcdata "anyone can check that the published result corresponds to ";
        pcdata "the contents of the ballot box. More information and ";
        pcdata "discussion can be found on the ";
        a ~service:belenios [pcdata "Belenios web page"] ();
        pcdata ".";
      ];
174 175 176 177 178
    ];
  ] in
  let login_box = pcdata "" in
  base ~title:site_title ~login_box ~content ()

179
let admin ~elections () =
180
  let title = site_title ^ " — Administration" in
181 182 183 184 185 186 187 188 189 190 191 192 193
  match elections with
  | None ->
     let content = [
       div [
         pcdata "To administer an election, you need to ";
         a ~service:site_login [pcdata "log in"] None;
         pcdata ". If you do not have an account, ";
         pcdata "please send an email to contact@belenios.org.";
       ]
     ] in
     lwt login_box = site_login_box () in
     base ~title ~login_box ~content ()
  | Some (elections, setup_elections) ->
Stephane Glondu's avatar
Stephane Glondu committed
194 195 196 197 198 199 200
    let setup_form = post_form ~service:election_setup_new
      (fun () ->
       [
         div [string_input ~a:[a_style "font-size:24px;"] ~input_type:`Submit ~value:"Prepare a new election" ()]
       ]
      ) ()
    in
201 202 203 204 205 206 207 208 209
    let elections =
      match elections with
      | [] -> p [pcdata "You own no such elections!"]
      | _ -> ul @@ List.map (format_election `Admin) elections
    in
    let setup_elections =
      match setup_elections with
      | [] -> p [pcdata "You own no such elections!"]
      | _ -> ul @@
210 211
         List.map (fun (k, title) ->
           li [a ~service:election_setup [pcdata title] k]
212 213 214 215
         ) setup_elections
    in
    let content = [
      div [
Stephane Glondu's avatar
Stephane Glondu committed
216 217
        div [setup_form];
        div [br ()];
218 219
        h2 [pcdata "Elections being prepared"];
        setup_elections;
Stephane Glondu's avatar
Stephane Glondu committed
220
        div [br ()];
221 222 223 224 225 226
        h2 [pcdata "Elections you can administer"];
        elections;
      ];
    ] in
    lwt login_box = site_login_box () in
    base ~title ~login_box ~content ()
227

228 229 230 231 232 233 234 235 236
let format_date = Platform.format_datetime "%a, %d %b %Y %T %z"

let make_button ~service contents =
  let uri = Eliom_uri.make_string_uri ~service () in
  Printf.ksprintf Unsafe.data (* FIXME: unsafe *)
    "<button onclick=\"location.href='%s';\" style=\"font-size:35px;\">%s</button>"
    uri
    contents

237
let new_election_failure reason () =
238 239 240 241 242 243 244 245 246 247 248 249
  let title = "Create new election" in
  let reason =
    match reason with
    | `Exists -> pcdata "An election with the same UUID already exists."
    | `Exception e -> pcdata @@ Printexc.to_string e
  in
  let content = [
    div [
      p [pcdata "The creation failed."];
      p [reason];
    ]
  ] in
250
  lwt login_box = site_login_box () in
251
  base ~title ~login_box ~content ()
252

253
let generic_page ~title message () =
254 255 256 257 258 259
  let content = [
    p [pcdata message];
  ] in
  let login_box = pcdata "" in
  base ~title ~login_box ~content ()

260
let election_setup uuid se () =
261
  let title = "Preparation of election " ^ se.se_questions.t_name in
262 263 264 265 266 267 268 269
  let make_form ?a service value title =
    post_form ?a ~service
      (fun name ->
       [
         div [
           h2 [pcdata title];
           div [textarea ~a:[a_rows 5; a_cols 80] ~name ~value ()];
           div [string_input ~input_type:`Submit ~value:"Submit" ()];
270
         ]
271 272 273
       ]
      ) ()
  in
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
  let form_description =
    post_form ~service:election_setup_description
      (fun (name, description) ->
        [
          div [
            pcdata "Name of the election: ";
            string_input ~name:name
              ~input_type:`Text ~value:se.se_questions.t_name ();
          ];
          div [
            div [pcdata "Description of the election: "];
            div [
              textarea ~name:description ~a:[a_cols 80]
                ~value:se.se_questions.t_description ();
            ];
          ];
          div [
            string_input ~input_type:`Submit ~value:"Submit" ();
          ];
        ]
      ) uuid
  in
  let div_description =
    div [
      h2 [pcdata "Name and description of the election"];
      form_description;
    ]
  in
302 303 304 305 306 307 308 309 310
  let form_group =
    make_form
      ~a:[a_style "display: none;"]
      (Eliom_service.preapply election_setup_group uuid)
      se.se_group "Group parameters"
  in
  let form_metadata =
    let value = string_of_metadata se.se_metadata in
    make_form
Stephane Glondu's avatar
Stephane Glondu committed
311
      ~a:[a_style "display: none;"]
312 313 314
      (Eliom_service.preapply election_setup_metadata uuid)
      value "Election metadata"
  in
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  let form_auth =
    let checked_dummy, checked_password, checked_cas =
      match se.se_metadata.e_auth_config with
      | Some [x] ->
         (match x.auth_system with
         | "dummy" -> true, false, false
         | "password" -> false, true, false
         | "cas" -> false, false, true
         | _ -> false, false, false)
      | _ -> false, false, false
    in
    post_form ~service:election_setup_auth
      (fun name ->
        [
          div [
Stephane Glondu's avatar
Stephane Glondu committed
330 331
            string_radio ~checked:checked_password ~name ~value:"password" ();
            pcdata "Password (passwords will be emailed to voters)";
332 333
          ];
          div [
Stephane Glondu's avatar
Stephane Glondu committed
334 335
            string_radio ~checked:checked_dummy ~name ~value:"dummy" ();
            pcdata "Dummy (typically for a test election)";
336 337 338
          ];
          div [
            string_radio ~checked:checked_cas ~name ~value:"cas" ();
Stephane Glondu's avatar
Stephane Glondu committed
339
            pcdata "CAS (external authentication server)";
340 341
          ];
          div [
Stephane Glondu's avatar
Stephane Glondu committed
342
            string_input ~input_type:`Submit ~value:"Change authentication mode" ();
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
          ];
        ])
      uuid
  in
  let form_cas =
    match se.se_metadata.e_auth_config with
    | Some [x] ->
       (match x.auth_system with
       | "cas" ->
          let value =
            match x.auth_config with
            | ["server", x] -> x
            | _ -> ""
          in
          post_form ~service:election_setup_auth_cas
            (fun name ->
              [
                div [
                  pcdata "CAS server address: ";
                  string_input ~name ~input_type:`Text ~a:[a_size 40] ~value ();
                  string_input ~input_type:`Submit ~value:"Submit" ();
                ]
              ]) uuid
       | _ -> pcdata "")
    | _ -> pcdata ""
  in
  let form_password =
    match se.se_metadata.e_auth_config with
    | Some [x] ->
       (match x.auth_system with
       | "password" ->
          post_form ~service:election_setup_auth_genpwd
            (fun () ->
              [div [
Stephane Glondu's avatar
Stephane Glondu committed
377
                string_input ~input_type:`Submit ~value:"Generate passwords" ()
378 379 380 381 382
              ]]
            ) uuid
       | _ -> pcdata "")
    | _ -> pcdata ""
  in
383
  let div_questions =
Stephane Glondu's avatar
Stephane Glondu committed
384 385 386 387 388 389 390 391
    div [
      h2 [
        a
          ~service:election_setup_questions
          [pcdata "Edit questions"]
          uuid;
      ]
    ]
392
  in
Stephane Glondu's avatar
Stephane Glondu committed
393 394
  let div_voters =
    div [
Stephane Glondu's avatar
Stephane Glondu committed
395 396 397 398 399
      h2 [
        a ~service:election_setup_voters
          [pcdata "Edit voters"]
          uuid
      ];
Stephane Glondu's avatar
Stephane Glondu committed
400 401 402 403 404 405
      div [
        pcdata @@ string_of_int @@ List.length se.se_voters;
        pcdata " voter(s) registered";
      ];
    ]
  in
406 407 408
  let div_trustees =
    div [
      h2 [pcdata "Trustees"];
409 410 411 412 413 414
      div [
        pcdata "By default, the election server manages the keys of the ";
        pcdata "election. If you do not wish the server to store any keys, ";
        pcdata "click ";
        a ~service:election_setup_trustees [pcdata "here"] uuid;
        pcdata "."];
415
    ]
416 417
  in
  let div_credentials =
418 419
    div [
      h2 [pcdata "Credentials"];
Stephane Glondu's avatar
Stephane Glondu committed
420
      div [
421 422 423
        pcdata "The server may generate and email the credentials to the voters. If you prefer to delegate this task to another authority, click ";
        a ~service:election_setup_credential_authority [pcdata "here"] uuid;
        pcdata ".";
Stephane Glondu's avatar
Stephane Glondu committed
424 425 426 427 428
      ];
      post_form ~service:election_setup_credentials_server
        (fun () ->
          [string_input ~input_type:`Submit ~value:"Generate on server" ()]
        ) uuid;
429
    ]
430 431 432 433 434 435 436 437 438 439 440
  in
  let form_create =
    post_form
      ~service:election_setup_create
      (fun () ->
       [div
          [h2 [pcdata "Finalize creation"];
           string_input ~input_type:`Submit ~value:"Create election" ()]]
      ) uuid
  in
  let content = [
441 442
    div_description;
    hr ();
Stephane Glondu's avatar
Stephane Glondu committed
443 444
    div_questions;
    hr ();
Stephane Glondu's avatar
Stephane Glondu committed
445
    div_voters;
Stephane Glondu's avatar
Stephane Glondu committed
446
    hr ();
447
    div_credentials;
Stephane Glondu's avatar
Stephane Glondu committed
448
    hr ();
449 450
    form_group;
    form_metadata;
451 452 453 454 455 456
    div [
      h2 [pcdata "Authentication"];
      form_auth;
      form_cas;
      form_password;
    ];
Stephane Glondu's avatar
Stephane Glondu committed
457 458 459
    hr ();
    div_trustees;
    hr ();
460 461
    form_create;
  ] in
462
  lwt login_box = site_login_box () in
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
  base ~title ~login_box ~content ()

let election_setup_trustees uuid se () =
  let title = "Trustees for election " ^ se.se_questions.t_name in
  let form_trustees_add =
    post_form
      ~service:election_setup_trustee_add
      (fun () ->
        [string_input ~input_type:`Submit ~value:"Add" ()]) uuid
  in
  let form_trustees_del =
    post_form
      ~service:election_setup_trustee_del
      (fun () ->
        [string_input ~input_type:`Submit ~value:"Delete" ()]) uuid
  in
  let div_content =
    div [
      div [pcdata "If you do not wish the server to store any keys, you may nominate trustees. In that case, each trustee will create her own secret key. Be careful, once the election is over, you will need the contribution of each trustee to compute the result!"];
      br ();
      ol
        (List.rev_map
           (fun (token, pk) ->
             li [
               a ~service:election_setup_trustee [
                 pcdata @@ rewrite_prefix @@ Eliom_uri.make_string_uri
                   ~absolute:true
                   ~service:election_setup_trustee
                   token
               ] token
             ];
           ) se.se_public_keys
        );
      (if se.se_public_keys <> [] then
          div [
            pcdata "There is one link per trustee. Send each trustee her link.";
            br ();
            br ();
          ]
       else pcdata "");
      form_trustees_add;
      form_trustees_del;
    ]
  in
  let content = [div_content] in
  lwt login_box = site_login_box () in
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
  base ~title ~login_box ~content ()

let election_setup_credential_authority uuid se () =
  let title = "Credentials for election " ^ se.se_questions.t_name in
  let content = [
    div [
      pcdata "If you wish the credentials to be generated and managed by ";
      pcdata "an external authority, please send her the following link:";
    ];
    ul [
      li [
        a
          ~service:election_setup_credentials
          [
            pcdata @@ rewrite_prefix @@ Eliom_uri.make_string_uri
              ~absolute:true
              ~service:election_setup_credentials
              se.se_public_creds
          ]
          se.se_public_creds;
      ];
    ];
    div [
      pcdata "Note that this authority will have to send each credential to each voter herself.";
    ];
  ] in
  lwt login_box = site_login_box () in
536 537
  base ~title ~login_box ~content ()

538
let election_setup_questions uuid se () =
539
  let title = "Questions for election " ^ se.se_questions.t_name in
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
  let form =
    let value = string_of_template se.se_questions in
    post_form
      ~service:election_setup_questions_post
      (fun name ->
       [
         div [pcdata "Questions:"];
         div [textarea ~a:[a_id "questions"; a_rows 5; a_cols 80] ~name ~value ()];
         div [string_input ~input_type:`Submit ~value:"Submit" ()]])
      uuid
  in
  let link =
    let service = Web_services.election_setup in
    div [a ~service [pcdata "Go back to election preparation"] uuid]
  in
  let interactivity =
    div
      ~a:[a_id "interactivity"]
      [
        script ~a:[a_src (uri_of_string (fun () -> "../static/sjcl.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/jsbn.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/jsbn2.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/random.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/tool_js_questions.js"))] (pcdata "");
      ]
  in
  let content = [
    interactivity;
    form;
    link;
  ] in
571
  lwt login_box = site_login_box () in
572 573
  base ~title ~login_box ~content ()

574
let election_setup_voters uuid se () =
575
  let title = "Voters for election " ^ se.se_questions.t_name in
Stephane Glondu's avatar
Stephane Glondu committed
576 577 578 579 580 581 582 583 584 585 586
  let form =
    post_form
      ~service:election_setup_voters_post
      (fun name ->
        let value = String.concat "\n" se.se_voters in
        [
          div [textarea ~a:[a_rows 20; a_cols 50] ~name ~value ()];
          div [string_input ~input_type:`Submit ~value:"Submit" ()]])
      uuid
  in
  let content = [
Stephane Glondu's avatar
Stephane Glondu committed
587 588
    div [pcdata "Please enter the email addresses of the voters, one per line."];
    form;
Stephane Glondu's avatar
Stephane Glondu committed
589
  ] in
590
  lwt login_box = site_login_box () in
Stephane Glondu's avatar
Stephane Glondu committed
591 592
  base ~title ~login_box ~content ()

593
let election_setup_credentials token uuid se () =
594
  let title = "Credentials for election " ^ se.se_questions.t_name in
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
  let form_textarea =
    post_form
      ~service:election_setup_credentials_post
      (fun name ->
       [div
          [div [pcdata "Public credentials:"];
           div [textarea ~a:[a_id "pks"; a_rows 5; a_cols 40] ~name ()];
           div [string_input ~input_type:`Submit ~value:"Submit" ()]]])
      token
  in
  let disclaimer =
    p
      [
        b [pcdata "Note:"];
        pcdata " submitting a large (> 200) number of credentials using the above form may fail; in this case, you have to use the command-line tool and the form below.";
      ]
  in
  let form_file =
    post_form
      ~service:election_setup_credentials_post_file
      (fun name ->
       [div
          [h2 [pcdata "Submit by file"];
           div [pcdata "Use this form to upload public credentials generated with the command-line tool."];
           div [file_input ~name ()];
           div [string_input ~input_type:`Submit ~value:"Submit" ()]]])
      token
  in
  let div_download =
    p [a ~service:election_setup_credentials_download
           [pcdata "Download current file"]
           token]
  in
  let group =
    let name : 'a Eliom_parameter.param_name = Obj.magic "group" in
    let value = se.se_group in
    div
      ~a:[a_style "display:none;"]
      [
        div [pcdata "UUID:"];
        div [textarea ~a:[a_id "uuid"; a_rows 1; a_cols 40; a_readonly `ReadOnly] ~name ~value:uuid ()];
        div [pcdata "Group parameters:"];
        div [textarea ~a:[a_id "group"; a_rows 5; a_cols 40; a_readonly `ReadOnly] ~name ~value ()];
      ]
  in
640 641 642 643 644 645 646 647
  let voters =
    let name : 'a Eliom_parameter.param_name = Obj.magic "voters" in
    let value = String.concat "\n" se.se_voters in
    div [
      div [pcdata "List of voters:"];
      div [textarea ~a:[a_id "voters"; a_rows 5; a_cols 40; a_readonly `ReadOnly] ~name ~value ()];
    ]
  in
648 649 650 651 652 653 654 655 656 657 658
  let interactivity =
    div
      ~a:[a_id "interactivity"]
      [
        script ~a:[a_src (uri_of_string (fun () -> "../static/sjcl.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/jsbn.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/jsbn2.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/random.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/tool_js_credgen.js"))] (pcdata "");
      ]
  in
659
  let div_textarea = div [group; voters; interactivity; form_textarea; disclaimer] in
660 661 662 663 664 665 666 667 668
  let content = [
    div_download;
    div_textarea;
    form_file;
  ] in
  let login_box = pcdata "" in
  base ~title ~login_box ~content ()

let election_setup_trustee token uuid se () =
669
  let title = "Trustee for election " ^ se.se_questions.t_name in
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
  let form =
    let value = !(List.assoc token se.se_public_keys) in
    let service = Eliom_service.preapply election_setup_trustee_post token in
    post_form
      ~service
      (fun name ->
       [
         div [
           div [pcdata "Public key:"];
           div [textarea ~a:[a_rows 5; a_cols 40; a_id "pk"] ~name ~value ()];
           div [string_input ~input_type:`Submit ~value:"Submit" ()];
         ]
       ]
      ) ()
  in
  let group =
    let name : 'a Eliom_parameter.param_name = Obj.magic "group" in
    let value = se.se_group in
    div
      ~a:[a_style "display:none;"]
      [
        div [pcdata "Group parameters:"];
        div [textarea ~a:[a_id "group"; a_rows 5; a_cols 40; a_readonly `ReadOnly] ~name ~value ()];
      ]
  in
  let interactivity =
    div
      ~a:[a_id "interactivity"]
      [
        script ~a:[a_src (uri_of_string (fun () -> "../static/sjcl.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/jsbn.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/jsbn2.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/random.js"))] (pcdata "");
        script ~a:[a_src (uri_of_string (fun () -> "../static/tool_js_tkeygen.js"))] (pcdata "");
      ]
  in
  let content = [
    group;
    interactivity;
    form;
  ] in
  let login_box = pcdata "" in
  base ~title ~login_box ~content ()


let election_login_box w =
716
  let module W = (val w : WEB_ELECTION) in
Stephane Glondu's avatar
Stephane Glondu committed
717 718 719 720 721 722 723 724
  let module A = struct
    let get_user () =
      Web_auth_state.get_election_user W.election.e_params.e_uuid
    let get_auth_systems () =
      lwt l = Web_auth_state.get_config (Some W.election.e_params.e_uuid) in
      return @@ List.map fst l
  end in
  let auth = (module A : AUTH_SERVICES) in
725 726 727 728 729 730
  let module L = struct
    let login x =
      Eliom_service.preapply
        election_login
        ((W.election.e_params.e_uuid, ()), x)
    let logout =
Stephane Glondu's avatar
Stephane Glondu committed
731
      Eliom_service.preapply logout ()
732 733 734 735 736
  end in
  let links = (module L : AUTH_LINKS) in
  fun () -> make_login_box "" auth links

let file w x =
737
  let module W = (val w : WEB_ELECTION) in
738 739 740 741 742
  Eliom_service.preapply
    election_dir
    (W.election.e_params.e_uuid, x)

let election_home w state () =
743
  let module W = (val w : WEB_ELECTION) in
744
  let params = W.election.e_params in
745
  let state_ =
746 747
    match state with
    | `Closed ->
748 749 750 751
      [
        pcdata " ";
        b [pcdata "This election is currently closed."];
      ]
752
    | `Open -> []
753
    | `EncryptedTally (_, _, hash) ->
754 755 756 757 758 759 760 761 762 763 764 765
       [
         pcdata " ";
         b [pcdata "The election is closed and being tallied."];
         pcdata " The ";
         a
           ~service:election_dir
           [pcdata "encrypted tally"]
           (W.election.e_params.e_uuid, ESETally);
         pcdata " hash is ";
         b [pcdata hash];
         pcdata ".";
       ]
766
    | `Tallied _ ->
767 768 769
       [
         pcdata " ";
         b [pcdata "This election has been tallied."];
Stephane Glondu's avatar
Stephane Glondu committed
770
         pcdata " The result with ";
771 772
         a
           ~service:election_dir
Stephane Glondu's avatar
Stephane Glondu committed
773
           [pcdata "cryptographic proofs"]
774 775 776
           (W.election.e_params.e_uuid, ESResult);
         pcdata " is available."
       ]
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
  in
  let ballots_link =
    p ~a:[a_style "text-align:center;"] [
        a
          ~a:[a_style "font-size:25px;"]
          ~service:election_pretty_ballots [
            pcdata "See accepted ballots"
          ] ((params.e_uuid, ()), 1)
      ]
  in
  let footer = div ~a:[a_style "line-height:1.5em;"] [
    div [
      div [
        pcdata "Election fingerprint: ";
        code [ pcdata W.election.e_fingerprint ];
      ];
      div [
        pcdata "Audit data: ";
        a ~service:(file w ESRaw) [
          pcdata "parameters"
        ] ();
        pcdata ", ";
        a ~service:(file w ESCreds) [
          pcdata "public credentials"
        ] ();
        pcdata ", ";
        a ~service:(file w ESKeys) [
          pcdata "trustee public keys"
        ] ();
        pcdata ", ";
        a ~service:(file w ESBallots) [
          pcdata "ballots";
        ] ();
        pcdata ".";
      ];
    ]
  ] in
814
  let go_to_the_booth =
815 816 817 818 819 820 821 822 823 824 825 826
    div ~a:[a_style "text-align:center;"] [
      div [
        make_button
          ~service:(Eliom_service.preapply election_vote (params.e_uuid, ()))
          "Go to the booth";
        ];
      div [
        pcdata "or ";
        a
          ~service:(Eliom_service.preapply election_cast (params.e_uuid, ()))
          [pcdata "submit a raw ballot"] ();
      ];
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
    ]
  in
  let middle =
    match state with
    | `Tallied result ->
       let questions = Array.to_list W.election.e_params.e_questions in
       ul (List.mapi (fun i x ->
         let answers = Array.to_list x.q_answers in
         let answers = List.mapi (fun j x ->
           tr [td [pcdata x]; td [pcdata @@ string_of_int result.(i).(j)]]
         ) answers in
         let answers =
           match answers with
           | [] -> pcdata ""
           | x :: xs -> table x xs
         in
         li [
           pcdata x.q_question;
           answers;
         ]
       ) questions)
    | _ -> go_to_the_booth
  in
  let content = [
851
    p state_;
852 853
    br ();
    middle;
854 855 856 857 858
    br ();
    ballots_link;
  ] in
  lwt login_box = election_login_box w () in
  base ~title:params.e_name ~login_box ~content ~footer ()
859

860
let election_admin w ~is_featured state () =
861
  let module W = (val w : WEB_ELECTION) in
862
  let title = W.election.e_params.e_name ^ " — Administration" in
863
  let uuid_s = Uuidm.to_string W.election.e_params.e_uuid in
864
  let state_form checked =
865 866 867 868 869 870 871 872
    let service, value, msg =
      if checked then
        election_close, "Close election",
        "The election is open. Voters can vote. "
      else
        election_open, "Open election",
        "The election is closed. No one can vote. "
    in
873
    post_form
874 875
      ~service
      (fun () ->
876
       [
877 878
         pcdata msg;
         string_input ~input_type:`Submit ~value ();
879 880
       ]) (W.election.e_params.e_uuid, ())
  in
881
  lwt state_div =
882 883
    match state with
    | `Open ->
884
       return @@ div [
885 886 887
         state_form true;
       ]
    | `Closed ->
888
       return @@ div [
889 890 891 892 893 894
         state_form false;
         post_form
           ~service:election_compute_encrypted_tally
           (fun () ->
             [string_input
                 ~input_type:`Submit
895 896 897
                 ~value:"Tally the election"
                 ();
              pcdata " (Warning, this action is irreversible.)";
898 899
             ]) (W.election.e_params.e_uuid, ());
       ]
900 901 902 903 904 905 906 907 908 909 910 911
    | `EncryptedTally (npks, _, hash) ->
       let rec seq a b =
         if a <= b then a :: (seq (a+1) b) else []
       in
       lwt pds = Web_persist.get_partial_decryptions uuid_s in
       let trustees =
         List.map
           (fun trustee_id ->
             tr [
               td [
                 a
                   ~service:election_tally_trustees
912 913 914 915 916 917
                   [
                     pcdata @@ rewrite_prefix @@ Eliom_uri.make_string_uri
                       ~absolute:true
                       ~service:election_tally_trustees
                       (W.election.e_params.e_uuid, ((), trustee_id))
                   ]
918 919 920 921 922 923 924 925
                   (W.election.e_params.e_uuid, ((), trustee_id))
               ];
               td [
                 pcdata (if List.mem_assoc trustee_id pds then "Yes" else "No")
               ];
             ]
           ) (seq 1 npks)
       in
926 927 928 929 930 931 932 933 934 935
       let release_form =
         post_form
           ~service:election_tally_release
           (fun () ->
             [string_input
                 ~input_type:`Submit
                 ~value:"Combine partial decryptions"
                 ()
             ]) (W.election.e_params.e_uuid, ())
       in
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
       return @@ div [
         div [
           pcdata "The ";
           a
             ~service:election_dir
             [pcdata "encrypted tally"]
             (W.election.e_params.e_uuid, ESETally);
           pcdata " has been computed. Its hash is ";
           b [pcdata hash];
           pcdata ".";
         ];
         div [
           div [pcdata "We are now waiting for trustees..."];
           table
             (tr [
951
               td [pcdata "Trustee link"];
952 953 954
               td [pcdata "Done?"];
             ])
             trustees
955 956 957
         ];
         release_form;
       ]
958
    | `Tallied _ ->
959 960
       return @@ div [
         pcdata "This election has been tallied.";
961 962
       ]
  in
963 964 965 966 967 968 969 970
  let uuid = W.election.e_params.e_uuid in
  let content = [
    div [
      a ~service:Web_services.election_home [pcdata "Election home"] (uuid, ());
    ];
    div [
      a ~service:election_update_credential [pcdata "Update a credential"] (uuid, ());
    ];
Stephane Glondu's avatar
Stephane Glondu committed
971 972 973
    div [
      a ~service:election_dir [pcdata "Voter list"] (uuid, ESVoters);
    ];
974 975 976
    div [
      a ~service:election_dir [pcdata "Voting records"] (uuid, ESRecords);
    ];
977
    div [state_div];
978
  ] in
979
  lwt login_box = site_login_box () in
980
  base ~title ~login_box ~content ()
981

982
let update_credential w () =
983
  let module W = (val w : WEB_ELECTION) in
984 985 986 987
  let params = W.election.e_params in
  let form = post_form ~service:election_update_credential_post
    (fun (old, new_) ->
      [
Stephane Glondu's avatar
Stephane Glondu committed
988
        div [
989 990 991 992 993 994
          p [
            pcdata "\
              This form allows you to change a single credential at \
              a time. To get the hash of a credential, run the \
              following command:\
            ";
Stephane Glondu's avatar
Stephane Glondu committed
995
          ];
996 997
          pre [
            pcdata "printf old-credential | sha256sum";
Stephane Glondu's avatar
Stephane Glondu committed
998
          ];
999 1000 1001 1002
          p [
            pcdata "In the above command, ";
            code [pcdata "old-credential"];
            pcdata " should look like a big number written in base 10.";
Stephane Glondu's avatar
Stephane Glondu committed
1003 1004
          ];
        ];
1005 1006 1007
        p [
          pcdata "Hash of the old credential: ";
          string_input ~name:old ~input_type:`Text ~a:[a_size 64] ();
1008
        ];
1009 1010 1011
        p [
          pcdata "New credential: ";
          string_input ~name:new_ ~input_type:`Text ~a:[a_size 617] ();
1012
        ];
1013 1014 1015 1016 1017 1018 1019
        p [string_input ~input_type:`Submit ~value:"Submit" ()];
      ]
    ) (params.e_uuid, ())
  in
  let content = [
    form;
  ] in
1020
  lwt login_box = site_login_box () in
1021 1022 1023
  base ~title:params.e_name ~login_box ~content ()

let cast_raw w () =
1024
  let module W = (val w : WEB_ELECTION) in
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  let params = W.election.e_params in
  let form_rawballot = post_form ~service:election_cast_post
    (fun (name, _) ->
      [
        div [pcdata "Please paste your raw ballot in JSON format in the following box:"];
        div [textarea ~a:[a_rows 10; a_cols 40] ~name ()];
        div [string_input ~input_type:`Submit ~value:"Submit" ()];
      ]
    ) (params.e_uuid, ())
  in
  let form_upload = post_form ~service:election_cast_post
    (fun (_, name) ->
      [
        div [pcdata "Alternatively, you can also upload a file containing your ballot:"];
1039
        div [
1040 1041
          pcdata "File: ";
          file_input ~name ();
Stephane Glondu's avatar
Stephane Glondu committed
1042
        ];
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
        div [string_input ~input_type:`Submit ~value:"Submit" ()];
      ]
    ) (params.e_uuid, ())
  in
  let content = [
    h3 [ pcdata "Submit by copy/paste" ];
    form_rawballot;
    h3 [ pcdata "Submit by file" ];
    form_upload;
  ] in
  lwt login_box = election_login_box w () in
  base ~title:params.e_name ~login_box ~content ()

1056
let cast_confirmation w hash () =
1057
  let module W = (val w : WEB_ELECTION) in
Stephane Glondu's avatar
Stephane Glondu committed
1058
  lwt user = Web_auth_state.get_election_user W.election.e_params.e_uuid in
1059 1060 1061
  let params = W.election.e_params in
  let name = params.e_name in
  let user_div = match user with
1062
    | Some u ->
1063 1064 1065 1066 1067 1068 1069 1070
      post_form ~service:election_cast_confirm (fun () -> [
        p ~a:[a_style "text-align: center; padding: 10px;"] [
          pcdata "I am ";
          format_user u;
          pcdata " and ";
          string_input
            ~a:[a_style "font-size: 20px; cursor: pointer;"]
            ~input_type:`Submit ~value:"I cast my vote" ();
Stephane Glondu's avatar
Stephane Glondu committed
1071
          pcdata ".";
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
        ]
      ]) (params.e_uuid, ())
    | None ->
      div [
        pcdata "Please log in to confirm your vote.";
      ]
  in
  let progress = div ~a:[a_style "text-align:center;margin-bottom:20px;"] [
    pcdata "Input credential — Answer to questions — Review and encrypt";
    pcdata " — Authenticate — ";
    b [pcdata "Confirm"];
    pcdata " — Done";
    hr ();
  ] in
  let content = [
    progress;
    p [
      pcdata "Your ballot for ";
      em [pcdata name];
      pcdata " has been received, but not recorded yet. ";
      pcdata "Your smart ballot tracker is ";
      b [pcdata hash];
      pcdata ".";
      br ();
    ];
    br ();
    p [pcdata "Note: your ballot is encrypted and nobody can see its contents."];
    user_div;
    p [
      (let service =
        Eliom_service.preapply
          Web_services.election_home (W.election.e_params.e_uuid, ())
Stephane Glondu's avatar
Stephane Glondu committed
1104
      in
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
      a ~service [
        pcdata "Go back to election"
      ] ());
      pcdata ".";
    ];
  ] in
  lwt login_box = election_login_box w () in
  base ~title:name ~login_box ~content ()

let cast_confirmed w ~result () =
1115
  let module W = (val w : WEB_ELECTION) in
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
  let params = W.election.e_params in
  let name = params.e_name in
  let progress = div ~a:[a_style "text-align:center;margin-bottom:20px;"] [
    pcdata "Input credential — Answer to questions — Review and encrypt";
    pcdata " — Authenticate — Confirm — ";
    b [pcdata "Done"];
    hr ();
  ] in
  let result =
    match result with
    | `Valid hash ->
       [pcdata " has been accepted, your smart ballot tracker is ";
        b [pcdata hash];
        pcdata ". We invite you to save it and check its presence in the ";
        a ~service:election_pretty_ballots [pcdata "ballot box"] ((params.e_uuid, ()), 1);
        pcdata ".";
       ]
    | `Error e ->
       [pcdata " is rejected, because ";
        pcdata (Web_common.explain_error e);
        pcdata ".";
       ]
  in
  let content = [
    progress;
    p ([
      pcdata "Your ballot for ";
      em [pcdata name];
      ] @ result);
    p [
Stephane Glondu's avatar
Stephane Glondu committed
1146
      (let service = Eliom_service.preapply logout () in
1147 1148 1149 1150 1151 1152 1153 1154
      a ~service [
        pcdata "Log out and come back to election page"
      ] ());
      pcdata ".";
    ];
  ] in
  lwt login_box = election_login_box w () in
  base ~title:name ~login_box ~content ()
Stephane Glondu's avatar
Stephane Glondu committed
1155

1156
let pretty_ballots w hashes () =
1157
  let module W = (val w : WEB_ELECTION) in
1158 1159 1160
  let params = W.election.e_params in
  let title = params.e_name ^ " — Accepted ballots" in
  let nballots = ref 0 in
1161 1162 1163
  let hashes =
    List.sort (fun a b -> String.(compare (uppercase a) (uppercase b))) hashes
  in
1164
  let ballots =
1165
    List.map
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
      (fun h ->
       incr nballots;
       li
         [a
            ~service:election_pretty_ballot
            [pcdata h]
            ((params.e_uuid, ()), h)]
      ) hashes
  in
  let links =
    p
      [a
         ~service:Web_services.election_home
         [pcdata "Back to election"]
         (params.e_uuid, ())]
  in
  let content = [
Stephane Glondu's avatar
Stephane Glondu committed
1183
    p [pcdata "This is the list of ballots accepted so far."];
1184 1185 1186 1187
    ul ballots;
    links;
  ] in
  lwt login_box = election_login_box w () in
1188
  base ~title ~login_box ~content ()
1189

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237