Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions hardwarecheckout/controllers/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@

@app.route('/login')
def login_page():
mlh = MLHSignIn()
return mlh.authorize()
return MLHSignIn().authorize()

@app.route('/callback/mlh')
def oauth_callback():
if 'jwt' in request.cookies:
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'))

Expand Down
49 changes: 40 additions & 9 deletions mlh_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,28 +30,46 @@ 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)

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')
)