• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include "iam_logger.h"
17 
18 #include "auth_instance_v9.h"
19 #include "nlohmann/json.hpp"
20 #include "user_auth_impl.h"
21 #include "user_auth_instance_v10.h"
22 #include "user_auth_widget_mgr_v10.h"
23 #include "user_auth_client_impl.h"
24 
25 #define LOG_TAG "USER_AUTH_NAPI"
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30 namespace {
31 const std::string NOTICE_WIDGET_CTXID = "widgetContextId";
32 const std::string NOTICE_EVENT_TYPE = "event";
33 const std::string NOTICE_VERSION = "version";
34 const std::string NOTICE_PAYLOAD = "payload";
35 const std::string NOTICE_PAYLOAD_TYPE = "type";
36 const std::string SUPPORT_NOTICE_VERSION = "1";
37 
UserAuthServiceConstructor(napi_env env,napi_callback_info info)38 napi_value UserAuthServiceConstructor(napi_env env, napi_callback_info info)
39 {
40     IAM_LOGI("start");
41     napi_value thisVar = nullptr;
42     size_t argc = ARGS_ONE;
43     napi_value argv[ARGS_ONE] = {nullptr};
44     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
45     return thisVar;
46 }
47 
GetVersion(napi_env env,napi_callback_info info)48 napi_value GetVersion(napi_env env, napi_callback_info info)
49 {
50     IAM_LOGI("start");
51     return UserAuthImpl::GetVersion(env, info);
52 }
53 
GetAvailableStatus(napi_env env,napi_callback_info info)54 napi_value GetAvailableStatus(napi_env env, napi_callback_info info)
55 {
56     IAM_LOGI("start");
57     return UserAuthImpl::GetAvailableStatus(env, info);
58 }
59 
GetEnrolledState(napi_env env,napi_callback_info info)60 napi_value GetEnrolledState(napi_env env, napi_callback_info info)
61 {
62     IAM_LOGI("start");
63     return UserAuthInstanceV10::GetEnrolledState(env, info);
64 }
65 
GetAvailableStatusV9(napi_env env,napi_callback_info info)66 napi_value GetAvailableStatusV9(napi_env env, napi_callback_info info)
67 {
68     IAM_LOGI("start");
69     UserAuthResultCode result = AuthInstanceV9::GetAvailableStatus(env, info);
70     if (result != UserAuthResultCode::SUCCESS) {
71         IAM_LOGE("fail");
72         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, result));
73     }
74     return nullptr;
75 }
76 
UnwrapAuthInstanceV9(napi_env env,napi_callback_info info,AuthInstanceV9 ** authInstanceV9)77 napi_status UnwrapAuthInstanceV9(napi_env env, napi_callback_info info, AuthInstanceV9 **authInstanceV9)
78 {
79     napi_value thisVar = nullptr;
80     size_t argc = ARGS_ONE;
81     napi_value argv[ARGS_ONE] = {nullptr};
82     napi_status ret = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
83     if (ret != napi_ok) {
84         IAM_LOGE("napi_get_cb_info fail");
85         return ret;
86     }
87     ret = napi_unwrap(env, thisVar, reinterpret_cast<void **>(authInstanceV9));
88     if (ret != napi_ok) {
89         IAM_LOGE("napi_unwrap fail");
90         return ret;
91     }
92     if (*authInstanceV9 == nullptr) {
93         IAM_LOGE("authInstanceV9 is null");
94         return napi_generic_failure;
95     }
96     return ret;
97 }
98 
AuthInstanceV9Constructor(napi_env env,napi_callback_info info)99 napi_value AuthInstanceV9Constructor(napi_env env, napi_callback_info info)
100 {
101     IAM_LOGI("start");
102     std::unique_ptr<AuthInstanceV9> authInstanceV9 {new (std::nothrow) AuthInstanceV9(env)};
103     if (authInstanceV9 == nullptr) {
104         IAM_LOGE("authInstanceV9 is nullptr");
105         return nullptr;
106     }
107 
108     napi_value thisVar = nullptr;
109     size_t argc = ARGS_ONE;
110     napi_value argv[ARGS_ONE] = {nullptr};
111     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
112     NAPI_CALL(env, napi_wrap(env, thisVar, authInstanceV9.get(),
113         [](napi_env env, void *data, void *hint) {
114             AuthInstanceV9 *authInstanceV9 = static_cast<AuthInstanceV9 *>(data);
115             if (authInstanceV9 != nullptr) {
116                 delete authInstanceV9;
117             }
118         },
119         nullptr, nullptr));
120     authInstanceV9.release();
121     return thisVar;
122 }
123 
On(napi_env env,napi_callback_info info)124 napi_value On(napi_env env, napi_callback_info info)
125 {
126     IAM_LOGI("start");
127     AuthInstanceV9 *authInstance;
128     napi_status ret = UnwrapAuthInstanceV9(env, info, &authInstance);
129     if (ret != napi_ok) {
130         IAM_LOGE("UnwrapAuthInstanceV9 fail:%{public}d", ret);
131         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
132         return nullptr;
133     }
134     UserAuthResultCode code = authInstance->On(env, info);
135     if (code != UserAuthResultCode::SUCCESS) {
136         IAM_LOGE("On fail:%{public}d", static_cast<int32_t>(code));
137         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
138     }
139     return nullptr;
140 }
141 
Off(napi_env env,napi_callback_info info)142 napi_value Off(napi_env env, napi_callback_info info)
143 {
144     IAM_LOGI("start");
145     AuthInstanceV9 *authInstance;
146     napi_status ret = UnwrapAuthInstanceV9(env, info, &authInstance);
147     if (ret != napi_ok) {
148         IAM_LOGE("UnwrapAuthInstanceV9 fail:%{public}d", ret);
149         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
150         return nullptr;
151     }
152     UserAuthResultCode code = authInstance->Off(env, info);
153     if (code != UserAuthResultCode::SUCCESS) {
154         IAM_LOGE("Off fail:%{public}d", static_cast<int32_t>(code));
155         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
156     }
157     return nullptr;
158 }
159 
Start(napi_env env,napi_callback_info info)160 napi_value Start(napi_env env, napi_callback_info info)
161 {
162     IAM_LOGI("start");
163     AuthInstanceV9 *authInstance;
164     napi_status ret = UnwrapAuthInstanceV9(env, info, &authInstance);
165     if (ret != napi_ok) {
166         IAM_LOGE("UnwrapAuthInstanceV9 fail:%{public}d", ret);
167         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
168         return nullptr;
169     }
170     UserAuthResultCode code = authInstance->Start(env, info);
171     if (code != UserAuthResultCode::SUCCESS) {
172         IAM_LOGE("Start fail:%{public}d", static_cast<int32_t>(code));
173         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
174     }
175     return nullptr;
176 }
177 
Cancel(napi_env env,napi_callback_info info)178 napi_value Cancel(napi_env env, napi_callback_info info)
179 {
180     IAM_LOGI("start");
181     AuthInstanceV9 *authInstance;
182     napi_status ret = UnwrapAuthInstanceV9(env, info, &authInstance);
183     if (ret != napi_ok) {
184         IAM_LOGE("UnwrapAuthInstanceV9 fail:%{public}d", ret);
185         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
186         return nullptr;
187     }
188     UserAuthResultCode code = authInstance->Cancel(env, info);
189     if (code != UserAuthResultCode::SUCCESS) {
190         IAM_LOGE("Cancel fail:%{public}d", static_cast<int32_t>(code));
191         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
192     }
193     return nullptr;
194 }
195 
AuthInstanceV9Class(napi_env env)196 napi_value AuthInstanceV9Class(napi_env env)
197 {
198     napi_value result = nullptr;
199     napi_property_descriptor clzDes[] = {
200         DECLARE_NAPI_FUNCTION("on", UserAuth::On),
201         DECLARE_NAPI_FUNCTION("off", UserAuth::Off),
202         DECLARE_NAPI_FUNCTION("start", UserAuth::Start),
203         DECLARE_NAPI_FUNCTION("cancel", UserAuth::Cancel),
204     };
205     NAPI_CALL(env, napi_define_class(env, "AuthInstace", NAPI_AUTO_LENGTH, AuthInstanceV9Constructor, nullptr,
206         sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &result));
207     return result;
208 }
209 
GetAuthInstanceV9(napi_env env,napi_callback_info info)210 napi_value GetAuthInstanceV9(napi_env env, napi_callback_info info)
211 {
212     IAM_LOGI("start");
213     napi_value authInstanceV9;
214     napi_status ret = napi_new_instance(env, AuthInstanceV9Class(env), 0, nullptr, &authInstanceV9);
215     if (ret != napi_ok) {
216         IAM_LOGE("napi_new_instance fail:%{public}d", ret);
217         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
218         return nullptr;
219     }
220     AuthInstanceV9 *authInstance;
221     ret = napi_unwrap(env, authInstanceV9, reinterpret_cast<void **>(&authInstance));
222     if (ret != napi_ok) {
223         IAM_LOGE("napi_unwrap fail");
224         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
225         return nullptr;
226     }
227     if (authInstance == nullptr) {
228         IAM_LOGE("authInstanceV9 is null");
229         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
230         return nullptr;
231     }
232     UserAuthResultCode code = authInstance->Init(env, info);
233     if (code != UserAuthResultCode::SUCCESS) {
234         IAM_LOGE("Init fail:%{public}d", static_cast<int32_t>(code));
235         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
236         return nullptr;
237     }
238     return authInstanceV9;
239 }
240 
UnwrapAuthInstanceV10(napi_env env,napi_callback_info info,UserAuthInstanceV10 ** authInstanceV10)241 napi_status UnwrapAuthInstanceV10(napi_env env, napi_callback_info info, UserAuthInstanceV10 **authInstanceV10)
242 {
243     napi_value thisVar = nullptr;
244     size_t argc = ARGS_ONE;
245     napi_value argv[ARGS_ONE] = {nullptr};
246     napi_status ret = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
247     if (ret != napi_ok) {
248         IAM_LOGE("napi_get_cb_info fail");
249         return ret;
250     }
251     ret = napi_unwrap(env, thisVar, reinterpret_cast<void **>(authInstanceV10));
252     if (ret != napi_ok) {
253         IAM_LOGE("napi_unwrap fail");
254         return ret;
255     }
256     if (*authInstanceV10 == nullptr) {
257         IAM_LOGE("authInstanceV10 is null");
258         return napi_generic_failure;
259     }
260     return ret;
261 }
262 
OnV10(napi_env env,napi_callback_info info)263 napi_value OnV10(napi_env env, napi_callback_info info)
264 {
265     IAM_LOGI("start");
266     UserAuthInstanceV10 *authInstance = nullptr;
267     napi_status ret = UnwrapAuthInstanceV10(env, info, &authInstance);
268     if (ret != napi_ok) {
269         IAM_LOGE("UnwrapAuthInstanceV10 fail:%{public}d", ret);
270         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
271         return nullptr;
272     }
273     UserAuthResultCode code = authInstance->On(env, info);
274     if (code != UserAuthResultCode::SUCCESS) {
275         IAM_LOGE("On fail:%{public}d", static_cast<int32_t>(code));
276         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
277     }
278     return nullptr;
279 }
280 
OffV10(napi_env env,napi_callback_info info)281 napi_value OffV10(napi_env env, napi_callback_info info)
282 {
283     IAM_LOGI("start");
284     UserAuthInstanceV10 *authInstance = nullptr;
285     napi_status ret = UnwrapAuthInstanceV10(env, info, &authInstance);
286     if (ret != napi_ok) {
287         IAM_LOGE("UnwrapAuthInstanceV10 fail:%{public}d", ret);
288         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
289         return nullptr;
290     }
291     UserAuthResultCode code = authInstance->Off(env, info);
292     if (code != UserAuthResultCode::SUCCESS) {
293         IAM_LOGE("Off fail:%{public}d", static_cast<int32_t>(code));
294         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
295     }
296     return nullptr;
297 }
298 
StartV10(napi_env env,napi_callback_info info)299 napi_value StartV10(napi_env env, napi_callback_info info)
300 {
301     IAM_LOGI("start");
302     UserAuthInstanceV10 *authInstance = nullptr;
303     napi_status ret = UnwrapAuthInstanceV10(env, info, &authInstance);
304     if (ret != napi_ok) {
305         IAM_LOGE("UnwrapAuthInstanceV10 fail:%{public}d", ret);
306         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
307         return nullptr;
308     }
309     UserAuthResultCode code = authInstance->Start(env, info);
310     if (code != UserAuthResultCode::SUCCESS) {
311         IAM_LOGE("Start fail:%{public}d", static_cast<int32_t>(code));
312         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
313     }
314     return nullptr;
315 }
316 
CancelV10(napi_env env,napi_callback_info info)317 napi_value CancelV10(napi_env env, napi_callback_info info)
318 {
319     IAM_LOGI("start");
320     UserAuthInstanceV10 *authInstance = nullptr;
321     napi_status ret = UnwrapAuthInstanceV10(env, info, &authInstance);
322     if (ret != napi_ok) {
323         IAM_LOGE("UnwrapAuthInstanceV10 fail:%{public}d", ret);
324         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
325         return nullptr;
326     }
327     UserAuthResultCode code = authInstance->Cancel(env, info);
328     if (code != UserAuthResultCode::SUCCESS) {
329         IAM_LOGE("Cancel fail:%{public}d", static_cast<int32_t>(code));
330         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
331     }
332     return nullptr;
333 }
334 
UserAuthInstanceV10Constructor(napi_env env,napi_callback_info info)335 napi_value UserAuthInstanceV10Constructor(napi_env env, napi_callback_info info)
336 {
337     IAM_LOGI("start");
338     std::unique_ptr<UserAuthInstanceV10> userAuthInstanceV10 {new (std::nothrow) UserAuthInstanceV10(env)};
339     if (userAuthInstanceV10 == nullptr) {
340         IAM_LOGE("userAuthInstanceV10 is nullptr");
341         return nullptr;
342     }
343 
344     napi_value thisVar = nullptr;
345     size_t argc = ARGS_TWO;
346     napi_value argv[ARGS_TWO] = {nullptr};
347     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
348     NAPI_CALL(env, napi_wrap(env, thisVar, userAuthInstanceV10.get(),
349         [](napi_env env, void *data, void *hint) {
350             UserAuthInstanceV10 *userAuthInstanceV10 = static_cast<UserAuthInstanceV10 *>(data);
351             if (userAuthInstanceV10 != nullptr) {
352                 delete userAuthInstanceV10;
353             }
354         },
355         nullptr, nullptr));
356     userAuthInstanceV10.release();
357     return thisVar;
358 }
359 
UserAuthInstanceV10Class(napi_env env)360 napi_value UserAuthInstanceV10Class(napi_env env)
361 {
362     napi_value result = nullptr;
363     napi_property_descriptor clzDes[] = {
364         DECLARE_NAPI_FUNCTION("on", UserAuth::OnV10),
365         DECLARE_NAPI_FUNCTION("off", UserAuth::OffV10),
366         DECLARE_NAPI_FUNCTION("start", UserAuth::StartV10),
367         DECLARE_NAPI_FUNCTION("cancel", UserAuth::CancelV10),
368     };
369     NAPI_CALL(env, napi_define_class(env, "UserAuthInstance", NAPI_AUTO_LENGTH, UserAuthInstanceV10Constructor, nullptr,
370         sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &result));
371     return result;
372 }
373 
UnwrapAuthWidgetMgrV10(napi_env env,napi_callback_info info,UserAuthWidgetMgr ** authWidgetMgrV10)374 napi_status UnwrapAuthWidgetMgrV10(napi_env env, napi_callback_info info, UserAuthWidgetMgr **authWidgetMgrV10)
375 {
376     napi_value thisVar = nullptr;
377     size_t argc = ARGS_ONE;
378     napi_value argv[ARGS_ONE] = {nullptr};
379     napi_status ret = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
380     if (ret != napi_ok) {
381         IAM_LOGE("napi_get_cb_info fail");
382         return ret;
383     }
384     ret = napi_unwrap(env, thisVar, reinterpret_cast<void **>(authWidgetMgrV10));
385     if (ret != napi_ok) {
386         IAM_LOGE("napi_unwrap fail");
387         return ret;
388     }
389     if (*authWidgetMgrV10 == nullptr) {
390         IAM_LOGE("authWidgetMgr is null");
391         return napi_generic_failure;
392     }
393     return ret;
394 }
395 
WidgetOn(napi_env env,napi_callback_info info)396 napi_value WidgetOn(napi_env env, napi_callback_info info)
397 {
398     IAM_LOGI("widgetOn");
399     UserAuthWidgetMgr *authWidgetMgr = nullptr;
400     napi_status ret = UnwrapAuthWidgetMgrV10(env, info, &authWidgetMgr);
401     if (ret != napi_ok) {
402         IAM_LOGE("UnwrapAuthWidgetMgrV10 fail:%{public}d", ret);
403         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
404         return nullptr;
405     }
406     UserAuthResultCode code = authWidgetMgr->On(env, info);
407     if (code != UserAuthResultCode::SUCCESS) {
408         IAM_LOGE("widgetOn fail:%{public}d", static_cast<int32_t>(code));
409         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
410     }
411     return nullptr;
412 }
413 
WidgetOff(napi_env env,napi_callback_info info)414 napi_value WidgetOff(napi_env env, napi_callback_info info)
415 {
416     IAM_LOGI("widgetOff");
417     UserAuthWidgetMgr *authWidgetMgr = nullptr;
418     napi_status ret = UnwrapAuthWidgetMgrV10(env, info, &authWidgetMgr);
419     if (ret != napi_ok) {
420         IAM_LOGE("UnwrapAuthWidgetMgrV10 fail:%{public}d", ret);
421         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
422         return nullptr;
423     }
424     UserAuthResultCode code = authWidgetMgr->Off(env, info);
425     if (code != UserAuthResultCode::SUCCESS) {
426         IAM_LOGE("widgetOff fail:%{public}d", static_cast<int32_t>(code));
427         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
428     }
429     return nullptr;
430 }
431 
VerifyNoticeParam(const std::string & eventData)432 static bool VerifyNoticeParam(const std::string &eventData)
433 {
434     auto json = nlohmann::json::parse(eventData.c_str(), nullptr, false);
435     if (json.is_null() || json.is_discarded()) {
436         IAM_LOGE("Notice data is invalid json object");
437         return false;
438     }
439 
440     if (json.find(NOTICE_EVENT_TYPE) == json.end() || !json[NOTICE_EVENT_TYPE].is_string()) {
441         IAM_LOGE("Invalid event type exist in notice data");
442         return false;
443     }
444 
445     if (json.find(NOTICE_PAYLOAD) == json.end() ||
446         json[NOTICE_PAYLOAD].find(NOTICE_PAYLOAD_TYPE) == json[NOTICE_PAYLOAD].end() ||
447         !json[NOTICE_PAYLOAD][NOTICE_PAYLOAD_TYPE].is_array()) {
448         IAM_LOGE("Invalid payload exist in notice data");
449         return false;
450     }
451     IAM_LOGI("valid notice parameter");
452     return true;
453 }
454 
ResultOfSendNotice(napi_env env,UserAuthResultCode errCode)455 napi_value ResultOfSendNotice(napi_env env, UserAuthResultCode errCode)
456 {
457     napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, errCode));
458     return nullptr;
459 }
460 
SendNotice(napi_env env,napi_callback_info info)461 napi_value SendNotice(napi_env env, napi_callback_info info)
462 {
463     IAM_LOGI("start SendNotice");
464     UserAuthResultCode errCode = UserAuthResultCode::SUCCESS;
465 
466     napi_value argv[ARGS_TWO];
467     size_t argc = ARGS_TWO;
468     napi_status ret = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
469     if (ret != napi_ok) {
470         IAM_LOGE("napi_get_cb_info fail:%{public}d", ret);
471         return ResultOfSendNotice(env, UserAuthResultCode::GENERAL_ERROR);
472     }
473     if (argc != ARGS_TWO) {
474         IAM_LOGE("invalid param, argc:%{public}zu", argc);
475         std::string msgStr = "Parameter error. The number of parameters should be 2";
476         napi_throw(env, UserAuthNapiHelper::GenerateErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr));
477         return nullptr;
478     }
479 
480     NoticeType noticeType = NoticeType::WIDGET_NOTICE;
481     int32_t noticeType_value = 0;
482     ret = UserAuthNapiHelper::GetInt32Value(env, argv[PARAM0], noticeType_value);
483     if (ret != napi_ok) {
484         IAM_LOGE("GetStrValue fail:%{public}d", ret);
485         std::string msgStr = "Parameter error. The type of \"noticeType\" must be NoticeType.";
486         napi_throw(env, UserAuthNapiHelper::GenerateErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr));
487         return nullptr;
488     }
489     IAM_LOGI("recv SendNotice noticeType:%{public}d", noticeType_value);
490 
491     if (noticeType_value != WIDGET_NOTICE) {
492         std::string msgStr = "Parameter error. The value of \"noticeType\" must be NoticeType.WIDGET_NOTICE.";
493         napi_throw(env, UserAuthNapiHelper::GenerateErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr));
494         return nullptr;
495     }
496 
497     std::string eventData = UserAuthNapiHelper::GetStringFromValueUtf8(env, argv[PARAM1]);
498     IAM_LOGI("recv SendNotice eventData:%{public}s", eventData.c_str());
499     if (!VerifyNoticeParam(eventData)) {
500         IAM_LOGE("Invalid notice parameter");
501         std::string msgStr = "Parameter error. The value of \"eventData\" for WIDGET_NOTICE must be json string.";
502         napi_throw(env, UserAuthNapiHelper::GenerateErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr));
503         return nullptr;
504     }
505 
506     int32_t result = UserAuthClientImpl::Instance().Notice(noticeType, eventData);
507     if (result != ResultCode::SUCCESS) {
508         errCode = UserAuthResultCode(UserAuthNapiHelper::GetResultCodeV10(result));
509         IAM_LOGE("SendNotice fail. result: %{public}d, errCode: %{public}d", result, errCode);
510         return ResultOfSendNotice(env, errCode);
511     }
512     IAM_LOGI("end SendNotice");
513     return nullptr;
514 }
515 
UserAuthWidgetMgrV10Constructor(napi_env env,napi_callback_info info)516 napi_value UserAuthWidgetMgrV10Constructor(napi_env env, napi_callback_info info)
517 {
518     IAM_LOGI("start");
519     std::unique_ptr<UserAuthWidgetMgr> userAuthWidgetMgrV10 {new (std::nothrow) UserAuthWidgetMgr(env)};
520     if (userAuthWidgetMgrV10 == nullptr) {
521         IAM_LOGE("userAuthWidgetMgrV10 is nullptr");
522         return nullptr;
523     }
524 
525     napi_value thisVar = nullptr;
526     size_t argc = ARGS_ONE;
527     napi_value argv[ARGS_ONE] = {nullptr};
528     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
529     NAPI_CALL(env, napi_wrap(env, thisVar, userAuthWidgetMgrV10.get(),
530         [](napi_env env, void *data, void *hint) {
531             UserAuthWidgetMgr *userAuthWidgetMgrV10 = static_cast<UserAuthWidgetMgr *>(data);
532             if (userAuthWidgetMgrV10 != nullptr) {
533                 delete userAuthWidgetMgrV10;
534             }
535         },
536         nullptr, nullptr));
537     userAuthWidgetMgrV10.release();
538     return thisVar;
539 }
540 
UserAuthWidgetMgrV10Class(napi_env env)541 napi_value UserAuthWidgetMgrV10Class(napi_env env)
542 {
543     napi_value result = nullptr;
544     napi_property_descriptor clzDes[] = {
545         DECLARE_NAPI_FUNCTION("on", UserAuth::WidgetOn),
546         DECLARE_NAPI_FUNCTION("off", UserAuth::WidgetOff),
547     };
548     NAPI_CALL(env, napi_define_class(env, "UserAuthWidgetMgr", NAPI_AUTO_LENGTH, UserAuthWidgetMgrV10Constructor,
549         nullptr, sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &result));
550     return result;
551 }
552 
GetUserAuthInstanceV10(napi_env env,napi_callback_info info)553 napi_value GetUserAuthInstanceV10(napi_env env, napi_callback_info info)
554 {
555     IAM_LOGI("start");
556     napi_value userAuthInstanceV10;
557     napi_status ret = napi_new_instance(env, UserAuthInstanceV10Class(env), 0, nullptr, &userAuthInstanceV10);
558     if (ret != napi_ok) {
559         IAM_LOGE("napi_new_instance fail:%{public}d", ret);
560         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
561         return nullptr;
562     }
563     UserAuthInstanceV10 *userAuthInstance = nullptr;
564     ret = napi_unwrap(env, userAuthInstanceV10, reinterpret_cast<void **>(&userAuthInstance));
565     if (ret != napi_ok) {
566         IAM_LOGE("napi_unwrap fail");
567         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
568         return nullptr;
569     }
570     if (userAuthInstance == nullptr) {
571         IAM_LOGE("userAuthInstanceV10 is null");
572         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
573         return nullptr;
574     }
575     UserAuthResultCode code = userAuthInstance->Init(env, info);
576     if (code != UserAuthResultCode::SUCCESS) {
577         IAM_LOGE("Init fail:%{public}d", static_cast<int32_t>(code));
578         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
579         return nullptr;
580     }
581 
582     IAM_LOGE("GetUserAuthInstanceV10 SUCCESS");
583     return userAuthInstanceV10;
584 }
585 
GetUserAuthWidgetMgrV10(napi_env env,napi_callback_info info)586 napi_value GetUserAuthWidgetMgrV10(napi_env env, napi_callback_info info)
587 {
588     IAM_LOGI("start");
589     napi_value userAuthWidgetMgrV10;
590     napi_status ret = napi_new_instance(env, UserAuthWidgetMgrV10Class(env), 0, nullptr, &userAuthWidgetMgrV10);
591     if (ret != napi_ok) {
592         IAM_LOGE("napi_new_instance fail:%{public}d", ret);
593         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
594         return nullptr;
595     }
596     UserAuthWidgetMgr *userAuthWidgetMgr = nullptr;
597     ret = napi_unwrap(env, userAuthWidgetMgrV10, reinterpret_cast<void **>(&userAuthWidgetMgr));
598     if (ret != napi_ok) {
599         IAM_LOGE("napi_unwrap fail");
600         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
601         return nullptr;
602     }
603     if (userAuthWidgetMgr == nullptr) {
604         IAM_LOGE("userAuthWidgetMgr is null");
605         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, UserAuthResultCode::GENERAL_ERROR));
606         return nullptr;
607     }
608     UserAuthResultCode code = userAuthWidgetMgr->Init(env, info);
609     if (code != UserAuthResultCode::SUCCESS) {
610         IAM_LOGE("Init fail:%{public}d", static_cast<int32_t>(code));
611         napi_throw(env, UserAuthNapiHelper::GenerateBusinessErrorV9(env, code));
612         return nullptr;
613     }
614     IAM_LOGI("end");
615     return userAuthWidgetMgrV10;
616 }
617 
Auth(napi_env env,napi_callback_info info)618 napi_value Auth(napi_env env, napi_callback_info info)
619 {
620     IAM_LOGI("start");
621     return UserAuthImpl::Auth(env, info);
622 }
623 
Execute(napi_env env,napi_callback_info info)624 napi_value Execute(napi_env env, napi_callback_info info)
625 {
626     IAM_LOGI("start");
627     return UserAuthImpl::Execute(env, info);
628 }
629 
CancelAuth(napi_env env,napi_callback_info info)630 napi_value CancelAuth(napi_env env, napi_callback_info info)
631 {
632     IAM_LOGI("start");
633     return UserAuthImpl::CancelAuth(env, info);
634 }
635 
AuthTrustLevelConstructor(napi_env env)636 napi_value AuthTrustLevelConstructor(napi_env env)
637 {
638     napi_value authTrustLevel = nullptr;
639     napi_value atl1 = nullptr;
640     napi_value atl2 = nullptr;
641     napi_value atl3 = nullptr;
642     napi_value atl4 = nullptr;
643     NAPI_CALL(env, napi_create_object(env, &authTrustLevel));
644     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthTrustLevel::ATL1), &atl1));
645     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthTrustLevel::ATL2), &atl2));
646     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthTrustLevel::ATL3), &atl3));
647     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthTrustLevel::ATL4), &atl4));
648     NAPI_CALL(env, napi_set_named_property(env, authTrustLevel, "ATL1", atl1));
649     NAPI_CALL(env, napi_set_named_property(env, authTrustLevel, "ATL2", atl2));
650     NAPI_CALL(env, napi_set_named_property(env, authTrustLevel, "ATL3", atl3));
651     NAPI_CALL(env, napi_set_named_property(env, authTrustLevel, "ATL4", atl4));
652     return authTrustLevel;
653 }
654 
ResultCodeConstructor(napi_env env)655 napi_value ResultCodeConstructor(napi_env env)
656 {
657     napi_value resultCode = nullptr;
658     napi_value success = nullptr;
659     napi_value fail = nullptr;
660     napi_value generalError = nullptr;
661     napi_value canceled = nullptr;
662     napi_value timeout = nullptr;
663     napi_value typeNotSupport = nullptr;
664     napi_value trustLevelNotSupport = nullptr;
665     napi_value busy = nullptr;
666     napi_value invalidParameters = nullptr;
667     napi_value locked = nullptr;
668     napi_value notEnrolled = nullptr;
669     napi_value canceledFromWidget = nullptr;
670     NAPI_CALL(env, napi_create_object(env, &resultCode));
671     NAPI_CALL(env, napi_create_int32(env, ResultCode::SUCCESS, &success));
672     NAPI_CALL(env, napi_create_int32(env, ResultCode::FAIL, &fail));
673     NAPI_CALL(env, napi_create_int32(env, ResultCode::GENERAL_ERROR, &generalError));
674     NAPI_CALL(env, napi_create_int32(env, ResultCode::CANCELED, &canceled));
675     NAPI_CALL(env, napi_create_int32(env, ResultCode::TIMEOUT, &timeout));
676     NAPI_CALL(env, napi_create_int32(env, ResultCode::TYPE_NOT_SUPPORT, &typeNotSupport));
677     NAPI_CALL(env, napi_create_int32(env, ResultCode::TRUST_LEVEL_NOT_SUPPORT, &trustLevelNotSupport));
678     NAPI_CALL(env, napi_create_int32(env, ResultCode::BUSY, &busy));
679     NAPI_CALL(env, napi_create_int32(env, ResultCode::INVALID_PARAMETERS, &invalidParameters));
680     NAPI_CALL(env, napi_create_int32(env, ResultCode::LOCKED, &locked));
681     NAPI_CALL(env, napi_create_int32(env, ResultCode::NOT_ENROLLED, &notEnrolled));
682     NAPI_CALL(env, napi_create_int32(env, ResultCode::CANCELED_FROM_WIDGET, &canceledFromWidget));
683     NAPI_CALL(env, napi_set_named_property(env, resultCode, "SUCCESS", success));
684     NAPI_CALL(env, napi_set_named_property(env, resultCode, "FAIL", fail));
685     NAPI_CALL(env, napi_set_named_property(env, resultCode, "GENERAL_ERROR", generalError));
686     NAPI_CALL(env, napi_set_named_property(env, resultCode, "CANCELED", canceled));
687     NAPI_CALL(env, napi_set_named_property(env, resultCode, "TIMEOUT", timeout));
688     NAPI_CALL(env, napi_set_named_property(env, resultCode, "TYPE_NOT_SUPPORT", typeNotSupport));
689     NAPI_CALL(env, napi_set_named_property(env, resultCode, "TRUST_LEVEL_NOT_SUPPORT", trustLevelNotSupport));
690     NAPI_CALL(env, napi_set_named_property(env, resultCode, "BUSY", busy));
691     NAPI_CALL(env, napi_set_named_property(env, resultCode, "INVALID_PARAMETERS", invalidParameters));
692     NAPI_CALL(env, napi_set_named_property(env, resultCode, "LOCKED", locked));
693     NAPI_CALL(env, napi_set_named_property(env, resultCode, "NOT_ENROLLED", notEnrolled));
694     NAPI_CALL(env, napi_set_named_property(env, resultCode, "CANCELED_FROM_WIDGET", canceledFromWidget));
695     return resultCode;
696 }
697 
UserAuthResultCodeExtConstructor(napi_env env,napi_value resultCode)698 napi_value UserAuthResultCodeExtConstructor(napi_env env, napi_value resultCode)
699 {
700     napi_value pinExpired = nullptr;
701     napi_value authTokenCheckFailed = nullptr;
702     napi_value authTokenExpired = nullptr;
703     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(UserAuthResultCode::PIN_EXPIRED), &pinExpired));
704     NAPI_CALL(env, napi_create_int32(env,
705         static_cast<int32_t>(UserAuthResultCode::AUTH_TOKEN_CHECK_FAILED), &authTokenCheckFailed));
706     NAPI_CALL(env, napi_create_int32(env,
707         static_cast<int32_t>(UserAuthResultCode::AUTH_TOKEN_EXPIRED), &authTokenExpired));
708     NAPI_CALL(env, napi_set_named_property(env, resultCode, "PIN_EXPIRED", pinExpired));
709     NAPI_CALL(env, napi_set_named_property(env, resultCode, "AUTH_TOKEN_CHECK_FAILED", authTokenCheckFailed));
710     NAPI_CALL(env, napi_set_named_property(env, resultCode, "AUTH_TOKEN_EXPIRED", authTokenExpired));
711     return resultCode;
712 }
713 
UserAuthResultCodeConstructor(napi_env env)714 napi_value UserAuthResultCodeConstructor(napi_env env)
715 {
716     napi_value resultCode = nullptr;
717     napi_value success = nullptr;
718     napi_value fail = nullptr;
719     napi_value generalError = nullptr;
720     napi_value canceled = nullptr;
721     napi_value timeout = nullptr;
722     napi_value typeNotSupport = nullptr;
723     napi_value trustLevelNotSupport = nullptr;
724     napi_value busy = nullptr;
725     napi_value locked = nullptr;
726     napi_value notEnrolled = nullptr;
727     napi_value canceledFromWidget = nullptr;
728     NAPI_CALL(env, napi_create_object(env, &resultCode));
729     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(UserAuthResultCode::SUCCESS), &success));
730     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(UserAuthResultCode::FAIL), &fail));
731     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR), &generalError));
732     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(UserAuthResultCode::CANCELED), &canceled));
733     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(UserAuthResultCode::TIMEOUT), &timeout));
734     NAPI_CALL(env, napi_create_int32(env,
735         static_cast<int32_t>(UserAuthResultCode::TYPE_NOT_SUPPORT), &typeNotSupport));
736     NAPI_CALL(env, napi_create_int32(env,
737         static_cast<int32_t>(UserAuthResultCode::TRUST_LEVEL_NOT_SUPPORT), &trustLevelNotSupport));
738     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(UserAuthResultCode::BUSY), &busy));
739     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(UserAuthResultCode::LOCKED), &locked));
740     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(UserAuthResultCode::NOT_ENROLLED), &notEnrolled));
741     NAPI_CALL(env, napi_create_int32(env,
742         static_cast<int32_t>(UserAuthResultCode::CANCELED_FROM_WIDGET), &canceledFromWidget));
743     NAPI_CALL(env, napi_set_named_property(env, resultCode, "SUCCESS", success));
744     NAPI_CALL(env, napi_set_named_property(env, resultCode, "FAIL", fail));
745     NAPI_CALL(env, napi_set_named_property(env, resultCode, "GENERAL_ERROR", generalError));
746     NAPI_CALL(env, napi_set_named_property(env, resultCode, "CANCELED", canceled));
747     NAPI_CALL(env, napi_set_named_property(env, resultCode, "TIMEOUT", timeout));
748     NAPI_CALL(env, napi_set_named_property(env, resultCode, "TYPE_NOT_SUPPORT", typeNotSupport));
749     NAPI_CALL(env, napi_set_named_property(env, resultCode, "TRUST_LEVEL_NOT_SUPPORT", trustLevelNotSupport));
750     NAPI_CALL(env, napi_set_named_property(env, resultCode, "BUSY", busy));
751     NAPI_CALL(env, napi_set_named_property(env, resultCode, "LOCKED", locked));
752     NAPI_CALL(env, napi_set_named_property(env, resultCode, "NOT_ENROLLED", notEnrolled));
753     NAPI_CALL(env, napi_set_named_property(env, resultCode, "CANCELED_FROM_WIDGET", canceledFromWidget));
754     return UserAuthResultCodeExtConstructor(env, resultCode);
755 }
756 
AuthenticationResultConstructor(napi_env env)757 napi_value AuthenticationResultConstructor(napi_env env)
758 {
759     napi_value resultCode = nullptr;
760     napi_value noSupport = nullptr;
761     napi_value success = nullptr;
762     napi_value compareFailure = nullptr;
763     napi_value canceled = nullptr;
764     napi_value timeout = nullptr;
765     napi_value cameraFail = nullptr;
766     napi_value busy = nullptr;
767     napi_value invalidParameters = nullptr;
768     napi_value locked = nullptr;
769     napi_value notEnrolled = nullptr;
770     napi_value generalError = nullptr;
771     NAPI_CALL(env, napi_create_object(env, &resultCode));
772     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthenticationResult::NO_SUPPORT), &noSupport));
773     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthenticationResult::SUCCESS), &success));
774     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthenticationResult::COMPARE_FAILURE),
775         &compareFailure));
776     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthenticationResult::CANCELED), &canceled));
777     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthenticationResult::TIMEOUT), &timeout));
778     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthenticationResult::CAMERA_FAIL), &cameraFail));
779     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthenticationResult::BUSY), &busy));
780     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthenticationResult::INVALID_PARAMETERS),
781         &invalidParameters));
782     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthenticationResult::LOCKED), &locked));
783     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthenticationResult::NOT_ENROLLED), &notEnrolled));
784     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthenticationResult::GENERAL_ERROR), &generalError));
785     NAPI_CALL(env, napi_set_named_property(env, resultCode, "NO_SUPPORT", noSupport));
786     NAPI_CALL(env, napi_set_named_property(env, resultCode, "SUCCESS", success));
787     NAPI_CALL(env, napi_set_named_property(env, resultCode, "COMPARE_FAILURE", compareFailure));
788     NAPI_CALL(env, napi_set_named_property(env, resultCode, "CANCELED", canceled));
789     NAPI_CALL(env, napi_set_named_property(env, resultCode, "TIMEOUT", timeout));
790     NAPI_CALL(env, napi_set_named_property(env, resultCode, "CAMERA_FAIL", cameraFail));
791     NAPI_CALL(env, napi_set_named_property(env, resultCode, "BUSY", busy));
792     NAPI_CALL(env, napi_set_named_property(env, resultCode, "INVALID_PARAMETERS", invalidParameters));
793     NAPI_CALL(env, napi_set_named_property(env, resultCode, "LOCKED", locked));
794     NAPI_CALL(env, napi_set_named_property(env, resultCode, "NOT_ENROLLED", notEnrolled));
795     NAPI_CALL(env, napi_set_named_property(env, resultCode, "GENERAL_ERROR", generalError));
796     return resultCode;
797 }
798 
FaceTipsCodeConstructor(napi_env env)799 napi_value FaceTipsCodeConstructor(napi_env env)
800 {
801     napi_value faceTipsCode = nullptr;
802     napi_value faceAuthTipTooBright = nullptr;
803     napi_value faceAuthTipTooDark = nullptr;
804     napi_value faceAuthTipTooClose = nullptr;
805     napi_value faceAuthTipTooFar = nullptr;
806     napi_value faceAuthTipTooHigh = nullptr;
807     napi_value faceAuthTipTooLow = nullptr;
808     napi_value faceAuthTipTooRight = nullptr;
809     napi_value faceAuthTipTooLeft = nullptr;
810     napi_value faceAuthTipTooMuchMotion = nullptr;
811     napi_value faceAuthTipPoorGaze = nullptr;
812     napi_value faceAuthTipNotDetected = nullptr;
813     NAPI_CALL(env, napi_create_object(env, &faceTipsCode));
814     NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_BRIGHT, &faceAuthTipTooBright));
815     NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_DARK, &faceAuthTipTooDark));
816     NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_CLOSE, &faceAuthTipTooClose));
817     NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_FAR, &faceAuthTipTooFar));
818     NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_HIGH, &faceAuthTipTooHigh));
819     NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_LOW, &faceAuthTipTooLow));
820     NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_RIGHT, &faceAuthTipTooRight));
821     NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_LEFT, &faceAuthTipTooLeft));
822     NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_MUCH_MOTION, &faceAuthTipTooMuchMotion));
823     NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_POOR_GAZE, &faceAuthTipPoorGaze));
824     NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_NOT_DETECTED, &faceAuthTipNotDetected));
825     NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_BRIGHT", faceAuthTipTooBright));
826     NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_DARK", faceAuthTipTooDark));
827     NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_CLOSE", faceAuthTipTooClose));
828     NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_FAR", faceAuthTipTooFar));
829     NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_HIGH", faceAuthTipTooHigh));
830     NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_LOW", faceAuthTipTooLow));
831     NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_RIGHT", faceAuthTipTooRight));
832     NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_LEFT", faceAuthTipTooLeft));
833     NAPI_CALL(env, napi_set_named_property(env, faceTipsCode,
834         "FACE_AUTH_TIP_TOO_MUCH_MOTION", faceAuthTipTooMuchMotion));
835     NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_POOR_GAZE", faceAuthTipPoorGaze));
836     NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_NOT_DETECTED", faceAuthTipNotDetected));
837     return faceTipsCode;
838 }
839 
FingerprintTipsConstructorForKits(napi_env env)840 napi_value FingerprintTipsConstructorForKits(napi_env env)
841 {
842     napi_value fingerprintTips = nullptr;
843     napi_value fingerprintTipGood = nullptr;
844     napi_value fingerprintTipImagerDirty = nullptr;
845     napi_value fingerprintTipInsufficient = nullptr;
846     napi_value fingerprintTipPartial = nullptr;
847     napi_value fingerprintTipTooFast = nullptr;
848     napi_value fingerprintTipTooSlow = nullptr;
849     NAPI_CALL(env, napi_create_object(env, &fingerprintTips));
850     NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_GOOD, &fingerprintTipGood));
851     NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_IMAGER_DIRTY,
852         &fingerprintTipImagerDirty));
853     NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_INSUFFICIENT,
854         &fingerprintTipInsufficient));
855     NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_PARTIAL, &fingerprintTipPartial));
856     NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_TOO_FAST, &fingerprintTipTooFast));
857     NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_TOO_SLOW, &fingerprintTipTooSlow));
858     NAPI_CALL(env, napi_set_named_property(env, fingerprintTips, "FINGERPRINT_AUTH_TIP_GOOD", fingerprintTipGood));
859     NAPI_CALL(env, napi_set_named_property(env, fingerprintTips,
860         "FINGERPRINT_AUTH_TIP_DIRTY", fingerprintTipImagerDirty));
861     NAPI_CALL(env, napi_set_named_property(env, fingerprintTips,
862         "FINGERPRINT_AUTH_TIP_INSUFFICIENT", fingerprintTipInsufficient));
863     NAPI_CALL(env, napi_set_named_property(env, fingerprintTips,
864         "FINGERPRINT_AUTH_TIP_PARTIAL", fingerprintTipPartial));
865     NAPI_CALL(env, napi_set_named_property(env, fingerprintTips,
866         "FINGERPRINT_AUTH_TIP_TOO_FAST", fingerprintTipTooFast));
867     NAPI_CALL(env, napi_set_named_property(env, fingerprintTips,
868         "FINGERPRINT_AUTH_TIP_TOO_SLOW", fingerprintTipTooSlow));
869     return fingerprintTips;
870 }
871 
UserAuthTypeConstructor(napi_env env)872 napi_value UserAuthTypeConstructor(napi_env env)
873 {
874     napi_value userAuthType = nullptr;
875     napi_value pin = nullptr;
876     napi_value face = nullptr;
877     napi_value fingerprint = nullptr;
878     napi_value privatePin = nullptr;
879     NAPI_CALL(env, napi_create_object(env, &userAuthType));
880     NAPI_CALL(env, napi_create_int32(env, AuthType::PIN, &pin));
881     NAPI_CALL(env, napi_create_int32(env, AuthType::FACE, &face));
882     NAPI_CALL(env, napi_create_int32(env, AuthType::FINGERPRINT, &fingerprint));
883     NAPI_CALL(env, napi_create_int32(env, AuthType::PRIVATE_PIN, &privatePin));
884     NAPI_CALL(env, napi_set_named_property(env, userAuthType, "PIN", pin));
885     NAPI_CALL(env, napi_set_named_property(env, userAuthType, "FACE", face));
886     NAPI_CALL(env, napi_set_named_property(env, userAuthType, "FINGERPRINT", fingerprint));
887     NAPI_CALL(env, napi_set_named_property(env, userAuthType, "PRIVATE_PIN", privatePin));
888     return userAuthType;
889 }
890 
NoticeTypeConstructor(napi_env env)891 napi_value NoticeTypeConstructor(napi_env env)
892 {
893     napi_value noticeType = nullptr;
894     napi_value widget_notice = nullptr;
895     NAPI_CALL(env, napi_create_object(env, &noticeType));
896     NAPI_CALL(env, napi_create_int32(env, NoticeType::WIDGET_NOTICE, &widget_notice));
897     NAPI_CALL(env, napi_set_named_property(env, noticeType, "WIDGET_NOTICE", widget_notice));
898     return noticeType;
899 }
900 
WindowModeTypeConstructor(napi_env env)901 napi_value WindowModeTypeConstructor(napi_env env)
902 {
903     napi_value windowModeType = nullptr;
904     napi_value dialog_box = nullptr;
905     napi_value fullscreen = nullptr;
906     napi_value none_interruption_dialog_box = nullptr;
907     NAPI_CALL(env, napi_create_object(env, &windowModeType));
908     NAPI_CALL(env, napi_create_int32(env, WindowModeType::DIALOG_BOX, &dialog_box));
909     NAPI_CALL(env, napi_create_int32(env, WindowModeType::FULLSCREEN, &fullscreen));
910     NAPI_CALL(env, napi_create_int32(env, WindowModeType::NONE_INTERRUPTION_DIALOG_BOX,
911         &none_interruption_dialog_box));
912     NAPI_CALL(env, napi_set_named_property(env, windowModeType, "DIALOG_BOX", dialog_box));
913     NAPI_CALL(env, napi_set_named_property(env, windowModeType, "FULLSCREEN", fullscreen));
914     NAPI_CALL(env, napi_set_named_property(env, windowModeType, "NONE_INTERRUPTION_DIALOG_BOX",
915         none_interruption_dialog_box));
916     return windowModeType;
917 }
918 
ReuseModeConstructor(napi_env env)919 napi_value ReuseModeConstructor(napi_env env)
920 {
921     napi_value reuseMode = nullptr;
922     napi_value auth_type_relevant = nullptr;
923     napi_value auth_type_irrelevant = nullptr;
924     napi_value caller_irrelevant_auth_type_relevant = nullptr;
925     napi_value caller_irrelevant_auth_type_irrelevant = nullptr;
926     NAPI_CALL(env, napi_create_object(env, &reuseMode));
927     NAPI_CALL(env, napi_create_int32(env, ReuseMode::AUTH_TYPE_RELEVANT, &auth_type_relevant));
928     NAPI_CALL(env, napi_create_int32(env, ReuseMode::AUTH_TYPE_IRRELEVANT, &auth_type_irrelevant));
929     NAPI_CALL(env, napi_create_int32(
930         env, ReuseMode::CALLER_IRRELEVANT_AUTH_TYPE_RELEVANT, &caller_irrelevant_auth_type_relevant));
931     NAPI_CALL(env, napi_create_int32(
932         env, ReuseMode::CALLER_IRRELEVANT_AUTH_TYPE_IRRELEVANT, &caller_irrelevant_auth_type_irrelevant));
933     NAPI_CALL(env, napi_set_named_property(env, reuseMode, "AUTH_TYPE_RELEVANT", auth_type_relevant));
934     NAPI_CALL(env, napi_set_named_property(env, reuseMode, "AUTH_TYPE_IRRELEVANT", auth_type_irrelevant));
935     NAPI_CALL(env, napi_set_named_property(
936         env, reuseMode, "CALLER_IRRELEVANT_AUTH_TYPE_RELEVANT", caller_irrelevant_auth_type_relevant));
937     NAPI_CALL(env, napi_set_named_property(
938         env, reuseMode, "CALLER_IRRELEVANT_AUTH_TYPE_IRRELEVANT", caller_irrelevant_auth_type_irrelevant));
939     return reuseMode;
940 }
941 
ConstantConstructor(napi_env env)942 napi_value ConstantConstructor(napi_env env)
943 {
944     napi_value staticValue = nullptr;
945     const int32_t MAX_ALLOWABLE_REUSE_DURATION = 300000;
946     NAPI_CALL(env, napi_create_int32(env, MAX_ALLOWABLE_REUSE_DURATION, &staticValue));
947     return staticValue;
948 }
949 
GetCtor(napi_env env)950 napi_value GetCtor(napi_env env)
951 {
952     IAM_LOGI("start");
953     napi_value cons = nullptr;
954     napi_property_descriptor clzDes[] = {
955         DECLARE_NAPI_FUNCTION("getVersion", UserAuth::GetVersion),
956         DECLARE_NAPI_FUNCTION("getAvailableStatus", UserAuth::GetAvailableStatus),
957         DECLARE_NAPI_FUNCTION("auth", UserAuth::Auth),
958         DECLARE_NAPI_FUNCTION("cancelAuth", UserAuth::CancelAuth),
959     };
960     NAPI_CALL(env, napi_define_class(env, "UserAuth", NAPI_AUTO_LENGTH, UserAuthServiceConstructor, nullptr,
961         sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons));
962     return cons;
963 }
964 
GetCtorForApi6(napi_env env)965 napi_value GetCtorForApi6(napi_env env)
966 {
967     napi_value cons = nullptr;
968     napi_property_descriptor clzDes[] = {
969         DECLARE_NAPI_FUNCTION("execute", UserAuth::Execute),
970     };
971     NAPI_CALL(env, napi_define_class(env, "UserAuth", NAPI_AUTO_LENGTH, UserAuthServiceConstructor, nullptr,
972         sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons));
973     return cons;
974 }
975 
ConstructorForApi6(napi_env env,napi_callback_info info)976 napi_value ConstructorForApi6(napi_env env, napi_callback_info info)
977 {
978     napi_value userAuthForApi6 = nullptr;
979     NAPI_CALL(env, napi_new_instance(env, GetCtorForApi6(env), 0, nullptr, &userAuthForApi6));
980     return userAuthForApi6;
981 }
982 
UserAuthInit(napi_env env,napi_value exports)983 napi_value UserAuthInit(napi_env env, napi_value exports)
984 {
985     IAM_LOGI("start");
986     napi_status status;
987     napi_property_descriptor exportFuncs[] = {
988         DECLARE_NAPI_FUNCTION("getAuthenticator", UserAuth::ConstructorForApi6),
989         DECLARE_NAPI_FUNCTION("getAvailableStatus", UserAuth::GetAvailableStatusV9),
990         DECLARE_NAPI_FUNCTION("getAuthInstance", UserAuth::GetAuthInstanceV9),
991         DECLARE_NAPI_FUNCTION("getUserAuthInstance", UserAuth::GetUserAuthInstanceV10),
992         DECLARE_NAPI_FUNCTION("getUserAuthWidgetMgr", UserAuth::GetUserAuthWidgetMgrV10),
993         DECLARE_NAPI_FUNCTION("getEnrolledState", UserAuth::GetEnrolledState),
994         DECLARE_NAPI_FUNCTION("sendNotice", UserAuth::SendNotice),
995     };
996     status = napi_define_properties(env, exports,
997         sizeof(exportFuncs) / sizeof(napi_property_descriptor), exportFuncs);
998     if (status != napi_ok) {
999         IAM_LOGE("napi_define_properties failed");
1000         NAPI_CALL(env, status);
1001     }
1002     status = napi_set_named_property(env, exports, "UserAuth", GetCtor(env));
1003     if (status != napi_ok) {
1004         IAM_LOGE("napi_set_named_property failed");
1005         NAPI_CALL(env, status);
1006     }
1007     return exports;
1008 }
1009 
ConstantsExport(napi_env env,napi_value exports)1010 napi_value ConstantsExport(napi_env env, napi_value exports)
1011 {
1012     napi_property_descriptor descriptors[] = {
1013         DECLARE_NAPI_STATIC_PROPERTY("MAX_ALLOWABLE_REUSE_DURATION", ConstantConstructor(env)),
1014     };
1015     NAPI_CALL(env, napi_define_properties(env, exports,
1016         sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors));
1017     return exports;
1018 }
1019 
EnumExport(napi_env env,napi_value exports)1020 napi_value EnumExport(napi_env env, napi_value exports)
1021 {
1022     napi_property_descriptor descriptors[] = {
1023         DECLARE_NAPI_PROPERTY("AuthTrustLevel", AuthTrustLevelConstructor(env)),
1024         DECLARE_NAPI_PROPERTY("ResultCode", ResultCodeConstructor(env)),
1025         DECLARE_NAPI_PROPERTY("UserAuthResultCode", UserAuthResultCodeConstructor(env)),
1026         DECLARE_NAPI_PROPERTY("FingerprintTips", FingerprintTipsConstructorForKits(env)),
1027         DECLARE_NAPI_PROPERTY("UserAuthType", UserAuthTypeConstructor(env)),
1028         DECLARE_NAPI_PROPERTY("FaceTips", FaceTipsCodeConstructor(env)),
1029         DECLARE_NAPI_PROPERTY("AuthenticationResult", AuthenticationResultConstructor(env)),
1030         //add
1031         DECLARE_NAPI_PROPERTY("NoticeType", NoticeTypeConstructor(env)),
1032         DECLARE_NAPI_PROPERTY("WindowModeType", WindowModeTypeConstructor(env)),
1033         DECLARE_NAPI_PROPERTY("ReuseMode", ReuseModeConstructor(env)),
1034     };
1035     NAPI_CALL(env, napi_define_properties(env, exports,
1036         sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors));
1037     return ConstantsExport(env, exports);
1038 }
1039 
ModuleInit(napi_env env,napi_value exports)1040 napi_value ModuleInit(napi_env env, napi_value exports)
1041 {
1042     napi_value val = UserAuthInit(env, exports);
1043     return EnumExport(env, val);
1044 }
1045 } // namespace
1046 
RegisterModule(void)1047 extern "C" __attribute__((constructor)) void RegisterModule(void)
1048 {
1049     napi_module module = {
1050         .nm_version = 1,
1051         .nm_flags = 0,
1052         .nm_filename = nullptr,
1053         .nm_register_func = ModuleInit,
1054         .nm_modname = "userIAM.userAuth",
1055         .nm_priv = nullptr,
1056         .reserved = {}
1057     };
1058     napi_module_register(&module);
1059 }
1060 } // namespace UserAuth
1061 } // namespace UserIam
1062 } // namespace OHOS
1063