Newer
Older
gnexus-tasks / backend / app / auth / client.py
"""Интеграция SSO через gnexus-gauth.

Библиотека gnexus-gauth framework-agnostic: ей передаются конфиг, token endpoint,
runtime user provider, webhook-верификатор и stores. Здесь собираем единый
GAuthClient для всего приложения. Полный OAuth-флоу (redirect → callback → cookie)
подключается в M0; пока — фабрика клиента и заглушки маршрутов.
"""

from gnexus_gauth.client import GAuthClient
from gnexus_gauth.config import GAuthConfig
from gnexus_gauth.oauth import HttpTokenEndpoint
from gnexus_gauth.runtime import HttpRuntimeUserProvider
from gnexus_gauth.support import InMemoryPkceStore, InMemoryStateStore
from gnexus_gauth.webhook import HmacWebhookVerifier, JsonWebhookParser

from app.config import get_settings

# TODO(M0): InMemory-заглушки заменить на постоянные (redis/db) до деплоя на VPS.
_client: GAuthClient | None = None


def get_gauth_client() -> GAuthClient:
    global _client
    if _client is None:
        s = get_settings()
        config = GAuthConfig(
            base_url=s.gauth_base_url,
            client_id=s.gauth_client_id,
            client_secret=s.gauth_client_secret,
            redirect_uri=s.gauth_redirect_uri,
        )
        _client = GAuthClient(
            config=config,
            token_endpoint=HttpTokenEndpoint(config),
            runtime_user_provider=HttpRuntimeUserProvider(config),
            webhook_verifier=HmacWebhookVerifier(config),
            webhook_parser=JsonWebhookParser(),
            state_store=InMemoryStateStore(),
            pkce_store=InMemoryPkceStore(),
        )
    return _client