Commit e24c3bf0 authored by Stephane Glondu's avatar Stephane Glondu

Combine partial decryptions and release the result

parent 6f72b1aa
......@@ -183,6 +183,7 @@ type election_file =
| ESBallots
| ESRecords
| ESETally
| ESResult
let election_file_of_string = function
| "election.json" -> ESRaw
......@@ -191,6 +192,7 @@ let election_file_of_string = function
| "ballots.jsons" -> ESBallots
| "records" -> ESRecords
| "encrypted_tally.json" -> ESETally
| "result.json" -> ESResult
| x -> invalid_arg ("election_dir_item: " ^ x)
let string_of_election_file = function
......@@ -200,6 +202,7 @@ let string_of_election_file = function
| ESBallots -> "ballots.jsons"
| ESRecords -> "records"
| ESETally -> "encrypted_tally.json"
| ESResult -> "result.json"
let election_file = Eliom_parameter.user_type
~of_string:election_file_of_string
......
......@@ -77,6 +77,7 @@ type election_file =
| ESBallots
| ESRecords
| ESETally
| ESResult
val election_file_of_string : string -> election_file
val string_of_election_file : election_file -> string
......
......@@ -26,6 +26,7 @@ type election_state =
[ `Open
| `Closed
| `EncryptedTally of int * int * string
| `Tallied
]
let election_states = Ocsipersist.open_table "election_states"
......
......@@ -23,6 +23,7 @@ type election_state =
[ `Open
| `Closed
| `EncryptedTally of int * int * string
| `Tallied
]
val get_election_state : string -> election_state Lwt.t
val set_election_state : string -> election_state -> unit Lwt.t
......
......@@ -72,6 +72,7 @@ let election_pretty_ballot = service ~path:["elections"] ~get_params:(suffix_pro
let election_compute_encrypted_tally = post_coservice ~csrf_safe:true ~fallback:election_admin ~post_params:unit ()
let election_tally_trustees = service ~path:["elections"] ~get_params:(suffix (uuid "uuid" ** suffix_const "trustees" ** int "trustee_id")) ()
let election_tally_trustees_post = post_service ~fallback:election_tally_trustees ~post_params:(string "partial_decryption") ()
let election_tally_release = post_service ~fallback:election_admin ~post_params:unit ()
let election_dir = service ~path:["elections"] ~get_params:(suffix (uuid "uuid" ** election_file "file")) ()
......
......@@ -1044,8 +1044,50 @@ let () =
Html5.send
))
let () =
Any.register
~service:election_tally_release
(fun (uuid, ()) () ->
let uuid_s = Uuidm.to_string uuid in
let w = SMap.find uuid_s !election_table in
let module W = (val w) in
lwt () =
match_lwt Web_site_auth.get_user () with
| Some u when W.metadata.e_owner = Some u -> return ()
| _ -> forbidden ()
in
lwt npks, ntallied =
match_lwt Web_persist.get_election_state uuid_s with
| `EncryptedTally (npks, ntallied, _) -> return (npks, ntallied)
| _ -> forbidden ()
in
lwt pds = Web_persist.get_partial_decryptions uuid_s in
lwt pds =
try
return @@ Array.init npks (fun i ->
List.assoc (i+1) pds |> partial_decryption_of_string W.G.read
)
with Not_found -> fail_http 404
in
lwt et =
W.dir / string_of_election_file ESETally |>
Lwt_io.chars_of_file |> Lwt_stream.to_string >>=
wrap1 (encrypted_tally_of_string W.G.read)
in
let result = W.E.combine_factors ntallied et pds in
lwt () =
let open Lwt_io in
with_file
~mode:Output (W.dir / string_of_election_file ESResult)
(fun oc -> Lwt_io.write_line oc (string_of_result W.G.write result))
in
lwt () = Web_persist.set_election_state uuid_s `Tallied in
Eliom_service.preapply
election_admin (W.election.e_params.e_uuid, ()) |>
Redirection.send)
let content_type_of_file = function
| ESRaw | ESKeys | ESBallots | ESETally -> "application/json"
| ESRaw | ESKeys | ESBallots | ESETally | ESResult -> "application/json"
| ESCreds | ESRecords -> "text/plain"
let handle_pseudo_file w u f site_user =
......
......@@ -597,6 +597,17 @@ let election_home w state () =
b [pcdata hash];
pcdata ".";
]
| `Tallied ->
[
pcdata " ";
b [pcdata "This election has been tallied."];
pcdata " Its ";
a
~service:election_dir
[pcdata "result"]
(W.election.e_params.e_uuid, ESResult);
pcdata " is available."
]
in
let ballots_link =
p ~a:[a_style "text-align:center;"] [
......@@ -717,6 +728,16 @@ let election_admin w ~is_featured state auth () =
]
) (seq 1 npks)
in
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
return @@ div [
div [
pcdata "The ";
......@@ -736,7 +757,12 @@ let election_admin w ~is_featured state auth () =
td [pcdata "Done?"];
])
trustees
]
];
release_form;
]
| `Tallied ->
return @@ div [
pcdata "This election has been tallied.";
]
in
let uuid = W.election.e_params.e_uuid in
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment