Add NationStates authentication

This commit is contained in:
RunasSudo 2019-02-13 09:46:11 +11:00
parent 1bc558fc97
commit 993ec142ac
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 144 additions and 0 deletions

30
eos/nsauth/election.py Normal file
View File

@ -0,0 +1,30 @@
# Eos - Verifiable elections
# Copyright © 2017-2019 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 <http://www.gnu.org/licenses/>.
from eos.base.election import *
from eos.core.objects import *
class NationStatesUser(User):
username = StringField()
@property
def name(self):
return self.username
def matched_by(self, other):
if not isinstance(other, NationStatesUser):
return False
return other.username.lower().strip().replace(' ', '_') == self.username.lower().strip().replace(' ', '_')

46
eosweb/nsauth/main.py Normal file
View File

@ -0,0 +1,46 @@
# Eos - Verifiable elections
# Copyright © 2017-2019 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 <http://www.gnu.org/licenses/>.
import flask
from eos.nsauth.election import *
import urllib.request, urllib.parse
blueprint = flask.Blueprint('eosweb.nsauth', __name__, template_folder='templates')
app = None
@blueprint.record
def reddit_register(setup_state):
global app
app = setup_state.app
@blueprint.route('/auth/nationstates/login')
def nationstates_login():
return flask.render_template('auth/nationstates/login.html')
@blueprint.route('/auth/nationstates/authenticate', methods=['POST'])
def nationstates_authenticate():
username = flask.request.form['username'].lower().strip().replace(' ', '_')
with urllib.request.urlopen(urllib.request.Request('https://www.nationstates.net/cgi-bin/api.cgi?a=verify&' + urllib.parse.urlencode({'nation': username, 'checksum': flask.request.form['checksum']}), headers={'User-Agent': app.config['NATIONSTATES_USER_AGENT']})) as resp:
if resp.read().decode('utf-8').strip() != '1':
return flask.render_template('auth/nationstates/login.html', error='The nation name or verification code you entered was invalid. Please check your details and try again. If the issue persists, contact the election administrator.')
flask.session['user'] = NationStatesUser(username=username)
return flask.redirect(flask.url_for('login_complete'))

17
eosweb/nsauth/settings.py Normal file
View File

@ -0,0 +1,17 @@
# Eos - Verifiable elections
# Copyright © 2017-2019 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 <http://www.gnu.org/licenses/>.
NATIONSTATES_USER_AGENT = 'FIXME'

View File

@ -0,0 +1,51 @@
{% extends 'semantic_base.html' %}
{#
Eos - Verifiable elections
Copyright © 2017-2019 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 <http://www.gnu.org/licenses/>.
#}
{% block title %}Log in{% endblock %}
{% block basecontent %}
<div class="ui middle aligned center aligned grid" style="height: 100%;">
<div class="column" style="max-width: 400px;">
<p>1. Log in to NationStates below if necessary, and copy your <i>Login Verification Code</i>.</p>
<iframe src="https://m.nationstates.net/page=verify_login" style="width: 100%; height: 10em;"></iframe>
<p>2. Type your nation name and paste your Login Verification Code into the form below.</p>
<form class="ui large form" action="{{ url_for('eosweb.nsauth.nationstates_authenticate') }}" method="post">
{% if error %}
<div class="ui visible error message">{{ error }}</div>
{% endif %}
<div class="ui stacked segment">
<div class="field">
<div class="ui left icon input">
<i class="user icon"></i>
<input type="text" name="username" placeholder="Nation name">
</div>
</div>
<div class="field">
<div class="ui left icon input">
<i class="linkify icon"></i>
<input type="text" name="checksum" placeholder="Login verification code">
</div>
</div>
<input type="submit" class="ui fluid large teal submit button" value="Log in">
</div>
</form>
</div>
</div>
{% endblock %}