@nihplod/sso-sdk 1.5.0 → 1.6.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 +21 -2
- package/dist/index.d.mts +25 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +79 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +79 -6
- package/dist/index.mjs.map +1 -1
- package/dist/next/index.d.mts +38 -2
- package/dist/next/index.d.ts +38 -2
- package/dist/next/index.js +82 -20
- package/dist/next/index.js.map +1 -1
- package/dist/next/index.mjs +82 -20
- package/dist/next/index.mjs.map +1 -1
- package/dist/react/index.d.mts +25 -0
- package/dist/react/index.d.ts +25 -0
- package/dist/react/index.js +90 -9
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +90 -9
- package/dist/react/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -84,7 +84,7 @@ if (params.get("sso_probe") === "no_session") {
|
|
|
84
84
|
|
|
85
85
|
### `sso.handleCallback(callbackUrl)`
|
|
86
86
|
|
|
87
|
-
Handle the OAuth callback. Parses `code` and `state` from the URL, validates state, and exchanges the code for tokens. When the requested scope includes `openid`, the token response **must** contain an `id_token` (fail-closed: a missing `id_token` is rejected instead of silently skipping validation). The SDK verifies the ID Token's signature, issuer, audience, expiry and `at_hash`, and — when the login was initiated via `login()` / `getLoginUrl()` / `loginPopup()` — also validates the OIDC `nonce` claim against the value generated at login time (constant-time comparison, protecting against ID Token replay
|
|
87
|
+
Handle the OAuth callback. Parses `code` and `state` from the URL, validates state, and exchanges the code for tokens. When the requested scope includes `openid`, the token response **must** contain an `id_token` (fail-closed: a missing `id_token` is rejected instead of silently skipping validation). The SDK verifies the ID Token's signature, issuer, audience, expiry and `at_hash`, and — when the login was initiated via `login()` / `getLoginUrl()` / `loginPopup()` — also validates the OIDC `nonce` claim against the value generated at login time (constant-time comparison, protecting against ID Token replay). The check is fail-closed both ways: a stored nonce requires a matching token nonce, and (since 1.6.0) a token carrying a `nonce` claim while no expected nonce is stored locally (e.g. the nonce cookie/sessionStorage entry was evicted) is rejected as well.
|
|
88
88
|
|
|
89
89
|
| Parameter | Type | Description |
|
|
90
90
|
|------|------|------|
|
|
@@ -450,6 +450,8 @@ export const POST = createBackchannelLogoutRouteHandler({
|
|
|
450
450
|
|
|
451
451
|
The backchannel handler receives `logout_token` pushes from the SSO center (OIDC Back-Channel Logout) when the user signs out globally or revokes consent, verifies the token (RS256 signature via JWKS, issuer/audience/expiry, `events` claim, `jti` replay protection), clears the SSO cookies, and calls your `onLogout` hook. If the hook throws, it responds 500 so the IdP retries delivery. **Remember to register this route's public URL as `backchannelLogoutUri` in the SSO admin console for your client.**
|
|
452
452
|
|
|
453
|
+
> **jti replay storage (since 1.6.0):** the default replay cache is an in-memory per-instance Map. Multi-instance / serverless deployments MUST inject shared storage via the `jtiStore` option (e.g. Redis: `addIfAbsent` = `SET key 1 NX EX ttl`, `remove` = `DEL key` — `remove` lets the SDK release the jti so the IdP can redeliver after an `onLogout` failure; without it, redeliveries are rejected as replays).
|
|
454
|
+
|
|
453
455
|
Trigger the logout endpoint with a POST request (recommended):
|
|
454
456
|
|
|
455
457
|
```tsx
|
|
@@ -469,7 +471,7 @@ Default cookie names:
|
|
|
469
471
|
| access_token | `__Host-nihplod_sso_at` | Requires Secure + Path=/ + no Domain |
|
|
470
472
|
| refresh_token | `__Host-nihplod_sso_rt` | Requires Secure + Path=/ + no Domain |
|
|
471
473
|
| state | `__Host-nihplod_sso_state` | Requires Secure + Path=/ + no Domain |
|
|
472
|
-
| nonce | `__Host-nihplod_sso_nonce` | OIDC nonce for ID Token replay protection. Validated when the nonce cookie is present (cookie TTL 10 minutes);
|
|
474
|
+
| nonce | `__Host-nihplod_sso_nonce` | OIDC nonce for ID Token replay protection. Validated fail-closed when the nonce cookie is present (cookie TTL 10 minutes); since 1.6.0, an id_token that carries a `nonce` while the cookie has expired is also rejected (restart the login flow). Requires Secure + Path=/ + no Domain |
|
|
473
475
|
| id_token | `__Host-nihplod_sso_id` | ID token cookie used as `id_token_hint` for RP-Initiated Logout. Requires Secure + Path=/ + no Domain |
|
|
474
476
|
| logout_state | `__Host-nihplod_sso_logout_state` | One-time logout state for CSRF protection on the RP-Initiated Logout callback. Requires Secure + Path=/ + no Domain |
|
|
475
477
|
| return_url | `__Host-nihplod_sso_return` | Requires Secure + Path=/ + no Domain |
|
|
@@ -481,6 +483,23 @@ Default cookie names:
|
|
|
481
483
|
|
|
482
484
|
---
|
|
483
485
|
|
|
486
|
+
## Changelog
|
|
487
|
+
|
|
488
|
+
### 1.6.0
|
|
489
|
+
|
|
490
|
+
Security and correctness hardening. Migration notes:
|
|
491
|
+
|
|
492
|
+
- **ID Token must carry `iat`.** `validateIdToken` now rejects tokens without an `iat` claim (`id_token_invalid`). Any spec-compliant IdP already includes it.
|
|
493
|
+
- **Nonce check fails closed.** An `id_token` that carries a `nonce` claim while no expected nonce is stored locally (e.g. the nonce cookie/sessionStorage entry was evicted) is now rejected with `id_token_nonce_mismatch` (OIDC Core §12.2), instead of silently skipping the check. Refresh-issued ID tokens must not carry `nonce` per spec, so the refresh flow is unaffected. If login fails with this error after upgrading, restart the login flow — do not downgrade the check.
|
|
494
|
+
- **New `jtiStore` option** for `createBackchannelLogoutRouteHandler` / `verifyLogoutToken(Detailed)`. The default jti replay cache is an in-memory per-instance Map; multi-instance / serverless deployments MUST inject shared storage (e.g. Redis `SET NX EX` for `addIfAbsent`, `DEL` for `remove` — `remove` lets the SDK release the jti so the IdP can redeliver after your `onLogout` hook fails). Replay keys are now prefixed with the token issuer to prevent cross-issuer jti collisions.
|
|
495
|
+
- **New `allowCrossOriginDiscoveryEndpoints` option** on `SsoClientConfig` (default `false`). Discovery documents whose `authorization_endpoint` is not same-origin with `ssoBaseUrl`, or whose `token_endpoint` / `userinfo_endpoint` is not same-origin with `ssoBaseUrl` (or `serverBaseUrl` when configured), are rejected with a warning and the SDK falls back to the default endpoints. Set this flag only if your SSO center genuinely hosts endpoints on another origin.
|
|
496
|
+
- **Multi-tab token sync**: after a silent refresh, the new token data is broadcast to other tabs via `BroadcastChannel` and persisted directly (light shape check), so other tabs no longer attempt their own refresh with an already-rotated refresh_token. Older SDK tabs (no payload) fall back to the previous `loadUser()`-only behavior.
|
|
497
|
+
- `handleCallback` now clears the one-time transient data (state/nonce/PKCE verifier/silent-probe marker) when the token endpoint returns an error (e.g. `invalid_grant`) or when the required `id_token` is missing — the authorization code is already consumed and retrying cannot succeed. Network failures and JWKS validation failures still retain the data for retry.
|
|
498
|
+
- `getAccessToken()` / `getUserInfo()` refresh when the token has less than 10s of validity left (expiry skew), instead of returning a token that may expire in flight. `isAuthenticated()` intentionally keeps the exact check (it's a synchronous UI hint).
|
|
499
|
+
- Next.js callback: the token exchange request now has a 10s timeout (previously unbounded) and still gets one retry before the 502 error page. RP-Initiated Logout state comparison is now constant-time. The middleware never asset-bypasses paths under `/api/` (e.g. `/api/app.js` no longer skips authentication; list public API paths in `publicPaths` explicitly).
|
|
500
|
+
|
|
501
|
+
---
|
|
502
|
+
|
|
484
503
|
## Security Recommendations and Token Storage
|
|
485
504
|
|
|
486
505
|
> ⚠️ **The Next.js middleware is only a UX gate.** It decides whether to redirect
|
package/dist/index.d.mts
CHANGED
|
@@ -112,6 +112,17 @@ interface SsoClientConfig {
|
|
|
112
112
|
redirectUri: string;
|
|
113
113
|
/** SSO 中心地址,如 "https://nihplod.cn" */
|
|
114
114
|
ssoBaseUrl: string;
|
|
115
|
+
/**
|
|
116
|
+
* 允许 Discovery 文档声明跨源端点(默认 false)。
|
|
117
|
+
*
|
|
118
|
+
* ⚠️ 安全警告:Discovery 文档若被篡改(如中间人/反向代理劫持),攻击者可把
|
|
119
|
+
* token_endpoint 指向自己的服务器窃取 authorization code / refresh_token。
|
|
120
|
+
* SDK 默认校验 authorization_endpoint 必须与 ssoBaseUrl 同源、
|
|
121
|
+
* token_endpoint / userinfo_endpoint 必须与 ssoBaseUrl(或 serverBaseUrl)同源,
|
|
122
|
+
* 不匹配时告警并回退到默认端点。仅当 SSO 中心确实把端点托管在其他域名时
|
|
123
|
+
* 才设置此开关,并自行确保 Discovery 通道可信。
|
|
124
|
+
*/
|
|
125
|
+
allowCrossOriginDiscoveryEndpoints?: boolean;
|
|
115
126
|
/** 请求的 scope(空格分隔),如 "openid profile phone" */
|
|
116
127
|
scopes?: string;
|
|
117
128
|
/** RP-Initiated Logout 返回地址(可选)。不传时回退到 redirectUri */
|
|
@@ -195,6 +206,8 @@ declare class SsoClient {
|
|
|
195
206
|
private _discovery;
|
|
196
207
|
private _discoveryFetchedAt;
|
|
197
208
|
private _refreshLock;
|
|
209
|
+
/** Discovery 端点 origin 校验失败只告警一次(拒绝的文档不缓存,避免每次调用刷屏) */
|
|
210
|
+
private _discoveryOriginWarned;
|
|
198
211
|
/** Discovery 文档缓存 TTL(5 分钟) */
|
|
199
212
|
private static readonly DISCOVERY_TTL_MS;
|
|
200
213
|
/** Discovery fetch 超时(10 秒) */
|
|
@@ -206,6 +219,15 @@ declare class SsoClient {
|
|
|
206
219
|
* 不会输出 token/授权码等敏感值。
|
|
207
220
|
*/
|
|
208
221
|
debugLog(...args: unknown[]): void;
|
|
222
|
+
/**
|
|
223
|
+
* 校验 Discovery 文档端点 origin(防文档被篡改后 token/code 被导向攻击者服务器):
|
|
224
|
+
* - authorization_endpoint 必须与 ssoBaseUrl 同源(浏览器跳转目标);
|
|
225
|
+
* - token_endpoint / userinfo_endpoint 必须与 ssoBaseUrl 同源,
|
|
226
|
+
* 或(配置了 serverBaseUrl 时)与内网地址同源。
|
|
227
|
+
* 任一不匹配:告警并返回 null(上层回退到默认端点,fail-safe)。
|
|
228
|
+
* 可通过 config.allowCrossOriginDiscoveryEndpoints 关闭(见配置项警告)。
|
|
229
|
+
*/
|
|
230
|
+
private _sanitizeDiscovery;
|
|
209
231
|
/**
|
|
210
232
|
* 获取 OIDC Discovery 文档(带缓存 + 超时)
|
|
211
233
|
*
|
|
@@ -327,6 +349,9 @@ declare class SsoClient {
|
|
|
327
349
|
* 检查是否已认证(不发起网络请求)
|
|
328
350
|
*
|
|
329
351
|
* 仅检查本地是否存在未过期的 access_token。
|
|
352
|
+
* 故意不做 EXPIRY_SKEW_MS 提前量:这是同步的 UI 提示判定(是否显示"已登录"),
|
|
353
|
+
* 提前 10s 翻转 false 会让调用方在 token 仍有效时误显示未登录;真正发请求的
|
|
354
|
+
* getAccessToken/getUserInfo 已带提前量。
|
|
330
355
|
*/
|
|
331
356
|
isAuthenticated(): boolean;
|
|
332
357
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -112,6 +112,17 @@ interface SsoClientConfig {
|
|
|
112
112
|
redirectUri: string;
|
|
113
113
|
/** SSO 中心地址,如 "https://nihplod.cn" */
|
|
114
114
|
ssoBaseUrl: string;
|
|
115
|
+
/**
|
|
116
|
+
* 允许 Discovery 文档声明跨源端点(默认 false)。
|
|
117
|
+
*
|
|
118
|
+
* ⚠️ 安全警告:Discovery 文档若被篡改(如中间人/反向代理劫持),攻击者可把
|
|
119
|
+
* token_endpoint 指向自己的服务器窃取 authorization code / refresh_token。
|
|
120
|
+
* SDK 默认校验 authorization_endpoint 必须与 ssoBaseUrl 同源、
|
|
121
|
+
* token_endpoint / userinfo_endpoint 必须与 ssoBaseUrl(或 serverBaseUrl)同源,
|
|
122
|
+
* 不匹配时告警并回退到默认端点。仅当 SSO 中心确实把端点托管在其他域名时
|
|
123
|
+
* 才设置此开关,并自行确保 Discovery 通道可信。
|
|
124
|
+
*/
|
|
125
|
+
allowCrossOriginDiscoveryEndpoints?: boolean;
|
|
115
126
|
/** 请求的 scope(空格分隔),如 "openid profile phone" */
|
|
116
127
|
scopes?: string;
|
|
117
128
|
/** RP-Initiated Logout 返回地址(可选)。不传时回退到 redirectUri */
|
|
@@ -195,6 +206,8 @@ declare class SsoClient {
|
|
|
195
206
|
private _discovery;
|
|
196
207
|
private _discoveryFetchedAt;
|
|
197
208
|
private _refreshLock;
|
|
209
|
+
/** Discovery 端点 origin 校验失败只告警一次(拒绝的文档不缓存,避免每次调用刷屏) */
|
|
210
|
+
private _discoveryOriginWarned;
|
|
198
211
|
/** Discovery 文档缓存 TTL(5 分钟) */
|
|
199
212
|
private static readonly DISCOVERY_TTL_MS;
|
|
200
213
|
/** Discovery fetch 超时(10 秒) */
|
|
@@ -206,6 +219,15 @@ declare class SsoClient {
|
|
|
206
219
|
* 不会输出 token/授权码等敏感值。
|
|
207
220
|
*/
|
|
208
221
|
debugLog(...args: unknown[]): void;
|
|
222
|
+
/**
|
|
223
|
+
* 校验 Discovery 文档端点 origin(防文档被篡改后 token/code 被导向攻击者服务器):
|
|
224
|
+
* - authorization_endpoint 必须与 ssoBaseUrl 同源(浏览器跳转目标);
|
|
225
|
+
* - token_endpoint / userinfo_endpoint 必须与 ssoBaseUrl 同源,
|
|
226
|
+
* 或(配置了 serverBaseUrl 时)与内网地址同源。
|
|
227
|
+
* 任一不匹配:告警并返回 null(上层回退到默认端点,fail-safe)。
|
|
228
|
+
* 可通过 config.allowCrossOriginDiscoveryEndpoints 关闭(见配置项警告)。
|
|
229
|
+
*/
|
|
230
|
+
private _sanitizeDiscovery;
|
|
209
231
|
/**
|
|
210
232
|
* 获取 OIDC Discovery 文档(带缓存 + 超时)
|
|
211
233
|
*
|
|
@@ -327,6 +349,9 @@ declare class SsoClient {
|
|
|
327
349
|
* 检查是否已认证(不发起网络请求)
|
|
328
350
|
*
|
|
329
351
|
* 仅检查本地是否存在未过期的 access_token。
|
|
352
|
+
* 故意不做 EXPIRY_SKEW_MS 提前量:这是同步的 UI 提示判定(是否显示"已登录"),
|
|
353
|
+
* 提前 10s 翻转 false 会让调用方在 token 仍有效时误显示未登录;真正发请求的
|
|
354
|
+
* getAccessToken/getUserInfo 已带提前量。
|
|
330
355
|
*/
|
|
331
356
|
isAuthenticated(): boolean;
|
|
332
357
|
/**
|
package/dist/index.js
CHANGED
|
@@ -612,7 +612,10 @@ async function validateIdToken(idToken, accessToken, expectedIssuer, expectedCli
|
|
|
612
612
|
if (Date.now() >= payload.exp * 1e3 + 6e4) {
|
|
613
613
|
throw new SsoError("id_token_expired", "ID Token \u5DF2\u8FC7\u671F");
|
|
614
614
|
}
|
|
615
|
-
if (typeof payload.iat
|
|
615
|
+
if (typeof payload.iat !== "number") {
|
|
616
|
+
throw new SsoError("id_token_invalid", "ID Token \u7F3A\u5C11 iat \u58F0\u660E");
|
|
617
|
+
}
|
|
618
|
+
if (payload.iat * 1e3 > Date.now() + 6e4) {
|
|
616
619
|
throw new SsoError("id_token_invalid", "ID Token iat \u5728\u672A\u6765\uFF0C\u7591\u4F3C\u4F2A\u9020\u6216\u65F6\u949F\u5F02\u5E38");
|
|
617
620
|
}
|
|
618
621
|
if (typeof payload.sub !== "string" || !payload.sub) {
|
|
@@ -629,13 +632,17 @@ async function validateIdToken(idToken, accessToken, expectedIssuer, expectedCli
|
|
|
629
632
|
if (!tokenNonce || !timingSafeEqualString(expectedNonce, tokenNonce)) {
|
|
630
633
|
throw new SsoError("id_token_nonce_mismatch", "ID Token nonce \u4E0D\u5339\u914D");
|
|
631
634
|
}
|
|
635
|
+
} else if (typeof payload.nonce === "string" && payload.nonce) {
|
|
636
|
+
throw new SsoError(
|
|
637
|
+
"id_token_nonce_mismatch",
|
|
638
|
+
"ID Token \u643A\u5E26 nonce \u4F46\u672C\u5730\u65E0\u671F\u671B\u503C\uFF08\u53EF\u80FD\u975E\u672C\u6B21\u767B\u5F55\u4F1A\u8BDD\u7B7E\u53D1\uFF09"
|
|
639
|
+
);
|
|
632
640
|
}
|
|
633
641
|
return { sub: payload.sub };
|
|
634
642
|
}
|
|
635
643
|
|
|
636
|
-
// src/core/
|
|
644
|
+
// src/core/http.ts
|
|
637
645
|
var REQUEST_TIMEOUT_MS = 1e4;
|
|
638
|
-
var REVOKE_TIMEOUT_MS = 3e3;
|
|
639
646
|
async function fetchWithTimeout(input, init = {}, timeoutMs = REQUEST_TIMEOUT_MS) {
|
|
640
647
|
const controller = new AbortController();
|
|
641
648
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
@@ -645,6 +652,10 @@ async function fetchWithTimeout(input, init = {}, timeoutMs = REQUEST_TIMEOUT_MS
|
|
|
645
652
|
clearTimeout(timer);
|
|
646
653
|
}
|
|
647
654
|
}
|
|
655
|
+
|
|
656
|
+
// src/core/SsoClient.ts
|
|
657
|
+
var REVOKE_TIMEOUT_MS = 3e3;
|
|
658
|
+
var EXPIRY_SKEW_MS = 1e4;
|
|
648
659
|
function assertValidTokenResponse(data, requireRefreshToken) {
|
|
649
660
|
if (typeof data.access_token !== "string" || !data.access_token) {
|
|
650
661
|
throw new SsoError("token_request_failed", "Token \u54CD\u5E94\u7F3A\u5C11 access_token");
|
|
@@ -672,6 +683,8 @@ var _SsoClient = class _SsoClient {
|
|
|
672
683
|
this._discovery = null;
|
|
673
684
|
this._discoveryFetchedAt = 0;
|
|
674
685
|
this._refreshLock = null;
|
|
686
|
+
/** Discovery 端点 origin 校验失败只告警一次(拒绝的文档不缓存,避免每次调用刷屏) */
|
|
687
|
+
this._discoveryOriginWarned = false;
|
|
675
688
|
if (!config.clientId) throw new SsoError("invalid_config", "clientId \u4E0D\u80FD\u4E3A\u7A7A");
|
|
676
689
|
if (!config.redirectUri) throw new SsoError("invalid_config", "redirectUri \u4E0D\u80FD\u4E3A\u7A7A");
|
|
677
690
|
if (!config.ssoBaseUrl) throw new SsoError("invalid_config", "ssoBaseUrl \u4E0D\u80FD\u4E3A\u7A7A");
|
|
@@ -695,6 +708,52 @@ var _SsoClient = class _SsoClient {
|
|
|
695
708
|
// ============================================
|
|
696
709
|
// 内部方法
|
|
697
710
|
// ============================================
|
|
711
|
+
/**
|
|
712
|
+
* 校验 Discovery 文档端点 origin(防文档被篡改后 token/code 被导向攻击者服务器):
|
|
713
|
+
* - authorization_endpoint 必须与 ssoBaseUrl 同源(浏览器跳转目标);
|
|
714
|
+
* - token_endpoint / userinfo_endpoint 必须与 ssoBaseUrl 同源,
|
|
715
|
+
* 或(配置了 serverBaseUrl 时)与内网地址同源。
|
|
716
|
+
* 任一不匹配:告警并返回 null(上层回退到默认端点,fail-safe)。
|
|
717
|
+
* 可通过 config.allowCrossOriginDiscoveryEndpoints 关闭(见配置项警告)。
|
|
718
|
+
*/
|
|
719
|
+
_sanitizeDiscovery(d) {
|
|
720
|
+
if (this.config.allowCrossOriginDiscoveryEndpoints) return d;
|
|
721
|
+
const allowedOrigins = /* @__PURE__ */ new Set();
|
|
722
|
+
try {
|
|
723
|
+
allowedOrigins.add(new URL(this.config.ssoBaseUrl).origin);
|
|
724
|
+
allowedOrigins.add(new URL(this._serverBase).origin);
|
|
725
|
+
} catch {
|
|
726
|
+
return null;
|
|
727
|
+
}
|
|
728
|
+
const sameOrigin = (endpoint) => {
|
|
729
|
+
try {
|
|
730
|
+
return allowedOrigins.has(new URL(endpoint).origin);
|
|
731
|
+
} catch {
|
|
732
|
+
return false;
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
const publicOrigin = new URL(this.config.ssoBaseUrl).origin;
|
|
736
|
+
let authorizationOk = false;
|
|
737
|
+
try {
|
|
738
|
+
authorizationOk = new URL(d.authorization_endpoint).origin === publicOrigin;
|
|
739
|
+
} catch {
|
|
740
|
+
authorizationOk = false;
|
|
741
|
+
}
|
|
742
|
+
const bad = [];
|
|
743
|
+
if (!authorizationOk) bad.push("authorization_endpoint");
|
|
744
|
+
if (!sameOrigin(d.token_endpoint)) bad.push("token_endpoint");
|
|
745
|
+
if (!sameOrigin(d.userinfo_endpoint)) bad.push("userinfo_endpoint");
|
|
746
|
+
if (bad.length > 0) {
|
|
747
|
+
if (!this._discoveryOriginWarned) {
|
|
748
|
+
this._discoveryOriginWarned = true;
|
|
749
|
+
console.warn(
|
|
750
|
+
`[SSO SDK] OIDC Discovery \u7AEF\u70B9 origin \u4E0E ssoBaseUrl/serverBaseUrl \u4E0D\u5339\u914D\uFF08${bad.join(", ")}\uFF09\uFF0C\u5DF2\u56DE\u9000\u5230\u9ED8\u8BA4\u7AEF\u70B9\u3002\u82E5 SSO \u4E2D\u5FC3\u786E\u5B9E\u6258\u7BA1\u8DE8\u6E90\u7AEF\u70B9\uFF0C\u8BF7\u8BBE\u7F6E allowCrossOriginDiscoveryEndpoints: true`
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
return null;
|
|
754
|
+
}
|
|
755
|
+
return d;
|
|
756
|
+
}
|
|
698
757
|
/**
|
|
699
758
|
* 获取 OIDC Discovery 文档(带缓存 + 超时)
|
|
700
759
|
*
|
|
@@ -721,7 +780,10 @@ var _SsoClient = class _SsoClient {
|
|
|
721
780
|
);
|
|
722
781
|
return null;
|
|
723
782
|
}
|
|
724
|
-
|
|
783
|
+
const doc = await res.json();
|
|
784
|
+
const sanitized = this._sanitizeDiscovery(doc);
|
|
785
|
+
if (!sanitized) return null;
|
|
786
|
+
this._discovery = sanitized;
|
|
725
787
|
this._discoveryFetchedAt = now;
|
|
726
788
|
return this._discovery;
|
|
727
789
|
} catch (err) {
|
|
@@ -1081,6 +1143,10 @@ var _SsoClient = class _SsoClient {
|
|
|
1081
1143
|
throw new SsoError("network_error", "\u7F51\u7EDC\u8BF7\u6C42\u5931\u8D25", err);
|
|
1082
1144
|
}
|
|
1083
1145
|
if (!res.ok) {
|
|
1146
|
+
removeOAuthState(this.config.clientId);
|
|
1147
|
+
removeOAuthNonce(this.config.clientId);
|
|
1148
|
+
removePkceVerifier(this.config.clientId);
|
|
1149
|
+
removeSilentProbe(this.config.clientId);
|
|
1084
1150
|
let errData = {};
|
|
1085
1151
|
try {
|
|
1086
1152
|
errData = await res.json();
|
|
@@ -1096,6 +1162,10 @@ var _SsoClient = class _SsoClient {
|
|
|
1096
1162
|
assertValidTokenResponse(data, true);
|
|
1097
1163
|
const requestedScopes = (this.config.scopes || "openid profile").split(" ").filter(Boolean);
|
|
1098
1164
|
if (requestedScopes.includes("openid") && !data.id_token) {
|
|
1165
|
+
removeOAuthState(this.config.clientId);
|
|
1166
|
+
removeOAuthNonce(this.config.clientId);
|
|
1167
|
+
removePkceVerifier(this.config.clientId);
|
|
1168
|
+
removeSilentProbe(this.config.clientId);
|
|
1099
1169
|
removeTokenData(this.config.clientId);
|
|
1100
1170
|
throw new SsoError(
|
|
1101
1171
|
"id_token_invalid",
|
|
@@ -1240,7 +1310,7 @@ var _SsoClient = class _SsoClient {
|
|
|
1240
1310
|
if (!tokenData) {
|
|
1241
1311
|
throw new SsoError("not_authenticated", "\u672A\u767B\u5F55");
|
|
1242
1312
|
}
|
|
1243
|
-
if (Date.now() >= tokenData.expires_at) {
|
|
1313
|
+
if (Date.now() >= tokenData.expires_at - EXPIRY_SKEW_MS) {
|
|
1244
1314
|
tokenData = await this.refreshToken();
|
|
1245
1315
|
}
|
|
1246
1316
|
const userinfoEndpoint = await this._getUserinfoEndpoint();
|
|
@@ -1275,7 +1345,7 @@ var _SsoClient = class _SsoClient {
|
|
|
1275
1345
|
async getAccessToken() {
|
|
1276
1346
|
let tokenData = getTokenData(this.config.clientId);
|
|
1277
1347
|
if (!tokenData) return null;
|
|
1278
|
-
if (Date.now() >= tokenData.expires_at) {
|
|
1348
|
+
if (Date.now() >= tokenData.expires_at - EXPIRY_SKEW_MS) {
|
|
1279
1349
|
tokenData = await this.refreshToken();
|
|
1280
1350
|
}
|
|
1281
1351
|
return tokenData.access_token;
|
|
@@ -1284,6 +1354,9 @@ var _SsoClient = class _SsoClient {
|
|
|
1284
1354
|
* 检查是否已认证(不发起网络请求)
|
|
1285
1355
|
*
|
|
1286
1356
|
* 仅检查本地是否存在未过期的 access_token。
|
|
1357
|
+
* 故意不做 EXPIRY_SKEW_MS 提前量:这是同步的 UI 提示判定(是否显示"已登录"),
|
|
1358
|
+
* 提前 10s 翻转 false 会让调用方在 token 仍有效时误显示未登录;真正发请求的
|
|
1359
|
+
* getAccessToken/getUserInfo 已带提前量。
|
|
1287
1360
|
*/
|
|
1288
1361
|
isAuthenticated() {
|
|
1289
1362
|
const tokenData = getTokenData(this.config.clientId);
|