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