From 20abbdae782d4cfeb17e24ac8b66909354dcf307 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 25 Nov 2017 23:16:29 +1100 Subject: [PATCH] Integrate authentication into voting booth Also fix a number (several too many) bugs that found their way into the system following the previous few commits --- eos/core/objects/__init__.py | 2 +- eosweb/core/main.py | 16 +++++-- eosweb/core/static/nunjucks/booth/cast.html | 48 ++++++++++++------- .../core/static/nunjucks/booth/complete.html | 4 +- .../core/static/nunjucks/booth/encrypt.html | 2 + eosweb/core/static/nunjucks/booth/review.html | 4 +- .../static/nunjucks/booth/selections.html | 8 ++-- .../question/approval/selections_make.html | 10 ++-- .../question/approval/selections_review.html | 2 +- eosweb/core/templates/auth/login.html | 2 +- eosweb/core/templates/election/booth.html | 7 ++- eosweb/redditauth/main.py | 4 +- 12 files changed, 69 insertions(+), 40 deletions(-) diff --git a/eos/core/objects/__init__.py b/eos/core/objects/__init__.py index 6f18e1c..396c083 100644 --- a/eos/core/objects/__init__.py +++ b/eos/core/objects/__init__.py @@ -149,7 +149,7 @@ class DateTimeField(Field): if is_python: return datetime.strptime(value, '%Y-%m-%dT%H:%M:%SZ') else: - return Date.parse(value) + return __pragma__('js', '{}', 'new Date(value)') @staticmethod def now(): diff --git a/eosweb/core/main.py b/eosweb/core/main.py index 3445211..6cf2546 100644 --- a/eosweb/core/main.py +++ b/eosweb/core/main.py @@ -96,6 +96,7 @@ def setup_test_election(): election.voters.append(Voter(name='Alice')) election.voters.append(Voter(name='Bob')) election.voters.append(Voter(name='Charlie')) + election.voters.append(Voter(name='RunasSudo')) election.mixing_trustees.append(InternalMixingTrustee(name='Eos Voting')) election.mixing_trustees.append(InternalMixingTrustee(name='Eos Voting')) @@ -172,8 +173,9 @@ def election_view(election): @using_election def election_booth(election): selection_model_view_map = EosObject.to_json({key._name: val for key, val in model_view_map.items()}) # ewww + auth_methods = EosObject.to_json(app.config['AUTH_METHODS']) - return flask.render_template('election/booth.html', election=election, selection_model_view_map=selection_model_view_map) + return flask.render_template('election/booth.html', election=election, selection_model_view_map=selection_model_view_map, auth_methods=auth_methods) @app.route('/election//view/questions') @using_election @@ -193,20 +195,24 @@ def election_view_trustees(election): @app.route('/election//cast_ballot', methods=['POST']) @using_election def election_api_cast_vote(election): - if election.workflow.get_task('eos.base.workflow.TaskOpenVoting').status >= WorkflowTask.Status.EXITED or election.workflow.get_task('eos.base.workflow.TaskCloseVoting').status <= WorkflowTask.Status.READY: + if election.workflow.get_task('eos.base.workflow.TaskOpenVoting').status < WorkflowTask.Status.EXITED or election.workflow.get_task('eos.base.workflow.TaskCloseVoting').status > WorkflowTask.Status.READY: # Voting is not yet open or has closed - return flask.Response('Voting is not yet open or has closed', 405) + return flask.Response('Voting is not yet open or has closed', 409) data = json.loads(flask.request.data) + if 'user' not in flask.session: + # User is not authenticated + return flask.Response('Not authenticated', 403) + voter = None for election_voter in election.voters: - if election_voter.name == data['auth']['username']: + if election_voter.name == flask.session['user'].username: voter = election_voter break if voter is None: - # User is not authenticated + # Invalid user return flask.Response('Invalid credentials', 403) # Cast the vote diff --git a/eosweb/core/static/nunjucks/booth/cast.html b/eosweb/core/static/nunjucks/booth/cast.html index 4709c84..c4ecc44 100644 --- a/eosweb/core/static/nunjucks/booth/cast.html +++ b/eosweb/core/static/nunjucks/booth/cast.html @@ -21,17 +21,24 @@ {% block content %}

Your vote has not yet been cast. If you have not already done so, please make a note of your ballot fingerprint, {{ eosjs.eos.core.hashing.__all__.SHA256().update_obj(ballot).hash_as_b64() }}.

-

This election requires you to log in to vote. Please enter your name below, then click ‘Cast ballot’ to cast your ballot.

-
-
- - -
-
- -
Error
-

The log in details you entered are not valid for this election. Please check your username and password and try again. If the issue persists, contact your election administrator.

-
+

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.

+ {% 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.

+ {% endif %} + + + +