• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import userAuth from '@ohos.userIAM.userAuth';
17import { BusinessError } from '@ohos.base';
18
19export class CheckUserAuthModel {
20  public isAuthTypeSupported(authType: userAuth.UserAuthType): boolean {
21    try {
22      userAuth.getAvailableStatus(authType, userAuth.AuthTrustLevel.ATL1);
23      console.info('[CM&CheckUserAuthModel]: ' + 'userAuthType' + authType + 'is supported');
24      return true;
25    } catch (error) {
26      let err: BusinessError = error as BusinessError;
27      console.error(`[CM&CheckUserAuthModel]: userAuthType ${authType} is not supported, message is ${err?.message}`);
28      return false;
29    }
30  }
31
32  public auth(titleStr: string, callback: (authResult: boolean) => void): void {
33    let fingerPrint: boolean = this.isAuthTypeSupported(userAuth.UserAuthType.FINGERPRINT);
34    let pin: boolean = this.isAuthTypeSupported(userAuth.UserAuthType.PIN);
35    let authTypeArray = [userAuth.UserAuthType.FINGERPRINT];
36    if (fingerPrint) {
37      authTypeArray = [userAuth.UserAuthType.FINGERPRINT];
38      if (pin) {
39        authTypeArray = [userAuth.UserAuthType.FINGERPRINT, userAuth.UserAuthType.PIN];
40      }
41    } else if (pin) {
42      authTypeArray = [userAuth.UserAuthType.PIN];
43    } else {
44      /* The user does not set identity authentication. */
45      callback(true);
46    }
47
48    const authParam: userAuth.AuthParam = {
49      challenge: new Uint8Array([49, 49, 49, 49, 49, 49]),
50      authType: authTypeArray,
51      authTrustLevel: userAuth.AuthTrustLevel.ATL1
52    }
53    const widgetParam: userAuth.WidgetParam = {
54      title: titleStr
55    };
56
57    try {
58      let userAuthInstance = userAuth.getUserAuthInstance(authParam, widgetParam);
59      console.info('[CM&CheckUserAuthModel]: get userAuth instance success');
60      userAuthInstance.start();
61
62      userAuthInstance.on('result', {
63        onResult(result) {
64          if (result.result === userAuth.UserAuthResultCode.SUCCESS) {
65            callback(true);
66          } else if (result.result === userAuth.UserAuthResultCode.CANCELED) {
67            /* User cancels authentication. */
68            callback(false);
69          } else {
70            /* User authentication failed. */
71            callback(false);
72          }
73        }
74      })
75    } catch (error) {
76      let err: BusinessError = error as BusinessError;
77      console.error(`[CM&CheckUserAuthModel]: auth catch error. code is ${err?.code}, message is ${err?.message}`);
78    }
79  }
80}
81
82let checkUserAuthModel = new CheckUserAuthModel();
83
84export default checkUserAuthModel as CheckUserAuthModel;