diff options
Diffstat (limited to 'fbin/login.py')
-rw-r--r-- | fbin/login.py | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/fbin/login.py b/fbin/login.py index b9602a8..00f969d 100644 --- a/fbin/login.py +++ b/fbin/login.py @@ -11,6 +11,7 @@ from .db import db, User, UserSession login_manager = LoginManager() + class BinUser: def __init__(self, user, user_session): self.user = user @@ -18,23 +19,26 @@ class BinUser: self.token = None def refresh_access_token(self): - response = requests.post(urljoin(current_app.config['OAUTH_URL'], 'token'), data = { + response = requests.post(urljoin(current_app.config['OAUTH_URL'], 'token'), data={ 'grant_type': 'refresh_token', 'client_id': current_app.config['OAUTH_CLIENT_ID'], 'client_secret': current_app.config['OAUTH_CLIENT_SECRET'], 'refresh_token': self.user_session.refresh_token, }) if response.status_code != 200: - flash('Failed to refresh authentication token (API call returned {} {})'.format(response.status_code, response.reason), 'error') + flash('Failed to refresh authentication token (API call returned {} {})'.format(response.status_code, + response.reason), 'error') return token = response.json() if 'error' in token: flash('Failed to refresh authentication token ({})'.format(token['error']), 'error') return try: - access_data = jwt.decode(token['access_token'], key = current_app.config['JWT_PUBLIC_KEY'], audience = current_app.config['OAUTH_CLIENT_ID']) - refresh_data = jwt.decode(token['refresh_token'], key = current_app.config['JWT_PUBLIC_KEY'], audience = current_app.config['OAUTH_CLIENT_ID']) - except jwt.InvalidTokenError as e: + jwt.decode(token['access_token'], key=current_app.config['JWT_PUBLIC_KEY'], + audience=current_app.config['OAUTH_CLIENT_ID']) + jwt.decode(token['refresh_token'], key=current_app.config['JWT_PUBLIC_KEY'], + audience=current_app.config['OAUTH_CLIENT_ID']) + except jwt.InvalidTokenError: traceback.print_exc() flash('Failed to refresh authentication token (verification failed)', 'error') return @@ -52,12 +56,13 @@ class BinUser: if self.token: return True try: - self.token = jwt.decode(self.user_session.access_token, key = current_app.config['JWT_PUBLIC_KEY'], audience = current_app.config['OAUTH_CLIENT_ID']) + self.token = jwt.decode(self.user_session.access_token, key=current_app.config['JWT_PUBLIC_KEY'], + audience=current_app.config['OAUTH_CLIENT_ID']) except jwt.ExpiredSignatureError: try: if not self.refresh_access_token(): return False - except: + except Exception: traceback.print_exc() flash('Failed to refresh authentication token (unhandled error; contact an admin)', 'error') return False @@ -83,12 +88,14 @@ class BinUser: def username(self): return self.user.username + @login_manager.user_loader def load_user(user_id): user_id, session_id = map(int, user_id.split(':', 1)) try: - user, user_session = db.session.query(User, UserSession).join(UserSession).filter(User.id == user_id, UserSession.id == session_id).one() + user, user_session = db.session.query(User, UserSession).join(UserSession) \ + .filter(User.id == user_id, UserSession.id == session_id).one() return BinUser(user, user_session) - except: + except Exception: traceback.print_exc() return None |