This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") |