1 /*
2 * Copyright (c) 2021-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 "account_helper_data.h"
17 #include <fstream>
18 #include <iostream>
19 #include <nlohmann/json.hpp>
20 #include <vector>
21 #include "account_log_wrapper.h"
22 #include "file_ex.h"
23
24 namespace OHOS {
25 namespace AccountSA {
26 using json = nlohmann::json;
27 namespace {
28 const std::string ACCOUNTMGR_HELPER_JSON_FILE = "/system/etc/account/accountmgr_helper.json";
29 const std::string KEY_BUNDLE_NAME_LIST = "BundleNameTrustList";
30 const std::string KEY_ACCOUNT_EVENT_MAP = "AccountEventMap";
31 const std::string KEY_ACCOUNT_EVENT_LOGIN = "LOGIN";
32 const std::string KEY_ACCOUNT_EVENT_LOGOUT = "LOGOUT";
33 const std::string KEY_ACCOUNT_EVENT_TOKEN_INVALID = "TOKEN_INVALID";
34 const std::string KEY_ACCOUNT_EVENT_LOGOFF = "LOGOFF";
35
ParseJsonData(nlohmann::json & jsonData)36 static bool ParseJsonData(nlohmann::json &jsonData)
37 {
38 if (!FileExists(ACCOUNTMGR_HELPER_JSON_FILE)) {
39 ACCOUNT_LOGI("File %{public}s not exist, empty default!", ACCOUNTMGR_HELPER_JSON_FILE.c_str());
40 return false;
41 }
42
43 std::ifstream fin(ACCOUNTMGR_HELPER_JSON_FILE);
44 if (!fin) {
45 ACCOUNT_LOGE("Failed to open file %{public}s", ACCOUNTMGR_HELPER_JSON_FILE.c_str());
46 return false;
47 }
48
49 jsonData = json::parse(fin, nullptr, false);
50 if (!jsonData.is_structured()) {
51 ACCOUNT_LOGE("not valid json file!");
52 fin.close();
53 return false;
54 }
55 fin.close();
56 return true;
57 }
58 }
59
GetBundleNameTrustList()60 std::vector<std::string> AccountHelperData::GetBundleNameTrustList()
61 {
62 std::vector<std::string> result = {""};
63 nlohmann::json jsonData;
64 if (!ParseJsonData(jsonData)) {
65 return result;
66 }
67
68 if (jsonData.find(KEY_BUNDLE_NAME_LIST) != jsonData.end() && jsonData.at(KEY_BUNDLE_NAME_LIST).is_array()) {
69 result = jsonData.at(KEY_BUNDLE_NAME_LIST).get<std::vector<std::string>>();
70 }
71
72 return result;
73 }
74
GetAccountEventMap()75 std::map<std::string, std::string> AccountHelperData::GetAccountEventMap()
76 {
77 std::map<std::string, std::string> result = {};
78 nlohmann::json jsonData;
79 if (!ParseJsonData(jsonData)) {
80 return result;
81 }
82
83 if (jsonData.find(KEY_ACCOUNT_EVENT_MAP) != jsonData.end()) {
84 result = jsonData.at(KEY_ACCOUNT_EVENT_MAP).get<std::map<std::string, std::string>>();
85 }
86
87 return result;
88 }
89 } // namespace AccountSA
90 } // namespace OHOS
91