private void CastVote(VoteTicket ticket, string voteContent) { var protector = dataprotection.CreateProtector("SecureBallot"); var secureVote = protector.Protect(voteContent); var voteId = Guid.NewGuid().ToString(); lock (ticketsDb) { ticketsDb.Put(ticket.HashId, ticket.ToJson()); } lock (secureBallot) { var ballot = new BallotContent() { ElectionId = ticket.ElectionId, SecureVote = secureVote }; secureBallot.Put(voteId, ballot.ToJson()); } }
public async Task <IActionResult> GenerateTicket(string otp) { var pq = from p in _context.Person join u in _context.UserLogin on p.Id equals u.PersonFk where u.Provider == "AzureAD" && u.UserId == this.User.Identity.Name select p; if (await pq.CountAsync() != 1) { throw new Exception("Internal error! Too many persons associated with login " + this.User.Identity.Name); } var person = await pq.FirstAsync(); var votes = await GetElections(person); var availableVotes = votes.Where(v => !v.Item4 && !v.Item5.HasValue).Select(v => v.Item1.Id).ToList(); if (availableVotes.Count == 0) { return(Forbid("No ballot to cast")); } var now = DateTime.Now; var ckvotes = await(from v in _context.Voter join r in _context.Recognition on v.RecognitionFk equals r.Id join e in _context.Election on v.ElectionFk equals e.Id where availableVotes.Contains(v.Id) && !v.Vote.HasValue // ensure that the vote has not been casted select new { Voter = v, Recognition = r, Election = e }).ToListAsync(); if (ckvotes.Count() == 0) { return(Forbid("OTP non valida")); } var otps = ckvotes.GroupBy(v => v.Recognition.Otp); if (otps.Count() != 1) { throw new Exception("Internal error: too many Otps for ticket generation"); } if (otp != otps.First().Key) { return(Forbid("OTP non valida")); } // Ticket generation var tickets = new List <VoteTicket>(); foreach (var v in ckvotes) { var ticketIdClearText = v.Voter.Id + ":" + v.Voter.PersonFk; var sha256 = SHA256.Create(); var ticket = new VoteTicket(); ticket.HashId = ComputeSha256Hash(ticketIdClearText); ticket.Issued = DateTime.Now; ticket.Expiration = DateTime.Now + TimeSpan.FromMinutes(30); ticket.ElectionId = v.Voter.ElectionFk.ToString(); ticket.ElectionName = v.Election.Name; tickets.Add(ticket); v.Recognition.State = 1; var dbt = new VotingTicket() { Id = Guid.NewGuid(), Hash = ticket.HashId, Content = ticket.ToJson(), VoterFk = v.Voter.Id }; v.Voter.VotingTicketFk = dbt.Id; _context.VotingTicket.Add(dbt); } await _context.SaveChangesAsync(); var sertickets = System.Text.Json.JsonSerializer.Serialize(tickets); var protector = dataprotection.CreateProtector("EVSKeyExchange"); var secureTickets = protector.Protect(Encoding.UTF8.GetBytes(sertickets)); return(Json(Convert.ToBase64String(secureTickets))); }