From ae0f0f967c52b863fe809cb4d8e39363695b86c0 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Wed, 13 Mar 2024 17:27:49 +0100 Subject: [PATCH] Remove application.properties --- backend/application.properties | 5 ----- .../controllers/auth/authentication_controller.py | 13 ++++--------- backend/controllers/auth/token_controller.py | 14 +++++--------- backend/controllers/properties/Properties.py | 15 --------------- backend/routes/login.py | 6 ++---- 5 files changed, 11 insertions(+), 42 deletions(-) delete mode 100644 backend/application.properties delete mode 100644 backend/controllers/properties/Properties.py diff --git a/backend/application.properties b/backend/application.properties deleted file mode 100644 index f54351c2..00000000 --- a/backend/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -[session] -service=https://localhost:8080/login -access_token_expire_minutes=10 -secret_key=f19a1fb01efac6d7d254065ce1949f0d3b584c867b5625306f0481f64f14471c -algorithm=HS256 \ No newline at end of file diff --git a/backend/controllers/auth/authentication_controller.py b/backend/controllers/auth/authentication_controller.py index aaf3563f..fb302457 100644 --- a/backend/controllers/auth/authentication_controller.py +++ b/backend/controllers/auth/authentication_controller.py @@ -4,7 +4,6 @@ from defusedxml.ElementTree import fromstring from sqlalchemy.orm import Session -from controllers.properties.Properties import Properties from domain.logic.student import create_student from domain.logic.teacher import create_teacher from domain.logic.user import get_user_with_email @@ -13,7 +12,7 @@ if TYPE_CHECKING: from _elementtree import Element -props: Properties = Properties() +cas_service = "https://localhost:8080/login" def authenticate_user(session: Session, ticket: str) -> UserDataclass | None: @@ -26,8 +25,7 @@ def authenticate_user(session: Session, ticket: str) -> UserDataclass | None: :param ticket: A ticket from login.ugent.be/login?service=https://localhost:8080/login :return: None if the authentication failed, user: UseDataclass is the authentication was successful """ - service = props.get("session", "service") - user_information = httpx.get(f"https://login.ugent.be/serviceValidate?service={service}&ticket={ticket}") + user_information = httpx.get(f"https://login.ugent.be/serviceValidate?service={cas_service}&ticket={ticket}") user_dict: dict | None = parse_cas_xml(user_information.text) if user_dict is None: return None @@ -59,10 +57,7 @@ def parse_cas_xml(xml: str) -> dict | None: surname: Element | None = user_information.find(f"{namespace}surname") email: Element | None = user_information.find(f"{namespace}mail") role: list | None = user_information.find(f"{namespace}objectClass") - if (role is not None - and givenname is not None - and surname is not None - and email is not None): + if role is not None and givenname is not None and surname is not None and email is not None: role_str: str = "" for r in role: if r.text == "ugentStudent" and role_str == "": @@ -74,5 +69,5 @@ def parse_cas_xml(xml: str) -> dict | None: "email": email.text.lower(), "name": f"{givenname.text} {surname.text}", "role": role_str, - } + } return None diff --git a/backend/controllers/auth/token_controller.py b/backend/controllers/auth/token_controller.py index 76393c18..7a07bcd1 100644 --- a/backend/controllers/auth/token_controller.py +++ b/backend/controllers/auth/token_controller.py @@ -1,28 +1,24 @@ import contextlib +import os from datetime import UTC, datetime, timedelta import jwt -from controllers.properties.Properties import Properties from domain.models.UserDataclass import UserDataclass -props: Properties = Properties() +jwt_secret = os.getenv("JWT_SECRET", "secret") def verify_token(token: str) -> int | None: - secret = props.get("session", "secret_key") - algorithm = props.get("session", "algorithm") with contextlib.suppress(jwt.ExpiredSignatureError, jwt.DecodeError): - payload = jwt.decode(token, secret, algorithms=[algorithm]) + payload = jwt.decode(token, jwt_secret) return payload.get("userid", None) def create_token(user: UserDataclass) -> str: - exprire = datetime.now(UTC) + timedelta(minutes=int(props.get("session", "access_token_expire_minutes"))) + exprire = datetime.now(UTC) + timedelta(days=1) to_encode: dict = { "userid": user.id, "exp": exprire, } - algorithm: str = props.get("session", "algorithm") - secret: str = props.get("session", "secret_key") - return jwt.encode(to_encode, secret, algorithm=algorithm) + return jwt.encode(to_encode, jwt_secret) diff --git a/backend/controllers/properties/Properties.py b/backend/controllers/properties/Properties.py deleted file mode 100644 index 951fdd0b..00000000 --- a/backend/controllers/properties/Properties.py +++ /dev/null @@ -1,15 +0,0 @@ -import configparser - - -class Properties: - def __init__(self) -> None: - config = configparser.ConfigParser() - config.read("application.properties") - self.properties: dict = {} - for section in config.sections(): - self.properties[section] = {} - for key, val in config[section].items(): - self.properties[section][key] = val - - def get(self, section: str, key: str) -> str: - return self.properties[section][key] diff --git a/backend/routes/login.py b/backend/routes/login.py index 2ac526f5..65d2d806 100644 --- a/backend/routes/login.py +++ b/backend/routes/login.py @@ -3,19 +3,17 @@ from controllers.auth.authentication_controller import authenticate_user from controllers.auth.token_controller import create_token -from controllers.properties.Properties import Properties from db.sessions import get_session from domain.models.UserDataclass import UserDataclass # test url: https://login.ugent.be/login?service=https://localhost:8080/api/login login_router = APIRouter() -props: Properties = Properties() @login_router.get("/login") def login( - ticket: str, - session: Session = Depends(get_session), + ticket: str, + session: Session = Depends(get_session), ) -> Response: """ This function starts a session for the user.