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