mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-05-12 12:45:55 +00:00
feat: add auth token storage module
This commit is contained in:
parent
482961875f
commit
a4a17a5bb2
56
nanobot/auth/__init__.py
Normal file
56
nanobot/auth/__init__.py
Normal file
@ -0,0 +1,56 @@
|
||||
"""Token storage for nanobot auth."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class AuthInfo:
|
||||
token: str
|
||||
server_url: str
|
||||
expires_at: float | None = None
|
||||
|
||||
|
||||
def _auth_file_path() -> Path:
|
||||
return Path.home() / ".nanobot" / "auth.json"
|
||||
|
||||
|
||||
def load_auth() -> AuthInfo | None:
|
||||
"""Load saved auth info from disk."""
|
||||
path = _auth_file_path()
|
||||
if not path.exists():
|
||||
return None
|
||||
try:
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
return AuthInfo(
|
||||
token=data["token"],
|
||||
server_url=data["server_url"],
|
||||
expires_at=data.get("expires_at"),
|
||||
)
|
||||
except (json.JSONDecodeError, KeyError):
|
||||
return None
|
||||
|
||||
|
||||
def save_auth(info: AuthInfo) -> None:
|
||||
"""Save auth info to disk."""
|
||||
path = _auth_file_path()
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
data = {
|
||||
"token": info.token,
|
||||
"server_url": info.server_url,
|
||||
}
|
||||
if info.expires_at is not None:
|
||||
data["expires_at"] = info.expires_at
|
||||
path.write_text(json.dumps(data, indent=2), encoding="utf-8")
|
||||
|
||||
|
||||
def delete_auth() -> bool:
|
||||
"""Delete saved auth info. Returns True if file was deleted."""
|
||||
path = _auth_file_path()
|
||||
if path.exists():
|
||||
path.unlink()
|
||||
return True
|
||||
return False
|
||||
Loading…
x
Reference in New Issue
Block a user