33from collections .abc import Callable , Coroutine
44from typing import Annotated , Any
55
6- from fastapi import Depends , Request , status
7- from fastapi .exceptions import HTTPException
6+ from fastapi import Depends , Request
87from fastapi .security import OAuth2PasswordBearer
98
109from syncmaster .backend .dependencies import Stub
1110from syncmaster .backend .providers .auth import AuthProvider
1211from syncmaster .db .models import User
12+ from syncmaster .exceptions import ActionNotAllowedError , EntityNotFoundError
1313
1414oauth_schema = OAuth2PasswordBearer (tokenUrl = "v1/auth/token" , auto_error = False )
1515
@@ -21,7 +21,7 @@ def get_user(
2121 async def wrapper (
2222 request : Request ,
2323 auth_provider : Annotated [AuthProvider , Depends (Stub (AuthProvider ))],
24- access_token : Annotated [str , Depends (oauth_schema )],
24+ access_token : Annotated [str | None , Depends (oauth_schema )],
2525 ) -> User :
2626 # keycloak provider patches session and store access_token in cookie,
2727 # when dummy auth stores it in "Authorization" header
@@ -31,20 +31,11 @@ async def wrapper(
3131 request = request ,
3232 )
3333 if user is None :
34- raise HTTPException (
35- status_code = status .HTTP_404_NOT_FOUND ,
36- detail = "User not found" ,
37- )
34+ raise EntityNotFoundError ("User not found" )
3835 if is_active and not user .is_active :
39- raise HTTPException (
40- status_code = status .HTTP_403_FORBIDDEN ,
41- detail = "Inactive user" ,
42- )
36+ raise ActionNotAllowedError ("Inactive user" )
4337 if is_superuser and not user .is_superuser :
44- raise HTTPException (
45- status_code = status .HTTP_403_FORBIDDEN ,
46- detail = "You have no power here" ,
47- )
38+ raise ActionNotAllowedError ("You have no power here" )
4839 return user
4940
5041 return wrapper
0 commit comments