Skip to content

Commit

Permalink
Implement middleware for checking User Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
augusto-herrmann committed Oct 3, 2024
1 parent 395c6ff commit 5520e7b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import os
from typing import Annotated, Union

from fastapi import Depends, FastAPI, HTTPException, status, Header, Response
from fastapi import Depends, FastAPI, HTTPException, status, Header, Request, Response
from fastapi.security import OAuth2PasswordRequestForm
from fastapi.responses import RedirectResponse
from fastapi.responses import JSONResponse, RedirectResponse
from sqlalchemy.exc import IntegrityError


Expand All @@ -20,7 +20,7 @@
from util import check_permissions

ACCESS_TOKEN_EXPIRE_MINUTES = int(os.environ.get("ACCESS_TOKEN_EXPIRE_MINUTES"))
TEST_ENVIRONMENT = os.environ.get("TEST_ENVIRONMENT", 'False') == 'True'
TEST_ENVIRONMENT = os.environ.get("TEST_ENVIRONMENT", "False") == "True"

# ## INIT --------------------------------------------------

Expand Down Expand Up @@ -55,6 +55,18 @@ async def on_startup():
await crud_auth.init_user_admin()


@app.middleware("http")
async def check_user_agent(request: Request, call_next):
user_agent = request.headers.get("User-Agent", None)

if not user_agent:
return JSONResponse(
status_code=400,
content={"detail": "User-Agent header is required"},
)
return await call_next(request)


@app.get("/", include_in_schema=False)
async def docs_redirect(
accept: Union[str, None] = Header(default="text/html")
Expand Down

0 comments on commit 5520e7b

Please sign in to comment.