| """Client-side credential storage: the Flash API key + control-plane URL. |
| |
| Stored in ``~/.flash/config.json`` (dir 0700, file 0600 — it holds a secret). |
| Environment variables take precedence so CI/agents can inject credentials without |
| touching the file: ``FREESOLO_API_KEY`` for the key, ``FLASH_API_URL`` for the URL. |
| """ |
|
|
| from __future__ import annotations |
|
|
| import os |
| from pathlib import Path |
|
|
| from .._fileio import read_json_or_empty, secure_json_write |
|
|
| DEFAULT_API_URL = "https://flash.freesolo.co" |
|
|
| CONFIG_DIR = Path.home() / ".flash" |
| CONFIG_PATH = CONFIG_DIR / "config.json" |
|
|
|
|
| def _read_config() -> dict: |
| return read_json_or_empty(CONFIG_PATH) |
|
|
|
|
| def load_credentials() -> tuple[str, str | None]: |
| """Resolve (api_url, api_key); the key is None when the user hasn't logged in.""" |
| cfg = _read_config() |
| api_url = os.environ.get("FLASH_API_URL") or cfg.get("api_url") or DEFAULT_API_URL |
| api_key = os.environ.get("FREESOLO_API_KEY") or cfg.get("api_key") |
| return api_url.rstrip("/"), api_key |
|
|
|
|
| def save_credentials(api_key: str, api_url: str | None = None) -> Path: |
| """Persist the key (and optionally a non-default URL) with private permissions.""" |
| cfg = _read_config() |
| cfg["api_key"] = api_key |
| if api_url: |
| |
| |
| |
| if api_url.rstrip("/") == DEFAULT_API_URL.rstrip("/"): |
| cfg.pop("api_url", None) |
| else: |
| cfg["api_url"] = api_url.rstrip("/") |
| secure_json_write(CONFIG_PATH, cfg) |
| return CONFIG_PATH |
|
|