Ejemplo n.º 1
0
def castVote():
    data = request.json
    print(data)
    electionId = data["election"]
    selection = data["selection"]
    selection = [int(x) for x in selection]
    selection.sort()
    voterId = data["voterId"]
    votingCode = data["votingCode"]
    manipulatedPublicCredential = data["manipulatedPublicCredential"]
    manipulatedPublicKey = data["manipulatedPublicKey"]

    if len(selection) == 0:
        return make_error(400, "Empty selection")

    try:
        # prepare voteService
        voteSvc = VoteService(electionId)

        # perform action
        voteSvc.castVote(voterId, selection, votingCode,
                         manipulatedPublicCredential, manipulatedPublicKey)

        # retrieve and persist modified state
        patches = voteSvc.persist()
        syncPatches(electionId, SyncType.ROOM, patches)

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        return make_error(
            500, '%s:%s: %s. %s' %
            (fname, exc_tb.tb_lineno, e, traceback.format_exc()))

    return json.dumps({'result': 'success'})
Ejemplo n.º 2
0
def deleteElection(id):
    try:
        result = db.elections.delete_one({'_id': ObjectId(id)})

        if result.deleted_count == 1:
            # update the election list on all clients
            syncElections(SyncType.BROADCAST)

            return json.dumps({})
        else:
            return make_error(500, 'Election with ID "%s" was not found.' % id)
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        return make_error(500, '%s:%s: %s' % (fname, exc_tb.tb_lineno, e))
Ejemplo n.º 3
0
def debugVotingSim():
    electionId = request.json["election"]
    from gmpy2 import mpz
    try:
        voteSvc = VoteService(electionId)  # prepare voteService
        #from chvote.Types import Ballot
        #voteSvc.bulletinBoard.confirmations[0].finalizations.append(1)
        #patches = voteSvc.persist()
        #syncPatches(electionId, SyncType.ROOM, patches)
        import app.utils.jsonpatch as jsonpatch
        import copy
        from chvote.Types import VoterConfirmation
        from app.utils.JsonParser import mpzconverter

        old = {'key': [{'someNumber': 0, 'someArray': [1, 2, 3]}]}

        new = {'key': [{'someNumber': 0, 'someArray': [1, 2, 3, 4]}]}

        patches = jsonpatch.make_patch(old, new)
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        return make_error(500, '%s:%s: %s' % (fname, exc_tb.tb_lineno, e))

    return json.dumps({'result': 'success'})
def generateElectorateData():
    data = request.json
    electionId = data["election"]
    authorityId = data["authorityId"]

    try:
        # prepare voteService
        voteSvc = VoteService(electionId)

        # perform action
        voteSvc.generateElectorateData(authorityId)

        #patches = voteSvc.getJSONPatches()
        # retrieve and persist modified state
        patches = voteSvc.persist()
        syncPatches(electionId, SyncType.ROOM, patches)

        # update election status
        # voteSvc.updateStatus(1)
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        return make_error(500, '%s:%s: %s' % (fname, exc_tb.tb_lineno, e))

    return json.dumps({'result': 'success'})
Ejemplo n.º 5
0
def sendVotingCards():
    electionId = request.json["election"]

    try:
        voteSvc = VoteService(electionId)  # prepare voteService
        voteSvc.sendVotingCards()
        patches = voteSvc.persist()
        syncPatches(electionId, SyncType.ROOM, patches)

        # update election status
        voteSvc.updateStatus(3)
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        return make_error(500, '%s:%s: %s' % (fname, exc_tb.tb_lineno, e))

    return json.dumps({'result': 'success'})
Ejemplo n.º 6
0
def startDecryptionPhase():
    data = request.json
    electionId = data["election"]
    try:
        # prepare voteService
        voteSvc = VoteService(electionId)

        voteSvc.startDecryption()

        # retrieve and persist modified state
        patches = voteSvc.persist()
        syncPatches(electionId, SyncType.ROOM, patches)
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        return make_error(500, '%s:%s: %s' % (fname, exc_tb.tb_lineno, e))

    return json.dumps({'id': str(id)})
Ejemplo n.º 7
0
def setUpElection():
    data = request.json
    electionId = data["election"]
    numberOfVoters = json.loads(data["numberOfVoters"])
    candidates = data["candidates"]
    countingCircles = json.loads(data["countingCircles"])
    numberOfSelections = data["numberOfSelections"]
    numberOfCandidates = data["numberOfCandidates"]
    titles = data["titles"]

    try:
        # validate vote parameters
        if len(candidates) != sum(numberOfCandidates):
            raise RuntimeError(
                "The numberOfCandidates must match the number of candidates")
        if numberOfVoters != len(countingCircles):
            raise RuntimeError(
                "The length of countingCircles must match the number of voters"
            )
        if len(numberOfSelections) != len(numberOfCandidates):
            raise RuntimeError(
                "The length of numberOfSelections must match the length of numberOfCandidates"
            )

        # prepare voteService
        voteSvc = VoteService(electionId)

        # perform action
        voteSvc.setupElection(numberOfVoters, countingCircles, candidates,
                              numberOfCandidates, numberOfSelections, titles)

        #patches = voteSvc.getJSONPatches()
        # retrieve and persist modified state
        patches = voteSvc.persist()
        syncPatches(electionId, SyncType.ROOM, patches)

        # update election status
        voteSvc.updateStatus(1)
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        return make_error(500, '%s:%s: %s' % (fname, exc_tb.tb_lineno, e))

    return json.dumps({'result': 'success'})
Ejemplo n.º 8
0
def revealCode():
    data = request.json
    electionId = data["election"]
    voterId = int(data["voterId"])
    codeIndex = int(data["codeIndex"])

    try:
        # prepare voteService
        voteSvc = VoteService(electionId)

        # perform action
        voteSvc.revealCode(voterId, codeIndex)

        # retrieve and persist modified state
        patches = voteSvc.persist()
        syncPatches(electionId, SyncType.ROOM, patches)

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        return make_error(500, '%s:%s: %s' % (fname, exc_tb.tb_lineno, e))

    return json.dumps({'id': str(id)})
Ejemplo n.º 9
0
def setAutoMode():
    data = request.json
    electionId = data["election"]
    authorityId = data["authorityId"]
    value = data["value"]

    try:
        # prepare voteService
        voteSvc = VoteService(electionId)

        # perform action
        voteSvc.authorities[authorityId].autoCheck = value

        # retrieve and persist modified state
        patches = voteSvc.persist()
        syncPatches(electionId, SyncType.ROOM, patches)

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        return make_error(500, '%s:%s: %s' % (fname, exc_tb.tb_lineno, e))

    return json.dumps({'id': str(id)})
Ejemplo n.º 10
0
def abortVote():
    data = request.json
    electionId = data["election"]
    voterId = data["voterId"]

    try:
        # prepare voteService
        voteSvc = VoteService(electionId)

        # perform action
        voteSvc.abortVote(voterId)

        # retrieve and persist modified state
        patches = voteSvc.persist()
        syncPatches(electionId, SyncType.ROOM, patches)

    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        return make_error(
            500, '%s:%s: %s. %s' %
            (fname, exc_tb.tb_lineno, e, traceback.format_exc()))

    return json.dumps({'result': 'success'})
Ejemplo n.º 11
0
def createElection():
    data = request.json
    securityLevel = data["securityLevel"]

    try:
        # Create a new election
        id = db.elections.insert({
            'title': data["title"],
            'status': 0,
            'revision': 0
        })

        # create a new (empty) BulletinBoardState
        newBBState = BulletinBoardState(str(id))
        newBBState.securityLevel = securityLevel
        db.bulletinBoardStates.insert({
            'election': str(id),
            'state': serializeState(newBBState)
        })

        # create new electionAuthority states
        for j in range(3):
            newAuthState = ElectionAuthorityState(j)
            if (j != 0):
                newAuthState.autoCheck = True
            db.electionAuthorityStates.insert({
                'election':
                str(id),
                'authorityID':
                j,
                'state':
                serializeState(newAuthState)
            })

        # create new printing authority state
        printingAuthState = PrintingAuthorityState()
        db.printingAuthorityStates.insert({
            'election':
            str(id),
            'state':
            serializeState(printingAuthState)
        })

        # create new election administrator state
        electionAdminState = ElectionAdministratorState()
        db.electionAdministratorStates.insert({
            'election':
            str(id),
            'state':
            serializeState(electionAdminState)
        })

        # update the election list on all clients
        syncElections(SyncType.BROADCAST)
        # return the new elections id to the client so it can load the overview directly
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        return make_error(500, '%s:%s: %s' % (fname, exc_tb.tb_lineno, e))

    return json.dumps({'id': str(id)})