"""Gitea OAuth2 support. Config shape (in ~/.hb.yaml): oauth: gitea: url: https://git.example.com client_id: client_secret: Register a Gitea OAuth2 application at: Gitea → Settings → Applications → OAuth2 Set the redirect URI to: https:///login/oauth/gitea/callback """ import logging import secrets import time import aiohttp logger = logging.getLogger(__name__) STATE_TTL = 600 # 10 minutes # state_token -> expiry timestamp _states: dict[str, float] = {} class OAuthError(Exception): """Raised when the OAuth2 flow fails for any reason.""" def _gitea_cfg(config: dict) -> dict: """Return the gitea sub-dict or {} if absent/incomplete.""" return config.get("oauth", {}).get("gitea", {}) def is_enabled(config: dict) -> bool: """Return True when all three required Gitea OAuth keys are present.""" g = _gitea_cfg(config) return bool(g.get("url") and g.get("client_id") and g.get("client_secret"))