@nihplod/sso-sdk 1.2.1 → 1.3.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.
@@ -62,6 +62,13 @@ interface SsoClientConfig {
62
62
  scopes?: string;
63
63
  /** RP-Initiated Logout 返回地址(可选)。不传时回退到 redirectUri */
64
64
  postLogoutRedirectUri?: string;
65
+ /**
66
+ * 服务端到服务端调用的内网地址(可选,如 http://127.0.0.1:3000)。
67
+ * 仅用于 discovery / token / userinfo / revocation 等服务器间请求;
68
+ * 浏览器跳转(authorize、end-session)始终使用 ssoBaseUrl 公网地址。
69
+ * 仅在 BFF/服务端场景配置;浏览器端(Public Client)不要配置。
70
+ */
71
+ serverBaseUrl?: string;
65
72
  }
66
73
  /** 用户信息 */
67
74
  interface SsoUser {
@@ -75,6 +82,8 @@ interface SsoUser {
75
82
  /** 生日(ISO 8601,需 birthday scope,未设置时为 null) */
76
83
  birthday?: string | null;
77
84
  membership_level?: string;
85
+ /** 累计消费金额(单位:元,需 membership scope,与 membership_level 同源下发) */
86
+ total_spent?: number;
78
87
  total_points?: number;
79
88
  }
80
89
  /** OIDC Discovery 文档 */
@@ -94,6 +103,8 @@ interface OidcDiscovery {
94
103
  }
95
104
  declare class SsoClient {
96
105
  readonly config: SsoClientConfig;
106
+ /** 服务器间调用的基准地址(未配置 serverBaseUrl 时等于 ssoBaseUrl) */
107
+ private readonly _serverBase;
97
108
  private _discovery;
98
109
  private _discoveryFetchedAt;
99
110
  private _refreshLock;
@@ -111,9 +122,9 @@ declare class SsoClient {
111
122
  private _getDiscovery;
112
123
  /** 获取 authorize 端点 URL(优先 Discovery,回退默认) */
113
124
  private _getAuthorizeEndpoint;
114
- /** 获取 token 端点 URL(优先 Discovery,回退默认) */
125
+ /** 获取 token 端点 URL(服务器间调用:serverBaseUrl 直连优先,其次 Discovery,回退默认) */
115
126
  private _getTokenEndpoint;
116
- /** 获取 userinfo 端点 URL(优先 Discovery,回退默认) */
127
+ /** 获取 userinfo 端点 URL(服务器间调用:serverBaseUrl 直连优先,其次 Discovery,回退默认) */
117
128
  private _getUserinfoEndpoint;
118
129
  /**
119
130
  * 开放重定向防护:仅保存相对路径或与当前页面同源的 returnUrl,
@@ -488,6 +488,7 @@ var _SsoClient = class _SsoClient {
488
488
  if (!config.ssoBaseUrl) throw new SsoError("invalid_config", "ssoBaseUrl \u4E0D\u80FD\u4E3A\u7A7A");
489
489
  const base = config.ssoBaseUrl.replace(/\/+$/, "");
490
490
  this.config = { ...config, ssoBaseUrl: base };
491
+ this._serverBase = (config.serverBaseUrl ?? base).replace(/\/+$/, "");
491
492
  }
492
493
  // ============================================
493
494
  // 内部方法
@@ -503,7 +504,7 @@ var _SsoClient = class _SsoClient {
503
504
  if (this._discovery && now - this._discoveryFetchedAt < _SsoClient.DISCOVERY_TTL_MS) {
504
505
  return this._discovery;
505
506
  }
506
- const url = `${this.config.ssoBaseUrl}/api/oauth/.well-known/openid-configuration`;
507
+ const url = `${this._serverBase}/api/oauth/.well-known/openid-configuration`;
507
508
  try {
508
509
  const controller = new AbortController();
509
510
  const timeoutId = setTimeout(
@@ -534,14 +535,16 @@ var _SsoClient = class _SsoClient {
534
535
  if (d) return d.authorization_endpoint;
535
536
  return `${this.config.ssoBaseUrl}/api/oauth/authorize`;
536
537
  }
537
- /** 获取 token 端点 URL(优先 Discovery,回退默认) */
538
+ /** 获取 token 端点 URL(服务器间调用:serverBaseUrl 直连优先,其次 Discovery,回退默认) */
538
539
  async _getTokenEndpoint() {
540
+ if (this.config.serverBaseUrl) return `${this._serverBase}/api/oauth/token`;
539
541
  const d = await this._getDiscovery();
540
542
  if (d) return d.token_endpoint;
541
543
  return `${this.config.ssoBaseUrl}/api/oauth/token`;
542
544
  }
543
- /** 获取 userinfo 端点 URL(优先 Discovery,回退默认) */
545
+ /** 获取 userinfo 端点 URL(服务器间调用:serverBaseUrl 直连优先,其次 Discovery,回退默认) */
544
546
  async _getUserinfoEndpoint() {
547
+ if (this.config.serverBaseUrl) return `${this._serverBase}/api/oauth/userinfo`;
545
548
  const d = await this._getDiscovery();
546
549
  if (d) return d.userinfo_endpoint;
547
550
  return `${this.config.ssoBaseUrl}/api/oauth/userinfo`;
@@ -1018,8 +1021,8 @@ var _SsoClient = class _SsoClient {
1018
1021
  clearAllSsoData(this.config.clientId);
1019
1022
  if (refreshToken && this.config.clientId) {
1020
1023
  try {
1021
- const discovery = await this._getDiscovery();
1022
- const revokeUrl = discovery?.revocation_endpoint || `${this.config.ssoBaseUrl}/api/oauth/revoke`;
1024
+ const discovery = this.config.serverBaseUrl ? null : await this._getDiscovery();
1025
+ const revokeUrl = this.config.serverBaseUrl ? `${this._serverBase}/api/oauth/revoke` : discovery?.revocation_endpoint || `${this.config.ssoBaseUrl}/api/oauth/revoke`;
1023
1026
  const revokeBody = new URLSearchParams({
1024
1027
  token: refreshToken,
1025
1028
  token_type_hint: "refresh_token",