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

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.42",
3
+ "version": "0.1.0-rc.44",
4
4
  "description": "Autional shared layer — API client, auth, types, hooks, utils",
5
5
  "license": "AGPL-3.0",
6
6
  "main": "./src/index.ts",
package/src/api/client.ts CHANGED
@@ -182,6 +182,10 @@ export function createApiClient(baseUrl: string): AxiosInstance {
182
182
  const instance = axios.create({
183
183
  baseURL: baseUrl,
184
184
  timeout: 30000,
185
+ // 平台跨源只认 Authorization 头(网关 cors.allow_credentials=false):
186
+ // 显式关闭凭证,避免将来默认值/调用方改动把 cookie 带进跨源请求
187
+ // (带凭证的跨源要求网关回写 ACAC,而动态租户 origin 永不回写)。
188
+ withCredentials: false,
185
189
  headers: { 'Content-Type': 'application/json' },
186
190
  });
187
191
  setupInterceptors(instance);
@@ -1,7 +1,7 @@
1
1
  // GENERATED FILE — DO NOT EDIT.
2
2
  // Source: contracts/errors.semantics.json(@autional/shared 仓内镜像,源 = service-core base/error/testdata/errors.semantics.json)
3
3
  // Regenerate: pnpm gen:errors · Verify: pnpm gen:errors:check
4
- // rows: 787 · source sha256: f846240c034effe8725e853eb67ab31bf8bcad2595e4ff815ab132c85dfb08aa
4
+ // rows: 792 · source sha256: 962af7ad9e1f41b957fa8cacb6f1bdbf0f0a56cb8cbdf144107e1329df80c0cd
5
5
  //
6
6
  // 单一真源链:service-core/base/error/semantics.go(Go)→ errors.semantics.json → 本文件。
7
7
  // ⚠️ 禁止在本文件或页面里手抄错误码分支:请消费 ERROR_SEMANTICS / UNPROVISIONED_CODES。
@@ -42,6 +42,7 @@ export interface ErrorSemantics {
42
42
  class: ErrorClass;
43
43
  absence: Absence;
44
44
  uiObservation: UIObservation;
45
+ name?: string;
45
46
  retryable: boolean;
46
47
  docsPath: string;
47
48
  }
@@ -364,11 +365,15 @@ export const ERROR_SEMANTICS: Readonly<Record<number, ErrorSemantics>> = {
364
365
  61000301: { code: 61000301, httpStatus: 404, class: 'not_found', absence: 'entity', uiObservation: 'not_found_page', retryable: false, docsPath: '/errors/61000301' },
365
366
  61000302: { code: 61000302, httpStatus: 409, class: 'conflict', absence: '', uiObservation: 'conflict', retryable: false, docsPath: '/errors/61000302' },
366
367
  61000401: { code: 61000401, httpStatus: 401, class: 'unauthorized', absence: '', uiObservation: 'session_expired', retryable: false, docsPath: '/errors/61000401' },
367
- 61000402: { code: 61000402, httpStatus: 400, class: 'invalid', absence: '', uiObservation: 'form_error', retryable: false, docsPath: '/errors/61000402' },
368
- 61000403: { code: 61000403, httpStatus: 403, class: 'forbidden', absence: '', uiObservation: 'reauth_required', retryable: false, docsPath: '/errors/61000403' },
368
+ 61000402: { code: 61000402, httpStatus: 400, class: 'invalid', absence: '', uiObservation: 'form_error', name: 'MFA_INVALID_CODE', retryable: false, docsPath: '/errors/61000402' },
369
+ 61000403: { code: 61000403, httpStatus: 403, class: 'forbidden', absence: '', uiObservation: 'reauth_required', name: 'MFA_CHALLENGE_REQUIRED', retryable: false, docsPath: '/errors/61000403' },
370
+ 61000404: { code: 61000404, httpStatus: 403, class: 'forbidden', absence: '', uiObservation: 'reauth_required', name: 'MFA_CHALLENGE_USED', retryable: false, docsPath: '/errors/61000404' },
371
+ 61000405: { code: 61000405, httpStatus: 403, class: 'forbidden', absence: '', uiObservation: 'reauth_required', name: 'MFA_CHALLENGE_ATTEMPTS_EXCEEDED', retryable: false, docsPath: '/errors/61000405' },
369
372
  61000406: { code: 61000406, httpStatus: 403, class: 'forbidden', absence: '', uiObservation: 'forbidden', retryable: false, docsPath: '/errors/61000406' },
370
373
  61000407: { code: 61000407, httpStatus: 410, class: 'gone', absence: '', uiObservation: 'expired', retryable: false, docsPath: '/errors/61000407' },
371
374
  61000408: { code: 61000408, httpStatus: 404, class: 'not_found', absence: 'entity', uiObservation: 'not_found_page', retryable: false, docsPath: '/errors/61000408' },
375
+ 61000409: { code: 61000409, httpStatus: 400, class: 'invalid', absence: '', uiObservation: 'form_error', name: 'MFA_CODE_ALREADY_USED', retryable: false, docsPath: '/errors/61000409' },
376
+ 61000410: { code: 61000410, httpStatus: 429, class: 'limit', absence: '', uiObservation: 'rate_limited', name: 'MFA_ATTEMPTS_THROTTLED', retryable: true, docsPath: '/errors/61000410' },
372
377
  61000501: { code: 61000501, httpStatus: 404, class: 'not_found', absence: 'entity', uiObservation: 'not_found_page', retryable: false, docsPath: '/errors/61000501' },
373
378
  61000502: { code: 61000502, httpStatus: 400, class: 'invalid', absence: '', uiObservation: 'form_error', retryable: false, docsPath: '/errors/61000502' },
374
379
  61000601: { code: 61000601, httpStatus: 404, class: 'not_found', absence: 'entity', uiObservation: 'not_found_page', retryable: false, docsPath: '/errors/61000601' },
@@ -554,6 +559,7 @@ export const ERROR_SEMANTICS: Readonly<Record<number, ErrorSemantics>> = {
554
559
  61050012: { code: 61050012, httpStatus: 409, class: 'conflict', absence: '', uiObservation: 'conflict', retryable: false, docsPath: '/errors/61050012' },
555
560
  61050013: { code: 61050013, httpStatus: 404, class: 'not_found', absence: 'entity', uiObservation: 'not_found_page', retryable: false, docsPath: '/errors/61050013' },
556
561
  61050014: { code: 61050014, httpStatus: 404, class: 'not_found', absence: 'entity', uiObservation: 'not_found_page', retryable: false, docsPath: '/errors/61050014' },
562
+ 61050015: { code: 61050015, httpStatus: 403, class: 'forbidden', absence: '', uiObservation: 'forbidden', retryable: false, docsPath: '/errors/61050015' },
557
563
  61050020: { code: 61050020, httpStatus: 404, class: 'not_found', absence: 'entity', uiObservation: 'not_found_page', retryable: false, docsPath: '/errors/61050020' },
558
564
  61050021: { code: 61050021, httpStatus: 429, class: 'limit', absence: '', uiObservation: 'rate_limited', retryable: true, docsPath: '/errors/61050021' },
559
565
  61050022: { code: 61050022, httpStatus: 400, class: 'invalid', absence: '', uiObservation: 'form_error', retryable: false, docsPath: '/errors/61050022' },
@@ -847,6 +853,18 @@ export const UNPROVISIONED_CODES: ReadonlySet<number> = new Set([]);
847
853
  export const EMPTY_STATE_CODES: ReadonlySet<number> = new Set([]);
848
854
 
849
855
  export const ERROR_SEMANTICS_META = {
850
- rowCount: 787,
851
- sourceSha256: 'f846240c034effe8725e853eb67ab31bf8bcad2595e4ff815ab132c85dfb08aa',
856
+ rowCount: 792,
857
+ sourceSha256: '962af7ad9e1f41b957fa8cacb6f1bdbf0f0a56cb8cbdf144107e1329df80c0cd',
852
858
  } as const;
859
+
860
+ /** 具名错误码常量(契约 name → code)。页面只消费常量,不写数字字面量。 */
861
+ export const ERROR_CODES = {
862
+ MFA_INVALID_CODE: 61000402,
863
+ MFA_CHALLENGE_REQUIRED: 61000403,
864
+ MFA_CHALLENGE_USED: 61000404,
865
+ MFA_CHALLENGE_ATTEMPTS_EXCEEDED: 61000405,
866
+ MFA_CODE_ALREADY_USED: 61000409,
867
+ MFA_ATTEMPTS_THROTTLED: 61000410,
868
+ } as const;
869
+
870
+ export type ErrorCodeName = keyof typeof ERROR_CODES;
@@ -3196,6 +3196,25 @@ export interface PublicCertificationListResponse {
3196
3196
  total?: number; // 总条数(平铺,便于直接读取)
3197
3197
  }
3198
3198
 
3199
+ // P2 四态评估(2026-10-27)—— 手改 additive:本源生成器 scripts/generate_api_ts.py
3200
+ // 仍在旧 monorepo(D:/go/auth_ms_new),多仓工作区不可用;字段与 compliance-service
3201
+ // swagger(PublicComplianceStatusResponse / SecurityScore)逐字段对齐,重生成前勿改形状。
3202
+ export interface AssessmentCoveragePublic {
3203
+ assessed?: number; // @example 7
3204
+ percent?: number; // @example 70
3205
+ total?: number; // @example 10
3206
+ }
3207
+
3208
+ export interface FrameworkAssessmentPublic {
3209
+ controlsAssessed?: number; // @example 7
3210
+ controlsTotal?: number; // @example 10
3211
+ controlsUnknown?: number; // @example 3
3212
+ displayName?: string; // @example ISO/IEC 27001:2022
3213
+ framework?: string; // 门禁键;独立标准为空 | @example iso27001
3214
+ standardId?: string; // @example iso27001_2022
3215
+ status?: string; // compliant / non_compliant / not_assessed / evaluation_error | @example not_assessed
3216
+ }
3217
+
3199
3218
  export interface PublicComplianceStatusDetailResponse {
3200
3219
  code?: number;
3201
3220
  data?: PublicComplianceStatusResponse;
@@ -3204,13 +3223,17 @@ export interface PublicComplianceStatusDetailResponse {
3204
3223
  }
3205
3224
 
3206
3225
  export interface PublicComplianceStatusResponse {
3226
+ assessmentCoverage?: AssessmentCoveragePublic;
3227
+ assessmentVersion?: string; // @example assessment-v2
3228
+ evaluationError?: boolean; // 只表示"是否发生评估异常",不含内部错误串 | @example False
3229
+ frameworkAssessments?: FrameworkAssessmentPublic[]; // 已启用门禁的评估结果(四态 + 覆盖度)
3207
3230
  frameworksEnabled?: string[]; // @example ['["gdpr"', '"iso27001"', '"sox"]']
3208
- gdprCompliant?: boolean; // @example True
3231
+ gdprCompliant?: boolean; // legacy:gate 已启用且状态为 compliant 才为 true | @example True
3209
3232
  iso27001Compliant?: boolean; // @example True
3210
3233
  lastAuditDate?: string; // @example 2026-03-15
3211
3234
  nextAuditDate?: string; // @example 2026-09-15
3212
3235
  openIssuesRange?: string; // @example 1-5
3213
- overallStatus?: string; // @example compliant
3236
+ overallStatus?: string; // compliant / non_compliant / not_assessed / evaluation_error | @example compliant
3214
3237
  soxCompliant?: boolean; // @example True
3215
3238
  }
3216
3239
 
@@ -3565,12 +3588,14 @@ export interface ScoreSnapshotItem {
3565
3588
  }
3566
3589
 
3567
3590
  export interface SecurityScore {
3591
+ assessmentCoverage?: AssessmentCoveragePublic;
3568
3592
  calculatedAt?: string; // @example 2026-05-09T12:00:00Z
3569
3593
  dimensions?: SecurityScoreDim[];
3570
3594
  grade?: string; // @example A
3571
3595
  maxScore?: number; // @example 100
3572
- methodologyVersion?: string; // @example v1.0
3596
+ methodologyVersion?: string; // @example assessment-v2
3573
3597
  overallScore?: number; // @example 94
3598
+ scoreAvailable?: boolean; // false = 无任何框架完成评估,overallScore 无意义 | @example True
3574
3599
  }
3575
3600
 
3576
3601
  export interface SecurityScoreDim {
@@ -1,7 +1,7 @@
1
1
  // @vitest-environment jsdom
2
2
  import React from 'react';
3
3
  import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
4
- import { renderHook, cleanup } from '@testing-library/react';
4
+ import { renderHook, cleanup, waitFor } from '@testing-library/react';
5
5
  import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
6
6
 
7
7
  // ============================================================
@@ -10,11 +10,12 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
10
10
  // 而 RequireAuth(唯一登录交棒点)在 Outlet 内 ⇒ 无会话冷启动永停 Spin。
11
11
  // ============================================================
12
12
 
13
- const { mockQueries } = vi.hoisted(() => ({
13
+ const { mockQueries, mockProfileAvatar } = vi.hoisted(() => ({
14
14
  mockQueries: {
15
15
  perms: { data: undefined as unknown, isError: false },
16
16
  tenants: { data: undefined as unknown, isError: false },
17
17
  },
18
+ mockProfileAvatar: vi.fn(),
18
19
  }));
19
20
 
20
21
  vi.mock('../../auth/auth-queries', () => ({
@@ -22,6 +23,11 @@ vi.mock('../../auth/auth-queries', () => ({
22
23
  useTenantsQuery: () => mockQueries.tenants,
23
24
  }));
24
25
 
26
+ // U447:启动补读头像走生成客户端(动态 import);此处拦截模块,避免测试打真实网络。
27
+ vi.mock('../../generated/api', () => ({
28
+ profileAvatar: mockProfileAvatar,
29
+ }));
30
+
25
31
  import { useBootstrap } from '../useBootstrap';
26
32
  import { useAuthStore } from '../../auth/store';
27
33
 
@@ -50,6 +56,8 @@ describe('useBootstrap(F-W5b 无会话语义)', () => {
50
56
  resetAuthStore();
51
57
  mockQueries.perms = { data: undefined, isError: false };
52
58
  mockQueries.tenants = { data: undefined, isError: false };
59
+ mockProfileAvatar.mockReset();
60
+ mockProfileAvatar.mockResolvedValue({});
53
61
  });
54
62
 
55
63
  afterEach(() => {
@@ -88,4 +96,33 @@ describe('useBootstrap(F-W5b 无会话语义)', () => {
88
96
  const { result } = renderHook(() => useBootstrap(), { wrapper });
89
97
  expect(result.current).toBe('loading');
90
98
  });
99
+
100
+ // U447:登录响应与 /auth/me 都不含 avatarUrl —— 启动必须补读一次 profile 头像,
101
+ // 否则换浏览器/新登录后顶栏头像恒为首字母。
102
+ it('U447:有会话 → 补读 /profile/avatar 并写回认证快照', async () => {
103
+ useAuthStore.setState({
104
+ accessToken: 'token-xyz',
105
+ user: { id: 'u-1', username: 'demo', email: 'demo@example.com', status: 'active' },
106
+ permissions: ['user.read'],
107
+ tenants: [{ id: 't1', name: 'demo', role: 'member' }],
108
+ });
109
+ mockProfileAvatar.mockResolvedValue({ avatarUrl: 'https://cdn.example.com/a.png' });
110
+ renderHook(() => useBootstrap(), { wrapper });
111
+ await waitFor(() =>
112
+ expect(useAuthStore.getState().user?.avatarUrl).toBe('https://cdn.example.com/a.png'),
113
+ );
114
+ });
115
+
116
+ it('U447:头像接口失败/为空 → 快照保持原样(展示层回落首字母)', async () => {
117
+ useAuthStore.setState({
118
+ accessToken: 'token-xyz',
119
+ user: { id: 'u-1', username: 'demo', email: 'demo@example.com', status: 'active' },
120
+ permissions: ['user.read'],
121
+ tenants: [{ id: 't1', name: 'demo', role: 'member' }],
122
+ });
123
+ mockProfileAvatar.mockRejectedValue(new Error('profile down'));
124
+ renderHook(() => useBootstrap(), { wrapper });
125
+ await waitFor(() => expect(mockProfileAvatar).toHaveBeenCalled());
126
+ expect(useAuthStore.getState().user?.avatarUrl).toBeUndefined();
127
+ });
91
128
  });
@@ -59,6 +59,10 @@ function shallowEqual(a: AuthSnapshot, b: AuthSnapshot): boolean {
59
59
  if (a.currentTenantId !== b.currentTenantId) return false;
60
60
  if (a.user?.username !== b.user?.username) return false;
61
61
  if (a.user?.email !== b.user?.email) return false;
62
+ // 头像/展示名可在登录后原地更新(个人资料页上传头像、magic-link 补全资料):
63
+ // 不参与比较时快照被判定"未变化",顶部头像会停在旧值直到刷新页面。
64
+ if (a.user?.avatarUrl !== b.user?.avatarUrl) return false;
65
+ if (a.user?.displayName !== b.user?.displayName) return false;
62
66
  if (a.permissions.length !== b.permissions.length) return false;
63
67
  if (a.tenants.length !== b.tenants.length) return false;
64
68
  // 简化的数组比较:内容变更时下个事件循环会触发重新计算
@@ -46,6 +46,33 @@ export function useBootstrap(): BootstrapState {
46
46
  }
47
47
  }, [tentsQuery.data]);
48
48
 
49
+ // U447:头像补读 —— 登录响应与 /auth/me 都不含 avatarUrl(头像存在 profile 服务),
50
+ // 认证快照里的头像只在上传当次或同浏览器持久化后才有值。启动时补读一次
51
+ // GET /profile/avatar 并写回快照,顶栏 UserMenu 才能显示账号真实头像;
52
+ // 失败静默(展示层回落首字母,不影响任何业务流程)。
53
+ // 用动态 import 引生成客户端:generated/api 反向 import '@autional/shared',
54
+ // 静态导入会在包入口形成循环,动态导入等入口初始化完再求值。
55
+ useEffect(() => {
56
+ if (!hasToken) return;
57
+ let cancelled = false;
58
+ void import('../generated/api')
59
+ .then(({ profileAvatar }) => profileAvatar())
60
+ .then((res) => {
61
+ const avatarUrl = ((res as { avatarUrl?: string } | undefined)?.avatarUrl ?? '').trim();
62
+ if (cancelled || !avatarUrl) return;
63
+ const current = useAuthStore.getState().user;
64
+ if (current && current.avatarUrl !== avatarUrl) {
65
+ useAuthStore.getState().setUser({ ...current, avatarUrl });
66
+ }
67
+ })
68
+ .catch(() => {
69
+ /* 忽略:缺头像不影响启动 */
70
+ });
71
+ return () => {
72
+ cancelled = true;
73
+ };
74
+ }, [hasToken]);
75
+
49
76
  // 无会话 = 无数据可引导:返回 'idle',登录交棒由 RequireAuth/守卫决策(见上)。
50
77
  // 不得返回 'loading':Layout 消费者(platform-console)以 bootstrap==='loading'
51
78
  // 挡 Outlet,而 RequireAuth 在 Outlet 里 ⇒ 无会话冷启动永停 Spin、守卫永不挂载(F-W5b)。
package/src/index.ts CHANGED
@@ -248,6 +248,7 @@ export type { PaginationInfo, ListResult } from './utils/response';
248
248
 
249
249
  export {
250
250
  EMPTY_STATE_CODES,
251
+ ERROR_CODES,
251
252
  ERROR_SEMANTICS,
252
253
  ERROR_SEMANTICS_META,
253
254
  MAX_RETRIES,
@@ -262,4 +263,4 @@ export {
262
263
  semanticsOfError,
263
264
  shouldRetry,
264
265
  } from './utils/error-semantics';
265
- export type { Absence, ErrorClass, ErrorSemantics, UIObservation } from './utils/error-semantics';
266
+ export type { Absence, ErrorClass, ErrorCodeName, ErrorSemantics, UIObservation } from './utils/error-semantics';
@@ -16,14 +16,21 @@
16
16
  import { getHttpStatus, isForbiddenError, isRetryableError } from './query-state';
17
17
  import {
18
18
  EMPTY_STATE_CODES,
19
+ ERROR_CODES,
19
20
  ERROR_SEMANTICS,
20
21
  ERROR_SEMANTICS_META,
21
22
  UNPROVISIONED_CODES,
22
23
  } from '../generated/error-semantics';
23
- import type { Absence, ErrorClass, ErrorSemantics, UIObservation } from '../generated/error-semantics';
24
+ import type {
25
+ Absence,
26
+ ErrorClass,
27
+ ErrorCodeName,
28
+ ErrorSemantics,
29
+ UIObservation,
30
+ } from '../generated/error-semantics';
24
31
 
25
- export { EMPTY_STATE_CODES, ERROR_SEMANTICS, ERROR_SEMANTICS_META, UNPROVISIONED_CODES };
26
- export type { Absence, ErrorClass, ErrorSemantics, UIObservation };
32
+ export { EMPTY_STATE_CODES, ERROR_CODES, ERROR_SEMANTICS, ERROR_SEMANTICS_META, UNPROVISIONED_CODES };
33
+ export type { Absence, ErrorClass, ErrorCodeName, ErrorSemantics, UIObservation };
27
34
  export { getHttpStatus, isForbiddenError, isRetryableError };
28
35
 
29
36
  /** 单次查询最多重试次数(策略参数,唯一出处)。 */