@h-ai/iam 0.1.0-alpha5
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/LICENSE +202 -0
- package/README.md +513 -0
- package/dist/api/index.d.ts +746 -0
- package/dist/api/index.js +3 -0
- package/dist/api/index.js.map +1 -0
- package/dist/browser.d.ts +2 -0
- package/dist/browser.js +3 -0
- package/dist/browser.js.map +1 -0
- package/dist/chunk-CBZ4LHKU.js +374 -0
- package/dist/chunk-CBZ4LHKU.js.map +1 -0
- package/dist/index.d.ts +1394 -0
- package/dist/index.js +4762 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1394 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import * as _h_ai_core from '@h-ai/core';
|
|
3
|
+
import { HaiResult, PaginationOptionsInput, PaginatedResult } from '@h-ai/core';
|
|
4
|
+
import { DmlWithTxOperations } from '@h-ai/reldb';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @h-ai/iam — 配置 Schema
|
|
8
|
+
*
|
|
9
|
+
* 本文件定义 IAM 模块的配置结构,使用 Zod 进行运行时校验。
|
|
10
|
+
* @module iam-config
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 认证策略类型
|
|
15
|
+
*
|
|
16
|
+
* 支持的认证方式:
|
|
17
|
+
* - `password` - 用户名/邮箱 + 密码
|
|
18
|
+
* - `otp` - 邮箱/短信 + 验证码
|
|
19
|
+
* - `ldap` - LDAP 目录认证
|
|
20
|
+
*/
|
|
21
|
+
declare const AuthStrategyTypeSchema: z.ZodEnum<{
|
|
22
|
+
password: "password";
|
|
23
|
+
otp: "otp";
|
|
24
|
+
ldap: "ldap";
|
|
25
|
+
apikey: "apikey";
|
|
26
|
+
}>;
|
|
27
|
+
/** 认证策略类型 */
|
|
28
|
+
type AuthStrategyType = z.infer<typeof AuthStrategyTypeSchema>;
|
|
29
|
+
/**
|
|
30
|
+
* 密码配置 Schema
|
|
31
|
+
*/
|
|
32
|
+
declare const PasswordConfigSchema: z.ZodObject<{
|
|
33
|
+
minLength: z.ZodDefault<z.ZodNumber>;
|
|
34
|
+
maxLength: z.ZodDefault<z.ZodNumber>;
|
|
35
|
+
requireUppercase: z.ZodDefault<z.ZodBoolean>;
|
|
36
|
+
requireLowercase: z.ZodDefault<z.ZodBoolean>;
|
|
37
|
+
requireNumber: z.ZodDefault<z.ZodBoolean>;
|
|
38
|
+
requireSpecialChar: z.ZodDefault<z.ZodBoolean>;
|
|
39
|
+
expirationDays: z.ZodDefault<z.ZodNumber>;
|
|
40
|
+
}, z.core.$strip>;
|
|
41
|
+
/** 密码配置类型 */
|
|
42
|
+
type PasswordConfig = z.infer<typeof PasswordConfigSchema>;
|
|
43
|
+
/**
|
|
44
|
+
* OTP 配置 Schema
|
|
45
|
+
*/
|
|
46
|
+
declare const OtpConfigSchema: z.ZodObject<{
|
|
47
|
+
length: z.ZodDefault<z.ZodNumber>;
|
|
48
|
+
expiresIn: z.ZodDefault<z.ZodNumber>;
|
|
49
|
+
maxAttempts: z.ZodDefault<z.ZodNumber>;
|
|
50
|
+
resendInterval: z.ZodDefault<z.ZodNumber>;
|
|
51
|
+
}, z.core.$strip>;
|
|
52
|
+
/** OTP 配置类型 */
|
|
53
|
+
type OtpConfig = z.infer<typeof OtpConfigSchema>;
|
|
54
|
+
/**
|
|
55
|
+
* 密码重置配置 Schema
|
|
56
|
+
*/
|
|
57
|
+
declare const PasswordResetConfigSchema: z.ZodObject<{
|
|
58
|
+
tokenExpiresIn: z.ZodDefault<z.ZodNumber>;
|
|
59
|
+
maxAttempts: z.ZodDefault<z.ZodNumber>;
|
|
60
|
+
}, z.core.$strip>;
|
|
61
|
+
/** 密码重置配置类型 */
|
|
62
|
+
type PasswordResetConfig = z.infer<typeof PasswordResetConfigSchema>;
|
|
63
|
+
/**
|
|
64
|
+
* LDAP 配置 Schema
|
|
65
|
+
*/
|
|
66
|
+
declare const LdapConfigSchema: z.ZodObject<{
|
|
67
|
+
url: z.ZodString;
|
|
68
|
+
bindDn: z.ZodString;
|
|
69
|
+
bindPassword: z.ZodString;
|
|
70
|
+
searchBase: z.ZodString;
|
|
71
|
+
searchFilter: z.ZodDefault<z.ZodString>;
|
|
72
|
+
usernameAttribute: z.ZodDefault<z.ZodString>;
|
|
73
|
+
emailAttribute: z.ZodDefault<z.ZodString>;
|
|
74
|
+
displayNameAttribute: z.ZodDefault<z.ZodString>;
|
|
75
|
+
useTls: z.ZodDefault<z.ZodBoolean>;
|
|
76
|
+
connectTimeout: z.ZodDefault<z.ZodNumber>;
|
|
77
|
+
}, z.core.$strip>;
|
|
78
|
+
/** LDAP 配置类型 */
|
|
79
|
+
type LdapConfig = z.infer<typeof LdapConfigSchema>;
|
|
80
|
+
/**
|
|
81
|
+
* API Key 配置 Schema
|
|
82
|
+
*/
|
|
83
|
+
declare const ApiKeyConfigSchema: z.ZodObject<{
|
|
84
|
+
maxKeysPerUser: z.ZodDefault<z.ZodNumber>;
|
|
85
|
+
defaultExpirationDays: z.ZodDefault<z.ZodNumber>;
|
|
86
|
+
prefix: z.ZodDefault<z.ZodString>;
|
|
87
|
+
}, z.core.$strip>;
|
|
88
|
+
/** API Key 配置类型 */
|
|
89
|
+
type ApiKeyConfig = z.infer<typeof ApiKeyConfigSchema>;
|
|
90
|
+
/** 登录类型启用配置 */
|
|
91
|
+
declare const LoginConfigSchema: z.ZodObject<{
|
|
92
|
+
password: z.ZodDefault<z.ZodBoolean>;
|
|
93
|
+
otp: z.ZodDefault<z.ZodBoolean>;
|
|
94
|
+
ldap: z.ZodDefault<z.ZodBoolean>;
|
|
95
|
+
apikey: z.ZodDefault<z.ZodBoolean>;
|
|
96
|
+
}, z.core.$strip>;
|
|
97
|
+
type LoginConfig = z.infer<typeof LoginConfigSchema>;
|
|
98
|
+
/** 注册配置 */
|
|
99
|
+
declare const RegisterConfigSchema: z.ZodObject<{
|
|
100
|
+
enabled: z.ZodDefault<z.ZodBoolean>;
|
|
101
|
+
defaultEnabled: z.ZodDefault<z.ZodBoolean>;
|
|
102
|
+
}, z.core.$strip>;
|
|
103
|
+
type RegisterConfig = z.infer<typeof RegisterConfigSchema>;
|
|
104
|
+
/** 安全策略配置 */
|
|
105
|
+
declare const SecurityConfigSchema: z.ZodObject<{
|
|
106
|
+
maxLoginAttempts: z.ZodDefault<z.ZodNumber>;
|
|
107
|
+
lockoutDuration: z.ZodDefault<z.ZodNumber>;
|
|
108
|
+
}, z.core.$strip>;
|
|
109
|
+
type SecurityConfig = z.infer<typeof SecurityConfigSchema>;
|
|
110
|
+
/** 协议展示配置 */
|
|
111
|
+
declare const AgreementConfigSchema: z.ZodObject<{
|
|
112
|
+
userAgreementUrl: z.ZodOptional<z.ZodURL>;
|
|
113
|
+
privacyPolicyUrl: z.ZodOptional<z.ZodURL>;
|
|
114
|
+
showOnRegister: z.ZodDefault<z.ZodBoolean>;
|
|
115
|
+
showOnLogin: z.ZodDefault<z.ZodBoolean>;
|
|
116
|
+
}, z.core.$strip>;
|
|
117
|
+
type AgreementConfig = z.infer<typeof AgreementConfigSchema>;
|
|
118
|
+
/**
|
|
119
|
+
* 会话配置 Schema
|
|
120
|
+
*/
|
|
121
|
+
declare const SessionConfigSchema: z.ZodObject<{
|
|
122
|
+
maxAge: z.ZodDefault<z.ZodNumber>;
|
|
123
|
+
sliding: z.ZodDefault<z.ZodBoolean>;
|
|
124
|
+
singleDevice: z.ZodDefault<z.ZodBoolean>;
|
|
125
|
+
refreshTokenMaxAge: z.ZodDefault<z.ZodNumber>;
|
|
126
|
+
}, z.core.$strip>;
|
|
127
|
+
/** 会话配置类型 */
|
|
128
|
+
type SessionConfig = z.infer<typeof SessionConfigSchema>;
|
|
129
|
+
/**
|
|
130
|
+
* RBAC 配置 Schema
|
|
131
|
+
*/
|
|
132
|
+
declare const RbacConfigSchema: z.ZodObject<{
|
|
133
|
+
enabled: z.ZodDefault<z.ZodBoolean>;
|
|
134
|
+
superAdminRole: z.ZodDefault<z.ZodString>;
|
|
135
|
+
defaultRole: z.ZodDefault<z.ZodString>;
|
|
136
|
+
}, z.core.$strip>;
|
|
137
|
+
/** RBAC 配置类型 */
|
|
138
|
+
type RbacConfig = z.infer<typeof RbacConfigSchema>;
|
|
139
|
+
/**
|
|
140
|
+
* IAM 统一配置 Schema
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* const config: IamConfig = {
|
|
145
|
+
* password: { minLength: 8 },
|
|
146
|
+
* session: {
|
|
147
|
+
* maxAge: 86400,
|
|
148
|
+
* sliding: true
|
|
149
|
+
* },
|
|
150
|
+
* login: { password: true, otp: true },
|
|
151
|
+
* register: { enabled: true, defaultEnabled: true },
|
|
152
|
+
* rbac: { enabled: true }
|
|
153
|
+
* }
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
declare const IamConfigSchema: z.ZodObject<{
|
|
157
|
+
password: z.ZodOptional<z.ZodObject<{
|
|
158
|
+
minLength: z.ZodDefault<z.ZodNumber>;
|
|
159
|
+
maxLength: z.ZodDefault<z.ZodNumber>;
|
|
160
|
+
requireUppercase: z.ZodDefault<z.ZodBoolean>;
|
|
161
|
+
requireLowercase: z.ZodDefault<z.ZodBoolean>;
|
|
162
|
+
requireNumber: z.ZodDefault<z.ZodBoolean>;
|
|
163
|
+
requireSpecialChar: z.ZodDefault<z.ZodBoolean>;
|
|
164
|
+
expirationDays: z.ZodDefault<z.ZodNumber>;
|
|
165
|
+
}, z.core.$strip>>;
|
|
166
|
+
otp: z.ZodOptional<z.ZodObject<{
|
|
167
|
+
length: z.ZodDefault<z.ZodNumber>;
|
|
168
|
+
expiresIn: z.ZodDefault<z.ZodNumber>;
|
|
169
|
+
maxAttempts: z.ZodDefault<z.ZodNumber>;
|
|
170
|
+
resendInterval: z.ZodDefault<z.ZodNumber>;
|
|
171
|
+
}, z.core.$strip>>;
|
|
172
|
+
ldap: z.ZodOptional<z.ZodObject<{
|
|
173
|
+
url: z.ZodString;
|
|
174
|
+
bindDn: z.ZodString;
|
|
175
|
+
bindPassword: z.ZodString;
|
|
176
|
+
searchBase: z.ZodString;
|
|
177
|
+
searchFilter: z.ZodDefault<z.ZodString>;
|
|
178
|
+
usernameAttribute: z.ZodDefault<z.ZodString>;
|
|
179
|
+
emailAttribute: z.ZodDefault<z.ZodString>;
|
|
180
|
+
displayNameAttribute: z.ZodDefault<z.ZodString>;
|
|
181
|
+
useTls: z.ZodDefault<z.ZodBoolean>;
|
|
182
|
+
connectTimeout: z.ZodDefault<z.ZodNumber>;
|
|
183
|
+
}, z.core.$strip>>;
|
|
184
|
+
apikey: z.ZodOptional<z.ZodObject<{
|
|
185
|
+
maxKeysPerUser: z.ZodDefault<z.ZodNumber>;
|
|
186
|
+
defaultExpirationDays: z.ZodDefault<z.ZodNumber>;
|
|
187
|
+
prefix: z.ZodDefault<z.ZodString>;
|
|
188
|
+
}, z.core.$strip>>;
|
|
189
|
+
passwordReset: z.ZodOptional<z.ZodObject<{
|
|
190
|
+
tokenExpiresIn: z.ZodDefault<z.ZodNumber>;
|
|
191
|
+
maxAttempts: z.ZodDefault<z.ZodNumber>;
|
|
192
|
+
}, z.core.$strip>>;
|
|
193
|
+
login: z.ZodDefault<z.ZodObject<{
|
|
194
|
+
password: z.ZodDefault<z.ZodBoolean>;
|
|
195
|
+
otp: z.ZodDefault<z.ZodBoolean>;
|
|
196
|
+
ldap: z.ZodDefault<z.ZodBoolean>;
|
|
197
|
+
apikey: z.ZodDefault<z.ZodBoolean>;
|
|
198
|
+
}, z.core.$strip>>;
|
|
199
|
+
register: z.ZodDefault<z.ZodObject<{
|
|
200
|
+
enabled: z.ZodDefault<z.ZodBoolean>;
|
|
201
|
+
defaultEnabled: z.ZodDefault<z.ZodBoolean>;
|
|
202
|
+
}, z.core.$strip>>;
|
|
203
|
+
agreements: z.ZodDefault<z.ZodObject<{
|
|
204
|
+
userAgreementUrl: z.ZodOptional<z.ZodURL>;
|
|
205
|
+
privacyPolicyUrl: z.ZodOptional<z.ZodURL>;
|
|
206
|
+
showOnRegister: z.ZodDefault<z.ZodBoolean>;
|
|
207
|
+
showOnLogin: z.ZodDefault<z.ZodBoolean>;
|
|
208
|
+
}, z.core.$strip>>;
|
|
209
|
+
security: z.ZodDefault<z.ZodObject<{
|
|
210
|
+
maxLoginAttempts: z.ZodDefault<z.ZodNumber>;
|
|
211
|
+
lockoutDuration: z.ZodDefault<z.ZodNumber>;
|
|
212
|
+
}, z.core.$strip>>;
|
|
213
|
+
session: z.ZodOptional<z.ZodObject<{
|
|
214
|
+
maxAge: z.ZodDefault<z.ZodNumber>;
|
|
215
|
+
sliding: z.ZodDefault<z.ZodBoolean>;
|
|
216
|
+
singleDevice: z.ZodDefault<z.ZodBoolean>;
|
|
217
|
+
refreshTokenMaxAge: z.ZodDefault<z.ZodNumber>;
|
|
218
|
+
}, z.core.$strip>>;
|
|
219
|
+
rbac: z.ZodOptional<z.ZodObject<{
|
|
220
|
+
enabled: z.ZodDefault<z.ZodBoolean>;
|
|
221
|
+
superAdminRole: z.ZodDefault<z.ZodString>;
|
|
222
|
+
defaultRole: z.ZodDefault<z.ZodString>;
|
|
223
|
+
}, z.core.$strip>>;
|
|
224
|
+
seedDefaultData: z.ZodDefault<z.ZodBoolean>;
|
|
225
|
+
}, z.core.$strip>;
|
|
226
|
+
/** IAM 配置类型 */
|
|
227
|
+
type IamConfig = z.infer<typeof IamConfigSchema>;
|
|
228
|
+
/**
|
|
229
|
+
* IAM 配置设置输入类型(仅设置字段,不含运行时依赖)
|
|
230
|
+
*
|
|
231
|
+
* 说明:Zod 的 default 会让输入端字段可省略,但输出端字段为必填。
|
|
232
|
+
*/
|
|
233
|
+
type IamConfigSettingsInput = z.input<typeof IamConfigSchema>;
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* @h-ai/iam — API Key 类型定义
|
|
237
|
+
*
|
|
238
|
+
* 包含 API Key 实体、创建选项、查询选项等类型。
|
|
239
|
+
* @module iam-authn-apikey-types
|
|
240
|
+
*/
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* API Key 实体(对外展示)
|
|
244
|
+
*
|
|
245
|
+
* 不含密钥哈希等敏感信息。
|
|
246
|
+
*/
|
|
247
|
+
interface ApiKey {
|
|
248
|
+
/** API Key ID */
|
|
249
|
+
id: string;
|
|
250
|
+
/** 所属用户 ID */
|
|
251
|
+
userId: string;
|
|
252
|
+
/** API Key 名称(用户自定义标识) */
|
|
253
|
+
name: string;
|
|
254
|
+
/** 密钥前缀(用于展示,如 'hai_abc1****') */
|
|
255
|
+
keyPrefix: string;
|
|
256
|
+
/** 是否启用 */
|
|
257
|
+
enabled: boolean;
|
|
258
|
+
/** 过期时间(null 表示永不过期) */
|
|
259
|
+
expiresAt: Date | null;
|
|
260
|
+
/** 创建时间 */
|
|
261
|
+
createdAt: Date;
|
|
262
|
+
/** 最后使用时间 */
|
|
263
|
+
lastUsedAt: Date | null;
|
|
264
|
+
/** 权限范围 */
|
|
265
|
+
scopes: string[];
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* 创建 API Key 的返回结果
|
|
269
|
+
*
|
|
270
|
+
* 仅在创建时返回明文密钥,之后无法再获取。
|
|
271
|
+
*/
|
|
272
|
+
interface CreateApiKeyResult {
|
|
273
|
+
/** API Key 元数据 */
|
|
274
|
+
apiKey: ApiKey;
|
|
275
|
+
/** 明文密钥(仅此一次展示) */
|
|
276
|
+
rawKey: string;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* 创建 API Key 选项
|
|
280
|
+
*/
|
|
281
|
+
interface CreateApiKeyOptions {
|
|
282
|
+
/** API Key 名称 */
|
|
283
|
+
name: string;
|
|
284
|
+
/** 有效期天数(0 或不传表示永不过期) */
|
|
285
|
+
expirationDays?: number;
|
|
286
|
+
/** 权限范围 */
|
|
287
|
+
scopes?: string[];
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* API Key 管理操作接口
|
|
291
|
+
*/
|
|
292
|
+
interface ApiKeyOperations {
|
|
293
|
+
/**
|
|
294
|
+
* 创建 API Key
|
|
295
|
+
*
|
|
296
|
+
* @param userId - 用户 ID
|
|
297
|
+
* @param options - 创建选项
|
|
298
|
+
* @returns 成功返回 API Key 元数据和明文密钥
|
|
299
|
+
*/
|
|
300
|
+
createApiKey: (userId: string, options: CreateApiKeyOptions) => Promise<HaiResult<CreateApiKeyResult>>;
|
|
301
|
+
/**
|
|
302
|
+
* 列出用户的所有 API Key
|
|
303
|
+
*
|
|
304
|
+
* @param userId - 用户 ID
|
|
305
|
+
* @returns API Key 列表(不含密钥哈希)
|
|
306
|
+
*/
|
|
307
|
+
listApiKeys: (userId: string) => Promise<HaiResult<ApiKey[]>>;
|
|
308
|
+
/**
|
|
309
|
+
* 获取 API Key 详情
|
|
310
|
+
*
|
|
311
|
+
* @param keyId - API Key ID
|
|
312
|
+
* @returns API Key 详情
|
|
313
|
+
*/
|
|
314
|
+
getApiKey: (keyId: string) => Promise<HaiResult<ApiKey | null>>;
|
|
315
|
+
/**
|
|
316
|
+
* 吊销/删除 API Key
|
|
317
|
+
*
|
|
318
|
+
* @param keyId - API Key ID
|
|
319
|
+
* @returns 操作结果
|
|
320
|
+
*/
|
|
321
|
+
revokeApiKey: (keyId: string) => Promise<HaiResult<void>>;
|
|
322
|
+
/**
|
|
323
|
+
* 验证 API Key 并返回关联用户 ID
|
|
324
|
+
*
|
|
325
|
+
* @param rawKey - 明文 API Key
|
|
326
|
+
* @returns 成功返回 API Key 实体(含用户 ID),失败返回错误码
|
|
327
|
+
*/
|
|
328
|
+
verifyApiKey: (rawKey: string) => Promise<HaiResult<ApiKey>>;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* @h-ai/iam — 授权子功能类型定义(RBAC)
|
|
333
|
+
*
|
|
334
|
+
* @h-ai/iam — 授权子功能类型定义(RBAC)
|
|
335
|
+
* @module iam-authz-types
|
|
336
|
+
*/
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* 权限类型
|
|
340
|
+
*
|
|
341
|
+
* - menu:菜单/导航可见性控制
|
|
342
|
+
* - api:接口级访问控制
|
|
343
|
+
* - button:操作按钮显隐控制
|
|
344
|
+
*/
|
|
345
|
+
type PermissionType = 'menu' | 'api' | 'button';
|
|
346
|
+
/**
|
|
347
|
+
* 权限查询参数
|
|
348
|
+
*
|
|
349
|
+
* 扩展分页参数,支持按类型和关键字筛选。
|
|
350
|
+
*/
|
|
351
|
+
interface PermissionQueryOptions extends PaginationOptionsInput {
|
|
352
|
+
/** 按权限类型筛选 */
|
|
353
|
+
type?: PermissionType;
|
|
354
|
+
/** 按关键字搜索(匹配 code 或 name) */
|
|
355
|
+
search?: string;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* 权限
|
|
359
|
+
*/
|
|
360
|
+
interface Permission {
|
|
361
|
+
/** 权限 ID */
|
|
362
|
+
id: string;
|
|
363
|
+
/** 权限代码(如 users:read, posts:write) */
|
|
364
|
+
code: string;
|
|
365
|
+
/** 权限名称 */
|
|
366
|
+
name: string;
|
|
367
|
+
/** 权限描述 */
|
|
368
|
+
description?: string;
|
|
369
|
+
/** 权限类型(menu / api / button) */
|
|
370
|
+
type?: PermissionType;
|
|
371
|
+
/** 资源类型 */
|
|
372
|
+
resource?: string;
|
|
373
|
+
/** 操作类型(create/read/update/delete) */
|
|
374
|
+
action?: string;
|
|
375
|
+
/** 创建时间 */
|
|
376
|
+
createdAt: Date;
|
|
377
|
+
/** 更新时间 */
|
|
378
|
+
updatedAt: Date;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* 角色
|
|
382
|
+
*/
|
|
383
|
+
interface Role {
|
|
384
|
+
/** 角色 ID */
|
|
385
|
+
id: string;
|
|
386
|
+
/** 角色代码 */
|
|
387
|
+
code: string;
|
|
388
|
+
/** 角色名称 */
|
|
389
|
+
name: string;
|
|
390
|
+
/** 角色描述 */
|
|
391
|
+
description?: string;
|
|
392
|
+
/** 是否系统角色(不可删除) */
|
|
393
|
+
isSystem?: boolean;
|
|
394
|
+
/** 创建时间 */
|
|
395
|
+
createdAt: Date;
|
|
396
|
+
/** 更新时间 */
|
|
397
|
+
updatedAt: Date;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* 授权子功能接口
|
|
401
|
+
*/
|
|
402
|
+
interface AuthzOperations {
|
|
403
|
+
/**
|
|
404
|
+
* 检查权限
|
|
405
|
+
*
|
|
406
|
+
* 超级管理员角色自动拥有所有权限。
|
|
407
|
+
* 支持通配符匹配(如 `user:*` 匹配 `user:read`)。
|
|
408
|
+
*
|
|
409
|
+
* **数据来源**:角色列表和权限代码均从数据库实时查询。
|
|
410
|
+
*
|
|
411
|
+
* @param userId - 用户 ID
|
|
412
|
+
* @param permission - 权限代码(如 `user:read`)
|
|
413
|
+
* @returns 成功返回 true/false
|
|
414
|
+
*/
|
|
415
|
+
checkPermission: (userId: string, permission: string) => Promise<HaiResult<boolean>>;
|
|
416
|
+
/**
|
|
417
|
+
* 获取用户权限列表
|
|
418
|
+
*
|
|
419
|
+
* 通过用户角色聚合所有权限(自动去重)。
|
|
420
|
+
*
|
|
421
|
+
* **数据来源**:直接查询数据库,返回最新数据。
|
|
422
|
+
*
|
|
423
|
+
* @param userId - 用户 ID
|
|
424
|
+
* @returns 成功返回去重后的权限列表
|
|
425
|
+
*/
|
|
426
|
+
getUserPermissions: (userId: string) => Promise<HaiResult<Permission[]>>;
|
|
427
|
+
/**
|
|
428
|
+
* 获取用户角色列表
|
|
429
|
+
*
|
|
430
|
+
* **数据来源**:直接查询数据库,返回最新数据。
|
|
431
|
+
*
|
|
432
|
+
* @param userId - 用户 ID
|
|
433
|
+
* @returns 成功返回角色列表
|
|
434
|
+
*/
|
|
435
|
+
getUserRoles: (userId: string) => Promise<HaiResult<Role[]>>;
|
|
436
|
+
/**
|
|
437
|
+
* 分配角色给用户
|
|
438
|
+
*
|
|
439
|
+
* @param userId - 用户 ID
|
|
440
|
+
* @param roleId - 角色 ID
|
|
441
|
+
* @returns 成功返回 ok;角色不存在返回 ROLE_NOT_FOUND
|
|
442
|
+
*/
|
|
443
|
+
assignRole: (userId: string, roleId: string, tx?: DmlWithTxOperations) => Promise<HaiResult<void>>;
|
|
444
|
+
/**
|
|
445
|
+
* 移除用户角色
|
|
446
|
+
*
|
|
447
|
+
* @param userId - 用户 ID
|
|
448
|
+
* @param roleId - 角色 ID
|
|
449
|
+
* @returns 成功返回 ok
|
|
450
|
+
*/
|
|
451
|
+
removeRole: (userId: string, roleId: string, tx?: DmlWithTxOperations) => Promise<HaiResult<void>>;
|
|
452
|
+
/**
|
|
453
|
+
* 同步用户角色(替换为目标角色列表)
|
|
454
|
+
*
|
|
455
|
+
* 自动计算当前角色与目标角色的差集,批量移除多余角色、添加缺失角色,
|
|
456
|
+
* 最终同步一次会话权限。若目标与当前一致则跳过操作。
|
|
457
|
+
*
|
|
458
|
+
* @param userId - 用户 ID
|
|
459
|
+
* @param roleIds - 目标角色 ID 列表
|
|
460
|
+
* @returns 成功返回 ok
|
|
461
|
+
*/
|
|
462
|
+
syncRoles: (userId: string, roleIds: string[], tx?: DmlWithTxOperations) => Promise<HaiResult<void>>;
|
|
463
|
+
/**
|
|
464
|
+
* 创建角色
|
|
465
|
+
*
|
|
466
|
+
* @param role - 角色数据(code、name、description、isSystem)
|
|
467
|
+
* @returns 成功返回创建的角色(含 id 和时间戳)
|
|
468
|
+
*/
|
|
469
|
+
createRole: (role: Omit<Role, 'id' | 'createdAt' | 'updatedAt'>, tx?: DmlWithTxOperations) => Promise<HaiResult<Role>>;
|
|
470
|
+
/**
|
|
471
|
+
* 获取角色
|
|
472
|
+
*
|
|
473
|
+
* @param roleId - 角色 ID
|
|
474
|
+
* @returns 成功返回角色或 null(不存在时)
|
|
475
|
+
*/
|
|
476
|
+
getRole: (roleId: string) => Promise<HaiResult<Role | null>>;
|
|
477
|
+
/**
|
|
478
|
+
* 根据角色代码获取角色
|
|
479
|
+
*
|
|
480
|
+
* @param code - 角色代码(如 'admin'、'user')
|
|
481
|
+
* @returns 成功返回角色或 null(不存在时)
|
|
482
|
+
*/
|
|
483
|
+
getRoleByCode: (code: string) => Promise<HaiResult<Role | null>>;
|
|
484
|
+
/**
|
|
485
|
+
* 获取所有角色(分页)
|
|
486
|
+
*
|
|
487
|
+
* @param options - 分页参数,可选
|
|
488
|
+
* @returns 成功返回分页角色列表
|
|
489
|
+
*/
|
|
490
|
+
getAllRoles: (options?: PaginationOptionsInput) => Promise<HaiResult<PaginatedResult<Role>>>;
|
|
491
|
+
/**
|
|
492
|
+
* 更新角色
|
|
493
|
+
*
|
|
494
|
+
* @param roleId - 角色 ID
|
|
495
|
+
* @param data - 要更新的字段(name、description 等)
|
|
496
|
+
* @returns 成功返回更新后的角色;角色不存在返回 ROLE_NOT_FOUND
|
|
497
|
+
*/
|
|
498
|
+
updateRole: (roleId: string, data: Partial<Omit<Role, 'id' | 'createdAt' | 'updatedAt'>>, tx?: DmlWithTxOperations) => Promise<HaiResult<Role>>;
|
|
499
|
+
/**
|
|
500
|
+
* 删除角色
|
|
501
|
+
*
|
|
502
|
+
* 级联删除用户-角色、角色-权限关联,并同步受影响用户的会话。
|
|
503
|
+
*
|
|
504
|
+
* @param roleId - 角色 ID
|
|
505
|
+
* @returns 成功返回 ok;角色不存在返回 ROLE_NOT_FOUND
|
|
506
|
+
*/
|
|
507
|
+
deleteRole: (roleId: string, tx?: DmlWithTxOperations) => Promise<HaiResult<void>>;
|
|
508
|
+
/**
|
|
509
|
+
* 创建权限
|
|
510
|
+
*
|
|
511
|
+
* @param permission - 权限数据(code、name、resource、action)
|
|
512
|
+
* @returns 成功返回创建的权限(含 id 和时间戳)
|
|
513
|
+
*/
|
|
514
|
+
createPermission: (permission: Omit<Permission, 'id' | 'createdAt' | 'updatedAt'>, tx?: DmlWithTxOperations) => Promise<HaiResult<Permission>>;
|
|
515
|
+
/**
|
|
516
|
+
* 获取权限
|
|
517
|
+
*
|
|
518
|
+
* @param permissionId - 权限 ID
|
|
519
|
+
* @returns 成功返回权限或 null(不存在时)
|
|
520
|
+
*/
|
|
521
|
+
getPermission: (permissionId: string) => Promise<HaiResult<Permission | null>>;
|
|
522
|
+
/**
|
|
523
|
+
* 根据权限代码获取权限
|
|
524
|
+
*
|
|
525
|
+
* @param code - 权限代码(如 `user:read`)
|
|
526
|
+
* @returns 成功返回权限或 null(不存在时)
|
|
527
|
+
*/
|
|
528
|
+
getPermissionByCode: (code: string) => Promise<HaiResult<Permission | null>>;
|
|
529
|
+
/**
|
|
530
|
+
* 获取所有权限(分页)
|
|
531
|
+
*
|
|
532
|
+
* 支持按权限类型(menu/api/button)和关键字(code/name)筛选。
|
|
533
|
+
*
|
|
534
|
+
* @param options - 分页及筛选参数,可选
|
|
535
|
+
* @returns 成功返回分页权限列表
|
|
536
|
+
*/
|
|
537
|
+
getAllPermissions: (options?: PermissionQueryOptions) => Promise<HaiResult<PaginatedResult<Permission>>>;
|
|
538
|
+
/**
|
|
539
|
+
* 删除权限
|
|
540
|
+
*
|
|
541
|
+
* 级联删除角色-权限关联,并同步受影响用户的会话。
|
|
542
|
+
*
|
|
543
|
+
* @param permissionId - 权限 ID
|
|
544
|
+
* @returns 成功返回 ok;权限不存在返回 PERMISSION_NOT_FOUND
|
|
545
|
+
*/
|
|
546
|
+
deletePermission: (permissionId: string, tx?: DmlWithTxOperations) => Promise<HaiResult<void>>;
|
|
547
|
+
/**
|
|
548
|
+
* 为角色分配权限
|
|
549
|
+
*
|
|
550
|
+
* @param roleId - 角色 ID
|
|
551
|
+
* @param permissionId - 权限 ID
|
|
552
|
+
* @returns 成功返回 ok;角色/权限不存在返回对应错误码
|
|
553
|
+
*/
|
|
554
|
+
assignPermissionToRole: (roleId: string, permissionId: string, tx?: DmlWithTxOperations) => Promise<HaiResult<void>>;
|
|
555
|
+
/**
|
|
556
|
+
* 移除角色权限
|
|
557
|
+
*
|
|
558
|
+
* @param roleId - 角色 ID
|
|
559
|
+
* @param permissionId - 权限 ID
|
|
560
|
+
* @returns 成功返回 ok;权限不存在返回 PERMISSION_NOT_FOUND
|
|
561
|
+
*/
|
|
562
|
+
removePermissionFromRole: (roleId: string, permissionId: string, tx?: DmlWithTxOperations) => Promise<HaiResult<void>>;
|
|
563
|
+
/**
|
|
564
|
+
* 获取角色的权限列表
|
|
565
|
+
*
|
|
566
|
+
* **数据来源**:直接查询数据库,返回最新数据。
|
|
567
|
+
*
|
|
568
|
+
* @param roleId - 角色 ID
|
|
569
|
+
* @returns 成功返回角色关联的权限列表
|
|
570
|
+
*/
|
|
571
|
+
getRolePermissions: (roleId: string) => Promise<HaiResult<Permission[]>>;
|
|
572
|
+
/**
|
|
573
|
+
* 批量获取多个用户的角色列表
|
|
574
|
+
*
|
|
575
|
+
* 单次查询替代 N 次 getUserRoles 调用,避免 N+1 问题。
|
|
576
|
+
* 返回 Map:key 为 userId,value 为该用户的角色列表;无角色的用户返回空数组。
|
|
577
|
+
*
|
|
578
|
+
* **数据来源**:直接查询数据库,返回最新数据。
|
|
579
|
+
*
|
|
580
|
+
* @param userIds - 用户 ID 列表
|
|
581
|
+
* @returns Map<userId, Role[]>
|
|
582
|
+
*/
|
|
583
|
+
getUserRolesForMany: (userIds: string[]) => Promise<HaiResult<Map<string, Role[]>>>;
|
|
584
|
+
/**
|
|
585
|
+
* 批量获取多个角色的权限列表
|
|
586
|
+
*
|
|
587
|
+
* 单次查询替代 N 次 getRolePermissions 调用,避免 N+1 问题。
|
|
588
|
+
* 返回 Map:key 为 roleId,value 为该角色的权限列表;无权限的角色返回空数组。
|
|
589
|
+
*
|
|
590
|
+
* **数据来源**:直接查询数据库,返回最新数据。
|
|
591
|
+
*
|
|
592
|
+
* @param roleIds - 角色 ID 列表
|
|
593
|
+
* @returns Map<roleId, Permission[]>
|
|
594
|
+
*/
|
|
595
|
+
getRolePermissionsForMany: (roleIds: string[]) => Promise<HaiResult<Map<string, Permission[]>>>;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* @h-ai/iam — 用户相关类型定义
|
|
600
|
+
*
|
|
601
|
+
* 包含: - 用户基础类型(User、StoredUser) - 注册选项(RegisterOptions) - 协议展示类型(AgreementDisplay) - 用户操作接口(UserOperations)
|
|
602
|
+
* @module iam-user-types
|
|
603
|
+
*/
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* 用户列表查询选项
|
|
607
|
+
*/
|
|
608
|
+
interface ListUsersOptions extends PaginationOptionsInput {
|
|
609
|
+
/** 搜索关键字(模糊匹配用户名、邮箱、手机号、显示名称) */
|
|
610
|
+
search?: string;
|
|
611
|
+
/** 按启用状态过滤,不传则返回全部 */
|
|
612
|
+
enabled?: boolean;
|
|
613
|
+
/** 包含关联数据(如 'roles' 表示同时返回用户角色列表) */
|
|
614
|
+
include?: ('roles')[];
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* 用户基础信息
|
|
618
|
+
*/
|
|
619
|
+
interface User {
|
|
620
|
+
/** 用户 ID */
|
|
621
|
+
id: string;
|
|
622
|
+
/** 用户名 */
|
|
623
|
+
username: string;
|
|
624
|
+
/** 邮箱 */
|
|
625
|
+
email?: string;
|
|
626
|
+
/** 手机号 */
|
|
627
|
+
phone?: string;
|
|
628
|
+
/** 显示名称 */
|
|
629
|
+
displayName?: string;
|
|
630
|
+
/** 头像 URL */
|
|
631
|
+
avatarUrl?: string;
|
|
632
|
+
/** 是否启用 */
|
|
633
|
+
enabled: boolean;
|
|
634
|
+
/** 是否邮箱验证 */
|
|
635
|
+
emailVerified?: boolean;
|
|
636
|
+
/** 是否手机验证 */
|
|
637
|
+
phoneVerified?: boolean;
|
|
638
|
+
/** 创建时间 */
|
|
639
|
+
createdAt: Date;
|
|
640
|
+
/** 更新时间 */
|
|
641
|
+
updatedAt: Date;
|
|
642
|
+
/** 扩展属性 */
|
|
643
|
+
metadata?: Record<string, unknown>;
|
|
644
|
+
/** 用户角色列表(仅在 include: ['roles'] 时返回) */
|
|
645
|
+
roles?: Role[];
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* 用户协议/隐私协议展示配置
|
|
649
|
+
*/
|
|
650
|
+
interface AgreementDisplay {
|
|
651
|
+
/** 用户协议 URL */
|
|
652
|
+
userAgreementUrl?: string;
|
|
653
|
+
/** 隐私协议 URL */
|
|
654
|
+
privacyPolicyUrl?: string;
|
|
655
|
+
/** 注册时展示协议 */
|
|
656
|
+
showOnRegister: boolean;
|
|
657
|
+
/** 登录时展示协议 */
|
|
658
|
+
showOnLogin: boolean;
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* 内部存储用户(包含密码哈希等敏感信息)
|
|
662
|
+
*/
|
|
663
|
+
interface StoredUser extends User {
|
|
664
|
+
/** 密码哈希 */
|
|
665
|
+
passwordHash?: string;
|
|
666
|
+
/** 密码更新时间 */
|
|
667
|
+
passwordUpdatedAt?: Date;
|
|
668
|
+
/** 登录失败次数 */
|
|
669
|
+
loginFailedCount?: number;
|
|
670
|
+
/** 最后登录失败时间 */
|
|
671
|
+
lastLoginFailedAt?: Date;
|
|
672
|
+
/** 锁定截止时间 */
|
|
673
|
+
lockedUntil?: Date;
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* 用户注册选项
|
|
677
|
+
*/
|
|
678
|
+
interface RegisterOptions {
|
|
679
|
+
/** 用户名 */
|
|
680
|
+
username: string;
|
|
681
|
+
/** 邮箱 */
|
|
682
|
+
email?: string;
|
|
683
|
+
/** 手机号 */
|
|
684
|
+
phone?: string;
|
|
685
|
+
/** 密码 */
|
|
686
|
+
password: string;
|
|
687
|
+
/** 显示名称 */
|
|
688
|
+
displayName?: string;
|
|
689
|
+
/** 扩展属性 */
|
|
690
|
+
metadata?: Record<string, unknown>;
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* 注册结果
|
|
694
|
+
*/
|
|
695
|
+
interface RegisterResult {
|
|
696
|
+
/** 用户信息 */
|
|
697
|
+
user: User;
|
|
698
|
+
/** 协议展示信息(可选) */
|
|
699
|
+
agreements?: AgreementDisplay;
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* 当前登录用户可自行修改的字段白名单
|
|
703
|
+
*
|
|
704
|
+
* 仅允许修改个人资料相关字段,禁止修改安全字段(enabled、emailVerified、roles 等)。
|
|
705
|
+
*/
|
|
706
|
+
interface UpdateCurrentUserInput {
|
|
707
|
+
/** 用户名 */
|
|
708
|
+
username?: string;
|
|
709
|
+
/** 邮箱 */
|
|
710
|
+
email?: string;
|
|
711
|
+
/** 显示名称 */
|
|
712
|
+
displayName?: string;
|
|
713
|
+
/** 头像 URL */
|
|
714
|
+
avatarUrl?: string;
|
|
715
|
+
/** 手机号 */
|
|
716
|
+
phone?: string;
|
|
717
|
+
/** 扩展属性 */
|
|
718
|
+
metadata?: Record<string, unknown>;
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* 用户子功能接口
|
|
722
|
+
*/
|
|
723
|
+
interface UserOperations {
|
|
724
|
+
/**
|
|
725
|
+
* 注册用户
|
|
726
|
+
*
|
|
727
|
+
* 校验注册开关、密码强度、用户名/邮箱唯一性后,
|
|
728
|
+
* 在事务中创建用户并分配默认角色。
|
|
729
|
+
*
|
|
730
|
+
* @param options - 注册选项(用户名、密码、邮箱等)
|
|
731
|
+
* @returns 成功返回用户信息及可选的协议展示;失败返回对应错误码
|
|
732
|
+
*/
|
|
733
|
+
register: (options: RegisterOptions) => Promise<HaiResult<RegisterResult>>;
|
|
734
|
+
/**
|
|
735
|
+
* 获取当前用户
|
|
736
|
+
*
|
|
737
|
+
* 通过访问令牌验证会话后查询用户信息。
|
|
738
|
+
*
|
|
739
|
+
* @param accessToken - 访问令牌
|
|
740
|
+
* @returns 成功返回用户信息;令牌无效/过期返回错误
|
|
741
|
+
*/
|
|
742
|
+
getCurrentUser: (accessToken: string) => Promise<HaiResult<User>>;
|
|
743
|
+
/**
|
|
744
|
+
* 更新当前登录用户信息(通过 accessToken 识别用户)
|
|
745
|
+
*
|
|
746
|
+
* 仅允许修改白名单字段(username、email、displayName、avatarUrl、phone、metadata),
|
|
747
|
+
* 禁止修改安全字段(enabled、emailVerified 等)。修改 username/email 时自动校验唯一性。
|
|
748
|
+
*
|
|
749
|
+
* @param accessToken - 访问令牌
|
|
750
|
+
* @param data - 要更新的字段(仅白名单字段)
|
|
751
|
+
* @returns 成功返回更新后的用户信息
|
|
752
|
+
*/
|
|
753
|
+
updateCurrentUser: (accessToken: string, data: UpdateCurrentUserInput) => Promise<HaiResult<User>>;
|
|
754
|
+
/**
|
|
755
|
+
* 获取用户信息
|
|
756
|
+
*
|
|
757
|
+
* @param userId - 用户 ID
|
|
758
|
+
* @param options - 查询选项(可选 include: ['roles'] 同时返回角色列表)
|
|
759
|
+
* @returns 成功返回用户信息或 null(用户不存在时)
|
|
760
|
+
*/
|
|
761
|
+
getUser: (userId: string, options?: {
|
|
762
|
+
include?: ('roles')[];
|
|
763
|
+
}) => Promise<HaiResult<User | null>>;
|
|
764
|
+
/**
|
|
765
|
+
* 获取用户列表(分页 + 搜索 + 过滤)
|
|
766
|
+
*
|
|
767
|
+
* @param options - 查询选项(页码、每页数量、搜索关键字、启用状态过滤)
|
|
768
|
+
* @returns 成功返回分页用户列表
|
|
769
|
+
*/
|
|
770
|
+
listUsers: (options?: ListUsersOptions) => Promise<HaiResult<PaginatedResult<User>>>;
|
|
771
|
+
/**
|
|
772
|
+
* 更新用户信息
|
|
773
|
+
*
|
|
774
|
+
* 空更新(无有效字段)时直接返回当前用户信息。
|
|
775
|
+
*
|
|
776
|
+
* @param userId - 用户 ID
|
|
777
|
+
* @param data - 要更新的字段(displayName、email 等)
|
|
778
|
+
* @returns 成功返回更新后的用户信息;用户不存在返回 USER_NOT_FOUND
|
|
779
|
+
*/
|
|
780
|
+
updateUser: (userId: string, data: Partial<User>) => Promise<HaiResult<User>>;
|
|
781
|
+
/**
|
|
782
|
+
* 删除用户
|
|
783
|
+
*
|
|
784
|
+
* 同时清理用户的角色关联数据。
|
|
785
|
+
*
|
|
786
|
+
* @param userId - 用户 ID
|
|
787
|
+
* @returns 成功返回 ok;用户不存在返回 USER_NOT_FOUND
|
|
788
|
+
*/
|
|
789
|
+
deleteUser: (userId: string) => Promise<HaiResult<void>>;
|
|
790
|
+
/**
|
|
791
|
+
* 管理员重置用户密码
|
|
792
|
+
*
|
|
793
|
+
* 无需旧密码,直接设置新密码(仅限管理员操作)。
|
|
794
|
+
*
|
|
795
|
+
* @param userId - 用户 ID
|
|
796
|
+
* @param newPassword - 新密码
|
|
797
|
+
* @returns 成功返回 ok;用户不存在返回 USER_NOT_FOUND,密码不合规返回 PASSWORD_POLICY_VIOLATION
|
|
798
|
+
*/
|
|
799
|
+
adminResetPassword: (userId: string, newPassword: string) => Promise<HaiResult<void>>;
|
|
800
|
+
/**
|
|
801
|
+
* 修改密码
|
|
802
|
+
*
|
|
803
|
+
* 验证旧密码 → 校验新密码强度 → 哈希并更新。
|
|
804
|
+
*
|
|
805
|
+
* @param userId - 用户 ID
|
|
806
|
+
* @param oldPassword - 旧密码
|
|
807
|
+
* @param newPassword - 新密码
|
|
808
|
+
* @returns 成功返回 ok;旧密码错误返回 INVALID_CREDENTIALS,新密码不合规返回 PASSWORD_POLICY_VIOLATION
|
|
809
|
+
*/
|
|
810
|
+
changePassword: (userId: string, oldPassword: string, newPassword: string) => Promise<HaiResult<void>>;
|
|
811
|
+
/**
|
|
812
|
+
* 当前登录用户修改密码
|
|
813
|
+
*
|
|
814
|
+
* 通过访问令牌识别用户 → 验证旧密码 → 校验新密码强度 → 哈希并更新 → 清除该用户所有会话。
|
|
815
|
+
*
|
|
816
|
+
* @param accessToken - 当前用户的访问令牌
|
|
817
|
+
* @param oldPassword - 旧密码
|
|
818
|
+
* @param newPassword - 新密码
|
|
819
|
+
* @returns 成功返回 ok;旧密码错误返回 INVALID_CREDENTIALS,新密码不合规返回 PASSWORD_POLICY_VIOLATION
|
|
820
|
+
*/
|
|
821
|
+
changeCurrentUserPassword: (accessToken: string, oldPassword: string, newPassword: string) => Promise<HaiResult<void>>;
|
|
822
|
+
/**
|
|
823
|
+
* 请求密码重置
|
|
824
|
+
*
|
|
825
|
+
* 查找用户 → 生成重置令牌(缓存存储、SHA-256 哈希)→ 调用 onPasswordResetRequest 回调通知业务层。
|
|
826
|
+
* 即使用户不存在也返回 ok(防止用户枚举攻击)。
|
|
827
|
+
*
|
|
828
|
+
* @param identifier - 用户标识(邮箱)
|
|
829
|
+
* @returns 始终返回 ok(防枚举);内部异常返回 INTERNAL_ERROR
|
|
830
|
+
*/
|
|
831
|
+
requestPasswordReset: (identifier: string) => Promise<HaiResult<void>>;
|
|
832
|
+
/**
|
|
833
|
+
* 确认密码重置
|
|
834
|
+
*
|
|
835
|
+
* 验证新密码强度 → 验证令牌有效性与尝试次数 → 哈希并更新密码 → 删除令牌 → 清除该用户所有会话。
|
|
836
|
+
*
|
|
837
|
+
* @param token - 重置令牌(明文,由 requestPasswordReset 生成并由业务层发送给用户)
|
|
838
|
+
* @param newPassword - 新密码
|
|
839
|
+
* @returns 成功返回 ok;令牌无效/过期返回 RESET_TOKEN_INVALID,尝试超限返回 RESET_TOKEN_MAX_ATTEMPTS
|
|
840
|
+
*/
|
|
841
|
+
confirmPasswordReset: (token: string, newPassword: string) => Promise<HaiResult<void>>;
|
|
842
|
+
/**
|
|
843
|
+
* 验证密码强度(同步方法)
|
|
844
|
+
*
|
|
845
|
+
* 根据密码策略校验长度、大小写、数字、特殊字符等要求。
|
|
846
|
+
*
|
|
847
|
+
* @param password - 待校验的密码
|
|
848
|
+
* @returns 通过返回 ok;不合规返回 PASSWORD_POLICY_VIOLATION
|
|
849
|
+
*/
|
|
850
|
+
validatePassword: (password: string) => HaiResult<void>;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* @h-ai/iam — 会话相关类型定义
|
|
855
|
+
*
|
|
856
|
+
* 包含: - 认证结果(AuthResult) - 会话类型(Session) - 会话子功能接口(SessionOperations)
|
|
857
|
+
* @module iam-session-types
|
|
858
|
+
*/
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* 认证令牌对(登录成功返回)
|
|
862
|
+
*
|
|
863
|
+
* 所有端(Web SSR / H5 SPA / Capacitor App / 未来小程序)
|
|
864
|
+
* 使用完全相同的 Bearer Token 认证流程。
|
|
865
|
+
*/
|
|
866
|
+
interface TokenPair {
|
|
867
|
+
/** 访问令牌(短期,用于 API 认证) */
|
|
868
|
+
accessToken: string;
|
|
869
|
+
/** 刷新令牌(长期,用于换取新 accessToken) */
|
|
870
|
+
refreshToken: string;
|
|
871
|
+
/** accessToken 过期时间(秒) */
|
|
872
|
+
expiresIn: number;
|
|
873
|
+
/** 令牌类型,固定为 'Bearer' */
|
|
874
|
+
tokenType: 'Bearer';
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* 认证结果
|
|
878
|
+
*
|
|
879
|
+
* 登录/注册成功后返回,包含用户信息、令牌、角色权限等。
|
|
880
|
+
* `roles` / `permissions` 来源于会话创建时写入的 RBAC 数据,
|
|
881
|
+
* 调用方无需再通过 `verifyToken` 获取。
|
|
882
|
+
*/
|
|
883
|
+
interface AuthResult {
|
|
884
|
+
/** 用户信息 */
|
|
885
|
+
user: User;
|
|
886
|
+
/** 令牌对(替代原来的单个 accessToken) */
|
|
887
|
+
tokens: TokenPair;
|
|
888
|
+
/** 角色 code 列表(登录时从 RBAC 写入) */
|
|
889
|
+
roles: string[];
|
|
890
|
+
/** 权限 code 列表(登录时从 RBAC 写入) */
|
|
891
|
+
permissions: string[];
|
|
892
|
+
/** 协议展示信息(可选) */
|
|
893
|
+
agreements?: AgreementDisplay;
|
|
894
|
+
}
|
|
895
|
+
/**
|
|
896
|
+
* 会话数据
|
|
897
|
+
*/
|
|
898
|
+
interface Session {
|
|
899
|
+
/** 用户 ID */
|
|
900
|
+
userId: string;
|
|
901
|
+
/** 用户名 */
|
|
902
|
+
username?: string;
|
|
903
|
+
/** 显示名称 */
|
|
904
|
+
displayName?: string;
|
|
905
|
+
/** 头像 URL */
|
|
906
|
+
avatarUrl?: string;
|
|
907
|
+
/** 角色 code 列表(登录时写入,用于会话解析) */
|
|
908
|
+
roles: string[];
|
|
909
|
+
/** 权限 code 列表(登录时写入,用于会话解析) */
|
|
910
|
+
permissions: string[];
|
|
911
|
+
/** 来源(如 pc/android/ios) */
|
|
912
|
+
source?: string;
|
|
913
|
+
/** 访问令牌 */
|
|
914
|
+
accessToken: string;
|
|
915
|
+
/** 创建时间 */
|
|
916
|
+
createdAt: Date;
|
|
917
|
+
/** 最后活动时间 */
|
|
918
|
+
lastActiveAt: Date;
|
|
919
|
+
/** 过期时间 */
|
|
920
|
+
expiresAt: Date;
|
|
921
|
+
/** 扩展数据(内部保留 _tokenPair 供 logout 时吊销 refreshToken) */
|
|
922
|
+
data?: SessionData;
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* 会话扩展数据
|
|
926
|
+
*
|
|
927
|
+
* 支持开放的 Record 扩展,同时约束内部保留字段 _tokenPair。
|
|
928
|
+
*/
|
|
929
|
+
type SessionData = Record<string, unknown> & {
|
|
930
|
+
/** 令牌对(内部保留字段,由 session.create 写入,logout 时读取) */
|
|
931
|
+
_tokenPair?: TokenPair;
|
|
932
|
+
};
|
|
933
|
+
/**
|
|
934
|
+
* 创建会话选项
|
|
935
|
+
*/
|
|
936
|
+
interface CreateSessionOptions {
|
|
937
|
+
/** 用户 ID */
|
|
938
|
+
userId: string;
|
|
939
|
+
/** 用户名 */
|
|
940
|
+
username?: string;
|
|
941
|
+
/** 显示名称 */
|
|
942
|
+
displayName?: string;
|
|
943
|
+
/** 头像 URL */
|
|
944
|
+
avatarUrl?: string;
|
|
945
|
+
/** 角色 code 列表 */
|
|
946
|
+
roles?: string[];
|
|
947
|
+
/** 权限 code 列表 */
|
|
948
|
+
permissions?: string[];
|
|
949
|
+
/** 来源(如 pc/android/ios) */
|
|
950
|
+
source?: string;
|
|
951
|
+
/** 过期时间(秒) */
|
|
952
|
+
maxAge?: number;
|
|
953
|
+
/** 扩展数据 */
|
|
954
|
+
data?: SessionData;
|
|
955
|
+
}
|
|
956
|
+
/**
|
|
957
|
+
* 会话子功能接口
|
|
958
|
+
*/
|
|
959
|
+
interface SessionOperations {
|
|
960
|
+
/**
|
|
961
|
+
* 创建会话
|
|
962
|
+
*
|
|
963
|
+
* 单设备模式下会先清除用户已有的所有会话。
|
|
964
|
+
*
|
|
965
|
+
* @param options - 会话创建选项(用户 ID、角色、来源等)
|
|
966
|
+
* @returns 成功返回完整的 Session 对象(含生成的访问令牌和过期时间)
|
|
967
|
+
*/
|
|
968
|
+
create: (options: CreateSessionOptions) => Promise<HaiResult<Session>>;
|
|
969
|
+
/**
|
|
970
|
+
* 获取会话
|
|
971
|
+
*
|
|
972
|
+
* 查询缓存中的会话数据。滑动窗口模式下自动续期。
|
|
973
|
+
* 已过期的会话会自动删除并返回 null。
|
|
974
|
+
*
|
|
975
|
+
* @param accessToken - 访问令牌
|
|
976
|
+
* @returns 成功返回会话对象或 null(会话不存在/已过期)
|
|
977
|
+
*/
|
|
978
|
+
get: (accessToken: string) => Promise<HaiResult<Session | null>>;
|
|
979
|
+
/**
|
|
980
|
+
* 验证访问令牌
|
|
981
|
+
*
|
|
982
|
+
* 等同于 get 但在会话不存在时返回错误而非 null。
|
|
983
|
+
*
|
|
984
|
+
* @param accessToken - 访问令牌
|
|
985
|
+
* @returns 成功返回会话;会话无效返回 SESSION_INVALID
|
|
986
|
+
*/
|
|
987
|
+
verifyToken: (accessToken: string) => Promise<HaiResult<Session>>;
|
|
988
|
+
/**
|
|
989
|
+
* 更新会话
|
|
990
|
+
*
|
|
991
|
+
* 合并 patch 到现有会话(data 字段浅合并)并更新 lastActiveAt。
|
|
992
|
+
*
|
|
993
|
+
* @param accessToken - 访问令牌
|
|
994
|
+
* @param data - 要更新的字段(roles、username、source、data 等)
|
|
995
|
+
* @returns 成功返回 ok;会话不存在返回 SESSION_NOT_FOUND
|
|
996
|
+
*/
|
|
997
|
+
update: (accessToken: string, data: Partial<Session>) => Promise<HaiResult<void>>;
|
|
998
|
+
/**
|
|
999
|
+
* 删除会话
|
|
1000
|
+
*
|
|
1001
|
+
* 同时清除用户令牌映射关系。
|
|
1002
|
+
*
|
|
1003
|
+
* @param accessToken - 访问令牌
|
|
1004
|
+
* @returns 始终返回 ok(令牌不存在时静默成功)
|
|
1005
|
+
*/
|
|
1006
|
+
delete: (accessToken: string) => Promise<HaiResult<void>>;
|
|
1007
|
+
/**
|
|
1008
|
+
* 删除用户所有会话
|
|
1009
|
+
*
|
|
1010
|
+
* 遍历用户的所有令牌并逐一删除。
|
|
1011
|
+
*
|
|
1012
|
+
* @param userId - 用户 ID
|
|
1013
|
+
* @returns 成功返回实际删除的会话数量
|
|
1014
|
+
*/
|
|
1015
|
+
deleteByUserId: (userId: string) => Promise<HaiResult<number>>;
|
|
1016
|
+
/**
|
|
1017
|
+
* 通过 refreshToken 换取新的 TokenPair
|
|
1018
|
+
*
|
|
1019
|
+
* 验证 refreshToken 有效性,签发新的 accessToken + refreshToken,
|
|
1020
|
+
* 旧 refreshToken 自动失效(Rotation 策略)。
|
|
1021
|
+
*
|
|
1022
|
+
* @param refreshToken - 刷新令牌
|
|
1023
|
+
* @returns 成功返回新的 TokenPair;失败返回 TOKEN_REFRESH_FAILED / TOKEN_EXPIRED
|
|
1024
|
+
*/
|
|
1025
|
+
refresh: (refreshToken: string) => Promise<HaiResult<TokenPair>>;
|
|
1026
|
+
/**
|
|
1027
|
+
* 吊销 refreshToken(登出时调用)
|
|
1028
|
+
*
|
|
1029
|
+
* @param refreshToken - 刷新令牌
|
|
1030
|
+
* @returns 成功返回 ok;令牌不存在时静默成功
|
|
1031
|
+
*/
|
|
1032
|
+
revokeRefresh: (refreshToken: string) => Promise<HaiResult<void>>;
|
|
1033
|
+
/**
|
|
1034
|
+
* 批量更新用户所有活跃会话的 roles / permissions 字段
|
|
1035
|
+
*
|
|
1036
|
+
* 遍历用户缓存中的所有 session token,合并更新指定字段,
|
|
1037
|
+
* 同时清理已失效的会话令牌。用于角色/权限变更后的会话同步。
|
|
1038
|
+
*
|
|
1039
|
+
* @param userId - 用户 ID
|
|
1040
|
+
* @param updates - 要更新的字段(roles / permissions)
|
|
1041
|
+
* @returns 成功返回 ok
|
|
1042
|
+
*/
|
|
1043
|
+
patchUserSessions: (userId: string, updates: SessionFieldUpdates) => Promise<HaiResult<void>>;
|
|
1044
|
+
}
|
|
1045
|
+
/**
|
|
1046
|
+
* 会话字段更新(仅限 roles / permissions)
|
|
1047
|
+
*
|
|
1048
|
+
* 用于角色/权限变更后的会话同步。
|
|
1049
|
+
*/
|
|
1050
|
+
interface SessionFieldUpdates {
|
|
1051
|
+
/** 角色 code 列表 */
|
|
1052
|
+
roles?: string[];
|
|
1053
|
+
/** 权限 code 列表 */
|
|
1054
|
+
permissions?: string[];
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* @h-ai/iam — 认证类型定义
|
|
1059
|
+
*
|
|
1060
|
+
* 包含: - 凭证类型(Credentials) - 认证策略接口(AuthStrategy) - 认证操作接口(AuthnOperations)
|
|
1061
|
+
* @module iam-authn-types
|
|
1062
|
+
*/
|
|
1063
|
+
|
|
1064
|
+
/**
|
|
1065
|
+
* 用户凭证(用于密码登录)
|
|
1066
|
+
*/
|
|
1067
|
+
interface PasswordCredentials {
|
|
1068
|
+
/** 用户名/邮箱/手机号 */
|
|
1069
|
+
identifier: string;
|
|
1070
|
+
/** 密码 */
|
|
1071
|
+
password: string;
|
|
1072
|
+
/** 记住我 */
|
|
1073
|
+
rememberMe?: boolean;
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* OTP 凭证(用于验证码登录)
|
|
1077
|
+
*/
|
|
1078
|
+
interface OtpCredentials {
|
|
1079
|
+
/** 邮箱/手机号 */
|
|
1080
|
+
identifier: string;
|
|
1081
|
+
/** 验证码 */
|
|
1082
|
+
code: string;
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
* LDAP 凭证
|
|
1086
|
+
*/
|
|
1087
|
+
interface LdapCredentials {
|
|
1088
|
+
/** 用户名 */
|
|
1089
|
+
username: string;
|
|
1090
|
+
/** 密码 */
|
|
1091
|
+
password: string;
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* API Key 凭证(用于 API Key 认证)
|
|
1095
|
+
*/
|
|
1096
|
+
interface ApiKeyCredentials {
|
|
1097
|
+
/** 明文 API Key */
|
|
1098
|
+
key: string;
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* 统一凭证类型
|
|
1102
|
+
*/
|
|
1103
|
+
type Credentials = {
|
|
1104
|
+
type: 'password';
|
|
1105
|
+
} & PasswordCredentials | {
|
|
1106
|
+
type: 'otp';
|
|
1107
|
+
} & OtpCredentials | {
|
|
1108
|
+
type: 'ldap';
|
|
1109
|
+
} & LdapCredentials | {
|
|
1110
|
+
type: 'apikey';
|
|
1111
|
+
} & ApiKeyCredentials;
|
|
1112
|
+
/**
|
|
1113
|
+
* 认证策略接口
|
|
1114
|
+
*
|
|
1115
|
+
* 所有认证方式(密码/OTP/LDAP)都实现此接口
|
|
1116
|
+
*/
|
|
1117
|
+
interface AuthStrategy {
|
|
1118
|
+
/** 策略类型 */
|
|
1119
|
+
readonly type: AuthStrategyType;
|
|
1120
|
+
/** 策略名称 */
|
|
1121
|
+
readonly name: string;
|
|
1122
|
+
/**
|
|
1123
|
+
* 执行认证
|
|
1124
|
+
*
|
|
1125
|
+
* @param credentials - 统一凭证(包含 type 字段标识认证方式)
|
|
1126
|
+
* @returns 认证成功返回用户信息;失败返回对应错误码(INVALID_CREDENTIALS / USER_LOCKED 等)
|
|
1127
|
+
*/
|
|
1128
|
+
authenticate: (credentials: Credentials) => Promise<HaiResult<User>>;
|
|
1129
|
+
}
|
|
1130
|
+
/**
|
|
1131
|
+
* 认证子功能接口
|
|
1132
|
+
*/
|
|
1133
|
+
interface AuthnOperations {
|
|
1134
|
+
/**
|
|
1135
|
+
* 登录(使用密码)
|
|
1136
|
+
*
|
|
1137
|
+
* 检查登录方式启用 → 密码策略认证 → 创建会话 → 返回令牌。
|
|
1138
|
+
*
|
|
1139
|
+
* @param credentials - 密码凭证(identifier + password)
|
|
1140
|
+
* @returns 成功返回 AuthResult(用户信息、令牌、协议展示);失败返回错误
|
|
1141
|
+
*/
|
|
1142
|
+
login: (credentials: PasswordCredentials) => Promise<HaiResult<AuthResult>>;
|
|
1143
|
+
/**
|
|
1144
|
+
* 使用验证码登录
|
|
1145
|
+
*
|
|
1146
|
+
* @param credentials - OTP 凭证(identifier + code)
|
|
1147
|
+
* @returns 成功返回 AuthResult;验证码无效/过期返回 OTP_INVALID
|
|
1148
|
+
*/
|
|
1149
|
+
loginWithOtp: (credentials: OtpCredentials) => Promise<HaiResult<AuthResult>>;
|
|
1150
|
+
/**
|
|
1151
|
+
* 使用 LDAP 登录
|
|
1152
|
+
*
|
|
1153
|
+
* @param credentials - LDAP 凭证(username + password)
|
|
1154
|
+
* @returns 成功返回 AuthResult(自动同步本地用户);失败返回错误
|
|
1155
|
+
*/
|
|
1156
|
+
loginWithLdap: (credentials: LdapCredentials) => Promise<HaiResult<AuthResult>>;
|
|
1157
|
+
/**
|
|
1158
|
+
* 使用 API Key 登录
|
|
1159
|
+
*
|
|
1160
|
+
* 验证 API Key 有效性 → 查找关联用户 → 创建会话 → 返回令牌。
|
|
1161
|
+
*
|
|
1162
|
+
* @param credentials - API Key 凭证
|
|
1163
|
+
* @returns 成功返回 AuthResult;API Key 无效/过期/禁用返回对应错误
|
|
1164
|
+
*/
|
|
1165
|
+
loginWithApiKey: (credentials: ApiKeyCredentials) => Promise<HaiResult<AuthResult>>;
|
|
1166
|
+
/**
|
|
1167
|
+
* 登出
|
|
1168
|
+
*
|
|
1169
|
+
* 删除会话数据和用户令牌映射。令牌无效时静默成功。
|
|
1170
|
+
*
|
|
1171
|
+
* @param accessToken - 访问令牌
|
|
1172
|
+
* @returns 始终返回 ok
|
|
1173
|
+
*/
|
|
1174
|
+
logout: (accessToken: string) => Promise<HaiResult<void>>;
|
|
1175
|
+
/**
|
|
1176
|
+
* 验证令牌
|
|
1177
|
+
*
|
|
1178
|
+
* 查询会话并校验有效性,滑动窗口模式下自动续期。
|
|
1179
|
+
*
|
|
1180
|
+
* @param accessToken - 访问令牌
|
|
1181
|
+
* @returns 成功返回会话信息;令牌无效/过期返回 SESSION_INVALID
|
|
1182
|
+
*/
|
|
1183
|
+
verifyToken: (accessToken: string) => Promise<HaiResult<Session>>;
|
|
1184
|
+
/**
|
|
1185
|
+
* 发送验证码
|
|
1186
|
+
*
|
|
1187
|
+
* @param identifier - 用户标识(邮箱/手机号)
|
|
1188
|
+
* @returns 成功返回验证码过期时间;频率限制返回 OTP_RESEND_TOO_FAST
|
|
1189
|
+
*/
|
|
1190
|
+
sendOtp: (identifier: string) => Promise<HaiResult<{
|
|
1191
|
+
expiresAt: Date;
|
|
1192
|
+
}>>;
|
|
1193
|
+
/**
|
|
1194
|
+
* 注册并登录(一站式)
|
|
1195
|
+
*
|
|
1196
|
+
* 先注册用户(含默认角色分配),成功后立即登录并返回令牌。
|
|
1197
|
+
* 注册失败则直接返回注册错误;注册成功但登录失败则返回登录错误。
|
|
1198
|
+
*
|
|
1199
|
+
* @param options - 注册选项(用户名、密码、邮箱等)
|
|
1200
|
+
* @returns 成功返回 AuthResult(用户信息、令牌、协议展示);失败返回对应错误码
|
|
1201
|
+
*/
|
|
1202
|
+
registerAndLogin: (options: RegisterOptions) => Promise<HaiResult<AuthResult>>;
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
/**
|
|
1206
|
+
* @h-ai/iam — LDAP 认证策略
|
|
1207
|
+
*
|
|
1208
|
+
* LDAP 目录认证方式
|
|
1209
|
+
* @module iam-authn-ldap-strategy
|
|
1210
|
+
*/
|
|
1211
|
+
|
|
1212
|
+
/**
|
|
1213
|
+
* LDAP 客户端接口
|
|
1214
|
+
*/
|
|
1215
|
+
interface LdapClient {
|
|
1216
|
+
/**
|
|
1217
|
+
* 绑定(认证)
|
|
1218
|
+
*/
|
|
1219
|
+
bind: (dn: string, password: string) => Promise<HaiResult<void>>;
|
|
1220
|
+
/**
|
|
1221
|
+
* 搜索用户
|
|
1222
|
+
*/
|
|
1223
|
+
search: (base: string, filter: string, attributes: string[]) => Promise<HaiResult<LdapSearchEntry[]>>;
|
|
1224
|
+
/**
|
|
1225
|
+
* 解除绑定
|
|
1226
|
+
*/
|
|
1227
|
+
unbind: () => Promise<HaiResult<void>>;
|
|
1228
|
+
}
|
|
1229
|
+
/**
|
|
1230
|
+
* LDAP 搜索结果条目
|
|
1231
|
+
*/
|
|
1232
|
+
interface LdapSearchEntry {
|
|
1233
|
+
/** DN */
|
|
1234
|
+
dn: string;
|
|
1235
|
+
/** 属性 */
|
|
1236
|
+
attributes: Record<string, string | string[]>;
|
|
1237
|
+
}
|
|
1238
|
+
/**
|
|
1239
|
+
* LDAP 客户端工厂
|
|
1240
|
+
*/
|
|
1241
|
+
type LdapClientFactory = (config: LdapConfig) => Promise<HaiResult<LdapClient>>;
|
|
1242
|
+
|
|
1243
|
+
declare const HaiIamError: {
|
|
1244
|
+
readonly AUTH_FAILED: _h_ai_core.HaiErrorDef;
|
|
1245
|
+
readonly INVALID_CREDENTIALS: _h_ai_core.HaiErrorDef;
|
|
1246
|
+
readonly USER_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1247
|
+
readonly USER_DISABLED: _h_ai_core.HaiErrorDef;
|
|
1248
|
+
readonly USER_LOCKED: _h_ai_core.HaiErrorDef;
|
|
1249
|
+
readonly USER_ALREADY_EXISTS: _h_ai_core.HaiErrorDef;
|
|
1250
|
+
readonly PASSWORD_EXPIRED: _h_ai_core.HaiErrorDef;
|
|
1251
|
+
readonly PASSWORD_POLICY_VIOLATION: _h_ai_core.HaiErrorDef;
|
|
1252
|
+
readonly OTP_INVALID: _h_ai_core.HaiErrorDef;
|
|
1253
|
+
readonly OTP_EXPIRED: _h_ai_core.HaiErrorDef;
|
|
1254
|
+
readonly OTP_RESEND_TOO_FAST: _h_ai_core.HaiErrorDef;
|
|
1255
|
+
readonly LOGIN_DISABLED: _h_ai_core.HaiErrorDef;
|
|
1256
|
+
readonly REGISTER_DISABLED: _h_ai_core.HaiErrorDef;
|
|
1257
|
+
readonly STRATEGY_NOT_SUPPORTED: _h_ai_core.HaiErrorDef;
|
|
1258
|
+
readonly APIKEY_INVALID: _h_ai_core.HaiErrorDef;
|
|
1259
|
+
readonly APIKEY_EXPIRED: _h_ai_core.HaiErrorDef;
|
|
1260
|
+
readonly APIKEY_DISABLED: _h_ai_core.HaiErrorDef;
|
|
1261
|
+
readonly APIKEY_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1262
|
+
readonly RESET_TOKEN_INVALID: _h_ai_core.HaiErrorDef;
|
|
1263
|
+
readonly RESET_TOKEN_EXPIRED: _h_ai_core.HaiErrorDef;
|
|
1264
|
+
readonly RESET_TOKEN_MAX_ATTEMPTS: _h_ai_core.HaiErrorDef;
|
|
1265
|
+
readonly SESSION_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1266
|
+
readonly SESSION_EXPIRED: _h_ai_core.HaiErrorDef;
|
|
1267
|
+
readonly SESSION_INVALID: _h_ai_core.HaiErrorDef;
|
|
1268
|
+
readonly SESSION_CREATE_FAILED: _h_ai_core.HaiErrorDef;
|
|
1269
|
+
readonly TOKEN_EXPIRED: _h_ai_core.HaiErrorDef;
|
|
1270
|
+
readonly TOKEN_INVALID: _h_ai_core.HaiErrorDef;
|
|
1271
|
+
readonly TOKEN_REFRESH_FAILED: _h_ai_core.HaiErrorDef;
|
|
1272
|
+
readonly PERMISSION_DENIED: _h_ai_core.HaiErrorDef;
|
|
1273
|
+
readonly ROLE_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1274
|
+
readonly PERMISSION_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1275
|
+
readonly ROLE_ALREADY_EXISTS: _h_ai_core.HaiErrorDef;
|
|
1276
|
+
readonly PERMISSION_ALREADY_EXISTS: _h_ai_core.HaiErrorDef;
|
|
1277
|
+
readonly LDAP_CONNECTION_FAILED: _h_ai_core.HaiErrorDef;
|
|
1278
|
+
readonly LDAP_BIND_FAILED: _h_ai_core.HaiErrorDef;
|
|
1279
|
+
readonly LDAP_SEARCH_FAILED: _h_ai_core.HaiErrorDef;
|
|
1280
|
+
readonly REPOSITORY_ERROR: _h_ai_core.HaiErrorDef;
|
|
1281
|
+
readonly NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1282
|
+
readonly CONFLICT: _h_ai_core.HaiErrorDef;
|
|
1283
|
+
readonly FORBIDDEN: _h_ai_core.HaiErrorDef;
|
|
1284
|
+
readonly INVALID_ARGUMENT: _h_ai_core.HaiErrorDef;
|
|
1285
|
+
readonly CONFIG_ERROR: _h_ai_core.HaiErrorDef;
|
|
1286
|
+
readonly NOT_INITIALIZED: _h_ai_core.HaiErrorDef;
|
|
1287
|
+
readonly INTERNAL_ERROR: _h_ai_core.HaiErrorDef;
|
|
1288
|
+
};
|
|
1289
|
+
/**
|
|
1290
|
+
* IAM 初始化配置
|
|
1291
|
+
*
|
|
1292
|
+
* 包含 Zod 校验的设置字段和运行时依赖。
|
|
1293
|
+
* 传入 `iam.init()` 的唯一参数。
|
|
1294
|
+
*/
|
|
1295
|
+
interface IamConfigInput extends IamConfigSettingsInput {
|
|
1296
|
+
/** LDAP 客户端工厂(启用 LDAP 登录时必填) */
|
|
1297
|
+
ldapClientFactory?: LdapClientFactory;
|
|
1298
|
+
/** LDAP 用户同步开关(默认 true) */
|
|
1299
|
+
ldapSyncUser?: boolean;
|
|
1300
|
+
/**
|
|
1301
|
+
* 密码重置令牌回调(可选)
|
|
1302
|
+
*
|
|
1303
|
+
* 当用户请求密码重置时,框架生成令牌后通过此回调通知业务层,
|
|
1304
|
+
* 业务层负责将令牌通过邮件/短信等渠道发送给用户。
|
|
1305
|
+
* 若未提供此回调,requestPasswordReset 将仅记录日志不发送通知。
|
|
1306
|
+
*
|
|
1307
|
+
* @param user - 请求重置的用户信息
|
|
1308
|
+
* @param token - 重置令牌(明文,需发送给用户)
|
|
1309
|
+
* @param expiresAt - 令牌过期时间
|
|
1310
|
+
*/
|
|
1311
|
+
onPasswordResetRequest?: (user: User, token: string, expiresAt: Date) => Promise<void>;
|
|
1312
|
+
/**
|
|
1313
|
+
* OTP 邮件验证码发送回调(可选)
|
|
1314
|
+
*
|
|
1315
|
+
* 当用户请求 OTP 验证码时,框架生成验证码后通过此回调通知业务层,
|
|
1316
|
+
* 业务层负责将验证码通过邮件发送给用户。
|
|
1317
|
+
* 若未提供此回调,sendOtp 将返回"发送方式未配置"错误。
|
|
1318
|
+
*
|
|
1319
|
+
* @param email - 目标邮箱
|
|
1320
|
+
* @param code - 验证码
|
|
1321
|
+
*/
|
|
1322
|
+
onOtpSendEmail?: (email: string, code: string) => Promise<void>;
|
|
1323
|
+
/**
|
|
1324
|
+
* OTP 短信验证码发送回调(可选)
|
|
1325
|
+
*
|
|
1326
|
+
* 当用户请求 OTP 验证码时,框架生成验证码后通过此回调通知业务层,
|
|
1327
|
+
* 业务层负责将验证码通过短信发送给用户。
|
|
1328
|
+
* 若未提供此回调,sendOtp 将返回"发送方式未配置"错误。
|
|
1329
|
+
*
|
|
1330
|
+
* @param phone - 目标手机号
|
|
1331
|
+
* @param code - 验证码
|
|
1332
|
+
*/
|
|
1333
|
+
onOtpSendSms?: (phone: string, code: string) => Promise<void>;
|
|
1334
|
+
}
|
|
1335
|
+
/**
|
|
1336
|
+
* IAM 函数接口
|
|
1337
|
+
*
|
|
1338
|
+
* 统一聚合所有 IAM 功能。
|
|
1339
|
+
*/
|
|
1340
|
+
interface IamFunctions {
|
|
1341
|
+
/**
|
|
1342
|
+
* 初始化 IAM 服务
|
|
1343
|
+
*
|
|
1344
|
+
* 幂等操作:已初始化时直接返回成功。
|
|
1345
|
+
* 内部按依赖顺序创建 session → authz → authn → user 子功能,
|
|
1346
|
+
* 并可选执行种子数据初始化。
|
|
1347
|
+
*
|
|
1348
|
+
* @param config - IAM 初始化配置(可选 session/password/LDAP 等策略配置)
|
|
1349
|
+
* @returns 成功返回 `ok(undefined)`;失败返回含错误码的 `err`
|
|
1350
|
+
*
|
|
1351
|
+
* @example
|
|
1352
|
+
* ```ts
|
|
1353
|
+
* const result = await iam.init({ session: { maxAge: 86400 } })
|
|
1354
|
+
* if (!result.success) {
|
|
1355
|
+
* console.error(result.error.message)
|
|
1356
|
+
* }
|
|
1357
|
+
* ```
|
|
1358
|
+
*/
|
|
1359
|
+
init: (config: IamConfigInput) => Promise<HaiResult<void>>;
|
|
1360
|
+
/**
|
|
1361
|
+
* 关闭 IAM 服务
|
|
1362
|
+
*
|
|
1363
|
+
* 释放所有内部子功能引用,将模块恢复到未初始化状态。
|
|
1364
|
+
* 关闭后访问 `iam.auth` 等属性将返回未初始化错误。
|
|
1365
|
+
*/
|
|
1366
|
+
close: () => Promise<void>;
|
|
1367
|
+
/** 当前配置(未初始化时为 null) */
|
|
1368
|
+
readonly config: IamConfig | null;
|
|
1369
|
+
/** 是否已初始化 */
|
|
1370
|
+
readonly isInitialized: boolean;
|
|
1371
|
+
/** 注册功能是否启用(未初始化或未配置时默认启用) */
|
|
1372
|
+
readonly isRegisterEnabled: boolean;
|
|
1373
|
+
/** 认证操作(登录、登出、令牌验证等) */
|
|
1374
|
+
readonly auth: AuthnOperations;
|
|
1375
|
+
/** 用户管理(注册、查询、更新、密码管理等) */
|
|
1376
|
+
readonly user: UserOperations;
|
|
1377
|
+
/** 授权管理(角色/权限 CRUD、用户角色分配、权限检查) */
|
|
1378
|
+
readonly authz: AuthzOperations;
|
|
1379
|
+
/** 会话管理(创建、查询、验证、删除会话) */
|
|
1380
|
+
readonly session: SessionOperations;
|
|
1381
|
+
/** API Key 管理(创建、列表、吐销、验证),未启用 apikey 登录时返回未初始化代理 */
|
|
1382
|
+
readonly apiKey: ApiKeyOperations;
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
/**
|
|
1386
|
+
* @h-ai/iam — IAM 服务主入口
|
|
1387
|
+
*
|
|
1388
|
+
* 管理运行时状态、实现生命周期(init / close)、通过 get 访问器暴露子功能。
|
|
1389
|
+
* @module iam-main
|
|
1390
|
+
*/
|
|
1391
|
+
|
|
1392
|
+
declare const iam: IamFunctions;
|
|
1393
|
+
|
|
1394
|
+
export { type AgreementConfig, AgreementConfigSchema, type AgreementDisplay, type ApiKey, type ApiKeyConfig, ApiKeyConfigSchema, type ApiKeyCredentials, type ApiKeyOperations, type AuthResult, type AuthStrategy, type AuthStrategyType, AuthStrategyTypeSchema, type AuthnOperations, type AuthzOperations, type CreateApiKeyOptions, type CreateApiKeyResult, type CreateSessionOptions, type Credentials, HaiIamError, type IamConfig, type IamConfigInput, IamConfigSchema, type IamConfigSettingsInput, type IamFunctions, type LdapClientFactory, type LdapConfig, LdapConfigSchema, type LdapCredentials, type ListUsersOptions, type LoginConfig, LoginConfigSchema, type OtpConfig, OtpConfigSchema, type OtpCredentials, type PasswordConfig, PasswordConfigSchema, type PasswordCredentials, type PasswordResetConfig, PasswordResetConfigSchema, type Permission, type PermissionQueryOptions, type PermissionType, type RbacConfig, RbacConfigSchema, type RegisterConfig, RegisterConfigSchema, type RegisterOptions, type RegisterResult, type Role, type SecurityConfig, SecurityConfigSchema, type Session, type SessionConfig, SessionConfigSchema, type SessionData, type SessionFieldUpdates, type SessionOperations, type StoredUser, type TokenPair, type UpdateCurrentUserInput, type User, type UserOperations, iam };
|