@nihplod/sso-sdk 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,6 +10,10 @@ import { NextRequest, NextResponse } from 'next/server';
10
10
  * - PKCE code_challenge 使用 Web Crypto API 的 crypto.subtle.digest(SHA-256)
11
11
  * (Edge Runtime 18+ 完全支持此 API)
12
12
  *
13
+ * ⚠️ 安全须知:middleware 只是 UX 层(未认证重定向的入口优化),
14
+ * 不能作为安全边界。敏感接口必须在 Route Handler / Server Component 中
15
+ * 用 @nihplod/sso-verify 对 access_token 做二次校验。
16
+ *
13
17
  * 用法 (src/middleware.ts):
14
18
  * ```ts
15
19
  * import { createSsoMiddleware } from "@nihplod/sso-sdk/next";
@@ -39,6 +43,13 @@ interface SsoMiddlewareConfig {
39
43
  /**
40
44
  * 主站用户会话 Cookie 名称,用于检测是否已有 SSO 会话。
41
45
  * 默认 "__Host-user_token"(与主站 C 端登录 Cookie 一致)。
46
+ *
47
+ * @deprecated 该「主站会话 Cookie 快速通道」已移除,此选项不再生效:
48
+ * 1. `__Host-user_token` 存的是主站内部 token(type="user", aud="user"),
49
+ * introspect 端点只接受 type="access_token",校验必然返回 inactive;
50
+ * 2. `__Host-` Cookie 不会下发到子域/其他域名,跨域子项目根本收不到。
51
+ * 该分支永不命中,却让每个请求白付一次 introspection 往返并被负缓存 30s。
52
+ * 保留字段仅为兼容旧配置,请勿使用;会话检测以本站 access_token Cookie 为准。
42
53
  */
43
54
  ssoCookieName?: string;
44
55
  /**
@@ -51,8 +62,27 @@ interface SsoMiddlewareConfig {
51
62
  * 默认 true(推荐)。设为 false 时仅检查 Cookie 存在性,延迟最低但可能放行
52
63
  * 已失效/被撤销的会话 —— 中间件本质上只是 UX 层,敏感数据必须在
53
64
  * Route Handler / Server Component 中二次校验。
65
+ *
66
+ * @deprecated 随「主站会话 Cookie 快速通道」一并移除(见 ssoCookieName),
67
+ * 此选项不再生效。保留字段仅为兼容旧配置,请勿使用。
54
68
  */
55
69
  validateSsoCookie?: boolean;
70
+ /**
71
+ * Introspection 不可达(网络异常/超时/5xx,token 有效性未确证)时的策略。
72
+ *
73
+ * 默认 false(fail-open):对已持有 access_token Cookie 的请求放行——
74
+ * middleware 只是 UX 层,敏感接口的鉴权由 Route Handler / Server Component
75
+ * 兜底,此时重定向到一个同样不可达的 SSO 中心没有意义。
76
+ *
77
+ * 设为 true(fail-closed)时:对持有 access_token Cookie 的请求返回
78
+ * 502 错误页而不是放行。适用于对可用性敏感、宁可拒绝也不放行的场景。
79
+ * 注意 fail-closed 只影响「不可达」这一种结果;token 确证无效(inactive)
80
+ * 无论该选项如何都会清除 Cookie 并重定向到 SSO 登录。
81
+ *
82
+ * ⚠️ 无论取何值,middleware 都只是 UX 层:敏感接口必须在
83
+ * Route Handler / Server Component 中用 @nihplod/sso-verify 二次校验。
84
+ */
85
+ failClosedOnIntrospectionError?: boolean;
56
86
  /** Access Token Cookie 名称,默认 __Host-nihplod_sso_at */
57
87
  accessTokenCookieName?: string;
58
88
  /** State Cookie 名称,默认 __Host-nihplod_sso_state */
@@ -137,6 +167,12 @@ interface CallbackRouteConfig {
137
167
  returnUrlCookieName?: string;
138
168
  /** PKCE Verifier Cookie 名称,默认 __Secure-nihplod_sso_verifier */
139
169
  verifierCookieName?: string;
170
+ /**
171
+ * 请求的 OAuth scope(空格分隔),建议与 createSsoMiddleware 的 scopes 保持一致。
172
+ * 仅当**显式配置**且包含 openid 时回调才强制要求 token 响应携带 id_token(fail-closed);
173
+ * 未配置时保持兼容行为(id_token 存在则校验,缺失不拒绝),避免升级后旧接入方登录失败。
174
+ */
175
+ scopes?: string;
140
176
  /**
141
177
  * 本地 HTTP 开发模式(默认 false)。关闭 Cookie 的 Secure 属性并去除
142
178
  * __Host-/__Secure- 前缀;必须与 createSsoMiddleware 的配置保持一致,
@@ -150,6 +186,20 @@ interface CallbackRouteConfig {
150
186
  * 仅用于 token 交换等服务器间请求;浏览器跳转仍使用 ssoBaseUrl 公网地址。
151
187
  */
152
188
  serverBaseUrl?: string;
189
+ /**
190
+ * 自定义回调错误响应(可选)。
191
+ * 传入后优先生效;返回 undefined 时回退默认行为。
192
+ * 默认行为:浏览器导航(Accept 含 text/html 且未带 ?format=json)渲染内置中文错误页
193
+ * (含"重新登录"入口);其他请求(fetch/自动化)返回 JSON,保持 API 兼容。
194
+ */
195
+ renderErrorPage?: (ctx: {
196
+ error: string;
197
+ errorDescription: string;
198
+ status: number;
199
+ request: NextRequest;
200
+ }) => Response | undefined | Promise<Response | undefined>;
201
+ /** 调试日志开关(默认 false):回调失败时输出服务端告警日志,便于定位配置/流程问题 */
202
+ debug?: boolean;
153
203
  }
154
204
  declare function createCallbackRouteHandler(config: CallbackRouteConfig): (request: NextRequest) => Promise<NextResponse<unknown>>;
155
205
 
@@ -10,6 +10,10 @@ import { NextRequest, NextResponse } from 'next/server';
10
10
  * - PKCE code_challenge 使用 Web Crypto API 的 crypto.subtle.digest(SHA-256)
11
11
  * (Edge Runtime 18+ 完全支持此 API)
12
12
  *
13
+ * ⚠️ 安全须知:middleware 只是 UX 层(未认证重定向的入口优化),
14
+ * 不能作为安全边界。敏感接口必须在 Route Handler / Server Component 中
15
+ * 用 @nihplod/sso-verify 对 access_token 做二次校验。
16
+ *
13
17
  * 用法 (src/middleware.ts):
14
18
  * ```ts
15
19
  * import { createSsoMiddleware } from "@nihplod/sso-sdk/next";
@@ -39,6 +43,13 @@ interface SsoMiddlewareConfig {
39
43
  /**
40
44
  * 主站用户会话 Cookie 名称,用于检测是否已有 SSO 会话。
41
45
  * 默认 "__Host-user_token"(与主站 C 端登录 Cookie 一致)。
46
+ *
47
+ * @deprecated 该「主站会话 Cookie 快速通道」已移除,此选项不再生效:
48
+ * 1. `__Host-user_token` 存的是主站内部 token(type="user", aud="user"),
49
+ * introspect 端点只接受 type="access_token",校验必然返回 inactive;
50
+ * 2. `__Host-` Cookie 不会下发到子域/其他域名,跨域子项目根本收不到。
51
+ * 该分支永不命中,却让每个请求白付一次 introspection 往返并被负缓存 30s。
52
+ * 保留字段仅为兼容旧配置,请勿使用;会话检测以本站 access_token Cookie 为准。
42
53
  */
43
54
  ssoCookieName?: string;
44
55
  /**
@@ -51,8 +62,27 @@ interface SsoMiddlewareConfig {
51
62
  * 默认 true(推荐)。设为 false 时仅检查 Cookie 存在性,延迟最低但可能放行
52
63
  * 已失效/被撤销的会话 —— 中间件本质上只是 UX 层,敏感数据必须在
53
64
  * Route Handler / Server Component 中二次校验。
65
+ *
66
+ * @deprecated 随「主站会话 Cookie 快速通道」一并移除(见 ssoCookieName),
67
+ * 此选项不再生效。保留字段仅为兼容旧配置,请勿使用。
54
68
  */
55
69
  validateSsoCookie?: boolean;
70
+ /**
71
+ * Introspection 不可达(网络异常/超时/5xx,token 有效性未确证)时的策略。
72
+ *
73
+ * 默认 false(fail-open):对已持有 access_token Cookie 的请求放行——
74
+ * middleware 只是 UX 层,敏感接口的鉴权由 Route Handler / Server Component
75
+ * 兜底,此时重定向到一个同样不可达的 SSO 中心没有意义。
76
+ *
77
+ * 设为 true(fail-closed)时:对持有 access_token Cookie 的请求返回
78
+ * 502 错误页而不是放行。适用于对可用性敏感、宁可拒绝也不放行的场景。
79
+ * 注意 fail-closed 只影响「不可达」这一种结果;token 确证无效(inactive)
80
+ * 无论该选项如何都会清除 Cookie 并重定向到 SSO 登录。
81
+ *
82
+ * ⚠️ 无论取何值,middleware 都只是 UX 层:敏感接口必须在
83
+ * Route Handler / Server Component 中用 @nihplod/sso-verify 二次校验。
84
+ */
85
+ failClosedOnIntrospectionError?: boolean;
56
86
  /** Access Token Cookie 名称,默认 __Host-nihplod_sso_at */
57
87
  accessTokenCookieName?: string;
58
88
  /** State Cookie 名称,默认 __Host-nihplod_sso_state */
@@ -137,6 +167,12 @@ interface CallbackRouteConfig {
137
167
  returnUrlCookieName?: string;
138
168
  /** PKCE Verifier Cookie 名称,默认 __Secure-nihplod_sso_verifier */
139
169
  verifierCookieName?: string;
170
+ /**
171
+ * 请求的 OAuth scope(空格分隔),建议与 createSsoMiddleware 的 scopes 保持一致。
172
+ * 仅当**显式配置**且包含 openid 时回调才强制要求 token 响应携带 id_token(fail-closed);
173
+ * 未配置时保持兼容行为(id_token 存在则校验,缺失不拒绝),避免升级后旧接入方登录失败。
174
+ */
175
+ scopes?: string;
140
176
  /**
141
177
  * 本地 HTTP 开发模式(默认 false)。关闭 Cookie 的 Secure 属性并去除
142
178
  * __Host-/__Secure- 前缀;必须与 createSsoMiddleware 的配置保持一致,
@@ -150,6 +186,20 @@ interface CallbackRouteConfig {
150
186
  * 仅用于 token 交换等服务器间请求;浏览器跳转仍使用 ssoBaseUrl 公网地址。
151
187
  */
152
188
  serverBaseUrl?: string;
189
+ /**
190
+ * 自定义回调错误响应(可选)。
191
+ * 传入后优先生效;返回 undefined 时回退默认行为。
192
+ * 默认行为:浏览器导航(Accept 含 text/html 且未带 ?format=json)渲染内置中文错误页
193
+ * (含"重新登录"入口);其他请求(fetch/自动化)返回 JSON,保持 API 兼容。
194
+ */
195
+ renderErrorPage?: (ctx: {
196
+ error: string;
197
+ errorDescription: string;
198
+ status: number;
199
+ request: NextRequest;
200
+ }) => Response | undefined | Promise<Response | undefined>;
201
+ /** 调试日志开关(默认 false):回调失败时输出服务端告警日志,便于定位配置/流程问题 */
202
+ debug?: boolean;
153
203
  }
154
204
  declare function createCallbackRouteHandler(config: CallbackRouteConfig): (request: NextRequest) => Promise<NextResponse<unknown>>;
155
205