From f31f5347e23c5567e9a89ab94d7cb03aa14269d5 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sun, 26 Nov 2017 11:17:28 +1100 Subject: [PATCH] Implement voters in terms of users --- build_js.sh | 1 + eos/base/election.py | 12 +++++-- eos/js.py | 2 ++ eos/redditauth/__init__.py | 0 eos/redditauth/election.py | 31 +++++++++++++++++ eosweb/core/main.py | 13 +++----- eosweb/core/static/nunjucks/booth/cast.html | 12 +++++-- .../core/templates/auth/login_cancelled.html | 33 +++++++++++++++++++ eosweb/redditauth/main.py | 6 +--- 9 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 eos/redditauth/__init__.py create mode 100644 eos/redditauth/election.py create mode 100644 eosweb/core/templates/auth/login_cancelled.html diff --git a/build_js.sh b/build_js.sh index fc765ab..535e25d 100755 --- a/build_js.sh +++ b/build_js.sh @@ -38,6 +38,7 @@ for f in eos.js_tests; do # Transcrypt bug perl -0777 -pi -e 's/property.call \((.*?), \g1.\g1.__impl__(.*?)\)/property.call ($1, $1.__impl__$2)/g' eos/__javascript__/$f.js + perl -0777 -pi -e 's/property.call \((.*?), \g1.\g1.__implpy_(.*?)\)/property.call ($1, $1.__impl__$2)/g' eos/__javascript__/$f.js done cp eos/__javascript__/eos.js_tests.js eosweb/core/static/js/eosjs.js diff --git a/eos/base/election.py b/eos/base/election.py index 0d7bc7e..7738e3f 100644 --- a/eos/base/election.py +++ b/eos/base/election.py @@ -41,11 +41,17 @@ class Vote(EmbeddedObject): class Voter(EmbeddedObject): _id = UUIDField() - name = StringField() votes = EmbeddedObjectListField() -class EmailVoter(Voter): - email = StringField() +class User(EmbeddedObject): + pass + +class UserVoter(Voter): + user = EmbeddedObjectField() + + @property + def name(self): + return self.user.name class Question(EmbeddedObject): prompt = StringField() diff --git a/eos/js.py b/eos/js.py index 17eaf9f..56608db 100644 --- a/eos/js.py +++ b/eos/js.py @@ -26,3 +26,5 @@ import eos.psr.crypto import eos.psr.election import eos.psr.mixnet import eos.psr.workflow + +import eos.redditauth.election diff --git a/eos/redditauth/__init__.py b/eos/redditauth/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/eos/redditauth/election.py b/eos/redditauth/election.py new file mode 100644 index 0000000..4c3790e --- /dev/null +++ b/eos/redditauth/election.py @@ -0,0 +1,31 @@ +# Eos - Verifiable elections +# Copyright © 2017 RunasSudo (Yingtong Li) +# +# 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. +# +# 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 . + +from eos.base.election import * +from eos.core.objects import * + +class RedditUser(User): + oauth_token = StringField(is_protected=True) + username = StringField() + + @property + def name(self): + return self.username + + def matched_by(self, other): + if not isinstance(other, RedditUser): + return False + return other.username == self.username diff --git a/eosweb/core/main.py b/eosweb/core/main.py index 6cf2546..ff7b0cd 100644 --- a/eosweb/core/main.py +++ b/eosweb/core/main.py @@ -92,11 +92,8 @@ def setup_test_election(): # Set election details election.name = 'Test Election' - voter = Voter() - election.voters.append(Voter(name='Alice')) - election.voters.append(Voter(name='Bob')) - election.voters.append(Voter(name='Charlie')) - election.voters.append(Voter(name='RunasSudo')) + from eos.redditauth.election import RedditUser + election.voters.append(UserVoter(user=RedditUser(username='RunasSudo'))) election.mixing_trustees.append(InternalMixingTrustee(name='Eos Voting')) election.mixing_trustees.append(InternalMixingTrustee(name='Eos Voting')) @@ -207,7 +204,7 @@ def election_api_cast_vote(election): voter = None for election_voter in election.voters: - if election_voter.name == flask.session['user'].username: + if election_voter.user.matched_by(flask.session['user']): voter = election_voter break @@ -223,8 +220,8 @@ def election_api_cast_vote(election): election.save() return flask.Response(json.dumps({ - 'voter': EosObject.serialise_and_wrap(voter), - 'vote': EosObject.serialise_and_wrap(vote) + 'voter': EosObject.serialise_and_wrap(voter, should_protect=True), + 'vote': EosObject.serialise_and_wrap(vote, should_protect=True) }), mimetype='application/json') @app.route('/debug') diff --git a/eosweb/core/static/nunjucks/booth/cast.html b/eosweb/core/static/nunjucks/booth/cast.html index c4ecc44..e50dccc 100644 --- a/eosweb/core/static/nunjucks/booth/cast.html +++ b/eosweb/core/static/nunjucks/booth/cast.html @@ -24,9 +24,9 @@

This election requires you to log in to vote. If you disconnected your internet connection earlier, you must now reconnect it before proceeding.

{% if username %} -

You are currently logged in as {{ username }}. Please select an option from the list below if you would like to switch accounts. Otherwise, click ‘Cast ballot’ to continue.

+

You are currently logged in as {{ username }}. Please select an option from the list below if you would like to switch accounts. Otherwise, click ‘Cast ballot’ to continue.

{% else %} -

You are not currently logged in. Please select an option from the list below to log in. Your ballot will be automatically cast once you have logged in.

+

You are not currently logged in. Please select an option from the list below to log in. Your ballot will be automatically cast once you have logged in.

{% endif %}