Use blueprints for apps

This commit is contained in:
RunasSudo 2018-03-09 18:49:49 +11:00
parent ede6fcfaf9
commit 51594687e2
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 35 additions and 25 deletions

View File

@ -402,7 +402,6 @@ def logout():
@app.route('/auth/login_callback') @app.route('/auth/login_callback')
def login_callback(): def login_callback():
print(flask.session)
if 'login_next' in flask.session and flask.session['login_next']: if 'login_next' in flask.session and flask.session['login_next']:
return flask.redirect(flask.session['login_next']) return flask.redirect(flask.session['login_next'])
else: else:
@ -443,7 +442,7 @@ def email_authenticate():
for app_name in app.config['APPS']: for app_name in app.config['APPS']:
app_main = importlib.import_module(app_name + '.main') app_main = importlib.import_module(app_name + '.main')
app_main.main(app) app.register_blueprint(app_main.blueprint)
# === Model-Views === # === Model-Views ===

View File

@ -1,5 +1,5 @@
# Eos - Verifiable elections # Eos - Verifiable elections
# Copyright © 2017 RunasSudo (Yingtong Li) # Copyright © 2017-18 RunasSudo (Yingtong Li)
# #
# This program is free software: you can redistribute it and/or modify # 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 # it under the terms of the GNU Affero General Public License as published by
@ -23,7 +23,18 @@ from eos.redditauth.election import *
import base64 import base64
import uuid import uuid
def main(app): blueprint = flask.Blueprint('eosweb.redditauth', __name__)
app = None
oauth = None
reddit = None
@blueprint.record
def reddit_register(setup_state):
global app, oauth, reddit
app = setup_state.app
oauth = OAuth() oauth = OAuth()
reddit = oauth.remote_app('Reddit', reddit = oauth.remote_app('Reddit',
request_token_url=None, request_token_url=None,
@ -39,28 +50,28 @@ def main(app):
consumer_secret=app.config['REDDIT_OAUTH_CLIENT_SECRET'] consumer_secret=app.config['REDDIT_OAUTH_CLIENT_SECRET']
) )
@app.route('/auth/reddit/login')
def reddit_login():
return reddit.authorize(callback=app.config['BASE_URI'] + flask.url_for('reddit_oauth_authorized'), state=uuid.uuid4())
@reddit.tokengetter @reddit.tokengetter
def get_reddit_oauth_token(): def get_reddit_oauth_token():
return (flask.session.get('user').oauth_token, '') return (flask.session.get('user').oauth_token, '')
@app.route('/auth/reddit/oauth_callback') @blueprint.route('/auth/reddit/login')
def reddit_oauth_authorized(): def reddit_login():
resp = reddit.authorized_response() return reddit.authorize(callback=app.config['BASE_URI'] + flask.url_for('eosweb.redditauth.reddit_oauth_authorized'), state=uuid.uuid4())
if resp is None:
# Request denied
return flask.redirect(flask.url_for('login_cancelled'))
user = RedditUser() @blueprint.route('/auth/reddit/oauth_callback')
user.oauth_token = resp['access_token'] def reddit_oauth_authorized():
flask.session['user'] = user resp = reddit.authorized_response()
if resp is None:
# Request denied
return flask.redirect(flask.url_for('login_cancelled'))
me = reddit.get('https://oauth.reddit.com/api/v1/me', headers={ user = RedditUser()
'User-Agent': app.config['REDDIT_USER_AGENT'] user.oauth_token = resp['access_token']
}) flask.session['user'] = user
user.username = me.data['name']
return flask.redirect(flask.url_for('login_complete')) me = reddit.get('https://oauth.reddit.com/api/v1/me', headers={
'User-Agent': app.config['REDDIT_USER_AGENT']
})
user.username = me.data['name']
return flask.redirect(flask.url_for('login_complete'))