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