@nihplod/sso-sdk 1.4.0 → 1.4.1

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.
@@ -82,6 +82,7 @@ function matchesPath(pathname, paths) {
82
82
  var introspectionCache = /* @__PURE__ */ new Map();
83
83
  var INTROSPECT_CACHE_TTL_MS = 3e4;
84
84
  var INTROSPECT_CACHE_MAX_ENTRIES = 500;
85
+ var INTROSPECT_TIMEOUT_MS = 5e3;
85
86
  async function introspectCacheKey(token, clientId) {
86
87
  const hash = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(token));
87
88
  const hex = Array.from(new Uint8Array(hash)).map((b) => b.toString(16).padStart(2, "0")).join("");
@@ -114,7 +115,7 @@ async function introspectAccessToken(token, ssoBaseUrl, clientId, clientSecret)
114
115
  const cacheKey = await introspectCacheKey(token, clientId);
115
116
  const cached = introspectCacheGet(cacheKey);
116
117
  if (cached) {
117
- return cached.active;
118
+ return cached.active ? "active" : "inactive";
118
119
  }
119
120
  try {
120
121
  const body = new URLSearchParams({
@@ -128,21 +129,22 @@ async function introspectAccessToken(token, ssoBaseUrl, clientId, clientSecret)
128
129
  const res = await fetch(`${ssoBaseUrl}/api/oauth/introspect`, {
129
130
  method: "POST",
130
131
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
131
- body: body.toString()
132
+ body: body.toString(),
133
+ signal: AbortSignal.timeout(INTROSPECT_TIMEOUT_MS)
132
134
  });
133
135
  if (!res.ok) {
134
136
  if (res.status !== 401 && res.status !== 403) {
135
- return false;
137
+ return "unreachable";
136
138
  }
137
139
  introspectCacheSet(cacheKey, false);
138
- return false;
140
+ return "inactive";
139
141
  }
140
142
  const data = await res.json();
141
143
  const active = data.active === true;
142
144
  introspectCacheSet(cacheKey, active);
143
- return active;
145
+ return active ? "active" : "inactive";
144
146
  } catch {
145
- return false;
147
+ return "unreachable";
146
148
  }
147
149
  }
148
150
  function createSsoMiddleware(config) {
@@ -192,13 +194,13 @@ function createSsoMiddleware(config) {
192
194
  const ssoSession = request.cookies.get(ssoCookieName);
193
195
  if (ssoSession?.value) {
194
196
  if (validateSsoCookie) {
195
- const tokenActive = await introspectAccessToken(
197
+ const introspectResult = await introspectAccessToken(
196
198
  ssoSession.value,
197
199
  normalizedServerBase,
198
200
  clientId,
199
201
  clientSecret
200
202
  );
201
- if (tokenActive) {
203
+ if (introspectResult === "active") {
202
204
  return NextResponse.next();
203
205
  }
204
206
  } else {
@@ -207,13 +209,19 @@ function createSsoMiddleware(config) {
207
209
  }
208
210
  const accessTokenCookie = request.cookies.get(accessTokenCookieName);
209
211
  if (accessTokenCookie?.value) {
210
- const tokenActive = await introspectAccessToken(
212
+ const introspectResult = await introspectAccessToken(
211
213
  accessTokenCookie.value,
212
214
  normalizedServerBase,
213
215
  clientId,
214
216
  clientSecret
215
217
  );
216
- if (tokenActive) {
218
+ if (introspectResult === "active") {
219
+ return NextResponse.next();
220
+ }
221
+ if (introspectResult === "unreachable") {
222
+ console.warn(
223
+ "[SSO SDK] introspection \u4E0D\u53EF\u8FBE\uFF08\u7F51\u7EDC\u5F02\u5E38/\u8D85\u65F6/5xx\uFF09\uFF0C\u5BF9\u6301\u6709 SSO access token Cookie \u7684\u8BF7\u6C42 fail-open \u653E\u884C\uFF1B\u654F\u611F\u6570\u636E\u7684\u9274\u6743\u7531 Route Handler \u515C\u5E95\u3002"
224
+ );
217
225
  return NextResponse.next();
218
226
  }
219
227
  }