Frontend API config and SSO toggles
Some checks failed
continuous-integration/drone/push Build was killed
Some checks failed
continuous-integration/drone/push Build was killed
This commit is contained in:
@@ -4,28 +4,58 @@
|
||||
import os
|
||||
from typing import Dict, Any
|
||||
|
||||
# Конфигурация провайдеров OpenID Connect
|
||||
OIDC_PROVIDERS = {
|
||||
"zitadel": {
|
||||
"name": "ZITADEL",
|
||||
"client_id": os.getenv("ZITADEL_CLIENT_ID", ""),
|
||||
"client_secret": os.getenv("ZITADEL_CLIENT_SECRET", ""),
|
||||
"server_metadata_url": os.getenv("ZITADEL_ISSUER", "") + "/.well-known/openid-configuration",
|
||||
"issuer": os.getenv("ZITADEL_ISSUER", ""),
|
||||
"scopes": ["openid", "email", "profile"],
|
||||
"icon": "🔐",
|
||||
"color": "bg-purple-600 hover:bg-purple-700"
|
||||
|
||||
def _is_truthy(value: str) -> bool:
|
||||
"""Безопасный парсинг bool из переменных окружения."""
|
||||
return value.strip().lower() in {"1", "true", "yes", "on"}
|
||||
|
||||
|
||||
def _is_config_value_set(value: str) -> bool:
|
||||
"""Проверка, что значение реально задано, а не заглушка."""
|
||||
normalized = value.strip().lower()
|
||||
return normalized not in {"", "none", "null", "undefined"}
|
||||
|
||||
|
||||
def is_sso_enabled() -> bool:
|
||||
"""Глобальный флаг включения SSO через env."""
|
||||
# По умолчанию SSO включён, чтобы не ломать существующее поведение.
|
||||
raw = os.getenv("SSO_ENABLED", "true")
|
||||
return _is_truthy(raw)
|
||||
|
||||
|
||||
def get_oidc_providers() -> Dict[str, Dict[str, Any]]:
|
||||
"""Собрать конфигурацию OpenID Connect провайдеров из env."""
|
||||
issuer = os.getenv("ZITADEL_ISSUER", "")
|
||||
return {
|
||||
"zitadel": {
|
||||
"name": "ZITADEL",
|
||||
"client_id": os.getenv("ZITADEL_CLIENT_ID", ""),
|
||||
"client_secret": os.getenv("ZITADEL_CLIENT_SECRET", ""),
|
||||
"server_metadata_url": issuer.rstrip("/") + "/.well-known/openid-configuration" if issuer else "",
|
||||
"issuer": issuer,
|
||||
"scopes": ["openid", "email", "profile"],
|
||||
"icon": "🔐",
|
||||
"color": "bg-purple-600 hover:bg-purple-700"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Для обратной совместимости с импортами из других модулей
|
||||
OIDC_PROVIDERS = get_oidc_providers()
|
||||
|
||||
|
||||
def get_enabled_providers() -> Dict[str, Dict[str, Any]]:
|
||||
"""Получить список включённых провайдеров (с настроенными client_id)"""
|
||||
enabled = {}
|
||||
for provider_id, config in OIDC_PROVIDERS.items():
|
||||
if config.get("client_id") and config.get("issuer"):
|
||||
"""Получить список включённых провайдеров (с настроенным client_id)."""
|
||||
if not is_sso_enabled():
|
||||
return {}
|
||||
|
||||
enabled: Dict[str, Dict[str, Any]] = {}
|
||||
for provider_id, config in get_oidc_providers().items():
|
||||
if _is_config_value_set(config.get("client_id", "")) and _is_config_value_set(config.get("issuer", "")):
|
||||
enabled[provider_id] = config
|
||||
return enabled
|
||||
|
||||
|
||||
def get_redirect_uri(provider_id: str, base_url: str = "http://localhost:8000") -> str:
|
||||
"""Получить redirect URI для провайдера"""
|
||||
return f"{base_url}/api/auth/oidc/{provider_id}/callback"
|
||||
"""Получить redirect URI для провайдера."""
|
||||
return f"{base_url}/api/auth/oidc/{provider_id}/callback"
|
||||
|
||||
Reference in New Issue
Block a user