• 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      return;
47    }
48
49    const authParam: userAuth.AuthParam = {
50      challenge: new Uint8Array([49, 49, 49, 49, 49, 49]),
51      authType: authTypeArray,
52      authTrustLevel: userAuth.AuthTrustLevel.ATL1
53    }
54    const widgetParam: userAuth.WidgetParam = {
55      title: titleStr
56    };
57
58    try {
59      let userAuthInstance = userAuth.getUserAuthInstance(authParam, widgetParam);
60      console.info('[CM&CheckUserAuthModel]: get userAuth instance success');
61      userAuthInstance.start();
62
63      userAuthInstance.on('result', {
64        onResult(result) {
65          if (result.result === userAuth.UserAuthResultCode.SUCCESS) {
66            callback(true);
67          } else if (result.result === userAuth.UserAuthResultCode.CANCELED) {
68            /* User cancels authentication. */
69            callback(false);
70          } else {
71            /* User authentication failed. */
72            callback(false);
73          }
74        }
75      })
76    } catch (error) {
77      let err: BusinessError = error as BusinessError;
78      console.error(`[CM&CheckUserAuthModel]: auth catch error. code is ${err?.code}, message is ${err?.message}`);
79    }
80  }
81}
82
83let checkUserAuthModel = new CheckUserAuthModel();
84
85export default checkUserAuthModel as CheckUserAuthModel;