• 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 */
15
16export function mockUserIAMUserAuth() {
17  const AuthenticationResult = {
18    NO_SUPPORT: -1,
19    SUCCESS: 0,
20    COMPARE_FAILURE: 1,
21    CANCELED: 2,
22    TIMEOUT: 3,
23    CAMERA_FAIL: 4,
24    BUSY: 5,
25    INVALID_PARAMETERS: 6,
26    LOCKED: 7,
27    NOT_ENROLLED: 8,
28    GENERAL_ERROR: 100,
29  }
30
31  const CheckAvailabilityResult = {
32    SUPPORTED: 0,
33    AUTH_TYPE_NOT_SUPPORT: 1,
34    SECURE_LEVEL_NOT_SUPPORT: 2,
35    DISTRIBUTED_AUTH_NOT_SUPPORT: 3,
36    NOT_ENROLLED: 4,
37    PARAM_NUM_ERROR: 5,
38  }
39
40  const TipEvent = {
41    RESULT: 1,
42    CANCEL: 2,
43    ACQUIRE: 3,
44    BUSY: 4,
45    OUT_OF_MEM: 5,
46    FACE_ID: 6,
47  }
48
49  const TipCode = {
50    FACE_AUTH_TIP_TOO_BRIGHT: 1,
51    FACE_AUTH_TIP_TOO_DARK: 2,
52    FACE_AUTH_TIP_TOO_CLOSE: 3,
53    FACE_AUTH_TIP_TOO_FAR: 4,
54    FACE_AUTH_TIP_TOO_HIGH: 5,
55    FACE_AUTH_TIP_TOO_LOW: 6,
56    FACE_AUTH_TIP_TOO_RIGHT: 7,
57    FACE_AUTH_TIP_TOO_LEFT: 8,
58    FACE_AUTH_TIP_TOO_MUCH_MOTION: 9,
59    FACE_AUTH_TIP_POOR_GAZE: 10,
60    FACE_AUTH_TIP_NOT_DETECTED: 11,
61  }
62
63  const Result = {
64    SUCCESS: 0,
65    FAILED: 1,
66  }
67
68  function checkAuthParam(authType, secureLevel) {
69    if ((typeof authType !== 'string') || (["ALL", "FACE_ONLY"].indexOf(authType) < 0)) {
70      console.error('invalid authType');
71      return CheckAvailabilityResult.AUTH_TYPE_NOT_SUPPORT;
72    }
73
74    if ((typeof secureLevel !== 'string') || (["S1", "S2", "S3", "S4"].indexOf(secureLevel) < 0)) {
75      console.error('invalid secureLevel');
76      return CheckAvailabilityResult.SECURE_LEVEL_NOT_SUPPORT;
77    }
78
79    return Result.SUCCESS;
80  }
81
82  let Authenticator = class {
83    constructor() {
84    }
85
86    async execute(authType, secureLevel, callback) {
87      console.warn("Authenticator.execute interface mocked in the Previewer. How this interface works on the" +
88        " Previewer may be different from that on a real device.")
89      function handleCallback(callback, ret) {
90        if (typeof callback === 'function') {
91          callback(ret);
92        } else {
93          if (ret === 0) {
94            return ret;
95          } else {
96            throw ret;
97          }
98        }
99      }
100
101      if (checkAuthParam(authType, secureLevel) != Result.SUCCESS) {
102        return handleCallback(callback, AuthenticationResult.INVALID_PARAMETERS);
103      }
104
105      let successTip = {
106        "errorCode": Result.FAILED,
107        "tipEvent": TipEvent.RESULT,
108        "tipCode": AuthenticationResult.SUCCESS,
109        "tipInfo": "success",
110      };
111
112      for (let localCallback of userAuth.subscribeTipSet.values()) {
113        localCallback(successTip);
114      }
115
116      // mock
117      return handleCallback(callback, AuthenticationResult.SUCCESS);
118    }
119
120    checkAvailability(authType, secureLevel) {
121      console.warn("Authenticator.checkAvailability interface mocked in the Previewer. How this interface works" +
122        " on the Previewer may be different from that on a real device.")
123      let result = checkAuthParam(authType, secureLevel);
124      if (result != Result.SUCCESS) {
125        return result;
126      }
127
128      // mock
129      return CheckAvailabilityResult.SUPPORTED;
130    }
131
132    cancel() {
133      console.warn("Authenticator.cancel interface mocked in the Previewer. How this interface works on the" +
134        " Previewer may be different from that on a real device.")
135
136      // mock
137      return Result.SUCCESS;
138    }
139
140    async on(type, callback) {
141      console.warn("Authenticator.on interface mocked in the Previewer. How this interface works on the" +
142        " Previewer may be different from that on a real device.")
143      let errorTip = {
144        "errorCode": Result.FAILED,
145        "tipEvent": 0,
146        "tipCode": 0,
147        "tipInfo": "",
148      };
149
150      if (type !== 'tip') {
151        console.error('type is invalid')
152        callback(errorTip);
153        return;
154      }
155
156      if (typeof callback !== 'function') {
157        console.error('callback parameter is invalid');
158        callback(errorTip);
159        return;
160      }
161
162      userAuth.subscribeTipSet.add(callback);
163      if (userAuth.subscribeToTip) {
164        console.log('already subscribe to event');
165        return;
166      }
167
168      // mock
169      userAuth.subscribeToTip = true;
170      console.info('subscribe success');
171    }
172
173    async off(type, optCallback) {
174      console.warn("Authenticator.off interface mocked in the Previewer. How this interface works on the" +
175        " Previewer may be different from that on a real device.")
176      if (type !== 'tip') {
177        console.error('type is invalid')
178        return;
179      }
180
181      if (typeof optCallback === 'function') {
182        userAuth.subscribeTipSet.delete(optCallback);
183      } else {
184        userAuth.subscribeTipSet.clear();
185      }
186
187      if (!userAuth.subscribeToTip) {
188        console.info('not subscribing event, no need unsubscribe');
189        return;
190      }
191
192      if (userAuth.subscribeTipSet.size !== 0) {
193        console.info('subscriber is not 0, no need unsubscribe');
194        return;
195      }
196
197      // mock
198      userAuth.subscribeToTip = false;
199      console.info('unsubscribe success');
200    }
201  }
202
203  var userAuth = {
204    moduleGroup: null,
205    eventGroup: null,
206    authenticator: null,
207    subscribeToTip: false,
208    subscribeTipSet: new Set([]),
209    getAuthenticator: function getAuthenticator() {
210      console.warn("userAuth.getAuthenticator interface mocked in the Previewer. How this interface works" +
211        " on the Previewer may be different from that on a real device.")
212      if (this.authenticator == null) {
213        this.authenticator = new Authenticator()
214      }
215      return this.authenticator;
216    }
217  }
218
219  global.systemplugin.userIAM = global.systemplugin.userIAM || {};
220  global.systemplugin.userIAM.userAuth = {
221    getAuthenticator: userAuth.getAuthenticator,
222    AuthenticationResult: AuthenticationResult,
223    CheckAvailabilityResult: CheckAvailabilityResult,
224    TipEvent: TipEvent,
225    TipCode: TipCode,
226    Result: Result,
227  };
228}