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 "user_auth_napi_helper.h"
17
18 #include <cinttypes>
19 #include <uv.h>
20
21 #include "securec.h"
22
23 #include "napi/native_api.h"
24 #include "napi/native_common.h"
25 #include "napi/native_node_api.h"
26
27 #include "iam_logger.h"
28 #include "iam_ptr.h"
29
30 #define LOG_TAG "USER_AUTH_NAPI"
31
32 namespace OHOS {
33 namespace UserIam {
34 namespace UserAuth {
35 namespace {
36 static constexpr const int MAX_STRING_LENGTH = 65536;
37 const std::map<UserAuthResultCode, std::string> g_resultV92Str = {
38 {UserAuthResultCode::OHOS_INVALID_PARAM, "Invalid authentication parameters."},
39 {UserAuthResultCode::OHOS_CHECK_PERMISSION_FAILED, "Permission denied."},
40 {UserAuthResultCode::OHOS_CHECK_SYSTEM_APP_FAILED, "The caller is not a system application."},
41 {UserAuthResultCode::SUCCESS, "Authentication succeeded."},
42 {UserAuthResultCode::FAIL, "Authentication failed."},
43 {UserAuthResultCode::GENERAL_ERROR, "Unknown errors."},
44 {UserAuthResultCode::CANCELED, "Authentication canceled."},
45 {UserAuthResultCode::TIMEOUT, "Authentication timeout."},
46 {UserAuthResultCode::TYPE_NOT_SUPPORT, "Unsupport authentication type."},
47 {UserAuthResultCode::TRUST_LEVEL_NOT_SUPPORT, "Unsupport authentication trust level."},
48 {UserAuthResultCode::BUSY, "Authentication service is busy."},
49 {UserAuthResultCode::LOCKED, "Authentication is lockout."},
50 {UserAuthResultCode::NOT_ENROLLED, "Authentication template has not been enrolled."},
51 {UserAuthResultCode::CANCELED_FROM_WIDGET, "Authentication is canceled from widget."},
52 {UserAuthResultCode::PIN_EXPIRED, "Operation failed because of PIN expired."},
53 {UserAuthResultCode::AUTH_TOKEN_CHECK_FAILED, "Operation failed because of authToken integrity check failed."},
54 {UserAuthResultCode::AUTH_TOKEN_EXPIRED, "Operation failed because of authToken has expired."}
55 };
56
57 struct DeleteRefHolder {
58 napi_env env {nullptr};
59 napi_ref ref {nullptr};
60 };
61 }
62
JsRefHolder(napi_env env,napi_value value)63 JsRefHolder::JsRefHolder(napi_env env, napi_value value)
64 {
65 if (env == nullptr || value == nullptr) {
66 IAM_LOGE("get null ptr");
67 return;
68 }
69 napi_status ret = UserAuthNapiHelper::GetFunctionRef(env, value, ref_);
70 if (ret != napi_ok) {
71 IAM_LOGE("GetFunctionRef fail %{public}d", ret);
72 ref_ = nullptr;
73 return;
74 }
75 env_ = env;
76 }
77
~JsRefHolder()78 JsRefHolder::~JsRefHolder()
79 {
80 if (!IsValid()) {
81 IAM_LOGI("invalid");
82 return;
83 }
84 IAM_LOGI("delete reference");
85 uv_loop_s *loop;
86 napi_status napiStatus = napi_get_uv_event_loop(env_, &loop);
87 if (napiStatus != napi_ok || loop == nullptr) {
88 IAM_LOGE("napi_get_uv_event_loop fail");
89 return;
90 }
91 std::shared_ptr<DeleteRefHolder> deleteRefHolder = Common::MakeShared<DeleteRefHolder>();
92 if (deleteRefHolder == nullptr) {
93 IAM_LOGE("deleteRefHolder is null");
94 return;
95 }
96 deleteRefHolder->env = env_;
97 deleteRefHolder->ref = ref_;
98 auto task = [deleteRefHolder] () {
99 IAM_LOGI("start");
100 if (deleteRefHolder == nullptr) {
101 IAM_LOGE("deleteRefHolder is invalid");
102 return;
103 }
104 napi_status ret = napi_delete_reference(deleteRefHolder->env, deleteRefHolder->ref);
105 if (ret != napi_ok) {
106 IAM_LOGE("napi_delete_reference fail %{public}d", ret);
107 return;
108 }
109 };
110 if (napi_status::napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) {
111 IAM_LOGE("napi_send_event: Failed to SendEvent");
112 }
113 }
114
IsValid() const115 bool JsRefHolder::IsValid() const
116 {
117 return (env_ != nullptr && ref_ != nullptr);
118 }
119
Get() const120 napi_ref JsRefHolder::Get() const
121 {
122 return ref_;
123 }
124
GetResultCodeV8(int32_t result)125 int32_t UserAuthNapiHelper::GetResultCodeV8(int32_t result)
126 {
127 if (result == CHECK_PERMISSION_FAILED) {
128 return static_cast<int32_t>(UserAuthResultCode::OHOS_CHECK_PERMISSION_FAILED);
129 }
130 if (result == PIN_EXPIRED) {
131 return GENERAL_ERROR;
132 }
133 if ((result < SUCCESS) || (result > NOT_ENROLLED)) {
134 return GENERAL_ERROR;
135 }
136 return result;
137 }
138
GetResultCodeV9(int32_t result)139 int32_t UserAuthNapiHelper::GetResultCodeV9(int32_t result)
140 {
141 if (result == CHECK_PERMISSION_FAILED) {
142 return static_cast<int32_t>(UserAuthResultCode::OHOS_CHECK_PERMISSION_FAILED);
143 }
144 if (result == INVALID_PARAMETERS) {
145 return static_cast<int32_t>(UserAuthResultCode::OHOS_INVALID_PARAM);
146 }
147 if (result == PIN_EXPIRED) {
148 return static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR);
149 }
150 if (result > (INT32_MAX - static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V9_MIN))) {
151 return static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR);
152 }
153 int32_t resultCodeV9 = result + static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V9_MIN);
154 if (resultCodeV9 >= static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V9_MIN) &&
155 resultCodeV9 <= static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V9_MAX)) {
156 return resultCodeV9;
157 }
158 return static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR);
159 }
160
GetResultCodeV10(int32_t result)161 int32_t UserAuthNapiHelper::GetResultCodeV10(int32_t result)
162 {
163 if (result == CHECK_PERMISSION_FAILED) {
164 return static_cast<int32_t>(UserAuthResultCode::OHOS_CHECK_PERMISSION_FAILED);
165 }
166 if (result == INVALID_PARAMETERS) {
167 return static_cast<int32_t>(UserAuthResultCode::OHOS_INVALID_PARAM);
168 }
169 if (result == CHECK_SYSTEM_APP_FAILED) {
170 return static_cast<int32_t>(UserAuthResultCode::OHOS_CHECK_SYSTEM_APP_FAILED);
171 }
172 if (result == HARDWARE_NOT_SUPPORTED) {
173 return static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR);
174 }
175 if (result > (INT32_MAX - static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V10_MIN))) {
176 return static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR);
177 }
178 int32_t resultCodeV10 = result + static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V10_MIN);
179 if (resultCodeV10 >= static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V10_MIN) &&
180 resultCodeV10 <= static_cast<int32_t>(UserAuthResultCode::RESULT_CODE_V10_MAX)) {
181 IAM_LOGI("version GetResultCodeV10 resultCodeV10 result: %{public}d", resultCodeV10);
182 return resultCodeV10;
183 }
184 IAM_LOGE("version GetResultCodeV10 resultCodeV10 error");
185 return static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR);
186 }
187
GenerateBusinessErrorV9(napi_env env,UserAuthResultCode result)188 napi_value UserAuthNapiHelper::GenerateBusinessErrorV9(napi_env env, UserAuthResultCode result)
189 {
190 napi_value code;
191 std::string msgStr;
192 auto res = g_resultV92Str.find(result);
193 if (res == g_resultV92Str.end()) {
194 IAM_LOGE("result %{public}d not found", static_cast<int32_t>(result));
195 msgStr = g_resultV92Str.at(UserAuthResultCode::GENERAL_ERROR);
196 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR), &code));
197 } else {
198 msgStr = res->second;
199 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(result), &code));
200 }
201 IAM_LOGI("get msg %{public}s", msgStr.c_str());
202
203 napi_value msg;
204 NAPI_CALL(env, napi_create_string_utf8(env, msgStr.c_str(), NAPI_AUTO_LENGTH, &msg));
205
206 napi_value businessError;
207 NAPI_CALL(env, napi_create_error(env, nullptr, msg, &businessError));
208 NAPI_CALL(env, napi_set_named_property(env, businessError, "code", code));
209
210 return businessError;
211 }
212
GenerateErrorMsg(napi_env env,UserAuthResultCode result,std::string errorMsg)213 napi_value UserAuthNapiHelper::GenerateErrorMsg(napi_env env, UserAuthResultCode result, std::string errorMsg)
214 {
215 napi_value code;
216 std::string msgStr;
217 auto res = g_resultV92Str.find(result);
218 if (res == g_resultV92Str.end()) {
219 IAM_LOGE("result %{public}d not found", static_cast<int32_t>(result));
220 msgStr = g_resultV92Str.at(UserAuthResultCode::GENERAL_ERROR);
221 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(UserAuthResultCode::GENERAL_ERROR), &code));
222 } else {
223 msgStr = errorMsg;
224 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(result), &code));
225 }
226 IAM_LOGI("error msg %{public}s", msgStr.c_str());
227
228 napi_value msg;
229 NAPI_CALL(env, napi_create_string_utf8(env, msgStr.c_str(), NAPI_AUTO_LENGTH, &msg));
230
231 napi_value businessError;
232 NAPI_CALL(env, napi_create_error(env, nullptr, msg, &businessError));
233 NAPI_CALL(env, napi_set_named_property(env, businessError, "code", code));
234
235 return businessError;
236 }
237
ThrowErrorMsg(napi_env env,UserAuthResultCode errorCode,std::string errorMsg)238 UserAuthResultCode UserAuthNapiHelper::ThrowErrorMsg(napi_env env, UserAuthResultCode errorCode, std::string errorMsg)
239 {
240 napi_throw(env, UserAuthNapiHelper::GenerateErrorMsg(env, errorCode, errorMsg));
241 return errorCode;
242 }
243
CheckNapiType(napi_env env,napi_value value,napi_valuetype type)244 napi_status UserAuthNapiHelper::CheckNapiType(napi_env env, napi_value value, napi_valuetype type)
245 {
246 napi_valuetype valuetype;
247 napi_status result = napi_typeof(env, value, &valuetype);
248 if (result != napi_ok) {
249 IAM_LOGE("napi_typeof fail");
250 return result;
251 }
252 if (valuetype != type) {
253 IAM_LOGE("check valuetype fail");
254 return napi_generic_failure;
255 }
256 return napi_ok;
257 }
258
GetInt32Value(napi_env env,napi_value value,int32_t & out)259 napi_status UserAuthNapiHelper::GetInt32Value(napi_env env, napi_value value, int32_t &out)
260 {
261 napi_status result = CheckNapiType(env, value, napi_number);
262 if (result != napi_ok) {
263 IAM_LOGE("CheckNapiType fail");
264 return result;
265 }
266 result = napi_get_value_int32(env, value, &out);
267 if (result != napi_ok) {
268 IAM_LOGE("napi_get_value_int32 fail");
269 }
270 return result;
271 }
272
GetUint32Value(napi_env env,napi_value value,uint32_t & out)273 napi_status UserAuthNapiHelper::GetUint32Value(napi_env env, napi_value value, uint32_t &out)
274 {
275 napi_status result = CheckNapiType(env, value, napi_number);
276 if (result != napi_ok) {
277 IAM_LOGE("CheckNapiType fail");
278 return result;
279 }
280 result = napi_get_value_uint32(env, value, &out);
281 if (result != napi_ok) {
282 IAM_LOGE("napi_get_value_uint32 fail");
283 }
284 return result;
285 }
286
GetStrValue(napi_env env,napi_value value,char * out,size_t & len)287 napi_status UserAuthNapiHelper::GetStrValue(napi_env env, napi_value value, char *out, size_t &len)
288 {
289 if (out == nullptr) {
290 IAM_LOGE("invalid out parameter");
291 return napi_invalid_arg;
292 }
293 napi_status result = CheckNapiType(env, value, napi_string);
294 if (result != napi_ok) {
295 IAM_LOGE("CheckNapiType fail");
296 return result;
297 }
298 size_t maxLen = len;
299 result = napi_get_value_string_utf8(env, value, out, maxLen, &len);
300 if (result != napi_ok) {
301 IAM_LOGE("napi_get_value_string_utf8 fail");
302 }
303 if (maxLen > 0) {
304 out[maxLen - 1] = '\0';
305 }
306 return result;
307 }
308
GetFunctionRef(napi_env env,napi_value value,napi_ref & ref)309 napi_status UserAuthNapiHelper::GetFunctionRef(napi_env env, napi_value value, napi_ref &ref)
310 {
311 napi_status result = CheckNapiType(env, value, napi_function);
312 if (result != napi_ok) {
313 IAM_LOGE("CheckNapiType fail");
314 return result;
315 }
316 result = napi_create_reference(env, value, 1, &ref);
317 if (result != napi_ok) {
318 IAM_LOGE("napi_create_reference fail");
319 }
320 return result;
321 }
322
GetUint8ArrayValue(napi_env env,napi_value value,size_t limitLen,std::vector<uint8_t> & array)323 napi_status UserAuthNapiHelper::GetUint8ArrayValue(napi_env env, napi_value value,
324 size_t limitLen, std::vector<uint8_t> &array)
325 {
326 bool isTypedarray;
327 napi_status result = napi_is_typedarray(env, value, &isTypedarray);
328 if (result != napi_ok) {
329 IAM_LOGE("napi_is_typedarray fail");
330 return result;
331 }
332 if (!isTypedarray) {
333 IAM_LOGE("value is not typedarray");
334 return napi_array_expected;
335 }
336 napi_typedarray_type type;
337 size_t length;
338 void *data;
339 napi_value buffer;
340 size_t offset;
341 result = napi_get_typedarray_info(env, value, &type, &length, &data, &buffer, &offset);
342 if (result != napi_ok) {
343 IAM_LOGE("napi_get_typedarray_info fail");
344 return result;
345 }
346 if (type != napi_uint8_array) {
347 IAM_LOGE("value is not napi_uint8_array");
348 return napi_invalid_arg;
349 }
350 if (length > limitLen) {
351 IAM_LOGE("array length reach limit");
352 return napi_generic_failure;
353 }
354 array.resize(length);
355 if (memcpy_s(array.data(), length, data, length) != EOK) {
356 IAM_LOGE("memcpy_s fail");
357 return napi_generic_failure;
358 }
359 return result;
360 }
361
Uint64ToNapiUint8Array(napi_env env,uint64_t value)362 napi_value UserAuthNapiHelper::Uint64ToNapiUint8Array(napi_env env, uint64_t value)
363 {
364 void *data = nullptr;
365 napi_value arraybuffer = nullptr;
366 size_t length = sizeof(value);
367 NAPI_CALL(env, napi_create_arraybuffer(env, length, &data, &arraybuffer));
368 if (memcpy_s(data, length, reinterpret_cast<const void *>(&value), length) != EOK) {
369 IAM_LOGE("memcpy_s fail");
370 return nullptr;
371 }
372 napi_value result = nullptr;
373 NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, length, arraybuffer, 0, &result));
374 return result;
375 }
376
CallVoidNapiFunc(napi_env env,napi_ref funcRef,size_t argc,const napi_value * argv)377 napi_status UserAuthNapiHelper::CallVoidNapiFunc(napi_env env, napi_ref funcRef, size_t argc, const napi_value *argv)
378 {
379 napi_value funcVal;
380 napi_status ret = napi_get_reference_value(env, funcRef, &funcVal);
381 if (ret != napi_ok) {
382 IAM_LOGE("napi_get_reference_value failed %{public}d", ret);
383 return ret;
384 }
385 napi_value undefined;
386 ret = napi_get_undefined(env, &undefined);
387 if (ret != napi_ok) {
388 IAM_LOGE("napi_get_undefined failed %{public}d", ret);
389 return ret;
390 }
391 napi_value callResult;
392 ret = napi_call_function(env, undefined, funcVal, argc, argv, &callResult);
393 if (ret != napi_ok) {
394 IAM_LOGE("napi_call_function failed %{public}d", ret);
395 }
396 return ret;
397 }
398
SetInt32Property(napi_env env,napi_value obj,const char * name,int32_t value)399 napi_status UserAuthNapiHelper::SetInt32Property(napi_env env, napi_value obj, const char *name, int32_t value)
400 {
401 napi_value napiValue = nullptr;
402 napi_status ret = napi_create_int32(env, value, &napiValue);
403 if (ret != napi_ok) {
404 IAM_LOGE("napi_create_int32 failed %{public}d", ret);
405 return ret;
406 }
407 ret = napi_set_named_property(env, obj, name, napiValue);
408 if (ret != napi_ok) {
409 IAM_LOGE("napi_set_named_property failed %{public}d", ret);
410 }
411 return ret;
412 }
413
SetUint32Property(napi_env env,napi_value obj,const char * name,uint32_t value)414 napi_status UserAuthNapiHelper::SetUint32Property(napi_env env, napi_value obj, const char *name, uint32_t value)
415 {
416 napi_value napiValue = nullptr;
417 napi_status ret = napi_create_uint32(env, value, &napiValue);
418 if (ret != napi_ok) {
419 IAM_LOGE("napi_create_uint32 failed %{public}d", ret);
420 return ret;
421 }
422 ret = napi_set_named_property(env, obj, name, napiValue);
423 if (ret != napi_ok) {
424 IAM_LOGE("napi_set_named_property failed %{public}d", ret);
425 }
426 return ret;
427 }
428
SetUint8ArrayProperty(napi_env env,napi_value obj,const char * name,const std::vector<uint8_t> & value)429 napi_status UserAuthNapiHelper::SetUint8ArrayProperty(napi_env env,
430 napi_value obj, const char *name, const std::vector<uint8_t> &value)
431 {
432 size_t size = value.size();
433 void *data;
434 napi_value buffer;
435 napi_status ret = napi_create_arraybuffer(env, size, &data, &buffer);
436 if (ret != napi_ok) {
437 IAM_LOGE("napi_create_arraybuffer failed %{public}d", ret);
438 return ret;
439 }
440 if (size != 0) {
441 if (memcpy_s(data, size, value.data(), value.size()) != EOK) {
442 IAM_LOGE("memcpy_s failed");
443 return napi_generic_failure;
444 }
445 }
446 napi_value napiValue;
447 ret = napi_create_typedarray(env, napi_uint8_array, size, buffer, 0, &napiValue);
448 if (ret != napi_ok) {
449 IAM_LOGE("napi_create_typedarray failed %{public}d", ret);
450 return ret;
451 }
452 ret = napi_set_named_property(env, obj, name, napiValue);
453 if (ret != napi_ok) {
454 IAM_LOGE("napi_set_named_property failed %{public}d", ret);
455 }
456 return ret;
457 }
458
SetEnrolledStateProperty(napi_env env,napi_value obj,const char * name,EnrolledState & value)459 napi_status UserAuthNapiHelper::SetEnrolledStateProperty(napi_env env, napi_value obj,
460 const char *name, EnrolledState &value)
461 {
462 napi_value napiValue = nullptr;
463 napi_status ret = napi_create_object(env, &napiValue);
464 if (ret != napi_ok) {
465 IAM_LOGE("napi_create_object failed %{public}d", ret);
466 return ret;
467 }
468 int32_t credentialDigest = static_cast<int32_t>(value.credentialDigest);
469 int32_t credentialCount = static_cast<int32_t>(value.credentialCount);
470 IAM_LOGI("get enrolled state success, credentialDigest = %{public}d, credentialCount = %{public}d",
471 credentialDigest, credentialCount);
472 ret = UserAuthNapiHelper::SetInt32Property(env, napiValue, "credentialDigest", credentialDigest);
473 if (ret != napi_ok) {
474 IAM_LOGE("napi_create_int32 failed %{public}d", ret);
475 return ret;
476 }
477 ret = UserAuthNapiHelper::SetInt32Property(env, napiValue, "credentialCount", credentialCount);
478 if (ret != napi_ok) {
479 IAM_LOGE("napi_create_int32 failed %{public}d", ret);
480 return ret;
481 }
482 ret = napi_set_named_property(env, obj, name, napiValue);
483 if (ret != napi_ok) {
484 IAM_LOGE("napi_set_named_property failed %{public}d", ret);
485 }
486 return ret;
487 }
488
GetNamedProperty(napi_env env,napi_value object,const std::string & propertyName)489 napi_value UserAuthNapiHelper::GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName)
490 {
491 napi_value value = nullptr;
492 bool hasProperty = false;
493 NAPI_CALL(env, napi_has_named_property(env, object, propertyName.c_str(), &hasProperty));
494 if (!hasProperty) {
495 return value;
496 }
497 NAPI_CALL(env, napi_get_named_property(env, object, propertyName.c_str(), &value));
498 return value;
499 }
500
GetStringFromValueUtf8(napi_env env,napi_value value)501 std::string UserAuthNapiHelper::GetStringFromValueUtf8(napi_env env, napi_value value)
502 {
503 if (CheckNapiType(env, value, napi_string) != napi_ok) {
504 return "";
505 }
506 std::string result;
507 std::vector<char> str(MAX_STRING_LENGTH + 1, '\0');
508 size_t length = 0;
509 NAPI_CALL(env, napi_get_value_string_utf8(env, value, &str[0], MAX_STRING_LENGTH, &length));
510 if (length > 0) {
511 return result.append(&str[0], length);
512 }
513 return result;
514 }
515
HasNamedProperty(napi_env env,napi_value object,const std::string & propertyName)516 bool UserAuthNapiHelper::HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName)
517 {
518 bool hasProperty = false;
519 NAPI_CALL_BASE(env, napi_has_named_property(env, object, propertyName.c_str(), &hasProperty), false);
520 return hasProperty;
521 }
522
GetStringPropertyUtf8(napi_env env,napi_value object,const std::string & propertyName)523 std::string UserAuthNapiHelper::GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName)
524 {
525 napi_value value = GetNamedProperty(env, object, propertyName);
526 napi_status result = CheckNapiType(env, value, napi_string);
527 if (result != napi_ok) {
528 return "";
529 }
530 return GetStringFromValueUtf8(env, value);
531 }
532
SetStringPropertyUtf8(napi_env env,napi_value object,const std::string & name,const std::string & value)533 bool UserAuthNapiHelper::SetStringPropertyUtf8(
534 napi_env env, napi_value object, const std::string &name, const std::string &value)
535 {
536 napi_value jsValue = nullptr;
537 if (napi_create_string_utf8(env, value.c_str(), strlen(value.c_str()), &jsValue) != napi_ok) {
538 IAM_LOGE("get string error");
539 return false;
540 }
541 napi_valuetype valueType = napi_undefined;
542 NAPI_CALL_BASE(env, napi_typeof(env, jsValue, &valueType), napi_undefined);
543 napi_set_named_property(env, object, name.c_str(), jsValue);
544 return true;
545 }
546
GetInt32Array(napi_env env,napi_value obj,std::vector<uint32_t> vec)547 bool UserAuthNapiHelper::GetInt32Array(napi_env env, napi_value obj, std::vector<uint32_t> vec)
548 {
549 vec.clear();
550 uint32_t len;
551 napi_get_array_length(env, obj, &len);
552 IAM_LOGI("GetInt32Array length: %{public}d", len);
553 for (uint32_t index = 0; index < len; index++) {
554 napi_value value;
555 uint32_t getValue;
556 NAPI_CALL_BASE(env, napi_get_element(env, obj, index, &value), napi_undefined);
557 NAPI_CALL_BASE(env, napi_get_value_uint32(env, value, &getValue), napi_undefined);
558 IAM_LOGI("vec[%{public}d]: %{public}d", index, len);
559 vec.emplace_back(getValue);
560 }
561 return true;
562 }
563
CheckAuthType(int32_t authType)564 bool UserAuthNapiHelper::CheckAuthType(int32_t authType)
565 {
566 if (authType != AuthType::FACE && authType != AuthType::FINGERPRINT) {
567 IAM_LOGE("authType check fail:%{public}d", authType);
568 return false;
569 }
570 return true;
571 }
572
CheckUserAuthType(int32_t authType)573 bool UserAuthNapiHelper::CheckUserAuthType(int32_t authType)
574 {
575 if (authType != AuthType::PIN && authType != AuthType::FACE &&
576 authType != AuthType::FINGERPRINT && authType != AuthType::PRIVATE_PIN) {
577 IAM_LOGE("authType check fail:%{public}d", authType);
578 return false;
579 }
580 return true;
581 }
582
CheckAuthTrustLevel(uint32_t authTrustLevel)583 bool UserAuthNapiHelper::CheckAuthTrustLevel(uint32_t authTrustLevel)
584 {
585 if (authTrustLevel != AuthTrustLevel::ATL1 && authTrustLevel != AuthTrustLevel::ATL2 &&
586 authTrustLevel != AuthTrustLevel::ATL3 && authTrustLevel != AuthTrustLevel::ATL4) {
587 IAM_LOGE("authTrustLevel check fail:%{public}d", authTrustLevel);
588 return false;
589 }
590 return true;
591 }
592
CheckReuseUnlockResult(ReuseUnlockResult reuseUnlockResult)593 bool UserAuthNapiHelper::CheckReuseUnlockResult(ReuseUnlockResult reuseUnlockResult)
594 {
595 if (reuseUnlockResult.reuseMode != ReuseMode::AUTH_TYPE_RELEVANT &&
596 reuseUnlockResult.reuseMode != ReuseMode::AUTH_TYPE_IRRELEVANT &&
597 reuseUnlockResult.reuseMode != ReuseMode::CALLER_IRRELEVANT_AUTH_TYPE_RELEVANT &&
598 reuseUnlockResult.reuseMode != ReuseMode::CALLER_IRRELEVANT_AUTH_TYPE_IRRELEVANT) {
599 IAM_LOGE("reuseMode check fail:%{public}u", reuseUnlockResult.reuseMode);
600 return false;
601 }
602 if (reuseUnlockResult.reuseDuration <= 0 || reuseUnlockResult.reuseDuration > MAX_ALLOWABLE_REUSE_DURATION) {
603 IAM_LOGE("reuseDuration check fail:%{public}" PRIu64, reuseUnlockResult.reuseDuration);
604 return false;
605 }
606 return true;
607 }
608 } // namespace UserAuth
609 } // namespace UserIam
610 } // namespace OHOS