30 lines
909 B
Python
30 lines
909 B
Python
#!/usr/bin/env python3
|
|
"""Local Gitea credential loading without tracked secrets."""
|
|
|
|
import os
|
|
import stat
|
|
from pathlib import Path
|
|
|
|
|
|
DEFAULT_TOKEN_FILE = Path("~/.config/opencode/secrets/gitea-token").expanduser()
|
|
|
|
|
|
def load_gitea_token(required: bool = True) -> str:
|
|
"""Load a token from the environment or a private local file."""
|
|
token = os.environ.get("GITEA_TOKEN", "").strip()
|
|
if token:
|
|
return token
|
|
|
|
path = Path(os.environ.get("GITEA_TOKEN_FILE", DEFAULT_TOKEN_FILE)).expanduser()
|
|
if path.is_file():
|
|
mode = stat.S_IMODE(path.stat().st_mode)
|
|
if mode & 0o077:
|
|
raise PermissionError(f"Gitea token file must be mode 0600: {path}")
|
|
token = path.read_text(encoding="utf-8").strip()
|
|
if token:
|
|
return token
|
|
|
|
if required:
|
|
raise RuntimeError("Set GITEA_TOKEN or create a mode-0600 GITEA_TOKEN_FILE")
|
|
return ""
|