1 /*
2 * Copyright (c) 2022 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 "auth_build.h"
17 #include <cinttypes>
18 #include "auth_common.h"
19 #include "userauth_hilog_wrapper.h"
20 #include "auth_object.h"
21
22 namespace OHOS {
23 namespace UserIAM {
24 namespace UserAuth {
25 AuthBuild::AuthBuild() = default;
26 AuthBuild::~AuthBuild() = default;
27
SetPropertyRequestBuild(napi_env env,napi_value object)28 Napi_SetPropertyRequest AuthBuild::SetPropertyRequestBuild(napi_env env, napi_value object)
29 {
30 Napi_SetPropertyRequest request;
31 if (object == nullptr) {
32 USERAUTH_HILOGE(MODULE_JS_NAPI, "SetPropertyRequestBuild object is null");
33 return request;
34 }
35 request.authType_ = convert_.GetInt32ValueByKey(env, object, "authType");
36 request.key_ = static_cast<uint32_t>(convert_.GetInt32ValueByKey(env, object, "key"));
37 request.setInfo_ = convert_.NapiGetValueUint8Array(env, object, "setInfo");
38 USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthBuild::SetPropertyRequestBuild authType = %{public}d", request.authType_);
39 return request;
40 }
41
GetPropertyRequestBuild(napi_env env,napi_value object)42 Napi_GetPropertyRequest AuthBuild::GetPropertyRequestBuild(napi_env env, napi_value object)
43 {
44 Napi_GetPropertyRequest request;
45 if (object == nullptr) {
46 USERAUTH_HILOGE(MODULE_JS_NAPI, "GetPropertyRequestBuild object is null");
47 return request;
48 }
49 request.authType_ = convert_.GetInt32ValueByKey(env, object, "authType");
50 request.keys_ = convert_.GetInt32ArrayValueByKey(env, object, "keys");
51 USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthBuild::GetPropertyRequestBuild authType = %{public}d", request.authType_);
52 return request;
53 }
54
NapiTypeObject(napi_env env,napi_value value)55 bool AuthBuild::NapiTypeObject(napi_env env, napi_value value)
56 {
57 if (value == nullptr) {
58 return false;
59 }
60 napi_valuetype isObject = convert_.GetType(env, value);
61 if (isObject == napi_object) {
62 return true;
63 }
64 return false;
65 }
66
NapiTypeNumber(napi_env env,napi_value value)67 bool AuthBuild::NapiTypeNumber(napi_env env, napi_value value)
68 {
69 if (value == nullptr) {
70 return false;
71 }
72 napi_valuetype isNumber = convert_.GetType(env, value);
73 if (isNumber == napi_number) {
74 return true;
75 }
76 return false;
77 }
78
GetUint8ArrayTo64(napi_env env,napi_value value)79 uint64_t AuthBuild::GetUint8ArrayTo64(napi_env env, napi_value value)
80 {
81 napi_typedarray_type arraytype;
82 size_t length = 0;
83 napi_value buffer = nullptr;
84 size_t offset = 0;
85 uint8_t *data = nullptr;
86 bool isTypedArray = false;
87 napi_is_typedarray(env, value, &isTypedArray);
88 if (!isTypedArray) {
89 USERAUTH_HILOGE(MODULE_JS_NAPI, "GetUint8ArrayTo64 value is not typedarray");
90 return 0;
91 }
92 napi_get_typedarray_info(env, value, &arraytype, &length, reinterpret_cast<void **>(&data), &buffer, &offset);
93 if (arraytype != napi_uint8_array) {
94 USERAUTH_HILOGE(MODULE_JS_NAPI, "GetUint8ArrayTo64 js value is not uint8Array");
95 return 0;
96 }
97 if (offset != 0) {
98 USERAUTH_HILOGE(MODULE_JS_NAPI, "offset is %{public}zu", offset);
99 return 0;
100 }
101 std::vector<uint8_t> result(data, data + length);
102 uint8_t tmp[sizeof(uint64_t)];
103 for (uint32_t i = 0; i < sizeof(uint64_t); i++) {
104 tmp[i] = result[i];
105 USERAUTH_HILOGI(MODULE_JS_NAPI, "GetUint8ArrayTo64 result is %{public}u", (unsigned)result[i]);
106 }
107 uint64_t *re = static_cast<uint64_t *>(static_cast<void *>(tmp));
108 USERAUTH_HILOGI(MODULE_JS_NAPI, "GetUint8ArrayTo64 resultUint64 is %{public}04" PRIx64 "", *re);
109 return *re;
110 }
111
NapiGetValueInt32(napi_env env,napi_value value)112 int32_t AuthBuild::NapiGetValueInt32(napi_env env, napi_value value)
113 {
114 return convert_.NapiGetValueInt32(env, value);
115 }
116
Uint64ToUint8Array(napi_env env,uint64_t value)117 napi_value AuthBuild::Uint64ToUint8Array(napi_env env, uint64_t value)
118 {
119 return convert_.Uint64ToUint8Napi(env, value);
120 }
121 } // namespace UserAuth
122 } // namespace UserIAM
123 } // namespace OHOS
124