1 /*
2 * Copyright (c) 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 "web_data_base.h"
17 #include "nweb_data_base.h"
18 #include "nweb_helper.h"
19 #include "web_errors.h"
20 #include "webview_utils.h"
21 #include "webview_log.h"
22 #include "securec.h"
23 #include <cstring>
24
25 namespace OHOS {
26 namespace NWeb {
27 const int DEFAULT_AUTH_LENGTH = 2;
28 const int HTTP_AUTH_INIT_LENGTH = -1;
29
CJGetHttpAuthCredentials(const std::string &host, const std::string &realm)30 CArrString WebDataBase::CJGetHttpAuthCredentials(const std::string &host, const std::string &realm)
31 {
32 CArrString ret = {nullptr, HTTP_AUTH_INIT_LENGTH};
33 std::string username_s;
34 char password[MAX_PWD_LENGTH + 1] = {0};
35 std::shared_ptr<NWebDataBase> database = NWebHelper::Instance().GetDataBase();
36 if (database != nullptr) {
37 database->GetHttpAuthCredentials(host, realm, username_s, password, MAX_PWD_LENGTH + 1);
38 }
39
40 if (username_s.empty() || strlen(password) == 0) {
41 ret.size = 0;
42 return ret;
43 }
44
45 char** result = static_cast<char**>(malloc(sizeof(char*) * DEFAULT_AUTH_LENGTH));
46 if (result == nullptr) {
47 WEBVIEWLOGI("Webdatabase getHttpAuthCredentials malloc result failed!");
48 return ret;
49 }
50
51 result[0] = OHOS::Webview::MallocCString(username_s);
52 if (result[0] == nullptr) {
53 WEBVIEWLOGI("Webdatabase getHttpAuthCredentials transfer username_s failed!");
54 free(result);
55 return ret;
56 }
57
58 result[1] = static_cast<char*>(malloc(sizeof(char) * (MAX_PWD_LENGTH + 1)));
59 if (result[1] == nullptr) {
60 WEBVIEWLOGI("Webdatabase getHttpAuthCredentials malloc password failed!");
61 free(result[0]);
62 free(result);
63 return ret;
64 }
65 result[1] = std::char_traits<char>::copy(result[1], password, MAX_PWD_LENGTH);
66 (void)memset_s(password, MAX_PWD_LENGTH + 1, 0, MAX_PWD_LENGTH + 1);
67 ret.head = result;
68 ret.size = DEFAULT_AUTH_LENGTH;
69 return ret;
70 }
71
CJSaveHttpAuthCredentials(const std::string &host, const std::string &realm, const std::string &username, const std::string &password)72 void WebDataBase::CJSaveHttpAuthCredentials(const std::string &host, const std::string &realm,
73 const std::string &username, const std::string &password)
74 {
75 // get web database instance;
76 std::shared_ptr<NWebDataBase> database = NWebHelper::Instance().GetDataBase();
77 if (database != nullptr) {
78 database->SaveHttpAuthCredentials(host, realm, username, password.c_str());
79 }
80 }
81
CJExistHttpAuthCredentials()82 bool WebDataBase::CJExistHttpAuthCredentials()
83 {
84 bool isExist = false;
85 // get web database instance;
86 std::shared_ptr<NWebDataBase> database = NWebHelper::Instance().GetDataBase();
87 if (database != nullptr) {
88 isExist = database->ExistHttpAuthCredentials();
89 }
90 return isExist;
91 }
92
CJDeleteHttpAuthCredentials()93 void WebDataBase::CJDeleteHttpAuthCredentials()
94 {
95 // get web database instance;
96 std::shared_ptr<NWebDataBase> database = NWebHelper::Instance().GetDataBase();
97 if (database != nullptr) {
98 database->DeleteHttpAuthCredentials();
99 }
100 }
101 }
102 }