• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "napi_web_data_base.h"
17 
18 #include <cstdint>
19 #include <cstring>
20 #include <vector>
21 
22 #include "business_error.h"
23 #include "napi/native_common.h"
24 #include "nweb_data_base.h"
25 #include "nweb_helper.h"
26 #include "web_errors.h"
27 #include "securec.h"
28 
29 namespace {
30 constexpr int32_t MAX_STRING_LENGTH = 40960;
31 constexpr int32_t MAX_PWD_LENGTH = 256;
32 constexpr int32_t PARAMZERO = 0;
33 constexpr int32_t PARAMONE = 1;
34 constexpr int32_t PARAMTWO = 2;
35 constexpr int32_t PARAMTHREE = 3;
36 constexpr int32_t PARAMFOUR = 4;
37 }
38 
39 namespace OHOS {
40 namespace NWeb {
Init(napi_env env,napi_value exports)41 napi_value NapiWebDataBase::Init(napi_env env, napi_value exports)
42 {
43     const std::string WEB_DATA_BASE_CLASS_NAME = "WebDataBase";
44     napi_property_descriptor properties[] = {
45         DECLARE_NAPI_STATIC_FUNCTION("deleteHttpAuthCredentials", NapiWebDataBase::JsDeleteHttpAuthCredentials),
46         DECLARE_NAPI_STATIC_FUNCTION("saveHttpAuthCredentials", NapiWebDataBase::JsSaveHttpAuthCredentials),
47         DECLARE_NAPI_STATIC_FUNCTION("getHttpAuthCredentials", NapiWebDataBase::JsGetHttpAuthCredentials),
48         DECLARE_NAPI_STATIC_FUNCTION("existHttpAuthCredentials", NapiWebDataBase::JsExistHttpAuthCredentials),
49     };
50     napi_value constructor = nullptr;
51 
52     napi_define_class(env, WEB_DATA_BASE_CLASS_NAME.c_str(), WEB_DATA_BASE_CLASS_NAME.length(), JsConstructor, nullptr,
53         sizeof(properties) / sizeof(properties[0]), properties, &constructor);
54     NAPI_ASSERT(env, constructor != nullptr, "NapiWebDataBase define js class failed");
55     napi_status status = napi_set_named_property(env, exports, "WebDataBase", constructor);
56     NAPI_ASSERT(env, status == napi_ok, "NapiWebDataBase set property failed");
57     return exports;
58 }
59 
JsDeleteHttpAuthCredentials(napi_env env,napi_callback_info info)60 napi_value NapiWebDataBase::JsDeleteHttpAuthCredentials(napi_env env, napi_callback_info info)
61 {
62     napi_value result = nullptr;
63     OHOS::NWeb::NWebDataBase* dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
64     if (dataBase != nullptr) {
65         dataBase->DeleteHttpAuthCredentials();
66     }
67     napi_get_undefined(env, &result);
68     return result;
69 }
70 
JsExistHttpAuthCredentials(napi_env env,napi_callback_info info)71 napi_value NapiWebDataBase::JsExistHttpAuthCredentials(napi_env env, napi_callback_info info)
72 {
73     bool isExist = false;
74     napi_value result = nullptr;
75 
76     OHOS::NWeb::NWebDataBase* dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
77     if (dataBase != nullptr) {
78         isExist = dataBase->ExistHttpAuthCredentials();
79     }
80     NAPI_CALL(env, napi_get_boolean(env, isExist, &result));
81     return result;
82 }
83 
GetStringPara(napi_env env,napi_value argv,std::string & outValue)84 bool NapiWebDataBase::GetStringPara(napi_env env, napi_value argv, std::string& outValue)
85 {
86     size_t bufferSize = 0;
87     napi_valuetype valueType = napi_null;
88 
89     napi_typeof(env, argv, &valueType);
90     if (valueType != napi_string) {
91         return false;
92     }
93     napi_get_value_string_utf8(env, argv, nullptr, 0, &bufferSize);
94     if (bufferSize > MAX_STRING_LENGTH) {
95         return false;
96     }
97     char stringValue[bufferSize + 1];
98     size_t jsStringLength = 0;
99     napi_get_value_string_utf8(env, argv, stringValue, bufferSize + 1, &jsStringLength);
100     if (jsStringLength != bufferSize) {
101         return false;
102     }
103     outValue = stringValue;
104     return true;
105 }
106 
GetSize(napi_env env,napi_value argv,size_t & outValue)107 bool NapiWebDataBase::GetSize(napi_env env, napi_value argv, size_t& outValue)
108 {
109     size_t bufferSize = 0;
110     napi_valuetype valueType = napi_null;
111 
112     napi_typeof(env, argv, &valueType);
113     if (valueType != napi_string) {
114         return false;
115     }
116     napi_get_value_string_utf8(env, argv, nullptr, 0, &bufferSize);
117     if (bufferSize > MAX_STRING_LENGTH) {
118         return false;
119     }
120     outValue = bufferSize;
121     return true;
122 }
123 
GetCharPara(napi_env env,napi_value argv,char * buffer,size_t bufferSize)124 bool NapiWebDataBase::GetCharPara(napi_env env, napi_value argv, char* buffer, size_t bufferSize)
125 {
126     if (bufferSize == 0) {
127         return false;
128     }
129     size_t jsStringLength = 0;
130     napi_get_value_string_utf8(env, argv, buffer, bufferSize + 1, &jsStringLength);
131     if (jsStringLength != bufferSize) {
132         return false;
133     }
134     return true;
135 }
136 
JsSaveHttpAuthCredentials(napi_env env,napi_callback_info info)137 napi_value NapiWebDataBase::JsSaveHttpAuthCredentials(napi_env env, napi_callback_info info)
138 {
139     napi_value retValue = nullptr;
140     size_t argc = 4;
141     napi_value argv[4] = { 0 };
142 
143     napi_get_cb_info(env, info, &argc, argv, &retValue, nullptr);
144     if (argc != PARAMFOUR) {
145         NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
146         return nullptr;
147     }
148 
149     std::string host;
150     if (!GetStringPara(env, argv[PARAMZERO], host)) {
151         NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
152         return nullptr;
153     }
154 
155     std::string realm;
156     if (!GetStringPara(env, argv[PARAMONE], realm)) {
157         NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
158         return nullptr;
159     }
160 
161     std::string username;
162     if (!GetStringPara(env, argv[PARAMTWO], username)) {
163         NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
164         return nullptr;
165     }
166 
167     if (host.empty() || realm.empty() || username.empty()) {
168         NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
169         return nullptr;
170     }
171 
172     size_t bufferSize = 0;
173     if (!GetSize(env, argv[PARAMTHREE], bufferSize) || bufferSize > MAX_PWD_LENGTH) {
174         NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
175         return nullptr;
176     }
177     if (bufferSize > 0) {
178         char password[bufferSize + 1];
179         if (!GetCharPara(env, argv[PARAMTHREE], password, bufferSize)) {
180             NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
181             return nullptr;
182         }
183 
184         OHOS::NWeb::NWebDataBase* dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
185         if (dataBase != nullptr) {
186             dataBase->SaveHttpAuthCredentials(host, realm, username, password);
187         }
188         (void)memset_s(password, sizeof(password), 0, sizeof(password));
189     }
190 
191     napi_value result = nullptr;
192     napi_get_undefined(env, &result);
193     return result;
194 }
195 
JsGetHttpAuthCredentials(napi_env env,napi_callback_info info)196 napi_value NapiWebDataBase::JsGetHttpAuthCredentials(napi_env env, napi_callback_info info)
197 {
198     napi_value retValue = nullptr;
199     size_t argc = 2;
200     napi_value argv[2] = { 0 };
201 
202     napi_get_cb_info(env, info, &argc, argv, &retValue, nullptr);
203     if (argc != PARAMTWO) {
204         NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
205         return nullptr;
206     }
207 
208     std::string host;
209     if (!GetStringPara(env, argv[PARAMZERO], host)) {
210         NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
211         return nullptr;
212     }
213 
214     std::string realm;
215     if (!GetStringPara(env, argv[PARAMONE], realm)) {
216         NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
217         return nullptr;
218     }
219 
220     if (host.empty() || realm.empty()) {
221         NWebError::BusinessError::ThrowErrorByErrcode(env, NWebError::PARAM_CHECK_ERROR);
222         return nullptr;
223     }
224 
225     std::string username;
226     char password[MAX_PWD_LENGTH + 1] = {0};
227     napi_value result = nullptr;
228     napi_create_array(env, &result);
229 
230     OHOS::NWeb::NWebDataBase* dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
231     if (dataBase != nullptr) {
232         dataBase->GetHttpAuthCredentials(host, realm, username, password, MAX_PWD_LENGTH + 1);
233     }
234     if (!username.empty() && strlen(password) > 0) {
235         napi_value nameVal = nullptr;
236         napi_value pwdVal = nullptr;
237         napi_create_string_utf8(env, username.c_str(), username.length(), &nameVal);
238         napi_set_element(env, result, PARAMZERO, nameVal);
239         napi_create_string_utf8(env, password, strlen(password), &pwdVal);
240         napi_set_element(env, result, PARAMONE, pwdVal);
241     }
242     (void)memset_s(password, MAX_PWD_LENGTH + 1, 0, MAX_PWD_LENGTH + 1);
243     return result;
244 }
245 
JsConstructor(napi_env env,napi_callback_info info)246 napi_value NapiWebDataBase::JsConstructor(napi_env env, napi_callback_info info)
247 {
248     napi_value thisVar = nullptr;
249     size_t argc = 2;
250     napi_value argv[2] = { 0 };
251     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
252     return thisVar;
253 }
254 } // namespace NWeb
255 } // namespace OHOS
256