feat: add MyWhoosh token persistence

This commit is contained in:
Bastian Wagner
2026-08-15 14:38:50 +02:00
parent ef9ca9eeca
commit a28d06ffdd
5 changed files with 77 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
import json
import os
from pathlib import Path
from app.mywhoosh.models import MyWhooshToken
class MyWhooshTokenStore:
def __init__(self, path: Path) -> None:
self.path = path
def load(self) -> MyWhooshToken | None:
try:
raw = json.loads(self.path.read_text("utf-8"))
except FileNotFoundError:
return None
return MyWhooshToken(
access_token=raw["access_token"],
refresh_token=raw.get("refresh_token"),
whoosh_id=raw.get("whoosh_id"),
)
def save(self, token: MyWhooshToken) -> None:
self.path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
tmp = self.path.with_suffix(".tmp")
tmp.write_text(
json.dumps(
{
"access_token": token.access_token,
"refresh_token": token.refresh_token,
"whoosh_id": token.whoosh_id,
},
indent=2,
),
"utf-8",
)
os.chmod(tmp, 0o600)
tmp.replace(self.path)
os.chmod(self.path, 0o600)
def clear(self) -> None:
self.path.unlink(missing_ok=True)