2022-06-12 10:47:07 +00:00
|
|
|
from fastapi import FastAPI
|
2024-12-25 10:07:13 +00:00
|
|
|
from fastapi.exceptions import HTTPException
|
2022-12-17 12:44:03 +00:00
|
|
|
|
2024-12-25 11:40:57 +00:00
|
|
|
from pssecret.models import Secret, SecretSaveResult
|
|
|
|
from pssecret.redis_db import redis
|
|
|
|
from pssecret.utils import get_new_key
|
2022-06-12 10:47:07 +00:00
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
2024-12-26 22:25:54 +00:00
|
|
|
@app.post(
|
|
|
|
"/secret",
|
|
|
|
summary="Store secret",
|
|
|
|
description=(
|
|
|
|
"Submit secret, it is saved on the server, get retrieval key in response. "
|
|
|
|
"Use that key to retrieve your data. Key could be used only once, "
|
|
|
|
"so use it wisely"
|
|
|
|
),
|
|
|
|
response_model=SecretSaveResult,
|
|
|
|
)
|
2022-12-17 12:44:03 +00:00
|
|
|
async def set_secret(data: Secret):
|
|
|
|
new_key = await get_new_key()
|
|
|
|
await redis.setex(new_key, 60 * 60 * 24, data.data)
|
|
|
|
|
|
|
|
return {
|
2024-12-26 22:25:54 +00:00
|
|
|
"key": new_key,
|
2022-12-17 12:44:03 +00:00
|
|
|
}
|
2022-12-17 15:02:09 +00:00
|
|
|
|
|
|
|
|
2024-12-25 10:07:13 +00:00
|
|
|
@app.get(
|
|
|
|
"/secret/{secret_key}",
|
2024-12-26 22:25:54 +00:00
|
|
|
summary="Retrieve secret",
|
|
|
|
description=(
|
|
|
|
"Returns previously saved data if it is still on the server. "
|
|
|
|
"Could be the other way around in two cases: "
|
|
|
|
"either it has already been retrieved, either storage timeout has expired"
|
|
|
|
),
|
2024-12-25 10:07:13 +00:00
|
|
|
response_model=Secret,
|
|
|
|
responses={404: {"description": "The item was not found"}},
|
|
|
|
)
|
2024-12-26 21:52:41 +00:00
|
|
|
async def get_secret(secret_key: str):
|
|
|
|
data: str | None = await redis.getdel(secret_key)
|
2024-12-25 10:07:13 +00:00
|
|
|
|
|
|
|
if data is None:
|
|
|
|
raise HTTPException(404)
|
|
|
|
|
2022-12-17 15:02:09 +00:00
|
|
|
return {
|
2024-12-25 10:07:13 +00:00
|
|
|
"data": data,
|
2022-12-17 15:02:09 +00:00
|
|
|
}
|