From 1ffcdc3ec1aa744d72f6a7f5da3447b668a75db1 Mon Sep 17 00:00:00 2001 From: Maxim De Clercq Date: Wed, 19 Apr 2023 22:03:40 +0200 Subject: [PATCH] Do not store UGent username since it is not exposed through Graph API --- app/app.py | 6 +++--- app/auth/microsoft.py | 11 +---------- app/models/user.py | 3 +-- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/app/app.py b/app/app.py index 1d38f73..262604f 100755 --- a/app/app.py +++ b/app/app.py @@ -8,22 +8,22 @@ import typing from datetime import datetime from logging.handlers import TimedRotatingFileHandler -from admin import init_admin from flask import Flask, render_template from flask_bootstrap import Bootstrap, StaticCDN from flask_debugtoolbar import DebugToolbarExtension from flask_login import LoginManager from flask_migrate import Migrate, MigrateCommand from flask_script import Manager, Server -from auth.login import init_login from markupsafe import Markup +from admin import init_admin +from auth.login import init_login +from auth.zeus import init_oauth from config import Configuration from models import db from models.anonymous_user import AnonymouseUser from sentry_sdk.integrations.flask import FlaskIntegration from utils import euro_string, price_range_string, ignore_none -from auth.zeus import init_oauth def register_plugins(app: Flask) -> Manager: diff --git a/app/auth/microsoft.py b/app/auth/microsoft.py index 35e542a..f10beb8 100644 --- a/app/auth/microsoft.py +++ b/app/auth/microsoft.py @@ -36,25 +36,16 @@ def authorized() -> typing.Any: oauth_code = request.args['code'] resp = client.exchange_code(url_for("auth_microsoft_bp.authorized", _external=True), oauth_code) - - # access_token = resp.data['access_token'] - # id_token = resp.data['id_token'] - # expires_in = resp.data['expires_in'] - client.set_token(resp.data) resp = client.users.get_me() - # print(resp.data) - username = resp.data['userPrincipalName'] microsoft_uuid = resp.data['id'] user = User.query.filter_by(username=username).first() - if username and user: return login_and_redirect_user(user) elif username: - # TODO Save 'ugent_username' or something similar user = create_user(username, microsoft_uuid) return login_and_redirect_user(user) @@ -71,7 +62,7 @@ def login_and_redirect_user(user) -> Response: def create_user(username, microsoft_uuid) -> User: """Create a temporary user if it is needed""" user = User() - user.configure(username, False, 1, microsoft_uuid) + user.configure(username, False, 1, microsoft_uuid=microsoft_uuid) db.session.add(user) db.session.commit() return user diff --git a/app/models/user.py b/app/models/user.py index a8cef69..5110cf7 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -12,7 +12,6 @@ class User(db.Model): bias = db.Column(db.Integer) # Microsoft OAUTH info microsoft_uuid = db.Column(db.String(120), unique=True) - ugent_username = db.Column(db.String(80), unique=True) # Association logic associations = db.Column(db.String(255), nullable=False, server_default="") @@ -28,7 +27,7 @@ class User(db.Model): def association_list(self) -> List[str]: return self.associations.split(",") - def configure(self, username: str, admin: bool, bias: int, microsoft_uuid: str = None, associations: Optional[List[str]] = None) -> None: + def configure(self, username: str, admin: bool, bias: int, *, microsoft_uuid: str = None, associations: Optional[List[str]] = None) -> None: """Configure the User""" if associations is None: associations = []