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

Commit

Permalink
start login_route
Browse files Browse the repository at this point in the history
  • Loading branch information
cstefc committed Mar 2, 2024
1 parent 5ce938a commit 65295e5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import uvicorn
from fastapi import FastAPI

from routes.session import session_router
from routes.teachers import teachers_router

app = FastAPI()

# Koppel routes uit andere modules.
app.include_router(teachers_router)
app.include_router(session_router)

if __name__ == "__main__":
uvicorn.run("app:app")
41 changes: 41 additions & 0 deletions backend/controllers/auth/session_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import httpx
from defusedxml.ElementTree import fromstring

# test url: https://login.ugent.be/login?service=https://localhost:8080/session
# TODO: get information out of a properties file
SERVICE = "https://localhost:8080/session"
DOMAIN = "localhost"
MAX_AGE = 24 * 60 * 60


def get_user_information(ticket: str) -> dict | None:
user_information = httpx.get(f"https://login.ugent.be/serviceValidate?service={SERVICE}&ticket={ticket}"
, headers={"Accept": "application/json,text/html"},
)
user: dict | None = parse_cas_xml(user_information.text)
return user


def parse_cas_xml(xml: str) -> dict | None:
namespace = "{http://www.yale.edu/tp/cas}"
user = {}

root = fromstring(xml)
if root.find(f"{namespace}authenticationSuccess"):
attributes_xml = (root
.find(f"{namespace}authenticationSuccess")
.find(f"{namespace}attributes"))

givenname = attributes_xml.find(f"{namespace}givenname").text
surname = attributes_xml.find(f"{namespace}surname").text
mail = attributes_xml.find(f"{namespace}mail").text

user["name"] = f"{givenname} {surname}"
user["mail"] = mail
return user
return None


# TODO: create a session_id for the given user, create the user if it doesn't exist already
def login_user(user_information: dict) -> str:
return "TestValueCookie"
2 changes: 2 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ SQLAlchemy==2.0.27
starlette==0.36.3
typing_extensions==4.10.0
uvicorn==0.27.1
httpx==0.27.0
defusedxml~=0.7.1
22 changes: 22 additions & 0 deletions backend/routes/session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from fastapi import APIRouter, HTTPException
from fastapi.responses import JSONResponse

from controllers.auth.session_controller import DOMAIN, MAX_AGE, get_user_information, login_user

session_router = APIRouter()


@session_router.get("/session")
def get_session(ticket: str) -> JSONResponse:
user_information: dict = get_user_information(ticket)
if user_information:
session_id = login_user(user_information)
response: JSONResponse = JSONResponse(content=user_information)
response.set_cookie(
key="session_id",
value=session_id,
max_age=MAX_AGE,
domain=DOMAIN,
secure=True)
return response
raise HTTPException(status_code=401, detail="Invalid Token!")

0 comments on commit 65295e5

Please sign in to comment.