@nihplod/sso-sdk 1.3.1 → 1.4.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.
- package/README.md +46 -5
- package/dist/index.d.mts +25 -5
- package/dist/index.d.ts +25 -5
- package/dist/index.js +64 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +64 -4
- package/dist/index.mjs.map +1 -1
- package/dist/next/index.d.mts +73 -3
- package/dist/next/index.d.ts +73 -3
- package/dist/next/index.js +213 -2
- package/dist/next/index.js.map +1 -1
- package/dist/next/index.mjs +212 -2
- package/dist/next/index.mjs.map +1 -1
- package/dist/react/index.d.mts +25 -5
- package/dist/react/index.d.ts +25 -5
- package/dist/react/index.js +72 -4
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +72 -4
- package/dist/react/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/next/index.d.ts
CHANGED
|
@@ -159,7 +159,9 @@ declare function createCallbackRouteHandler(config: CallbackRouteConfig): (reque
|
|
|
159
159
|
* 在 /api/auth/logout 路由中处理本地登出 + RP-Initiated Logout:
|
|
160
160
|
* 1. 从 cookie 读取 refresh_token 并调用 revocation_endpoint 撤销
|
|
161
161
|
* 2. 清除所有 SSO cookie
|
|
162
|
-
* 3.
|
|
162
|
+
* 3. 按退出范围(defaultScope / 表单 global 字段)决定:
|
|
163
|
+
* - "local"(默认):仅退出本站,重定向回本站首页
|
|
164
|
+
* - "global":重定向到 SSO 中心 end-session,全局退出所有 NIHPLOD 平台
|
|
163
165
|
*
|
|
164
166
|
* 用法 (src/app/api/auth/logout/route.ts):
|
|
165
167
|
* ```ts
|
|
@@ -193,7 +195,18 @@ interface LogoutRouteConfig {
|
|
|
193
195
|
clientSecret?: string;
|
|
194
196
|
/** 登出后跳转回子项目的地址,默认取 redirectUri 的 origin */
|
|
195
197
|
postLogoutRedirectUri?: string;
|
|
196
|
-
/**
|
|
198
|
+
/**
|
|
199
|
+
* 默认退出范围(默认 "local"):
|
|
200
|
+
* - "local":仅退出本站(撤销 refresh_token + 清本地 cookie),不跳转 SSO 中心;
|
|
201
|
+
* - "global":同时跳转 SSO 中心 end-session 全局退出(RP-Initiated Logout)。
|
|
202
|
+
* 用户可在 GET 确认页勾选"同时退出所有 NIHPLOD 平台"后通过表单字段
|
|
203
|
+
* global=1 覆盖默认值;POST 表单携带 global 字段时以表单为准。
|
|
204
|
+
*/
|
|
205
|
+
defaultScope?: "local" | "global";
|
|
206
|
+
/**
|
|
207
|
+
* @deprecated 请改用 defaultScope(true → "global",false → "local")。
|
|
208
|
+
* 仅为兼容保留;与 defaultScope 同时传入时以 defaultScope 为准。
|
|
209
|
+
*/
|
|
197
210
|
redirectToSso?: boolean;
|
|
198
211
|
/** Access Token Cookie 名称 */
|
|
199
212
|
accessTokenCookieName?: string;
|
|
@@ -230,6 +243,63 @@ interface LogoutRouteConfig {
|
|
|
230
243
|
}
|
|
231
244
|
declare function createLogoutRouteHandler(config: LogoutRouteConfig): (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
232
245
|
|
|
246
|
+
/** 验证通过的 logout_token 负载(仅暴露子站清会话所需的标识) */
|
|
247
|
+
interface LogoutTokenPayload {
|
|
248
|
+
sub?: string;
|
|
249
|
+
sid?: string;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* App Router Backchannel Logout 接收端 Route Handler
|
|
254
|
+
*
|
|
255
|
+
* 接收 SSO 中心在用户全局登出 / 撤销授权时推送的 logout_token
|
|
256
|
+
* (OIDC Back-Channel Logout 1.0),验证通过后清除本站 SSO cookie 并
|
|
257
|
+
* 调用 onLogout 钩子(子站在钩子里清除自己的本地会话,如数据库 session)。
|
|
258
|
+
*
|
|
259
|
+
* 前置条件:在 SSO 中心管理后台为本 client 注册 backchannelLogoutUri
|
|
260
|
+
* 指向本路由的公网地址(必须 HTTPS)。
|
|
261
|
+
*
|
|
262
|
+
* 用法 (src/app/api/auth/backchannel-logout/route.ts):
|
|
263
|
+
* ```ts
|
|
264
|
+
* import { createBackchannelLogoutRouteHandler } from "@nihplod/sso-sdk/next";
|
|
265
|
+
*
|
|
266
|
+
* export const POST = createBackchannelLogoutRouteHandler({
|
|
267
|
+
* clientId: "my-app",
|
|
268
|
+
* ssoBaseUrl: "https://nihplod.cn",
|
|
269
|
+
* onLogout: async ({ sub, sid }) => {
|
|
270
|
+
* // 清除子站本地会话(按 sub 或 sid 定位)
|
|
271
|
+
* },
|
|
272
|
+
* });
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
|
|
276
|
+
interface BackchannelLogoutRouteConfig {
|
|
277
|
+
/** OAuth Client ID(logout_token 的 aud 必须等于它) */
|
|
278
|
+
clientId: string;
|
|
279
|
+
/** SSO 中心地址(Discovery / JWKS 基准) */
|
|
280
|
+
ssoBaseUrl: string;
|
|
281
|
+
/**
|
|
282
|
+
* 登出通知钩子:logout_token 验证通过后调用,
|
|
283
|
+
* 子站在此处按 sub / sid 清除自己的本地会话(数据库 session 等)。
|
|
284
|
+
* 抛错时返回 500,让 IdP 重投。
|
|
285
|
+
*/
|
|
286
|
+
onLogout?: (payload: LogoutTokenPayload, request: NextRequest) => void | Promise<void>;
|
|
287
|
+
/** Access Token Cookie 名称 */
|
|
288
|
+
accessTokenCookieName?: string;
|
|
289
|
+
/** Refresh Token Cookie 名称 */
|
|
290
|
+
refreshTokenCookieName?: string;
|
|
291
|
+
/** ID Token Cookie 名称,默认 __Host-nihplod_sso_id */
|
|
292
|
+
idTokenCookieName?: string;
|
|
293
|
+
/**
|
|
294
|
+
* 本地 HTTP 开发模式(默认 false)。关闭 Cookie 的 Secure 属性并去除
|
|
295
|
+
* __Host-/__Secure- 前缀;必须与 middleware / callback / logout 的配置保持一致。
|
|
296
|
+
* 生产严禁启用——生产环境(NODE_ENV=production 且 ssoBaseUrl 为 https)下
|
|
297
|
+
* 该开关会被强制忽略并告警。
|
|
298
|
+
*/
|
|
299
|
+
insecureLocalDev?: boolean;
|
|
300
|
+
}
|
|
301
|
+
declare function createBackchannelLogoutRouteHandler(config: BackchannelLogoutRouteConfig): (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
302
|
+
|
|
233
303
|
/**
|
|
234
304
|
* Next.js 集成默认 Cookie 名称
|
|
235
305
|
*
|
|
@@ -271,4 +341,4 @@ declare function getSecureCookieOptions(maxAge?: number, path?: string, secure?:
|
|
|
271
341
|
maxAge?: number;
|
|
272
342
|
};
|
|
273
343
|
|
|
274
|
-
export { type CallbackRouteConfig, DEFAULT_ACCESS_TOKEN_COOKIE_NAME, DEFAULT_ID_TOKEN_COOKIE_NAME, DEFAULT_LOGOUT_STATE_COOKIE_NAME, DEFAULT_NONCE_COOKIE_NAME, DEFAULT_REFRESH_TOKEN_COOKIE_NAME, DEFAULT_RETURN_COOKIE_NAME, DEFAULT_STATE_COOKIE_NAME, DEFAULT_VERIFIER_COOKIE_NAME, type LogoutRouteConfig, type SsoMiddlewareConfig, createCallbackRouteHandler, createLogoutRouteHandler, createSsoMiddleware, getHostCookieOptions, getSecureCookieOptions, toInsecureCookieName };
|
|
344
|
+
export { type BackchannelLogoutRouteConfig, type CallbackRouteConfig, DEFAULT_ACCESS_TOKEN_COOKIE_NAME, DEFAULT_ID_TOKEN_COOKIE_NAME, DEFAULT_LOGOUT_STATE_COOKIE_NAME, DEFAULT_NONCE_COOKIE_NAME, DEFAULT_REFRESH_TOKEN_COOKIE_NAME, DEFAULT_RETURN_COOKIE_NAME, DEFAULT_STATE_COOKIE_NAME, DEFAULT_VERIFIER_COOKIE_NAME, type LogoutRouteConfig, type SsoMiddlewareConfig, createBackchannelLogoutRouteHandler, createCallbackRouteHandler, createLogoutRouteHandler, createSsoMiddleware, getHostCookieOptions, getSecureCookieOptions, toInsecureCookieName };
|
package/dist/next/index.js
CHANGED
|
@@ -28,6 +28,7 @@ __export(next_exports, {
|
|
|
28
28
|
DEFAULT_RETURN_COOKIE_NAME: () => DEFAULT_RETURN_COOKIE_NAME,
|
|
29
29
|
DEFAULT_STATE_COOKIE_NAME: () => DEFAULT_STATE_COOKIE_NAME,
|
|
30
30
|
DEFAULT_VERIFIER_COOKIE_NAME: () => DEFAULT_VERIFIER_COOKIE_NAME,
|
|
31
|
+
createBackchannelLogoutRouteHandler: () => createBackchannelLogoutRouteHandler,
|
|
31
32
|
createCallbackRouteHandler: () => createCallbackRouteHandler,
|
|
32
33
|
createLogoutRouteHandler: () => createLogoutRouteHandler,
|
|
33
34
|
createSsoMiddleware: () => createSsoMiddleware,
|
|
@@ -790,6 +791,9 @@ function buildLogoutConfirmHtml() {
|
|
|
790
791
|
<body style="display:flex;align-items:center;justify-content:center;min-height:100vh;font-family:system-ui,sans-serif;">
|
|
791
792
|
<form method="post" style="text-align:center;">
|
|
792
793
|
<p>\u786E\u5B9A\u8981\u9000\u51FA\u767B\u5F55\u5417\uFF1F</p>
|
|
794
|
+
<label style="display:flex;align-items:center;justify-content:center;gap:6px;margin:12px 0;font-size:14px;color:#374151;cursor:pointer;">
|
|
795
|
+
<input type="checkbox" name="global" value="1">\u540C\u65F6\u9000\u51FA\u6240\u6709 NIHPLOD \u5E73\u53F0
|
|
796
|
+
</label>
|
|
793
797
|
<button type="submit" style="padding:10px 20px;background-color:#ef4444;color:white;border:none;border-radius:8px;cursor:pointer;font-size:14px;">\u9000\u51FA\u767B\u5F55</button>
|
|
794
798
|
<p><a href="/" style="color:#2563eb;text-decoration:underline;">\u53D6\u6D88\u5E76\u8FD4\u56DE\u9996\u9875</a></p>
|
|
795
799
|
</form>
|
|
@@ -817,10 +821,18 @@ function createLogoutRouteHandler(config) {
|
|
|
817
821
|
redirectUri,
|
|
818
822
|
clientSecret,
|
|
819
823
|
postLogoutRedirectUri = new URL(redirectUri).origin + "/",
|
|
820
|
-
redirectToSso = true,
|
|
821
824
|
callbackPath = "/api/auth/callback",
|
|
822
825
|
insecureLocalDev: insecureLocalDevOpt = false
|
|
823
826
|
} = config;
|
|
827
|
+
let defaultScope = config.defaultScope ?? "local";
|
|
828
|
+
if (config.redirectToSso !== void 0) {
|
|
829
|
+
console.warn(
|
|
830
|
+
'[SSO SDK] redirectToSso \u5DF2\u5F03\u7528\uFF0C\u8BF7\u6539\u7528 defaultScope\uFF08redirectToSso: true \u2192 defaultScope: "global"\uFF0Cfalse \u2192 "local"\uFF09\u3002\u6CE8\u610F\uFF1A\u9ED8\u8BA4\u9000\u51FA\u8303\u56F4\u5DF2\u53D8\u66F4\u4E3A "local"\uFF08\u4EC5\u9000\u51FA\u672C\u7AD9\uFF0C\u4E0D\u8DF3\u8F6C SSO \u4E2D\u5FC3\uFF09\u3002'
|
|
831
|
+
);
|
|
832
|
+
if (config.defaultScope === void 0) {
|
|
833
|
+
defaultScope = config.redirectToSso ? "global" : "local";
|
|
834
|
+
}
|
|
835
|
+
}
|
|
824
836
|
const insecureLocalDev = resolveInsecureLocalDev(insecureLocalDevOpt, ssoBaseUrl);
|
|
825
837
|
const secureCookies = !insecureLocalDev;
|
|
826
838
|
const pickName = (explicit, fallback) => insecureLocalDev ? toInsecureCookieName(explicit ?? fallback) : explicit ?? fallback;
|
|
@@ -855,6 +867,12 @@ function createLogoutRouteHandler(config) {
|
|
|
855
867
|
headers: { "content-type": "text/html; charset=utf-8" }
|
|
856
868
|
});
|
|
857
869
|
}
|
|
870
|
+
let formGlobal = null;
|
|
871
|
+
try {
|
|
872
|
+
formGlobal = new URLSearchParams(await request.text()).get("global");
|
|
873
|
+
} catch {
|
|
874
|
+
}
|
|
875
|
+
const effectiveScope = formGlobal !== null ? formGlobal === "1" || formGlobal === "true" ? "global" : "local" : defaultScope;
|
|
858
876
|
const refreshToken = request.cookies.get(refreshTokenCookieName)?.value;
|
|
859
877
|
const idTokenHint = request.cookies.get(idTokenCookieName)?.value;
|
|
860
878
|
if (refreshToken) {
|
|
@@ -889,7 +907,7 @@ function createLogoutRouteHandler(config) {
|
|
|
889
907
|
res.cookies.set(verifierCookieName, "", getSecureCookieOptions(0, callbackPath, secureCookies));
|
|
890
908
|
return res;
|
|
891
909
|
};
|
|
892
|
-
if (
|
|
910
|
+
if (effectiveScope === "global") {
|
|
893
911
|
const discovery = await fetchDiscoveryCached(normalizedServerBase);
|
|
894
912
|
const endSessionEndpoint = discovery?.end_session_endpoint || `${normalizedBase}/api/oauth/end-session`;
|
|
895
913
|
const logoutUrl = new URL(endSessionEndpoint);
|
|
@@ -912,6 +930,198 @@ function createLogoutRouteHandler(config) {
|
|
|
912
930
|
);
|
|
913
931
|
};
|
|
914
932
|
}
|
|
933
|
+
|
|
934
|
+
// src/next/backchannel-logout.ts
|
|
935
|
+
var import_server4 = require("next/server");
|
|
936
|
+
|
|
937
|
+
// src/core/logout-token.ts
|
|
938
|
+
var BACKCHANNEL_LOGOUT_EVENT = "http://schemas.openid.net/event/backchannel-logout";
|
|
939
|
+
var JTI_CACHE_CAPACITY = 1e3;
|
|
940
|
+
var seenJti = /* @__PURE__ */ new Map();
|
|
941
|
+
function recordJti(jti, expiresAtMs) {
|
|
942
|
+
const now = Date.now();
|
|
943
|
+
const existing = seenJti.get(jti);
|
|
944
|
+
if (existing !== void 0 && existing > now) return false;
|
|
945
|
+
for (const [key, exp] of seenJti) {
|
|
946
|
+
if (exp <= now) seenJti.delete(key);
|
|
947
|
+
}
|
|
948
|
+
while (seenJti.size >= JTI_CACHE_CAPACITY) {
|
|
949
|
+
const oldest = seenJti.keys().next().value;
|
|
950
|
+
if (oldest === void 0) break;
|
|
951
|
+
seenJti.delete(oldest);
|
|
952
|
+
}
|
|
953
|
+
seenJti.set(jti, expiresAtMs);
|
|
954
|
+
return true;
|
|
955
|
+
}
|
|
956
|
+
async function verifyLogoutToken(logoutToken, ssoBaseUrl, clientId) {
|
|
957
|
+
const baseUrl = ssoBaseUrl.replace(/\/+$/, "");
|
|
958
|
+
const header = decodeJwtHeader(logoutToken);
|
|
959
|
+
if (!header) {
|
|
960
|
+
throw new SsoError("logout_token_invalid", "Logout Token \u683C\u5F0F\u9519\u8BEF");
|
|
961
|
+
}
|
|
962
|
+
if (header.alg !== "RS256") {
|
|
963
|
+
throw new SsoError(
|
|
964
|
+
"logout_token_unsupported_alg",
|
|
965
|
+
`\u4E0D\u652F\u6301\u7684 Logout Token \u7B7E\u540D\u7B97\u6CD5: ${String(header.alg)}`
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
const discovery = await fetchDiscoveryDoc(baseUrl);
|
|
969
|
+
const normalizedIssuer = (discovery?.issuer || baseUrl).replace(/\/+$/, "");
|
|
970
|
+
const jwks = await fetchJwks(baseUrl);
|
|
971
|
+
if (!jwks) {
|
|
972
|
+
throw new SsoError(
|
|
973
|
+
"logout_token_invalid_signature",
|
|
974
|
+
"\u65E0\u6CD5\u83B7\u53D6 JWKS \u9A8C\u8BC1 Logout Token \u7B7E\u540D"
|
|
975
|
+
);
|
|
976
|
+
}
|
|
977
|
+
const kid = typeof header.kid === "string" ? header.kid : void 0;
|
|
978
|
+
const matchCandidates = (set) => set.keys.filter(
|
|
979
|
+
(k) => k.kty === "RSA" && k.alg === "RS256" && k.use === "sig" && (kid ? k.kid === kid : true)
|
|
980
|
+
);
|
|
981
|
+
const verifyAny = async (keys) => {
|
|
982
|
+
for (const key of keys) {
|
|
983
|
+
if (await verifyRs256Signature(logoutToken, key)) return true;
|
|
984
|
+
}
|
|
985
|
+
return false;
|
|
986
|
+
};
|
|
987
|
+
let candidates = matchCandidates(jwks);
|
|
988
|
+
let validSig = candidates.length > 0 ? await verifyAny(candidates) : false;
|
|
989
|
+
if (candidates.length === 0 || !validSig) {
|
|
990
|
+
const freshJwks = await fetchJwks(baseUrl, { forceRefresh: true });
|
|
991
|
+
if (freshJwks) {
|
|
992
|
+
candidates = matchCandidates(freshJwks);
|
|
993
|
+
validSig = candidates.length > 0 ? await verifyAny(candidates) : false;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
if (candidates.length === 0) {
|
|
997
|
+
throw new SsoError(
|
|
998
|
+
"logout_token_invalid_signature",
|
|
999
|
+
"JWKS \u4E2D\u672A\u627E\u5230\u5339\u914D\u7684 RS256 \u516C\u94A5"
|
|
1000
|
+
);
|
|
1001
|
+
}
|
|
1002
|
+
if (!validSig) {
|
|
1003
|
+
throw new SsoError(
|
|
1004
|
+
"logout_token_invalid_signature",
|
|
1005
|
+
"Logout Token \u7B7E\u540D\u9A8C\u8BC1\u5931\u8D25"
|
|
1006
|
+
);
|
|
1007
|
+
}
|
|
1008
|
+
const payload = decodeJwtPayload(logoutToken);
|
|
1009
|
+
if (!payload) {
|
|
1010
|
+
throw new SsoError("logout_token_invalid", "Logout Token payload \u89E3\u6790\u5931\u8D25");
|
|
1011
|
+
}
|
|
1012
|
+
const tokenIssuer = typeof payload.iss === "string" ? payload.iss.replace(/\/+$/, "") : "";
|
|
1013
|
+
if (tokenIssuer !== normalizedIssuer) {
|
|
1014
|
+
throw new SsoError(
|
|
1015
|
+
"logout_token_issuer_mismatch",
|
|
1016
|
+
"Logout Token issuer \u4E0D\u5339\u914D"
|
|
1017
|
+
);
|
|
1018
|
+
}
|
|
1019
|
+
const aud = payload.aud;
|
|
1020
|
+
const audList = Array.isArray(aud) ? aud : typeof aud === "string" ? [aud] : [];
|
|
1021
|
+
if (!audList.includes(clientId)) {
|
|
1022
|
+
throw new SsoError(
|
|
1023
|
+
"logout_token_audience_mismatch",
|
|
1024
|
+
"Logout Token audience \u4E0D\u5339\u914D"
|
|
1025
|
+
);
|
|
1026
|
+
}
|
|
1027
|
+
if (typeof payload.exp !== "number") {
|
|
1028
|
+
throw new SsoError("logout_token_invalid", "Logout Token \u7F3A\u5C11 exp \u58F0\u660E");
|
|
1029
|
+
}
|
|
1030
|
+
if (Date.now() >= payload.exp * 1e3 + 6e4) {
|
|
1031
|
+
throw new SsoError("logout_token_expired", "Logout Token \u5DF2\u8FC7\u671F");
|
|
1032
|
+
}
|
|
1033
|
+
const events = payload.events;
|
|
1034
|
+
if (!events || typeof events !== "object" || !(BACKCHANNEL_LOGOUT_EVENT in events)) {
|
|
1035
|
+
throw new SsoError(
|
|
1036
|
+
"logout_token_invalid",
|
|
1037
|
+
"Logout Token \u7F3A\u5C11 backchannel-logout events \u58F0\u660E"
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
const sub = typeof payload.sub === "string" && payload.sub ? payload.sub : void 0;
|
|
1041
|
+
const sid = typeof payload.sid === "string" && payload.sid ? payload.sid : void 0;
|
|
1042
|
+
if (!sub && !sid) {
|
|
1043
|
+
throw new SsoError(
|
|
1044
|
+
"logout_token_invalid",
|
|
1045
|
+
"Logout Token \u5FC5\u987B\u5305\u542B sub \u6216 sid \u81F3\u5C11\u5176\u4E00"
|
|
1046
|
+
);
|
|
1047
|
+
}
|
|
1048
|
+
const jti = typeof payload.jti === "string" ? payload.jti : "";
|
|
1049
|
+
if (!jti) {
|
|
1050
|
+
throw new SsoError("logout_token_invalid", "Logout Token \u7F3A\u5C11 jti \u58F0\u660E");
|
|
1051
|
+
}
|
|
1052
|
+
if (!recordJti(jti, payload.exp * 1e3 + 6e4)) {
|
|
1053
|
+
throw new SsoError("logout_token_replay", "Logout Token jti \u91CD\u653E");
|
|
1054
|
+
}
|
|
1055
|
+
return { sub, sid };
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
// src/next/backchannel-logout.ts
|
|
1059
|
+
function createBackchannelLogoutRouteHandler(config) {
|
|
1060
|
+
const { clientId, ssoBaseUrl, onLogout } = config;
|
|
1061
|
+
const insecureLocalDev = resolveInsecureLocalDev(
|
|
1062
|
+
config.insecureLocalDev ?? false,
|
|
1063
|
+
ssoBaseUrl
|
|
1064
|
+
);
|
|
1065
|
+
const secureCookies = !insecureLocalDev;
|
|
1066
|
+
const pickName = (explicit, fallback) => insecureLocalDev ? toInsecureCookieName(explicit ?? fallback) : explicit ?? fallback;
|
|
1067
|
+
const accessTokenCookieName = pickName(
|
|
1068
|
+
config.accessTokenCookieName,
|
|
1069
|
+
DEFAULT_ACCESS_TOKEN_COOKIE_NAME
|
|
1070
|
+
);
|
|
1071
|
+
const refreshTokenCookieName = pickName(
|
|
1072
|
+
config.refreshTokenCookieName,
|
|
1073
|
+
DEFAULT_REFRESH_TOKEN_COOKIE_NAME
|
|
1074
|
+
);
|
|
1075
|
+
const idTokenCookieName = pickName(
|
|
1076
|
+
config.idTokenCookieName,
|
|
1077
|
+
DEFAULT_ID_TOKEN_COOKIE_NAME
|
|
1078
|
+
);
|
|
1079
|
+
return async function handler(request) {
|
|
1080
|
+
if (request.method !== "POST") {
|
|
1081
|
+
return import_server4.NextResponse.json(
|
|
1082
|
+
{ error: "method_not_allowed", error_description: "\u4EC5\u63A5\u53D7 POST \u8BF7\u6C42" },
|
|
1083
|
+
{ status: 405 }
|
|
1084
|
+
);
|
|
1085
|
+
}
|
|
1086
|
+
const logoutToken = new URLSearchParams(await request.text()).get(
|
|
1087
|
+
"logout_token"
|
|
1088
|
+
);
|
|
1089
|
+
if (!logoutToken) {
|
|
1090
|
+
return import_server4.NextResponse.json(
|
|
1091
|
+
{ error: "invalid_request", error_description: "\u7F3A\u5C11 logout_token" },
|
|
1092
|
+
{ status: 400 }
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
let payload;
|
|
1096
|
+
try {
|
|
1097
|
+
payload = await verifyLogoutToken(logoutToken, ssoBaseUrl, clientId);
|
|
1098
|
+
} catch (err) {
|
|
1099
|
+
const code = err instanceof SsoError ? err.code : "logout_token_invalid";
|
|
1100
|
+
const description = err instanceof SsoError ? err.description : "Logout Token \u9A8C\u8BC1\u5931\u8D25";
|
|
1101
|
+
return import_server4.NextResponse.json(
|
|
1102
|
+
{ error: code, error_description: description },
|
|
1103
|
+
{ status: 400 }
|
|
1104
|
+
);
|
|
1105
|
+
}
|
|
1106
|
+
try {
|
|
1107
|
+
await onLogout?.(payload, request);
|
|
1108
|
+
} catch (err) {
|
|
1109
|
+
console.error(
|
|
1110
|
+
"[SSO SDK] backchannel logout onLogout \u94A9\u5B50\u6267\u884C\u5931\u8D25:",
|
|
1111
|
+
err
|
|
1112
|
+
);
|
|
1113
|
+
return import_server4.NextResponse.json(
|
|
1114
|
+
{ error: "server_error", error_description: "onLogout \u5904\u7406\u5931\u8D25" },
|
|
1115
|
+
{ status: 500 }
|
|
1116
|
+
);
|
|
1117
|
+
}
|
|
1118
|
+
const res = new import_server4.NextResponse(null, { status: 200 });
|
|
1119
|
+
res.cookies.set(accessTokenCookieName, "", getHostCookieOptions(0, secureCookies));
|
|
1120
|
+
res.cookies.set(refreshTokenCookieName, "", getHostCookieOptions(0, secureCookies));
|
|
1121
|
+
res.cookies.set(idTokenCookieName, "", getHostCookieOptions(0, secureCookies));
|
|
1122
|
+
return res;
|
|
1123
|
+
};
|
|
1124
|
+
}
|
|
915
1125
|
// Annotate the CommonJS export names for ESM import in node:
|
|
916
1126
|
0 && (module.exports = {
|
|
917
1127
|
DEFAULT_ACCESS_TOKEN_COOKIE_NAME,
|
|
@@ -922,6 +1132,7 @@ function createLogoutRouteHandler(config) {
|
|
|
922
1132
|
DEFAULT_RETURN_COOKIE_NAME,
|
|
923
1133
|
DEFAULT_STATE_COOKIE_NAME,
|
|
924
1134
|
DEFAULT_VERIFIER_COOKIE_NAME,
|
|
1135
|
+
createBackchannelLogoutRouteHandler,
|
|
925
1136
|
createCallbackRouteHandler,
|
|
926
1137
|
createLogoutRouteHandler,
|
|
927
1138
|
createSsoMiddleware,
|