-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathurls.py
More file actions
37 lines (29 loc) · 1.26 KB
/
urls.py
File metadata and controls
37 lines (29 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from fastapi import HTTPException, Request
import auth
import auth.crud
import database
# TODO: move other utils into this module
async def logged_in_or_raise(
request: Request,
db_session: database.DBSession
) -> tuple[str, str]:
"""gets the user's computing_id, or raises an exception if the current request is not logged in"""
session_id = request.cookies.get("session_id", None)
if session_id is None:
raise HTTPException(status_code=401, detail="no session id")
session_computing_id = await auth.crud.get_computing_id(db_session, session_id)
if session_computing_id is None:
raise HTTPException(status_code=401, detail="no computing id")
return session_id, session_computing_id
async def is_logged_in(
request: Request,
db_session: database.DBSession
) -> tuple[bool, str | None, str | None]:
"""gets the user's computing_id, or raises an exception if the current request is not logged in"""
session_id = request.cookies.get("session_id", None)
if session_id is None:
return False, None, None
session_computing_id = await auth.crud.get_computing_id(db_session, session_id)
if session_computing_id is None:
return False, None, None
return True, session_id, session_computing_id