From 145aa88da2e9f4ee69bae791417e99ba98501674 Mon Sep 17 00:00:00 2001 From: Vidminas Mikucionis <5411598+Vidminas@users.noreply.github.com> Date: Wed, 4 Apr 2018 18:54:31 +0100 Subject: [PATCH] Added MLH scopes and more data --- hardwarecheckout/controllers/login.py | 16 ++++----- mlh_oauth.py | 49 ++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/hardwarecheckout/controllers/login.py b/hardwarecheckout/controllers/login.py index 62c1a12..4f80497 100644 --- a/hardwarecheckout/controllers/login.py +++ b/hardwarecheckout/controllers/login.py @@ -18,8 +18,7 @@ @app.route('/login') def login_page(): - mlh = MLHSignIn() - return mlh.authorize() + return MLHSignIn().authorize() @app.route('/callback/mlh') def oauth_callback(): @@ -27,20 +26,21 @@ def oauth_callback(): token = verify_token(request.cookies['jwt']) if token is not None: return redirect('/inventory') - mlh = MLHSignIn() - id_, email = mlh.callback() - if id_ is None: + + mlh_user = MLHSignIn().callback() + + if mlh_user is None or mlh_user.id is None: flash('Authentication failed.') return redirect('/inventory') - if User.query.filter_by(email=email).count() == 0: + + if User.query.filter_by(email=mlh_user.email).count() == 0: admin = email in config.ADMINS user = User(email, admin) db.session.add(user) db.session.commit() # generate token since we cut out quill - token = generate_auth_token(email) - + token = generate_auth_token(mlh_user.email) response = app.make_response(redirect('/inventory')) response.set_cookie('jwt', token.encode('utf-8')) diff --git a/mlh_oauth.py b/mlh_oauth.py index 745cba0..900cc1a 100644 --- a/mlh_oauth.py +++ b/mlh_oauth.py @@ -3,6 +3,19 @@ from rauth import OAuth1Service, OAuth2Service from hardwarecheckout import config +class MLHUser(): + def __init__(self, id, email, + level_of_study, school, major, + shirt_size, dietary_restrictions, special_needs): + self.id = id + self.email = email + self.level_of_study = level_of_study + self.school = school + self.major = major + self.shirt_size = shirt_size + self.dietary_restrictions = dietary_restrictions + self.special_needs = special_needs + class MLHSignIn(object): def __init__(self): credentials = config.OAUTH_CREDENTIALS @@ -17,6 +30,14 @@ def __init__(self): base_url='https://my.mlh.io/' ) + # Permission scopes for user data requests + # Possible scopes at https://my.mlh.io/docs#scopes_reference + self.scopes = [ + 'email', # Email address + 'education', # Level of study, school, major + 'event' # Shirt size, dietary restrictions, special needs + ] + def get_callback_url(self): return url_for("oauth_callback", _external=True) @@ -24,21 +45,31 @@ def authorize(self): return redirect(self.service.get_authorize_url( response_type='code', redirect_uri=self.get_callback_url()) + scopes='+'.join(self.scopes) ) def callback(self): if 'code' not in request.args: - return None, None, None + return None + oauth_session = self.service.get_auth_session( - data={'code': request.args['code'], - 'grant_type': 'authorization_code', - 'redirect_uri': self.get_callback_url()}, - decoder=json.loads + data = { + 'code': request.args['code'], + 'redirect_uri': self.get_callback_url(), + 'grant_type': 'authorization_code' + }, + decoder = json.loads ) me = oauth_session.get('/api/v2/user.json').json() + medata = me.get('data') - return ( - me.get('data').get('id'), - me.get('data').get('email') + return MLHUser( + medata.get('id'), + medata.get('email'), + medata.get('level_of_study'), + medata.get('school'), + medata.get('major'), + medata.get('shirt_size'), + medata.get('dietary_restrictions'), + medata.get('special_needs') ) -