-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdeps.py
More file actions
32 lines (26 loc) · 985 Bytes
/
deps.py
File metadata and controls
32 lines (26 loc) · 985 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from fastapi import Depends, HTTPException, Request, Security, status
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from config import settings
from graphiti_core import Graphiti
bearerScheme = HTTPBearer(auto_error=False)
def get_graphiti(request: Request) -> Graphiti:
return request.app.state.graphiti
def verify_api_key(
request: Request,
credentials: HTTPAuthorizationCredentials | None = Security(bearerScheme),
):
if settings.api_key is None:
return
# 支持 Bearer 和 Api-Key 两种格式
token = None
if credentials is not None:
token = credentials.credentials
else:
auth_header = request.headers.get("Authorization", "")
if auth_header.lower().startswith("api-key "):
token = auth_header[8:]
if token != settings.api_key:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid or missing API key",
)