Code review fixes

This commit is contained in:
Joseph Schorr 2014-09-04 17:54:51 -04:00
parent 11176215e1
commit 1c2de35f28
3 changed files with 20 additions and 26 deletions

View file

@ -39,7 +39,6 @@ def user_view(user):
organizations = model.get_user_organizations(user.username) organizations = model.get_user_organizations(user.username)
def login_view(login): def login_view(login):
print login.metadata_json
try: try:
metadata = json.loads(login.metadata_json) metadata = json.loads(login.metadata_json)
except: except:

View file

@ -4,7 +4,7 @@ from flask import request, redirect, url_for, Blueprint
from flask.ext.login import current_user from flask.ext.login import current_user
from endpoints.common import render_page_template, common_login, route_show_if from endpoints.common import render_page_template, common_login, route_show_if
from app import app, analytics from app import app, analytics, get_app_url
from data import model from data import model
from util.names import parse_repository_name from util.names import parse_repository_name
from util.validation import generate_valid_usernames from util.validation import generate_valid_usernames
@ -22,6 +22,11 @@ client = app.config['HTTPCLIENT']
callback = Blueprint('callback', __name__) callback = Blueprint('callback', __name__)
def render_ologin_error(service_name,
error_message='Could not load user data. The token may have expired.'):
return render_page_template('ologinerror.html', service_name=service_name,
error_message=error_message,
service_url=get_app_url())
def exchange_code_for_token(code, service_name='GITHUB', for_login=True, form_encode=False, def exchange_code_for_token(code, service_name='GITHUB', for_login=True, form_encode=False,
redirect_suffix=''): redirect_suffix=''):
@ -96,15 +101,12 @@ def conduct_oauth_login(service_name, user_id, username, email, metadata={}):
analytics.alias(to_login.username, state) analytics.alias(to_login.username, state)
except model.DataModelException, ex: except model.DataModelException, ex:
return render_page_template('ologinerror.html', service_name=service_name, return render_ologin_error(service_name, ex.message)
error_message=ex.message)
if common_login(to_login): if common_login(to_login):
return redirect(url_for('web.index')) return redirect(url_for('web.index'))
return render_page_template('ologinerror.html', service_name=service_name, return render_ologin_error(service_name)
error_message='Unknown error')
def get_google_username(user_data): def get_google_username(user_data):
username = user_data['email'] username = user_data['email']
@ -120,17 +122,16 @@ def get_google_username(user_data):
def google_oauth_callback(): def google_oauth_callback():
error = request.args.get('error', None) error = request.args.get('error', None)
if error: if error:
return render_page_template('ologinerror.html', service_name='Google', error_message=error) return render_ologin_error('Google', error)
token = exchange_code_for_token(request.args.get('code'), service_name='GOOGLE', form_encode=True) token = exchange_code_for_token(request.args.get('code'), service_name='GOOGLE', form_encode=True)
user_data = get_google_user(token) user_data = get_google_user(token)
if not user_data or not user_data.get('id', None) or not user_data.get('email', None): if not user_data or not user_data.get('id', None) or not user_data.get('email', None):
return render_page_template('ologinerror.html', service_name = 'Google', return render_ologin_error('Google')
error_message='Could not load user data')
username = get_google_username(user_data) username = get_google_username(user_data)
metadata = { metadata = {
'service_username': username 'service_username': user_data['email']
} }
return conduct_oauth_login('Google', user_data['id'], username, user_data['email'], return conduct_oauth_login('Google', user_data['id'], username, user_data['email'],
@ -142,13 +143,12 @@ def google_oauth_callback():
def github_oauth_callback(): def github_oauth_callback():
error = request.args.get('error', None) error = request.args.get('error', None)
if error: if error:
return render_page_template('ologinerror.html', service_name = 'GitHub', error_message=error) return render_ologin_error('GitHub', error)
token = exchange_code_for_token(request.args.get('code'), service_name='GITHUB') token = exchange_code_for_token(request.args.get('code'), service_name='GITHUB')
user_data = get_github_user(token) user_data = get_github_user(token)
if not user_data: if not user_data:
return render_page_template('ologinerror.html', service_name = 'GitHub', return render_ologin_error('GitHub')
error_message='Could not load user data')
username = user_data['login'] username = user_data['login']
github_id = user_data['id'] github_id = user_data['id']
@ -186,15 +186,14 @@ def google_oauth_attach():
user_data = get_google_user(token) user_data = get_google_user(token)
if not user_data or not user_data.get('id', None): if not user_data or not user_data.get('id', None):
return render_page_template('ologinerror.html', service_name = 'Google', return render_ologin_error('Google')
error_message='Could not load user data')
google_id = user_data['id'] google_id = user_data['id']
user_obj = current_user.db_user() user_obj = current_user.db_user()
username = get_google_username(user_data) username = get_google_username(user_data)
metadata = { metadata = {
'service_username': username 'service_username': user_data['email']
} }
try: try:
@ -202,9 +201,7 @@ def google_oauth_attach():
except IntegrityError: except IntegrityError:
err = 'Google account %s is already attached to a %s account' % ( err = 'Google account %s is already attached to a %s account' % (
username, app.config['REGISTRY_TITLE_SHORT']) username, app.config['REGISTRY_TITLE_SHORT'])
return render_ologin_error('Google', err)
return render_page_template('ologinerror.html', service_name = 'Google',
error_message=err)
return redirect(url_for('web.user')) return redirect(url_for('web.user'))
@ -216,8 +213,7 @@ def github_oauth_attach():
token = exchange_code_for_token(request.args.get('code'), service_name='GITHUB') token = exchange_code_for_token(request.args.get('code'), service_name='GITHUB')
user_data = get_github_user(token) user_data = get_github_user(token)
if not user_data: if not user_data:
return render_page_template('ologinerror.html', service_name = 'GitHub', return render_ologin_error('GitHub')
error_message='Could not load user data')
github_id = user_data['id'] github_id = user_data['id']
user_obj = current_user.db_user() user_obj = current_user.db_user()
@ -233,8 +229,7 @@ def github_oauth_attach():
err = 'Github account %s is already attached to a %s account' % ( err = 'Github account %s is already attached to a %s account' % (
username, app.config['REGISTRY_TITLE_SHORT']) username, app.config['REGISTRY_TITLE_SHORT'])
return render_page_template('ologinerror.html', service_name = 'Github', return render_ologin_error('GitHub', err)
error_message=err)
return redirect(url_for('web.user')) return redirect(url_for('web.user'))

View file

@ -15,7 +15,7 @@
{% endif %} {% endif %}
<div> <div>
Please register using the <a href="/">registration form</a> to continue. Please register using the <a ng-href="{{ service_url }}/signin" target="_self">registration form</a> to continue.
You will be able to connect your account to your Quay.io account You will be able to connect your account to your Quay.io account
in the user settings. in the user settings.
</div> </div>