1/*
2 * Copyright (c) 2022-2023 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 Log from '../utils/log';
17import CommonController from '../controller/commonController';
18import account_osAccount from '@ohos.account.osAccount';
19
20class UserAuthModel {
21  protected readonly TAG: string = 'UserAuthModel';
22  protected pinAuthManger : account_osAccount.PINAuth;
23  protected userAuthManager : account_osAccount.UserAuth;
24  private readonly CMD_SET_SURFACE_ID = 100;
25
26  constructor() {
27    Log.info(this.TAG, 'constructor+');
28    this.pinAuthManger = new account_osAccount.PINAuth();
29    this.userAuthManager = new account_osAccount.UserAuth();
30    Log.info(this.TAG, 'constructor-');
31  }
32
33  async authPin(challenge: Uint8Array):Promise<Uint8Array> {
34    Log.info(this.TAG, 'auth Pin+');
35    this.pinAuthManger.registerInputer({
36      onGetData: (authSubType, iInputData) => {
37        Log.info(this.TAG, 'FaceEnroll pin.registerInputer start');
38        const SIX_DIGITS_PIN = 10000;
39        const PIN_ASCII_LIST = [49, 50, 51, 52, 53, 54];
40        iInputData.onSetData(SIX_DIGITS_PIN, new Uint8Array(PIN_ASCII_LIST));
41      }
42    });
43
44    let ret = new Promise<Uint8Array>((resolve)=> {
45      const AUTH_TYPE = account_osAccount.AuthType.PIN;
46      const AUTH_TRUST_LEVEL = account_osAccount.AuthTrustLevel.ATL1;
47      this.userAuthManager.auth(challenge, AUTH_TYPE, AUTH_TRUST_LEVEL, {
48        onResult: (result, extraInfo) => {
49          Log.info(this.TAG, 'authPin onResult: ' + result);
50          this.pinAuthManger.unregisterInputer();
51          if ((result === 0) && extraInfo.token && (Object.keys(extraInfo.token).length > 0)) {
52            resolve(extraInfo.token);
53            Log.info(this.TAG, 'authPin success');
54            const TEN_MINUTE = 10;
55            const SIXTY_SECOND = 60;
56            const THOUSAND_MS = 1000;
57            setTimeout(() => {
58              CommonController.terminateAbility();
59            }, TEN_MINUTE * SIXTY_SECOND * THOUSAND_MS);
60          } else {
61            Log.error(this.TAG, 'authPin fail');
62            resolve(new Uint8Array([]));
63          }
64        }
65      });
66    });
67    Log.info(this.TAG, 'auth Pin-');
68    return ret;
69  }
70}
71
72let userAuthModel = new UserAuthModel();
73export default userAuthModel as UserAuthModel;