• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         (void)memset_s(password, MAX_PWD_LENGTH + 1, 0, MAX_PWD_LENGTH + 1);
49         return ret;
50     }
51 
52     result[0] = OHOS::Webview::MallocCString(username_s);
53     if (result[0] == nullptr) {
54         WEBVIEWLOGI("Webdatabase getHttpAuthCredentials transfer username_s failed!");
55         free(result);
56         (void)memset_s(password, MAX_PWD_LENGTH + 1, 0, MAX_PWD_LENGTH + 1);
57         return ret;
58     }
59 
60     result[1] = static_cast<char*>(malloc(sizeof(char) * (MAX_PWD_LENGTH + 1)));
61     if (result[1] == nullptr) {
62         WEBVIEWLOGI("Webdatabase getHttpAuthCredentials malloc password failed!");
63         free(result[0]);
64         free(result);
65         (void)memset_s(password, MAX_PWD_LENGTH + 1, 0, MAX_PWD_LENGTH + 1);
66         return ret;
67     }
68     result[1] = std::char_traits<char>::copy(result[1], password, MAX_PWD_LENGTH);
69     (void)memset_s(password, MAX_PWD_LENGTH + 1, 0, MAX_PWD_LENGTH + 1);
70     ret.head = result;
71     ret.size = DEFAULT_AUTH_LENGTH;
72     return ret;
73 }
74 
CJSaveHttpAuthCredentials(const std::string & host,const std::string & realm,const std::string & username,const std::string & password)75 void WebDataBase::CJSaveHttpAuthCredentials(const std::string &host, const std::string &realm,
76     const std::string &username, const std::string &password)
77 {
78     // get web database instance;
79     std::shared_ptr<NWebDataBase> database = NWebHelper::Instance().GetDataBase();
80     if (database != nullptr) {
81         database->SaveHttpAuthCredentials(host, realm, username, password.c_str());
82     }
83 }
84 
CJExistHttpAuthCredentials()85 bool WebDataBase::CJExistHttpAuthCredentials()
86 {
87     bool isExist = false;
88     // get web database instance;
89     std::shared_ptr<NWebDataBase> database = NWebHelper::Instance().GetDataBase();
90     if (database != nullptr) {
91         isExist = database->ExistHttpAuthCredentials();
92     }
93     return isExist;
94 }
95 
CJDeleteHttpAuthCredentials()96 void WebDataBase::CJDeleteHttpAuthCredentials()
97 {
98     // get web database instance;
99     std::shared_ptr<NWebDataBase> database = NWebHelper::Instance().GetDataBase();
100     if (database != nullptr) {
101         database->DeleteHttpAuthCredentials();
102     }
103 }
104 }
105 }
106