Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Remove application.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
msathieu committed Mar 13, 2024
1 parent daed288 commit ae0f0f9
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 42 deletions.
5 changes: 0 additions & 5 deletions backend/application.properties

This file was deleted.

13 changes: 4 additions & 9 deletions backend/controllers/auth/authentication_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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 == "":
Expand All @@ -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
14 changes: 5 additions & 9 deletions backend/controllers/auth/token_controller.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 0 additions & 15 deletions backend/controllers/properties/Properties.py

This file was deleted.

6 changes: 2 additions & 4 deletions backend/routes/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ae0f0f9

Please sign in to comment.