1717 PlainTextResponse ,
1818 RedirectResponse ,
1919)
20- from starlette .status import HTTP_303_SEE_OTHER
20+ from starlette .status import HTTP_303_SEE_OTHER , HTTP_401_UNAUTHORIZED
2121
2222from piccolo_api .mfa .provider import MFAProvider
2323from piccolo_api .session_auth .tables import SessionsBase
@@ -92,7 +92,8 @@ async def post(self, request: Request) -> Response:
9292 cookie = request .cookies .get (self ._cookie_name , None )
9393 if not cookie :
9494 raise HTTPException (
95- status_code = 401 , detail = "The session cookie wasn't found."
95+ status_code = HTTP_401_UNAUTHORIZED ,
96+ detail = "The session cookie wasn't found." ,
9697 )
9798 await self ._session_table .remove_session (token = cookie )
9899
@@ -204,11 +205,14 @@ def _get_error_response(
204205 ) -> Response :
205206 if response_format == "html" :
206207 return self ._render_template (
207- request , template_context = {"error" : error }, status_code = 401
208+ request ,
209+ template_context = {"error" : error },
210+ status_code = HTTP_401_UNAUTHORIZED ,
208211 )
209212 else :
210213 return PlainTextResponse (
211- status_code = 401 , content = f"Login failed: { error } "
214+ status_code = HTTP_401_UNAUTHORIZED ,
215+ content = f"Login failed: { error } " ,
212216 )
213217
214218 async def get (self , request : Request ) -> HTMLResponse :
@@ -261,7 +265,8 @@ async def post(self, request: Request) -> Response:
261265 )
262266 else :
263267 raise HTTPException (
264- status_code = 401 , detail = validate_response
268+ status_code = HTTP_401_UNAUTHORIZED ,
269+ detail = validate_response ,
265270 )
266271
267272 # Attempt login
@@ -314,7 +319,8 @@ async def post(self, request: Request) -> Response:
314319 )
315320 else :
316321 raise HTTPException (
317- status_code = 401 , detail = message
322+ status_code = HTTP_401_UNAUTHORIZED ,
323+ detail = message ,
318324 )
319325
320326 # Work out which MFA provider to use:
@@ -325,7 +331,7 @@ async def post(self, request: Request) -> Response:
325331
326332 if mfa_provider_name is None :
327333 raise HTTPException (
328- status_code = 401 ,
334+ status_code = HTTP_401_UNAUTHORIZED ,
329335 detail = "MFA provider must be specified" ,
330336 )
331337
@@ -337,13 +343,13 @@ async def post(self, request: Request) -> Response:
337343
338344 if len (filtered_mfa_providers ) == 0 :
339345 raise HTTPException (
340- status_code = 401 ,
346+ status_code = HTTP_401_UNAUTHORIZED ,
341347 detail = "MFA provider not recognised." ,
342348 )
343349
344350 if len (filtered_mfa_providers ) > 1 :
345351 raise HTTPException (
346- status_code = 401 ,
352+ status_code = HTTP_401_UNAUTHORIZED ,
347353 detail = (
348354 "Multiple matching MFA providers found."
349355 ),
@@ -368,7 +374,7 @@ async def post(self, request: Request) -> Response:
368374 )
369375 else :
370376 raise HTTPException (
371- status_code = 401 ,
377+ status_code = HTTP_401_UNAUTHORIZED ,
372378 detail = "MFA failed" ,
373379 )
374380
@@ -404,7 +410,9 @@ async def post(self, request: Request) -> Response:
404410 },
405411 )
406412 else :
407- raise HTTPException (status_code = 401 , detail = "Login failed" )
413+ raise HTTPException (
414+ status_code = HTTP_401_UNAUTHORIZED , detail = "Login failed"
415+ )
408416
409417 now = datetime .now ()
410418 expiry_date = now + self ._session_expiry
0 commit comments