66import requests # TODO: make this async
77import xmltodict
88from fastapi import APIRouter , BackgroundTasks , HTTPException , Request , Response
9- from fastapi .responses import JSONResponse , PlainTextResponse , RedirectResponse
9+ from fastapi .responses import JSONResponse , RedirectResponse
1010
1111import database
1212from auth import crud
13- from auth .models import LoginBodyModel
13+ from auth .models import LoginBodyParams , SiteUserModel , UpdateUserParams
1414from constants import DOMAIN , IS_PROD , SAMESITE
15- from utils .shared_models import DetailModel
15+ from utils .shared_models import DetailModel , MessageModel
1616
1717_logger = logging .getLogger (__name__ )
1818
@@ -51,7 +51,7 @@ async def login_user(
5151 request : Request ,
5252 db_session : database .DBSession ,
5353 background_tasks : BackgroundTasks ,
54- body : LoginBodyModel
54+ body : LoginBodyParams
5555):
5656 # verify the ticket is valid
5757 service_url = body .service
@@ -94,8 +94,9 @@ async def login_user(
9494
9595@router .get (
9696 "/logout" ,
97- operation_id = "logout" ,
9897 description = "Logs out the current user by invalidating the session_id cookie" ,
98+ operation_id = "logout" ,
99+ response_model = MessageModel
99100)
100101async def logout_user (
101102 request : Request ,
@@ -119,6 +120,10 @@ async def logout_user(
119120 "/user" ,
120121 operation_id = "get_user" ,
121122 description = "Get info about the current user. Only accessible by that user" ,
123+ response_model = SiteUserModel ,
124+ responses = {
125+ 401 : { "description" : "Not logged in." , "model" : DetailModel }
126+ },
122127)
123128async def get_user (
124129 request : Request ,
@@ -129,35 +134,38 @@ async def get_user(
129134 """
130135 session_id = request .cookies .get ("session_id" , None )
131136 if session_id is None :
132- raise HTTPException (status_code = 401 , detail = "User must be authenticated to get their info" )
137+ raise HTTPException (status_code = 401 , detail = "user must be authenticated to get their info" )
133138
134139 user_info = await crud .get_site_user (db_session , session_id )
135140 if user_info is None :
136- raise HTTPException (status_code = 401 , detail = "Could not find user with session_id, please log in" )
141+ raise HTTPException (status_code = 401 , detail = "could not find user with session_id, please log in" )
137142
138- return JSONResponse (user_info .serializable_dict ())
143+ return JSONResponse (user_info .serialize ())
139144
140145
146+ # TODO: We should change this so that the admins can change people's pictures too, so they can remove offensive stuff
141147@router .patch (
142148 "/user" ,
143149 operation_id = "update_user" ,
144150 description = "Update information for the currently logged in user. Only accessible by that user" ,
151+ response_model = str ,
152+ responses = {
153+ 401 : { "description" : "Not logged in." , "model" : DetailModel }
154+ },
145155)
146156async def update_user (
147- profile_picture_url : str ,
157+ body : UpdateUserParams ,
148158 request : Request ,
149159 db_session : database .DBSession ,
150160):
151161 """
152162 Returns the info stored in the site_user table in the auth module, if the user is logged in.
153163 """
154- session_id = request .cookies .get ("session_id" , None )
164+ session_id = request .cookies .get ("session_id" )
155165 if session_id is None :
156- raise HTTPException (status_code = 401 , detail = "User must be authenticated to get their info" )
166+ raise HTTPException (status_code = 401 , detail = "user must be authenticated to get their info" )
157167
158- ok = await crud .update_site_user (db_session , session_id , profile_picture_url )
168+ ok = await crud .update_site_user (db_session , session_id , body . profile_picture_url )
159169 await db_session .commit ()
160170 if not ok :
161- raise HTTPException (status_code = 401 , detail = "Could not find user with session_id, please log in" )
162-
163- return PlainTextResponse ("ok" )
171+ raise HTTPException (status_code = 401 , detail = "could not find user with session_id, please log in" )
0 commit comments