Skip to content

Commit 2c15b03

Browse files
committed
feat: configure limiter in main app and add rate limiting to authentication endpoints
1 parent 98c3e4b commit 2c15b03

File tree

4 files changed

+172
-2
lines changed

4 files changed

+172
-2
lines changed

app/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from contextlib import asynccontextmanager
33

44
from fastapi import FastAPI
5+
from slowapi import Limiter, _rate_limit_exceeded_handler
6+
from slowapi.util import get_remote_address
57

68
from app.routers.router import setup_router as setup_router_v2
79
from app.services.database.database import AsyncSessionLocal, init_db
@@ -20,13 +22,19 @@ async def lifespan(app: FastAPI):
2022
pass
2123

2224

25+
limiter = Limiter(key_func=get_remote_address)
26+
27+
2328
app = FastAPI(
2429
lifespan=lifespan,
2530
title="pynews-server",
2631
description="PyNews Server",
2732
)
2833

2934

35+
app.state.limiter = limiter
36+
app.add_exception_handler(429, _rate_limit_exceeded_handler)
37+
3038
app.include_router(setup_router_v2(), prefix="/api")
3139

3240
logger.info("PyNews Server Starter")

app/routers/authentication.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from fastapi import APIRouter, Depends, HTTPException, Request, status
55
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
66
from jwt.exceptions import InvalidTokenError
7+
from slowapi import Limiter
8+
from slowapi.util import get_remote_address
79
from sqlmodel.ext.asyncio.session import AsyncSession
810

911
from app.schemas import Community, Token, TokenPayload
@@ -12,6 +14,7 @@
1214
from app.services.database.orm.community import get_community_by_username
1315

1416
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/authentication/token")
17+
limiter = Limiter(key_func=get_remote_address)
1518

1619

1720
def setup():
@@ -88,6 +91,7 @@ async def create_community(request: Request):
8891
# Teste
8992

9093
@router.post("/token", response_model=Token)
94+
@limiter.limit("60/minute")
9195
async def login_for_access_token(
9296
request: Request, form_data: OAuth2PasswordRequestForm = Depends()
9397
):
@@ -109,6 +113,7 @@ async def login_for_access_token(
109113
}
110114

111115
@router.get("/me", response_model=Community)
116+
@limiter.limit("60/minute")
112117
async def read_community_me(
113118
current_community: Annotated[
114119
DBCommunity, Depends(get_current_active_community)

poetry.lock

Lines changed: 158 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pre-commit = "^4.2.0"
2020
python-multipart = "^0.0.20"
2121
pyjwt = "^2.10.1"
2222
bcrypt = "^4.3.0"
23+
slowapi = "^0.1.9"
2324

2425
[tool.poetry.group.dev.dependencies]
2526
pytest = "^8.3.2"

0 commit comments

Comments
 (0)