diff --git a/backend/controllers/auth/authentication_controller.py b/backend/controllers/auth/authentication_controller.py index 9797221b..2a026b67 100644 --- a/backend/controllers/auth/authentication_controller.py +++ b/backend/controllers/auth/authentication_controller.py @@ -6,6 +6,7 @@ props: Properties = Properties() +# TODO: Should return a user object instead of a dict def authenticate_user(ticket: str) -> dict | None: service = props.get("session", "service") user_information = httpx.get(f"https://login.ugent.be/serviceValidate?service={service}&ticket={ticket}" diff --git a/backend/controllers/auth/cookie_controller.py b/backend/controllers/auth/cookie_controller.py index 5e873b16..198cd822 100644 --- a/backend/controllers/auth/cookie_controller.py +++ b/backend/controllers/auth/cookie_controller.py @@ -1,12 +1,13 @@ from fastapi import Request, Response +from starlette.responses import JSONResponse -from controllers.auth.encryption_controller import encrypt, generate_keys +from controllers.auth.encryption_controller import encrypt from controllers.properties.Properties import Properties props: Properties = Properties() -def set_cookies(response: Response, key: str, value: str) -> Response: +def set_session_cookies(response: JSONResponse, key: str, value: str) -> JSONResponse: value: str = encrypt(value) max_age: int = int(props.get("session", "max_cookie_age")) domain: str = props.get("session", "cookie_domain") diff --git a/backend/routes/authentication.py b/backend/routes/authentication.py index 176b8df3..9e5f0491 100644 --- a/backend/routes/authentication.py +++ b/backend/routes/authentication.py @@ -2,7 +2,7 @@ from fastapi.responses import JSONResponse from controllers.auth.authentication_controller import authenticate_user -from controllers.auth.cookie_controller import delete_cookie, set_cookies +from controllers.auth.cookie_controller import delete_cookie, set_session_cookies from controllers.auth.encryption_controller import delete_key from controllers.auth.login_controller import verify_session @@ -10,7 +10,7 @@ @session_router.get("/api/login") -def login(ticket: str) -> JSONResponse: +def login(ticket: str) -> Response: """ This function start a session for the user. For authentication, it uses the given ticket and the UGent CAS server (https://login.ugent.be). @@ -20,13 +20,11 @@ def login(ticket: str) -> JSONResponse: - Valid Ticket: A JSONResponse with a user object; a cookie will be set with a session_id - Invalid Ticket: A JSONResponse with status_code 401 and an error message """ - user: dict = authenticate_user(ticket) # This should be a user object + user: dict = authenticate_user(ticket) # TODO: This should be a user object if user: response: JSONResponse = JSONResponse(content=user) # TODO: Change mail to user id - print("here") - response = set_cookies(response, "session_id", user["mail"]) - return response + return set_session_cookies(response, "session_id", user["mail"]) return JSONResponse(status_code=401, content="Invalid Ticket")