implemented rate limit and set up redis container
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from fastapi import APIRouter, Cookie, Depends, Request, Response
|
||||
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
||||
|
||||
from src.cache.rate_limit import rate_limiter
|
||||
from src.models.configs_read.env import env_settings
|
||||
from src.models.pydantic_models.model import UserOut
|
||||
from src.service.auth.auth import CurrentUserService, auth_service
|
||||
@@ -12,14 +13,19 @@ oauth2_schema=OAuth2PasswordBearer(tokenUrl="/protected/token", refreshUrl="/pro
|
||||
def require_permissions(*permissions: str): #permissions check dependency
|
||||
async def checker(
|
||||
token: str = Depends(oauth2_schema),
|
||||
auth: CurrentUserService = Depends(auth_service), #noqa: B008
|
||||
auth: CurrentUserService = Depends(auth_service),
|
||||
) -> 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
|
||||
async def get_access_token(request: Request,
|
||||
response:Response,
|
||||
auth:CurrentUserService=Depends(auth_service),
|
||||
form_data:OAuth2PasswordRequestForm=Depends(),
|
||||
_:None = Depends(rate_limiter.check_rate_limit)
|
||||
)->dict:
|
||||
|
||||
access_token, refresh_token=await auth.login(form_data_email=form_data.username, form_data_password=form_data.password, request=request)
|
||||
|
||||
@@ -35,7 +41,10 @@ async def get_access_token(request: Request,response:Response,auth:CurrentUserSe
|
||||
|
||||
|
||||
@router.post("/refresh")
|
||||
async def get_refresh_token(request:Request,response:Response, refresh_token: str = Cookie(), auth:CurrentUserService=Depends(auth_service))->dict: # noqa: B008
|
||||
async def get_refresh_token(request:Request,
|
||||
response:Response,
|
||||
refresh_token: str = Cookie(),
|
||||
auth:CurrentUserService=Depends(auth_service))->dict:
|
||||
|
||||
access_token, refresh_token= await auth.refresh_token(refresh_token=refresh_token,request=request)
|
||||
|
||||
@@ -51,10 +60,13 @@ async def get_refresh_token(request:Request,response:Response, refresh_token: st
|
||||
return {"access_token":access_token, "token_type": "bearer"}
|
||||
|
||||
@router.get("/logout")
|
||||
async def logout(response:Response,refresh_token: str = Cookie(),auth:CurrentUserService=Depends(auth_service),current_user:UserOut=Depends(require_permissions()))->bool: # noqa: B008
|
||||
async def logout(response:Response,
|
||||
refresh_token: str = Cookie(),
|
||||
auth:CurrentUserService=Depends(auth_service),
|
||||
current_user:UserOut=Depends(require_permissions()))->bool:
|
||||
response.delete_cookie("refresh_token")
|
||||
return await auth.logout(refresh_token)
|
||||
|
||||
@router.get("")
|
||||
async def protected(current_user:UserOut=Depends(require_permissions()))->dict: # noqa: B008
|
||||
async def protected(current_user:UserOut=Depends(require_permissions()))->dict:
|
||||
return {"protected router": "Hello, this is a protected router"}
|
||||
|
||||
Reference in New Issue
Block a user