-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathexample_custom_auth.py
More file actions
45 lines (30 loc) · 1.17 KB
/
Copy pathexample_custom_auth.py
File metadata and controls
45 lines (30 loc) · 1.17 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Minimal example showing custom authentication for FastAPI Radar."""
from fastapi import FastAPI, HTTPException, Request
from fastapi_radar import Radar
app = FastAPI(title="FastAPI Radar with Custom Auth")
async def verify_api_key(request: Request):
"""Custom authentication using API key from header."""
api_key = request.headers.get("X-API-Key")
if api_key != "radar-secret-key":
raise HTTPException(status_code=401, detail="Invalid or missing API key")
return True
# Initialize Radar with custom authentication
radar = Radar(app, auth_dependency=verify_api_key)
radar.create_tables()
@app.get("/")
async def root():
return {
"message": "Use X-API-Key header to access dashboard",
"header": "X-API-Key: radar-secret-key",
}
if __name__ == "__main__":
import uvicorn
print("\n" + "=" * 60)
print("🔐 FastAPI Radar with Custom API Key")
print("=" * 60)
print("\nAPI Key: radar-secret-key")
print("\nAccess dashboard:")
print(' curl -H "X-API-Key: radar-secret-key" \\')
print(" http://localhost:8000/__radar/api/stats")
print("=" * 60 + "\n")
uvicorn.run(app, host="0.0.0.0", port=8000)