-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (73 loc) · 2.5 KB
/
main.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import shelve
from contextlib import asynccontextmanager
from datetime import datetime
from types import SimpleNamespace
from uuid import uuid4
import httpx
from dotenv import load_dotenv
from fastapi import Depends, FastAPI, Header, HTTPException
from pydantic import UUID4, BaseModel, Field
from typing_extensions import Annotated
ctx = None
@asynccontextmanager
async def lifespan(app: FastAPI):
global ctx
load_dotenv(verbose=True)
ctx = SimpleNamespace(db=shelve.open("app"), store_id=os.getenv("OPENFGA_STORE_ID"))
if not ctx.store_id:
raise RuntimeError("OPENFGA_STORE_ID is not set")
yield
ctx.db.close()
app = FastAPI(lifespan=lifespan)
class SecretCreate(BaseModel):
name: str = Field(None)
value: str = Field(None)
class Secret(SecretCreate):
id: UUID4 = Field(default_factory=uuid4)
created_at: datetime = Field(default_factory=datetime.now)
updated_at: datetime = Field(default_factory=datetime.now)
async def authenticate(user: Annotated[str, Header()] = None) -> str:
if not user:
raise HTTPException(status_code=401, detail="Unauthorized")
return user
authed_id = Annotated[str, Depends(authenticate)]
@app.get("/me")
async def get_me(user: authed_id):
return {"user": user}
@app.get("/projects/{project_id}/secrets")
async def get_secrets(user: authed_id, project_id: str):
# Authorize request
if not httpx.post(
f"http://localhost:8080/stores/{ctx.store_id}/check",
json={
"tuple_key": {
"user": f"user:{user}",
"relation": "reader",
"object": f"project:{project_id}",
}
},
).json()["allowed"]:
raise HTTPException(status_code=403, detail="Forbidden")
# Return secrets
secrets = ctx.db.get(project_id, [])
return secrets
@app.post("/projects/{project_id}/secret")
async def set_secret(user: authed_id, project_id: str, secret: SecretCreate):
# Authorize request
if not httpx.post(
f"http://localhost:8080/stores/{ctx.store_id}/check",
json={
"tuple_key": {
"user": f"user:{user}",
"relation": "writer",
"object": f"project:{project_id}",
}
},
).json()["allowed"]:
raise HTTPException(status_code=403, detail="Forbidden")
# Set secret
secrets = ctx.db.get(project_id, [])
secrets.append(Secret(**secret.model_dump()))
ctx.db[project_id] = secrets
return {"status": "success"}