Co-authored-by: Kevin Lin <klin5061@gmail.com> Co-authored-by: Matthew Zhou <mattzh1314@gmail.com> Co-authored-by: Kian Jones <11655409+kianjones9@users.noreply.github.com> Co-authored-by: Andy Li <55300002+cliandy@users.noreply.github.com> Co-authored-by: jnjpng <jin@letta.com> Co-authored-by: Jin Peng <jinjpeng@Jins-MacBook-Pro.local> Co-authored-by: Eric Ly <111820150+lyeric2022@users.noreply.github.com> Co-authored-by: Eric Ly <lyyeric@letta.com> Co-authored-by: Shubham Naik <shub@letta.com> Co-authored-by: Shubham Naik <shub@memgpt.ai>
25 lines
852 B
Python
25 lines
852 B
Python
from starlette.middleware.base import BaseHTTPMiddleware
|
|
from starlette.responses import JSONResponse
|
|
|
|
|
|
class CheckPasswordMiddleware(BaseHTTPMiddleware):
|
|
def __init__(self, app, password: str):
|
|
super().__init__(app)
|
|
self.password = password
|
|
|
|
async def dispatch(self, request, call_next):
|
|
# Exclude health check endpoint from password protection
|
|
if request.url.path in {"/v1/health", "/v1/health/", "/latest/health/"}:
|
|
return await call_next(request)
|
|
|
|
if (
|
|
request.headers.get("X-BARE-PASSWORD") == f"password {self.password}"
|
|
or request.headers.get("Authorization") == f"Bearer {self.password}"
|
|
):
|
|
return await call_next(request)
|
|
|
|
return JSONResponse(
|
|
content={"detail": "Unauthorized"},
|
|
status_code=401,
|
|
)
|