@autional/shared 0.1.0-rc.35 → 0.1.0-rc.37

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autional/shared",
3
- "version": "0.1.0-rc.35",
3
+ "version": "0.1.0-rc.37",
4
4
  "description": "Autional shared layer — API client, auth, types, hooks, utils",
5
5
  "license": "AGPL-3.0",
6
6
  "main": "./src/index.ts",
@@ -182,38 +182,85 @@ describe('buildLoginUrl', () => {
182
182
  });
183
183
  });
184
184
 
185
- describe('buildLogoutUrl(F-W5c 登出意图标记)', () => {
185
+ describe('buildLogoutUrl(TASK-LS-08 同源标准端点 + legacy 兜底)', () => {
186
186
  const AUTH_CONFIG = {
187
187
  VITE_PORTAL_CONFIG: {
188
188
  auth: { host: 'auth', base: '' },
189
189
  },
190
190
  };
191
+ /** env 级 client_id(三级来源之二) */
192
+ const CID_CONFIG = {
193
+ ...AUTH_CONFIG,
194
+ VITE_OAUTH_CLIENT_ID: 'cid-env',
195
+ };
191
196
 
192
- it('门户域登出:auth 裸根 + redirect 回程 + logout=1', () => {
193
- mockWindow('app.autional.com', 'https:', 'https://app.autional.com', AUTH_CONFIG);
197
+ beforeEach(() => {
198
+ // localStorage 桩按需设置;测试间清掉全局桩防跨用例泄漏(node 环境无真 localStorage)
199
+ vi.unstubAllGlobals();
200
+ });
201
+
202
+ it('门户域登出:同源标准端点 + client_id + post_logout_redirect_uri=<origin>/ + state=returnUrl', () => {
203
+ mockWindow('app.autional.com', 'https:', 'https://app.autional.com', CID_CONFIG);
194
204
  const url = buildLogoutUrl('https://app.autional.com/demo/');
195
205
  expect(url).toBe(
196
- 'https://auth.autional.com/?redirect=' +
197
- encodeURIComponent('https://app.autional.com/demo/') +
198
- '&logout=1',
206
+ 'https://app.autional.com/oauth/api/v1/oauth/logout?client_id=cid-env&post_logout_redirect_uri=' +
207
+ encodeURIComponent('https://app.autional.com/') +
208
+ '&state=' +
209
+ encodeURIComponent('https://app.autional.com/demo/'),
199
210
  );
200
211
  });
201
212
 
202
- it('auth 域租户页登出也走裸根:不经 /<slug>/login(那里 logout=1 被静默忽略)', () => {
203
- mockWindow('auth.autional.local', 'https:', 'https://auth.autional.local', AUTH_CONFIG);
213
+ it('auth 域租户页登出同样走同源标准端点(不再裸根 / 不经 /<slug>/login)', () => {
214
+ mockWindow('auth.autional.local', 'https:', 'https://auth.autional.local', CID_CONFIG);
204
215
  const url = buildLogoutUrl('https://auth.autional.local/acme-corp/dashboard');
205
216
  expect(url).toBe(
206
- 'https://auth.autional.local/?redirect=' +
207
- encodeURIComponent('https://auth.autional.local/acme-corp/dashboard') +
208
- '&logout=1',
217
+ 'https://auth.autional.local/oauth/api/v1/oauth/logout?client_id=cid-env&post_logout_redirect_uri=' +
218
+ encodeURIComponent('https://auth.autional.local/') +
219
+ '&state=' +
220
+ encodeURIComponent('https://auth.autional.local/acme-corp/dashboard'),
209
221
  );
210
222
  expect(url).not.toContain('/acme-corp/login');
211
223
  });
212
224
 
213
- it('无回程:auth 裸根 + logout=1', () => {
214
- mockWindow('auth.autional.local', 'https:', 'https://auth.autional.local', AUTH_CONFIG);
225
+ it('无回程:仅 client_id + post_logout_redirect_uri,不产 state', () => {
226
+ mockWindow('app.autional.com', 'https:', 'https://app.autional.com', CID_CONFIG);
215
227
  const url = buildLogoutUrl();
216
- expect(url).toBe('https://auth.autional.local/?logout=1');
228
+ expect(url).toBe(
229
+ 'https://app.autional.com/oauth/api/v1/oauth/logout?client_id=cid-env&post_logout_redirect_uri=' +
230
+ encodeURIComponent('https://app.autional.com/'),
231
+ );
232
+ });
233
+
234
+ describe('client_id 三级来源(会话持久化 → env → 空)', () => {
235
+ it('localStorage 会话值优先于 env', () => {
236
+ vi.stubGlobal('localStorage', { getItem: () => 'cid-session' });
237
+ mockWindow('app.autional.com', 'https:', 'https://app.autional.com', CID_CONFIG);
238
+ const url = buildLogoutUrl('https://app.autional.com/demo/');
239
+ expect(url).toContain('client_id=cid-session');
240
+ expect(url).not.toContain('cid-env');
241
+ });
242
+
243
+ it('localStorage 空 → env 兜底', () => {
244
+ vi.stubGlobal('localStorage', { getItem: () => null });
245
+ mockWindow('app.autional.com', 'https:', 'https://app.autional.com', CID_CONFIG);
246
+ expect(buildLogoutUrl()).toContain('client_id=cid-env');
247
+ });
248
+
249
+ it('双空 → legacy 兜底:auth 裸根 + redirect 回程 + logout=1', () => {
250
+ mockWindow('app.autional.com', 'https:', 'https://app.autional.com', AUTH_CONFIG);
251
+ const url = buildLogoutUrl('https://app.autional.com/demo/');
252
+ expect(url).toBe(
253
+ 'https://auth.autional.com/?redirect=' +
254
+ encodeURIComponent('https://app.autional.com/demo/') +
255
+ '&logout=1',
256
+ );
257
+ });
258
+
259
+ it('双空 + 无回程 → legacy 裸根 + logout=1(现行为保持)', () => {
260
+ mockWindow('auth.autional.local', 'https:', 'https://auth.autional.local', AUTH_CONFIG);
261
+ const url = buildLogoutUrl();
262
+ expect(url).toBe('https://auth.autional.local/?logout=1');
263
+ });
217
264
  });
218
265
  });
219
266
  });
@@ -58,8 +58,7 @@ export function usePermissionsQuery(tenantId?: string | null, enabled = true) {
58
58
  []) as string[];
59
59
  },
60
60
  staleTime: AUTH_DATA_STALE_TIME,
61
- retry: 1,
62
- retryDelay: 1000,
61
+ // 重试策略不在此声明:交给消费方的 QueryClient 默认(本包不得有 per-query 覆盖,否则会盖过默认)。
63
62
  // 无 token 时禁止发起请求:未登录时该查询必然 401 → apiClient 401 拦截
64
63
  // → refreshToken() 无 refresh token → onUnauthorized() 抢跑 buildLoginUrl,
65
64
  // 与 RequireAuth 的 OAuth PKCE 触发形成时序竞争(跨域 admin 场景偶发 302 回登录页)。
@@ -92,8 +91,7 @@ export function useTenantsQuery(tenantId?: string | null, enabled = true) {
92
91
  }>;
93
92
  },
94
93
  staleTime: AUTH_DATA_STALE_TIME,
95
- retry: 1,
96
- retryDelay: 1000,
94
+ // 重试策略不在此声明:交给消费方的 QueryClient 默认(本包不得有 per-query 覆盖,否则会盖过默认)。
97
95
  // 与 usePermissionsQuery 一致:无 token 时禁止发起请求,
98
96
  // 避免 401 → onUnauthorized 抢跑 RequireAuth 的 OAuth PKCE 流程。
99
97
  enabled,
@@ -112,7 +110,6 @@ export function useMeQuery() {
112
110
  return extractItem(res.data) ?? res.data;
113
111
  },
114
112
  staleTime: AUTH_DATA_STALE_TIME,
115
- retry: 1,
116
- retryDelay: 1000,
113
+ // 重试策略不在此声明:交给消费方的 QueryClient 默认(本包不得有 per-query 覆盖,否则会盖过默认)。
117
114
  });
118
115
  }
package/src/auth/roles.ts CHANGED
@@ -8,6 +8,7 @@
8
8
  */
9
9
 
10
10
  import { getPortalUrl } from '../config';
11
+ import { resolveClientIdForSession } from './oauth-client-id-store';
11
12
 
12
13
  /** auth-pages 的静态/受保护路由 — 首段不会是租户 slug */
13
14
  const AUTH_ROUTE_RESERVED = new Set([
@@ -20,6 +21,7 @@ const AUTH_ROUTE_RESERVED = new Set([
20
21
  'privacy',
21
22
  'error',
22
23
  'logout',
24
+ 'logout-confirm',
23
25
  'passkey',
24
26
  'reapply',
25
27
  'mfa',
@@ -37,20 +39,41 @@ const AUTH_ROUTE_RESERVED = new Set([
37
39
  ]);
38
40
 
39
41
  /**
40
- * 登出回程 URL:auth 域**裸根**入口 + `logout=1` 显式登出意图标记。
42
+ * 登出 URL(TASK-LS-08):同源标准端点(RP-Initiated Logout 无 hint 形态)优先,
43
+ * client_id 不可得时保持 legacy 形态。
41
44
  *
42
- * 刻意不复用 buildLoginUrl:其租户 slug 保留会把 auth 域页面的登出落到
43
- * `/<slug>/login`(登录页路由,不经入口路由),`logout=1` 在那里被静默忽略;
45
+ * 标准端点形态(ADR-12 同源直通):`${origin}/oauth/api/v1/oauth/logout`
46
+ * - client_id:本部署的 OAuth 客户端(`resolveClientIdForSession` 三级来源:会话持久化
47
+ * localStorage `oauth_client_id` → env),服务端据此做 post_logout_redirect_uri 注册面白名单校验;
48
+ * - post_logout_redirect_uri:本门户 origin 根(`<origin>/`,注册形态同 PlatformPortalRedirectURIs);
49
+ * - state:returnUrl 的 URL-safe 编码(门户零行为驱动——服务端只原样回拼到回跳 URL,门户不解析)。
50
+ * 浏览器整页导航不带 Bearer → 服务端「无 hint 无会话浏览器」支 302 确认页承接;
51
+ * 用户确认后由确认页经 BFF(会话 cookie → Bearer)触达端点,走认证会话支并导航服务端 location。
52
+ *
53
+ * legacy 兜底(D2=A 兼容分支保留,EntryRouter 承接):client_id 不可得 → auth 域**裸根**入口 +
54
+ * `logout=1` 显式登出意图标记。刻意不复用 buildLoginUrl:其租户 slug 保留会把 auth 域页面的
55
+ * 登出落到 `/<slug>/login`(登录页路由,不经入口路由),`logout=1` 在那里被静默忽略;
44
56
  * 带会话回程甚至会被登录页 checkAndRedirect 直接放行成静默重登(F-W5c)。
45
- * 裸根 = EntryRouter 唯一登出闸门:先终结 auth 域会话,再决定落点——
46
- * 回程能解析出真实租户 → 裸 `/<slug>/login`(不带回程参数:登出即"重新开始",
47
- * 登录后走默认去向,去向选择权交还用户);否则落 brand 裸根。
57
+ * 裸根 = EntryRouter 唯一登出闸门:先终结 auth 域会话,再决定落点——回程能解析出真实租户 →
58
+ * 裸 `/<slug>/login`;否则落 brand 裸根。
48
59
  */
49
60
  export function buildLogoutUrl(returnUrl?: string): string {
50
61
  const base = getPortalUrl('auth');
51
- if (!base) return '/';
52
- const redirect = returnUrl ? `redirect=${encodeURIComponent(returnUrl)}&` : '';
53
- return `${base}/?${redirect}logout=1`;
62
+ // legacy 兜底:现行为保持(auth 裸根 + logout=1 意图标记)
63
+ const legacy = (): string => {
64
+ if (!base) return '/';
65
+ const redirect = returnUrl ? `redirect=${encodeURIComponent(returnUrl)}&` : '';
66
+ return `${base}/?${redirect}logout=1`;
67
+ };
68
+
69
+ const clientId = resolveClientIdForSession();
70
+ if (!clientId || typeof window === 'undefined' || !window.location?.origin) return legacy();
71
+
72
+ const params = new URLSearchParams();
73
+ params.set('client_id', clientId);
74
+ params.set('post_logout_redirect_uri', `${window.location.origin}/`);
75
+ if (returnUrl) params.set('state', returnUrl);
76
+ return `${window.location.origin}/oauth/api/v1/oauth/logout?${params.toString()}`;
54
77
  }
55
78
 
56
79
  /**
package/src/auth/store.ts CHANGED
@@ -201,7 +201,8 @@ export function loginWithTokens(
201
201
  *
202
202
  * redirectTo(U350):显式落点(会话过期→auth /error、注销完成→/<slug>/login?account_deleted=true 等
203
203
  * 旗标页)——清理完成后整页前往。缺省仅清会话、不导航;注意与 useLogout 口径区分:该 hook 走
204
- * buildLogoutUrl 登出意图流(logout=1 交入口路由定落点),不得用于需要保留业务 query 旗标的目标。
204
+ * buildLogoutUrl 登出流(LS-08:同源标准端点;client_id 不可得时退 legacy logout=1 交入口路由),
205
+ * 不得用于需要保留业务 query 旗标的目标。
205
206
  */
206
207
  export async function logout(redirectTo?: string): Promise<void> {
207
208
  const { AuthService } = await import('./service');
@@ -5,7 +5,7 @@
5
5
  * (不复制 4 份,违反 DRY)。
6
6
  *
7
7
  * - usePublicTenantSlugs: 调公开 tenants API(无认证端点),staleTime 5m 缓存,
8
- * retry 1 + 8s 超时;失败上抛(不吞成空数组成功)——放行语义由消费方
8
+ * 8s 超时(重试由消费方默认策略决定);失败上抛(不吞成空数组成功)——放行语义由消费方
9
9
  * (TenantIndexGuard / TenantRootRedirect)在失败态自行 fail-open(rc.35)。
10
10
  * - TenantIndexGuard: 租户 slug 白名单守卫(P0-2),防止未知 slug 被贪婪渲染为
11
11
  * 租户内容(index 首页与 /:slug/... 子路由通用)。404 页由各 portal 通过
@@ -32,7 +32,7 @@ export interface PublicTenantInfo {
32
32
  }
33
33
 
34
34
  /** 公开租户名单请求超时(ms)。跨境链路(Vercel edge → 源站)实测新连接丢 ~45%,
35
- * 失败连接 edge 端要 ~31s 才吐 502;正常成功 ≤2s,8s 留 4x 余量,失败快速交给 retry。
35
+ * 失败连接 edge 端要 ~31s 才吐 502;正常成功 ≤2s,8s 留 4x 余量,失败快速交由消费方的重试策略。
36
36
  * (rc.35:此前无超时 + catch 吞错 → retry 死码、失败被当"空名单"成功。) */
37
37
  export const PUBLIC_TENANTS_FETCH_TIMEOUT_MS = 8000;
38
38
 
@@ -40,7 +40,7 @@ export const PUBLIC_TENANTS_FETCH_TIMEOUT_MS = 8000;
40
40
  * 公开租户列表(无认证端点,用于校验 URL slug 有效性)。
41
41
  * 从 end-user-portal App.tsx 原样抽取。
42
42
  * - staleTime 5m(plan §5 资源列:public tenants API 有缓存)
43
- * - retry 1;非 2xx / 网络错 / 超时一律上抛(不吞成空数组成功)——
43
+ * - 非 2xx / 网络错 / 超时一律上抛(不吞成空数组成功)——
44
44
  * error 态不落缓存,下次调用自然重试
45
45
  * - 消费方 fail-open:名单缺失(error 态 data=undefined 或空数组)→ 不拦截
46
46
  * (TenantIndexGuard 渲染 children;TenantRootRedirect 漏斗 brand;
@@ -69,7 +69,7 @@ export function usePublicTenantSlugs() {
69
69
  }
70
70
  },
71
71
  staleTime: 5 * 60 * 1000,
72
- retry: 1,
72
+ // 重试策略交给消费方 QueryClient 默认(本包不得有 per-query 覆盖)。
73
73
  });
74
74
  }
75
75
 
@@ -9,9 +9,10 @@ import { TenantIndexGuard, usePublicTenantSlugs } from '../TenantIndexGuard';
9
9
  function createQueryClient() {
10
10
  return new QueryClient({
11
11
  defaultOptions: {
12
- // retryDelay 0:hook 内部 retry:1 覆盖 retry,但重试退避仍走 defaultOptions,
13
- // 不设 0 时失败用例要白等默认 1s 退避
14
- queries: { retry: false, retryDelay: 0, gcTime: Infinity },
12
+ // 重试由**本测试客户端**声明(生产里由消费方 QueryClient 默认决定;本包不再声明 retry,
13
+ // 否则会盖过消费方默认 —— rc.35 起如此)。retry:1 + retryDelay:0 让
14
+ // 「重试耗尽后错误必须上抛」这一断言仍可验证,且失败用例不白等默认退避。
15
+ queries: { retry: 1, retryDelay: 0, gcTime: Infinity },
15
16
  },
16
17
  });
17
18
  }