Spaces:
Sleeping
Sleeping
File size: 2,759 Bytes
2f1a5fb 4046a11 2f1a5fb 4046a11 2f1a5fb 4046a11 4e31d1f 4046a11 4e31d1f 4046a11 2f1a5fb 2b0510e 2f1a5fb 4046a11 4e31d1f 4046a11 4e31d1f 2f1a5fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | from __future__ import annotations
import os
from typing import Any
import keyring
from .config import KEYRING_ACCOUNT, KEYRING_SERVICE, TOKEN_ENV_VAR
from .exceptions import AuthenticationError
class AuthService:
"""Authenticate and securely store Hugging Face tokens.
Token resolution order (highest to lowest priority):
1. HF_TOKEN environment variable (useful for container/CI)
2. keyring (OS-specific secure storage)
3. Manual --token CLI option
"""
KEYRING_SERVICE = KEYRING_SERVICE
KEYRING_ACCOUNT = KEYRING_ACCOUNT
def authenticate(self, token: str) -> dict[str, Any]:
token = token.strip()
if not token:
raise AuthenticationError("Token is required")
try:
# Import HfApi lazily so caller can configure HF-related monkeypatches/timeouts first
from huggingface_hub import HfApi
api = HfApi(token=token)
whoami = api.whoami()
except Exception as exc: # pragma: no cover - external API behavior
raise AuthenticationError(f"Token validation failed: {exc}") from exc
try:
keyring.set_password(self.KEYRING_SERVICE, self.KEYRING_ACCOUNT, token)
except Exception as exc: # pragma: no cover - backend-specific behavior
raise AuthenticationError(f"Token validated, but could not be saved securely: {exc}") from exc
return {
"username": whoami.get("name") or "unknown",
"email": whoami.get("email", ""),
"user_id": whoami.get("id") or whoami.get("user_id", ""),
}
def get_token(self) -> str | None:
"""Retrieve token from environment or secure storage.
Priority: HF_TOKEN env var > keyring storage.
"""
env_token = os.getenv(TOKEN_ENV_VAR)
if env_token:
return env_token.strip()
try:
return keyring.get_password(self.KEYRING_SERVICE, self.KEYRING_ACCOUNT)
except Exception as exc: # pragma: no cover - backend-specific behavior
raise AuthenticationError(f"Could not read stored token: {exc}") from exc
def require_token(self) -> str:
token = self.get_token()
if not token:
raise AuthenticationError("No stored token found. Run 'birdnet-uploader login' first.")
return token
def clear_token(self) -> None:
try:
keyring.delete_password(self.KEYRING_SERVICE, self.KEYRING_ACCOUNT)
except keyring.errors.PasswordDeleteError:
return
except Exception as exc: # pragma: no cover - backend-specific behavior
raise AuthenticationError(f"Could not clear stored token: {exc}") from exc
else:
return
|