permissions added
This commit is contained in:
@@ -8,6 +8,16 @@ from src.service.auth.auth import CurrentUserService, auth_service
|
||||
router=APIRouter(prefix="/protected")
|
||||
oauth2_schema=OAuth2PasswordBearer(tokenUrl="/protected/token", refreshUrl="/protected/refresh")
|
||||
|
||||
|
||||
def require_permissions(*permissions: str): #permissions check dependency
|
||||
async def checker(
|
||||
token: str = Depends(oauth2_schema),
|
||||
auth: CurrentUserService = Depends(auth_service), #noqa: B008
|
||||
) -> UserOut:
|
||||
return UserOut.model_validate(await auth.get_current_user(token, *permissions))
|
||||
return checker
|
||||
|
||||
|
||||
@router.post("/token")
|
||||
async def get_access_token(request: Request,response:Response,auth:CurrentUserService=Depends(auth_service), form_data:OAuth2PasswordRequestForm=Depends())->dict: # noqa: B008
|
||||
|
||||
@@ -40,16 +50,11 @@ async def get_refresh_token(request:Request,response:Response, refresh_token: st
|
||||
|
||||
return {"access_token":access_token, "token_type": "bearer"}
|
||||
|
||||
|
||||
async def get_current_user(token:str = Depends(oauth2_schema), auth:CurrentUserService=Depends(auth_service)) -> UserOut: # noqa: B008
|
||||
return UserOut.model_validate(await auth.get_current_user(token))
|
||||
|
||||
|
||||
@router.get("/logout")
|
||||
async def logout(response:Response,refresh_token: str = Cookie(),auth:CurrentUserService=Depends(auth_service),current_user:UserOut=Depends(get_current_user))->bool: # noqa: B008
|
||||
async def logout(response:Response,refresh_token: str = Cookie(),auth:CurrentUserService=Depends(auth_service),current_user:UserOut=Depends(require_permissions()))->bool: # noqa: B008
|
||||
response.delete_cookie("refresh_token")
|
||||
return await auth.logout(refresh_token)
|
||||
|
||||
@router.get("")
|
||||
async def protected(current_user:UserOut=Depends(get_current_user))->dict: # noqa: B008
|
||||
async def protected(current_user:UserOut=Depends(require_permissions()))->dict: # noqa: B008
|
||||
return {"protected router": "Hello, this is a protected router"}
|
||||
|
||||
Reference in New Issue
Block a user