Files
TermiNexus 139c2514c3 feat(oidc): 泛化 OIDC Provider 为多客户端并支持 user 身份策略
- secrets 新增 sections() 枚举配置段,客户端按 [oidc_client_<id>] 自动注册
- 身份策略 IDENTITY 支持 gitea(绑定 Gitea,sub=用户名)/ user(sub=UserUUID,全用户开放)
- authorize/token/userinfo 按 client_id 校验凭据与回调,code 绑定客户端防串用
- [gitea_oidc] 默认客户端行为零差异,兼容现有 Gitea 免密登录
2026-08-13 01:18:00 +08:00

64 lines
1.5 KiB
Python

import os
import configparser
from pathlib import Path
_SECRETS_FILE = Path(__file__).resolve().parent.parent / "secrets.ini"
_config = None
def _load_config():
global _config
if _config is not None:
return _config
_config = configparser.ConfigParser()
if _SECRETS_FILE.exists():
_config.read(str(_SECRETS_FILE), encoding="utf-8")
return _config
def get(section, key, fallback=None):
cfg = _load_config()
env_key = f"{section.upper()}_{key.upper()}"
env_val = os.environ.get(env_key)
if env_val is not None:
return env_val
return cfg.get(section, key, fallback=fallback)
def sections():
"""返回 secrets.ini 中所有已定义配置段名列表(环境变量动态覆盖不参与)。"""
return _load_config().sections()
def getint(section, key, fallback=0):
val = get(section, key, fallback=None)
if val is None:
return fallback
try:
return int(val)
except (ValueError, TypeError):
return fallback
def getfloat(section, key, fallback=0.0):
val = get(section, key, fallback=None)
if val is None:
return fallback
try:
return float(val)
except (ValueError, TypeError):
return fallback
def getboolean(section, key, fallback=False):
val = get(section, key, fallback=None)
if val is None:
return fallback
return val.lower() in ("true", "1", "yes", "on")
def reload():
global _config
_config = None
_load_config()