2017-11-25 22:37:54 +11:00
|
|
|
# Eos - Verifiable elections
|
2018-03-09 18:49:49 +11:00
|
|
|
# Copyright © 2017-18 RunasSudo (Yingtong Li)
|
2017-11-25 22:37:54 +11:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2020-07-11 17:59:05 +10:00
|
|
|
from authlib.integrations.flask_client import OAuth
|
2017-11-25 22:37:54 +11:00
|
|
|
|
|
|
|
import flask
|
|
|
|
|
2017-11-26 11:17:28 +11:00
|
|
|
from eos.redditauth.election import *
|
2017-11-25 22:37:54 +11:00
|
|
|
|
|
|
|
import base64
|
|
|
|
import uuid
|
|
|
|
|
2018-03-09 18:49:49 +11:00
|
|
|
blueprint = flask.Blueprint('eosweb.redditauth', __name__)
|
|
|
|
|
|
|
|
app = None
|
|
|
|
oauth = None
|
|
|
|
|
|
|
|
@blueprint.record
|
|
|
|
def reddit_register(setup_state):
|
2020-07-11 17:59:05 +10:00
|
|
|
global app, oauth
|
2018-03-09 18:49:49 +11:00
|
|
|
|
|
|
|
app = setup_state.app
|
|
|
|
|
2020-07-11 17:59:05 +10:00
|
|
|
oauth = OAuth(app)
|
|
|
|
oauth.register('reddit',
|
|
|
|
#request_token_url=None,
|
2017-11-25 22:37:54 +11:00
|
|
|
authorize_url='https://www.reddit.com/api/v1/authorize.compact',
|
2020-07-11 17:59:05 +10:00
|
|
|
authorize_params={'duration': 'temporary', 'scope': 'identity'},
|
2017-11-25 22:37:54 +11:00
|
|
|
access_token_url='https://www.reddit.com/api/v1/access_token',
|
|
|
|
access_token_method='POST',
|
|
|
|
access_token_headers={
|
|
|
|
'Authorization': 'Basic ' + base64.b64encode('{}:{}'.format(app.config['REDDIT_OAUTH_CLIENT_ID'], app.config['REDDIT_OAUTH_CLIENT_SECRET']).encode('ascii')).decode('ascii'),
|
|
|
|
'User-Agent': app.config['REDDIT_USER_AGENT']
|
|
|
|
},
|
2020-07-11 17:59:05 +10:00
|
|
|
client_id=app.config['REDDIT_OAUTH_CLIENT_ID'],
|
|
|
|
client_secret=app.config['REDDIT_OAUTH_CLIENT_SECRET'],
|
|
|
|
fetch_token=lambda: flask.session.get('user').oauth_token
|
2017-11-25 22:37:54 +11:00
|
|
|
)
|
2018-03-09 18:49:49 +11:00
|
|
|
|
|
|
|
@blueprint.route('/auth/reddit/login')
|
|
|
|
def reddit_login():
|
2020-07-11 17:59:05 +10:00
|
|
|
return oauth.reddit.authorize_redirect(redirect_uri=app.config['BASE_URI'] + flask.url_for('eosweb.redditauth.reddit_oauth_authorized'), state=str(uuid.uuid4()))
|
2018-03-09 18:49:49 +11:00
|
|
|
|
|
|
|
@blueprint.route('/auth/reddit/oauth_callback')
|
|
|
|
def reddit_oauth_authorized():
|
2020-07-11 17:59:05 +10:00
|
|
|
try:
|
|
|
|
token = oauth.reddit.authorize_access_token()
|
|
|
|
except:
|
2018-03-09 18:49:49 +11:00
|
|
|
# Request denied
|
|
|
|
return flask.redirect(flask.url_for('login_cancelled'))
|
2017-11-25 22:37:54 +11:00
|
|
|
|
2018-03-09 18:49:49 +11:00
|
|
|
user = RedditUser()
|
2020-07-11 17:59:05 +10:00
|
|
|
user.oauth_token = token
|
2018-03-09 18:49:49 +11:00
|
|
|
flask.session['user'] = user
|
|
|
|
|
2020-07-11 17:59:05 +10:00
|
|
|
me = oauth.reddit.get('https://oauth.reddit.com/api/v1/me', headers={
|
2018-03-09 18:49:49 +11:00
|
|
|
'User-Agent': app.config['REDDIT_USER_AGENT']
|
|
|
|
})
|
2020-07-11 17:59:05 +10:00
|
|
|
user.username = me.json()['name']
|
2018-03-09 18:49:49 +11:00
|
|
|
|
|
|
|
return flask.redirect(flask.url_for('login_complete'))
|