定位:身份层(IdP + IAP),不含动态域名转发 / LLM 上游代理 / 用量计费。 代码(source/rust,nginx 动态模块 + Rust,单进程零外部依赖) - IdP:authorization_code(含 PKCE)/ refresh_token / client_credentials / password 四种 grant,含 discovery / introspect / userinfo / jwks 占位 - IAP:access 阶段门禁,注入 X-Auth-* 到后端,客户端伪造同名头被覆盖 - MaxKey 互通:/oauth/v20/* 与 /oauth2.0/* 别名 + context-path 归一化 + 扁平字段 - 管理接口:账号(含角色/部门/用户组)与 OAuth 接入方注册表,热写穿共享内存 - 模块改名 ngx_http_sso_module,指令 sso_state / sso_data_dir / sso_api / sso_iap, Cookie sso_sid(终端用户)与 sso_admin_session(管理员)两套身份两套会话 配置(source/conf) - nginx.conf 生产模板(TLS、IAP 子域、含 auth_sub 的日志格式) - nginx.test.conf 本机验证配置(明文 18095-18099) - tls/README.md 证书约定与公钥比对校验 前端(source/www/admin.html) - 三 tab:账号管理 / 接入方 / 接入指南;去掉审核队列、域名、LLM 上游、用量统计 测试与脚本 - tests/ 三套端到端:协议基线 14 项 · MaxKey 互通与接入 24 项 · 管理接口 31 项,全绿 - tests/demo-system 演示业务系统(Python 标准库),覆盖应用自建客户端与 IAP 零改造两种姿势 - scripts/run-local-e2e.sh 本机一键起停;scripts/seed-data.sh 物化运行时数据目录 文档 - README.md 概览与快速开始;CLAUDE.md 硬约定与排障速查 - docs/design.md 完整设计;docs/roadmap.md 待执行任务(v2 RS256+JWKS、用户管理界面)
154 lines
6.4 KiB
Plaintext
154 lines
6.4 KiB
Plaintext
# ============================================================================
|
||
# SSO 统一认证网关 —— 本机/容器验证配置
|
||
#
|
||
# 只启「认证层」,不含生产 TLS、不含真实业务后端。用途:
|
||
# * 手工观察 IdP 与 IAP 行为
|
||
# * `scripts/run-local-e2e.sh` 起停本配置跑三套端到端
|
||
#
|
||
# 明文 server:
|
||
# 18098 IdP + 管理台:/oauth/*、/oauth/v20/*(MaxKey 别名)、
|
||
# /.well-known/*、/login、/admin、/api/*
|
||
# 18099 IAP:受保护代理,注入 X-Auth-* 到 18097 回显后端(断言用)
|
||
# 18097 echo-backend:把收到的请求头原样回显为 JSON(用于断言 IAP 注入)
|
||
# 18095 IAP 接入 demo 业务系统:前置认证 + 注入 X-Auth-* → 18096
|
||
# 18096 demo 系统本体(**不由 nginx 监听**,是 tests/demo-system/app.py
|
||
# 自己起的进程;直连 18096 时它自己做 OAuth2 客户端,经 18095 访问时
|
||
# 网关已完成认证并注入身份,应用零改造)
|
||
#
|
||
# 跑法:scripts/run-local-e2e.sh(会自动把下面的绝对路径改成本机路径)
|
||
# ============================================================================
|
||
|
||
load_module /usr/local/nginx/modules/libsso_gateway.so;
|
||
|
||
worker_processes auto;
|
||
error_log /usr/local/nginx/logs/error.log info;
|
||
pid /usr/local/nginx/logs/nginx.pid;
|
||
|
||
# nginx 默认会剥掉所有环境变量,机制类配置必须显式白名单。
|
||
# 未设置时不报错,模块按各自缺省值兜底(见 source/rust/src/config.rs)。
|
||
env ADMIN_INIT_PASSWORD;
|
||
env OAUTH_SIGNING_SECRET;
|
||
env OAUTH_ISSUER;
|
||
env OAUTH_COOKIE_DOMAIN;
|
||
|
||
events {
|
||
worker_connections 1024;
|
||
}
|
||
|
||
http {
|
||
include mime.types;
|
||
default_type application/json;
|
||
|
||
# TLS 子配置目录(生产用;本验证配置不依赖,空目录不影响启动)
|
||
include /usr/local/nginx/conf/tls/*.conf;
|
||
|
||
# 共享内存 + 数据目录(OAuth 接入方注册表从这里加载)
|
||
sso_state state 10m;
|
||
sso_data_dir /usr/local/nginx/data;
|
||
|
||
# ------------------------------------------------------------------
|
||
# 18097 回显后端:把注入进来的 X-Auth-* 请求头回显成 JSON,供断言
|
||
# ------------------------------------------------------------------
|
||
server {
|
||
listen 18097;
|
||
location / {
|
||
default_type application/json;
|
||
return 200 '{"x_auth_user":"$http_x_auth_user","x_auth_email":"$http_x_auth_email","x_auth_groups":"$http_x_auth_groups","x_auth_sub":"$http_x_auth_sub","x_auth_token":"$http_x_auth_token","remote_addr":"$remote_addr"}';
|
||
}
|
||
}
|
||
|
||
# ------------------------------------------------------------------
|
||
# 18098 IdP + 管理台
|
||
# ------------------------------------------------------------------
|
||
server {
|
||
listen 18098 default_server;
|
||
server_name sso.yourdomain.com;
|
||
|
||
client_max_body_size 2m;
|
||
client_body_buffer_size 1m;
|
||
|
||
# 管理台单页(数据全部通过 /api/admin/* 拉取,故与 IdP 同端口不影响端点语义)
|
||
location = /admin {
|
||
default_type text/html;
|
||
alias /usr/local/nginx/www/admin.html;
|
||
}
|
||
location = /admin/ {
|
||
default_type text/html;
|
||
alias /usr/local/nginx/www/admin.html;
|
||
}
|
||
|
||
# 全部交给模块分发:MaxKey 别名在 /oauth/v20/*、/oauth2.0/*,还有
|
||
# /maxkey/oauth/v20/* 这类带 context-path 的写法(模块内做前缀归一化),
|
||
# 逐条列 location 必然漏,故整站交给 sso_api。
|
||
location / { sso_api on; }
|
||
}
|
||
|
||
# ------------------------------------------------------------------
|
||
# 18099 IAP:受保护代理示例(注入 X-Auth-* 到回显后端)
|
||
# ------------------------------------------------------------------
|
||
server {
|
||
listen 18099 default_server;
|
||
server_name ~^(?<subdomain>.+)\.app\.yourdomain\.com$;
|
||
|
||
location / {
|
||
# 门禁:IAP 身份感知代理开关(access 阶段)
|
||
sso_iap on;
|
||
|
||
# 本验证配置用固定后端;生产用按环境生成的 proxy_pass
|
||
proxy_pass http://127.0.0.1:18097;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Real-IP $remote_addr;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
|
||
# ---- IAP 身份注入:网关断言覆盖客户端伪造同名头 ----
|
||
# 后端只信这五个头,它们由网关在 access 阶段写入(见 lib.rs set_auth_vars)
|
||
proxy_set_header X-Auth-User $auth_user;
|
||
proxy_set_header X-Auth-Email $auth_email;
|
||
proxy_set_header X-Auth-Groups $auth_groups;
|
||
proxy_set_header X-Auth-Sub $auth_sub;
|
||
proxy_set_header X-Auth-Token $auth_token;
|
||
|
||
# 无身份(401)→ 内部跳登录页并回跳
|
||
error_page 401 = @oauth_login;
|
||
}
|
||
|
||
location @oauth_login {
|
||
return 302 "http://127.0.0.1:18098/login?redirect=$scheme://$host$request_uri";
|
||
}
|
||
}
|
||
|
||
# ------------------------------------------------------------------
|
||
# 18095 IAP 接入:把统一认证前置于 demo 业务系统
|
||
#
|
||
# 与 18099 的区别:18099 指向回显后端(只为断言请求头),本 server 指向真正的
|
||
# 业务系统,演示**应用零改造接入**——应用不写 OAuth 代码,只读 X-Auth-* 头。
|
||
# ------------------------------------------------------------------
|
||
server {
|
||
listen 18095 default_server;
|
||
server_name 127.0.0.1;
|
||
|
||
location / {
|
||
sso_iap on;
|
||
|
||
proxy_pass http://127.0.0.1:18096;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Host $host;
|
||
proxy_set_header X-Real-IP $remote_addr;
|
||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||
|
||
proxy_set_header X-Auth-User $auth_user;
|
||
proxy_set_header X-Auth-Email $auth_email;
|
||
proxy_set_header X-Auth-Groups $auth_groups;
|
||
proxy_set_header X-Auth-Sub $auth_sub;
|
||
proxy_set_header X-Auth-Token $auth_token;
|
||
|
||
error_page 401 = @oauth_login_demo;
|
||
}
|
||
|
||
location @oauth_login_demo {
|
||
return 302 "http://127.0.0.1:18098/login?redirect=$scheme://$host$request_uri";
|
||
}
|
||
}
|
||
}
|