• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2021 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 */
15import BaseSettingsController from '../../../../../../../common/component/src/main/ets/default/controller/BaseSettingsController';
16import ConfigData from '../../../../../../../common/utils/src/main/ets/default/baseUtil/ConfigData';
17import LogUtil from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil';
18import ISettingsController from '../../../../../../../common/component/src/main/ets/default/controller/ISettingsController';
19import PasswordModel, {ResultCode} from '../../model/passwordImpl/PasswordModel';
20import Router from '@system.router';
21import Prompt from '@system.prompt';
22
23const OPEN_SESSION_FAILED = '0';
24
25export default class PasswordCheckController extends BaseSettingsController {
26  private TAG = ConfigData.TAG + 'PasswordCheckController ';
27  private pageRequestCode: number = -1;
28  private prevPageUri: string = undefined;
29  private pinChallenge: string = undefined;
30  private pinToken: string = undefined;
31  private passwordType: number = -1;
32  private freezingTime: number = 0;
33  private remainTimes: number = -1;
34
35// private Properties
36  private password: string = ''
37
38  initData(): ISettingsController {
39    this.loadData()
40    return this
41  }
42
43  subscribe(): ISettingsController {
44    PasswordModel.openSession((data) => {
45      if (data === OPEN_SESSION_FAILED) {
46        LogUtil.info(`${this.TAG}subscribe->openSession failed`);
47      } else {
48        LogUtil.info(`${this.TAG}subscribe->openSession success`);
49      }
50      this.pinChallenge = data;
51    });
52    PasswordModel.registerInputer();
53    return this;
54  };
55
56  unsubscribe(): ISettingsController {
57    if (this.pinToken && this.pageRequestCode === ConfigData.PAGE_REQUEST_CODE_PASSWORD_CHANGE) {
58      LogUtil.info(`${this.TAG}unsubscribe->password change dont closeSession`);
59    } else {
60      PasswordModel.closeSession();
61    }
62    PasswordModel.unregisterInputer();
63    return this;
64  };
65
66//------------------------------ Handler ---------------------------
67/**
68   * change password type.
69   *
70   * @param value : inputting password
71   */
72  passwordOnChange(value: string) {
73    LogUtil.info(this.TAG + 'passwordOnChange in.');
74    this.password = value;
75    LogUtil.info(this.TAG + 'passwordOnChange out.');
76  }
77
78//------------------------------ check ---------------------------
79/**
80   * Input finish. Start simple check.
81   */
82  inputFinish(event?: ClickEvent) {
83    if (!this.password) {
84      return;
85    }
86
87    // clear page data
88    this.freezingTime = 0
89    this.remainTimes = -1;
90
91    this.checkInputSuccess()
92  }
93
94/**
95   * Input check success.
96   */
97  checkInputSuccess() {
98    this.checkPasswordCorrect((result, extraInfo) => {
99      LogUtil.info(`${this.TAG} check password correct, result: ${result}`);
100      if (result === ResultCode.SUCCESS) {
101        if (extraInfo) {
102          if (this.pinChallenge) {
103            this.pinToken = extraInfo.token;
104          }
105        }
106        this.authSuccess()
107
108      } else {
109        if (extraInfo) {
110          this.freezingTime = extraInfo.freezingTime;
111          this.remainTimes = extraInfo.remainTimes;
112        } else {
113          LogUtil.info(this.TAG + 'checkInputSuccess : callback not success. And there is no extra info.');
114          Prompt.showToast({
115            message: 'Pin auth is not success.\nAnd extraInfo has no data.\nresult = ' + result,
116            duration: 3500
117          })
118        }
119      }
120    });
121  }
122
123/**
124   * Check password result changed.
125   */
126  authSuccess() {
127    if (this.pinToken && this.pageRequestCode == ConfigData.PAGE_REQUEST_CODE_PASSWORD_CHANGE) {
128      this.gotoPasswordCreatePage();
129    } else if (this.pinToken && this.pageRequestCode == ConfigData.PAGE_REQUEST_CODE_PASSWORD_DISABLE) {
130      this.delCredential(() => {
131        this.goBackCorrect();
132      });
133    } else {
134      this.goBackCorrect();
135    }
136  }
137
138//------------------------------ Router -----------------------------
139/**
140   * Go to password create page
141   */
142  gotoPasswordCreatePage() {
143    Router.replace({
144      uri: 'pages/passwordInput',
145      params: {
146        'pageRequestCode': this.pageRequestCode,
147        'prevPageUri': this.prevPageUri,
148        'pinChallenge': this.pinChallenge,
149        'pinToken': this.pinToken
150      },
151    })
152  }
153
154/**
155   * Auth check ok, return OK result.
156   */
157  goBackCorrect() {
158    Router.back()
159  }
160
161//------------------------------ api ---------------------------
162/**
163   * Call api to check if has the password
164   */
165  loadData() {
166    PasswordModel.hasPinPassword((havePassword) => {
167      if (havePassword) {
168        this.getAuthProperty();
169      } else {
170        this.goBackCorrect();
171      }
172    });
173  }
174
175/**
176   * Call api to check the password
177   *
178   * @param value : password type
179   */
180  checkPasswordCorrect(successCallback: (result: number, extraInfo: any) => void): void {
181    LogUtil.info(`${this.TAG} check password correct auth pin.`);
182    PasswordModel.authPin(this.pinChallenge, this.password, (result, extraInfo) => {
183      if (result === ResultCode.SUCCESS) {
184        LogUtil.info(`${this.TAG}check password correct success`);
185      } else {
186        LogUtil.info(`${this.TAG}check password correct failed`);
187        this.password = '';
188        LogUtil.info(`${this.TAG} password set empty`);
189      }
190      successCallback(result, extraInfo)
191    });
192  }
193
194/**
195   * Call api to get passwordType, freezingTime and remainTimes
196   *
197   * @param successCallback: api success callback
198   */
199  getAuthProperty() {
200    PasswordModel.getAuthProperty((data: any) => {
201      if (data.result === ResultCode.SUCCESS) {
202        this.passwordType = data.authSubType ? data.authSubType : -1;
203        this.freezingTime = data.freezingTime ? data.freezingTime : 0;
204        this.remainTimes = data.remainTimes ? data.remainTimes : -1;
205        LogUtil.info(`${this.TAG}getAuthProperty success`);
206      } else {
207        LogUtil.info(`${this.TAG}getAuthProperty failed`);
208      }
209    });
210  }
211
212/**
213   * Call api to delete the credential
214   *
215   * @param successCallback: api success callback
216   */
217  delCredential(successCallback) {
218    PasswordModel.delAllCredential(this.pinToken, (result, extraInfo) => {
219      if (result === ResultCode.SUCCESS) {
220        LogUtil.info(`${this.TAG}del success`);
221        this.pinToken = '';
222        successCallback();
223      } else {
224        LogUtil.info(`${this.TAG}del failed`);
225      }
226    })
227  }
228}
229