• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.userIAM.userAuth (用户认证)(系统接口)
2
3提供用户认证能力,可应用于设备解锁、支付、应用登录等身份认证场景。
4
5> **说明:**
6>
7> - 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.userIAM.userAuth (用户认证)](js-apis-useriam-userauth.md)。
9
10## 导入模块
11
12```ts
13import { userAuth } from '@kit.UserAuthenticationKit';
14```
15
16## AuthParam<sup>10+</sup>
17
18用户认证相关参数。
19
20**系统能力**:SystemCapability.UserIAM.UserAuth.Core
21
22| 名称           | 类型                               | 必填 | 说明                                                         |
23| -------------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
24| userId<sup>18+</sup> | number | 否   |要认证的目标用户ID,值为大于等于0的正整数。默认值为当前用户的ID。|
25
26## WindowModeType<sup>10+</sup>
27
28用户认证界面的显示类型。
29
30**系统能力**:SystemCapability.UserIAM.UserAuth.Core
31
32**系统接口**: 此接口为系统接口。
33
34| 名称       | 值   | 说明       |
35| ---------- | ---- | ---------- |
36| DIALOG_BOX | 1    | 弹框类型。 |
37| FULLSCREEN | 2    | 全屏类型。 |
38
39## WidgetParam<sup>10+</sup>
40
41用户认证界面配置相关参数。
42
43**系统能力**:SystemCapability.UserIAM.UserAuth.Core
44
45| 名称                 | 类型                                | 必填 | 说明                                                         |
46| -------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
47| windowMode           | [WindowModeType](#windowmodetype10) | 否   | 代表用户认证界面的显示类型,默认值为WindowModeType.DIALOG_BOX。<br>**系统接口**: 此接口为系统接口。 |
48
49## NoticeType<sup>10+</sup>
50
51用户身份认证的通知类型。
52
53**系统能力**:SystemCapability.UserIAM.UserAuth.Core
54
55**系统接口**: 此接口为系统接口。
56
57| 名称          | 值   | 说明                 |
58| ------------- | ---- | -------------------- |
59| WIDGET_NOTICE | 1    | 表示来自组件的通知。 |
60
61## userAuth.sendNotice<sup>10+</sup>
62
63sendNotice(noticeType: NoticeType, eventData: string): void
64
65在使用统一身份认证控件进行用户身份认证时,用于接收来自统一身份认证控件的通知。
66
67**需要权限**:ohos.permission.SUPPORT_USER_AUTH
68
69**系统能力**:SystemCapability.UserIAM.UserAuth.Core
70
71**系统接口**: 此接口为系统接口。
72
73**参数:**
74
75| 参数名     | 类型                        | 必填 | 说明       |
76| ---------- | --------------------------- | ---- | ---------- |
77| noticeType | [NoticeType](#noticetype10) | 是   | 通知类型。 |
78| eventData  | string                      | 是   | 事件数据。数据长度限制为0-65536。 |
79
80**错误码:**
81
82以下错误码的详细介绍请参见[用户认证错误码](errorcode-useriam.md)。
83
84| 错误码ID | 错误信息                                |
85| -------- | --------------------------------------- |
86| 201      | Permission verification failed.         |
87| 202      | The caller is not a system application. |
88| 401      | Incorrect parameters. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed.    |
89| 12500002 | General operation error.                |
90
91**示例:**
92
93```ts
94import { userAuth } from '@kit.UserAuthenticationKit';
95
96interface  EventData {
97  widgetContextId: number;
98  event: string;
99  version: string;
100  payload: PayLoad;
101}
102interface PayLoad {
103  type: Object[];
104}
105try {
106  const eventData : EventData = {
107    widgetContextId: 123456,
108    event: 'EVENT_AUTH_TYPE_READY',
109    version: '1',
110    payload: {
111      type: ['pin']
112    } as PayLoad,
113  };
114  const jsonEventData = JSON.stringify(eventData);
115  let noticeType = userAuth.NoticeType.WIDGET_NOTICE;
116  userAuth.sendNotice(noticeType, jsonEventData);
117  console.info('sendNotice success');
118} catch (error) {
119  console.error(`sendNotice catch error: ${JSON.stringify(error)}`);
120}
121```
122
123## UserAuthWidgetMgr<sup>10+</sup>
124
125组件管理接口,可将用身份认证控件注册到UserAuthWidgetMgr中,由UserAuthWidgetMgr进行管理、调度。
126
127### on<sup>10+</sup>
128
129on(type: 'command', callback: IAuthWidgetCallback): void
130
131身份认证控件订阅来自用户认证框架的命令。
132
133**系统能力**:SystemCapability.UserIAM.UserAuth.Core
134
135**系统接口**: 此接口为系统接口。
136
137**参数:**
138
139| 参数名   | 类型                                          | 必填 | 说明                                                         |
140| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
141| type     | 'command'                                     | 是   | 订阅事件类型,表明该事件用于用户认证框架向身份认证控件发送命令。 |
142| callback | [IAuthWidgetCallback](#iauthwidgetcallback10) | 是   | 组件管理接口的回调函数,用于用户认证框架向身份认证控件发送命令。 |
143
144**错误码:**
145
146以下错误码的详细介绍请参见[用户认证错误码](errorcode-useriam.md)。
147
148| 错误码ID | 错误信息                 |
149| -------- | ------------------------ |
150| 401      | Incorrect parameters. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. |
151| 12500002 | General operation error. |
152
153**示例:**
154
155```ts
156import { userAuth } from '@kit.UserAuthenticationKit';
157
158const userAuthWidgetMgrVersion = 1;
159try {
160  let userAuthWidgetMgr = userAuth.getUserAuthWidgetMgr(userAuthWidgetMgrVersion);
161  console.info('get userAuthWidgetMgr instance success');
162  userAuthWidgetMgr.on('command', {
163    sendCommand(cmdData) {
164      console.info(`The cmdData is ${cmdData}`);
165    }
166  })
167  console.info('subscribe authentication event success');
168} catch (error) {
169  console.error(`userAuth widgetMgr catch error: ${JSON.stringify(error)}`);
170}
171```
172
173### off<sup>10+</sup>
174
175off(type: 'command', callback?: IAuthWidgetCallback): void
176
177身份认证控件取消订阅来自用户认证框架的命令。
178
179**系统能力**:SystemCapability.UserIAM.UserAuth.Core
180
181**系统接口**: 此接口为系统接口。
182
183**参数:**
184
185| 参数名   | 类型                                          | 必填 | 说明                                                         |
186| -------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
187| type     | 'command'                                     | 是   | 订阅事件类型,表明该事件用于用户认证框架向身份认证控件发送命令。 |
188| callback | [IAuthWidgetCallback](#iauthwidgetcallback10) | 否   | 组件管理接口的回调函数,用于用户认证框架向身份认证控件发送命令。 |
189
190**错误码:**
191
192以下错误码的详细介绍请参见[用户认证错误码](errorcode-useriam.md)。
193
194| 错误码ID | 错误信息                 |
195| -------- | ------------------------ |
196| 401      | Incorrect parameters. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. |
197| 12500002 | General operation error. |
198
199**示例:**
200
201```ts
202import { userAuth } from '@kit.UserAuthenticationKit';
203
204const userAuthWidgetMgrVersion = 1;
205try {
206  let userAuthWidgetMgr = userAuth.getUserAuthWidgetMgr(userAuthWidgetMgrVersion);
207  console.info('get userAuthWidgetMgr instance success');
208  userAuthWidgetMgr.off('command', {
209    sendCommand(cmdData) {
210      console.info(`The cmdData is ${cmdData}`);
211    }
212  })
213  console.info('cancel subscribe authentication event success');
214} catch (error) {
215  console.error(`userAuth widgetMgr catch error: ${JSON.stringify(error)}`);
216}
217```
218
219## userAuth.getUserAuthWidgetMgr<sup>10+</sup>
220
221getUserAuthWidgetMgr(version: number): UserAuthWidgetMgr
222
223获取UserAuthWidgetMgr对象,用于执行用户身份认证。
224
225> **说明:**
226> 每个UserAuthInstance只能进行一次认证,若需要再次进行认证则需重新获取UserAuthInstance。
227
228**需要权限**:ohos.permission.SUPPORT_USER_AUTH
229
230**系统能力**:SystemCapability.UserIAM.UserAuth.Core
231
232**系统接口**: 此接口为系统接口。
233
234**参数:**
235
236| 参数名  | 类型   | 必填 | 说明                 |
237| ------- | ------ | ---- | -------------------- |
238| version | number | 是   | 表示认证组件的版本。 |
239
240**返回值:**
241
242| 类型                                      | 说明         |
243| ----------------------------------------- | ------------ |
244| [UserAuthWidgetMgr](#userauthwidgetmgr10) | 认证器对象。 |
245
246**错误码:**
247
248以下错误码的详细介绍请参见[用户认证错误码](errorcode-useriam.md)。
249
250| 错误码ID | 错误信息                                |
251| -------- | --------------------------------------- |
252| 201      | Permission verification failed.         |
253| 202      | The caller is not a system application. |
254| 401      | Incorrect parameters. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.                                     |
255| 12500002 | General operation error.                |
256
257**示例:**
258
259```ts
260import { userAuth } from '@kit.UserAuthenticationKit';
261
262let userAuthWidgetMgrVersion = 1;
263try {
264  let userAuthWidgetMgr = userAuth.getUserAuthWidgetMgr(userAuthWidgetMgrVersion);
265  console.info('get userAuthWidgetMgr instance success');
266} catch (error) {
267  console.error(`userAuth widgetMgr catch error: ${JSON.stringify(error)}`);
268}
269```
270
271## IAuthWidgetCallback<sup>10+</sup>
272
273认证组件通过该回调获取用户认证框架发送的命令。
274
275### sendCommand<sup>10+</sup>
276
277sendCommand(cmdData: string): void
278
279回调函数,用于用户认证框架向组件发送命令。
280
281**系统能力**:SystemCapability.UserIAM.UserAuth.Core
282
283**系统接口**: 此接口为系统接口。
284
285**参数:**
286
287| 参数名  | 类型   | 必填 | 说明                               |
288| ------- | ------ | ---- | ---------------------------------- |
289| cmdData | string | 是   | 用户身份认证框架向控件发送的命令。|
290
291**示例:**
292
293```ts
294import { userAuth } from '@kit.UserAuthenticationKit';
295
296const userAuthWidgetMgrVersion = 1;
297try {
298  let userAuthWidgetMgr = userAuth.getUserAuthWidgetMgr(userAuthWidgetMgrVersion);
299  console.info('get userAuthWidgetMgr instance success');
300  userAuthWidgetMgr.on('command', {
301    sendCommand(cmdData) {
302      console.info(`The cmdData is ${cmdData}`);
303    }
304  })
305  console.info('subscribe authentication event success');
306} catch (error) {
307  console.error(`userAuth widgetMgr catch error: ${JSON.stringify(error)}`);
308}
309```
310
311## UserAuthType<sup>8+</sup>
312
313表示身份认证的凭据类型枚举。
314
315**系统能力**:SystemCapability.UserIAM.UserAuth.Core
316
317| 名称        | 值   | 说明       |
318| ----------- | ---- | ---------- |
319| PRIVATE_PIN<sup>14+</sup>  | 16   | 隐私口令。 |
320
321**示例:**
322
323发起用户认证,采用认证可信等级≥ATL3的隐私密码认证,获取认证结果:
324
325```ts
326import { BusinessError } from '@kit.BasicServicesKit';
327import { cryptoFramework } from '@kit.CryptoArchitectureKit';
328import { userAuth } from '@kit.UserAuthenticationKit';
329
330try {
331  const rand = cryptoFramework.createRandom();
332  const len: number = 16;
333  const randData: Uint8Array = rand?.generateRandomSync(len)?.data;
334  const authParam: userAuth.AuthParam = {
335    challenge: randData,
336    authType: [userAuth.UserAuthType.PRIVATE_PIN],
337    authTrustLevel: userAuth.AuthTrustLevel.ATL3,
338  };
339  const widgetParam: userAuth.WidgetParam = {
340    title: '请输入密码',
341  };
342
343  const userAuthInstance = userAuth.getUserAuthInstance(authParam, widgetParam);
344  console.info('get userAuth instance success');
345  // 需要调用UserAuthInstance的start()接口,启动认证后,才能通过onResult获取到认证结果。
346  userAuthInstance.on('result', {
347    onResult (result) {
348      console.info(`userAuthInstance callback result = ${JSON.stringify(result)}`);
349    }
350  });
351  console.info('auth on success');
352} catch (error) {
353  const err: BusinessError = error as BusinessError;
354  console.error(`auth catch error. Code is ${err?.code}, message is ${err?.message}`);
355}
356```
357
358## UserAuthResultCode<sup>18+</sup>
359
360表示返回码的枚举。
361
362**系统能力**:SystemCapability.UserIAM.UserAuth.Core
363
364| 名称                    |   值   | 说明                 |
365| ----------------------- | ------ | -------------------- |
366| AUTH_TOKEN_CHECK_FAILED | 12500015      | verifyAuthToken系统接口错误码,表示验证的AuthToken无效。|
367| AUTH_TOKEN_EXPIRED      | 12500016      | verifyAuthToken系统接口错误码,AuthToken的签发时间至发起验证时的时间间隔超过传入的最大有效时长。|