@autional/shared 0.1.0-rc.41 → 0.1.0-rc.42

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.41",
3
+ "version": "0.1.0-rc.42",
4
4
  "description": "Autional shared layer — API client, auth, types, hooks, utils",
5
5
  "license": "AGPL-3.0",
6
6
  "main": "./src/index.ts",
@@ -43,6 +43,11 @@ function expiredJwt(): string {
43
43
  return `eyJhbGciOiJIUzI1NiJ9.${payload}.sig`;
44
44
  }
45
45
 
46
+ function liveJwt(): string {
47
+ const payload = btoa(JSON.stringify({ exp: Math.floor(Date.now() / 1000) + 3600 }));
48
+ return `eyJhbGciOiJIUzI1NiJ9.${payload}.sig`;
49
+ }
50
+
46
51
  describe('U352: 预刷新成功后注入链完整性', () => {
47
52
  beforeEach(() => {
48
53
  captured = [];
@@ -87,3 +92,37 @@ describe('U352: 预刷新成功后注入链完整性', () => {
87
92
  expect(captured[0].headers['X-Tenant-ID']).toBe('tenant-abc');
88
93
  });
89
94
  });
95
+
96
+ // 地址栏唯一权威(Wave 2):无 token 的公开认证入口不得被 localStorage 残留租户污染。
97
+ describe('无 token 不注入 X-Tenant-ID', () => {
98
+ beforeEach(() => {
99
+ captured = [];
100
+ installCaptureAdapter();
101
+ useAuthStore.getState().clearAuth();
102
+ useAuthStore.getState().setCurrentTenant('tenant-stale');
103
+ });
104
+
105
+ afterEach(() => {
106
+ apiClient.defaults.adapter = originalAdapter as any;
107
+ vi.restoreAllMocks();
108
+ useAuthStore.getState().clearAuth();
109
+ });
110
+
111
+ it('无 token:残留租户不注入 header', async () => {
112
+ await apiClient.get('/probe/public');
113
+ expect(captured[0].headers['X-Tenant-ID']).toBeUndefined();
114
+ expect(captured[0].headers['Authorization']).toBeUndefined();
115
+ });
116
+
117
+ it('调用方显式 X-Tenant-ID 原样保留(不注入也不清除)', async () => {
118
+ await apiClient.get('/probe/public', { headers: { 'X-Tenant-ID': 'explicit-tenant' } });
119
+ expect(captured[0].headers['X-Tenant-ID']).toBe('explicit-tenant');
120
+ });
121
+
122
+ it('有 token:仍注入当前租户(内部调用链回归)', async () => {
123
+ useAuthStore.getState().setAuth(liveJwt(), 'rt-live', { id: 'u-1', username: 'demo' } as any);
124
+ await apiClient.get('/probe/authed');
125
+ expect(captured[0].headers['X-Tenant-ID']).toBe('tenant-stale');
126
+ expect(String(captured[0].headers['Authorization'])).toMatch(/^Bearer /);
127
+ });
128
+ });
package/src/api/client.ts CHANGED
@@ -35,8 +35,11 @@ function setupInterceptors(instance: AxiosInstance) {
35
35
  config.headers.Authorization = `Bearer ${bearer}`;
36
36
  }
37
37
 
38
+ // 无 token 的公开认证入口(登录/OAuth/QR/恢复)以地址栏租户为唯一权威:
39
+ // 此时 localStorage 里的 currentTenantId 可能是上次会话残留,注入会覆盖显式租户。
40
+ // 有 token 才注入(内部调用链沿用它);调用方显式设置的 X-Tenant-ID 不受影响。
38
41
  const tenantId = AuthService.getCurrentTenantId();
39
- if (tenantId) {
42
+ if (bearer && tenantId) {
40
43
  config.headers['X-Tenant-ID'] = tenantId;
41
44
  }
42
45
 
@@ -5664,8 +5664,10 @@ export async function authOauthAccounts() {
5664
5664
  * 获取OAuth提供商列表
5665
5665
  * 获取系统支持的所有OAuth登录提供者列表
5666
5666
  */
5667
- export async function authOauthProviders() {
5668
- const res = await api.get(`/identity/api/v1/auth/oauth/providers`);
5667
+ export async function authOauthProviders(params?: {
5668
+ tenantId?: string; // 显式租户(地址栏/slug 优先于 localStorage 残留)
5669
+ }) {
5670
+ const res = await api.get(`/identity/api/v1/auth/oauth/providers`, { params });
5669
5671
  return res.data;
5670
5672
  }
5671
5673
 
@@ -5770,8 +5772,8 @@ export async function authQrLoginConfirmPost(data: QrLoginConfirmRequest) {
5770
5772
  * 发起二维码登录
5771
5773
  * 生成一个新的二维码登录会话,返回会话令牌和数字匹配码供用户扫描
5772
5774
  */
5773
- export async function authQrLoginInitiatePost() {
5774
- const res = await api.post(`/identity/api/v1/auth/qr-login/initiate`);
5775
+ export async function authQrLoginInitiatePost(data?: QrLoginInitiateRequest) {
5776
+ const res = await api.post(`/identity/api/v1/auth/qr-login/initiate`, data);
5775
5777
  return res.data;
5776
5778
  }
5777
5779
 
@@ -4760,6 +4760,7 @@ export interface CompleteAccountRecoveryRequest {
4760
4760
  newPassword: string; // 新密码
4761
4761
  passwordTransmission?: string; // plain|hash|symmetric|asymmetric
4762
4762
  recoveryToken: string; // 恢复令牌
4763
+ tenantId?: string; // 显式租户(非空时必须与恢复令牌租户一致)
4763
4764
  }
4764
4765
 
4765
4766
  export interface CompleteAccountRecoveryResponse {
@@ -5656,6 +5657,10 @@ export interface QrLoginConfirmRequest {
5656
5657
  token: string;
5657
5658
  }
5658
5659
 
5660
+ export interface QrLoginInitiateRequest {
5661
+ tenantId?: string; // 显式租户(地址栏/slug 优先)
5662
+ }
5663
+
5659
5664
  export interface QrLoginInitiateResponse {
5660
5665
  expiresIn?: number;
5661
5666
  numberMatching?: string;
@@ -5831,6 +5836,7 @@ export interface RegisterResponse {
5831
5836
  export interface RequestAccountRecoveryRequest {
5832
5837
  identity: string; // 已知的身份标识 | @example john@example.com
5833
5838
  method: "backup_email" | "security_qa" | "trusted_device" | "sms"; // 恢复方法
5839
+ tenantId?: string; // 显式租户(地址栏/slug 优先)
5834
5840
  }
5835
5841
 
5836
5842
  export interface RequestAccountRecoveryResponseWrapper {
@@ -6283,6 +6289,7 @@ export interface VerificationTrackingQueryResponse {
6283
6289
  export interface VerifyAccountRecoveryRequest {
6284
6290
  code: string; // 验证码
6285
6291
  recoveryToken: string; // 恢复令牌
6292
+ tenantId?: string; // 显式租户(非空时必须与恢复令牌租户一致)
6286
6293
  }
6287
6294
 
6288
6295
  /** 验证邮箱/手机号变更 */