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 24namespace OHOS { 25namespace AccountSA { 26using json = nlohmann::json; 27namespace { 28const std::string ACCOUNTMGR_HELPER_JSON_FILE = "/system/etc/account/accountmgr_helper.json"; 29const std::string KEY_BUNDLE_NAME_LIST = "BundleNameTrustList"; 30const std::string KEY_ACCOUNT_EVENT_MAP = "AccountEventMap"; 31const std::string KEY_ACCOUNT_EVENT_LOGIN = "LOGIN"; 32const std::string KEY_ACCOUNT_EVENT_LOGOUT = "LOGOUT"; 33const std::string KEY_ACCOUNT_EVENT_TOKEN_INVALID = "TOKEN_INVALID"; 34const std::string KEY_ACCOUNT_EVENT_LOGOFF = "LOGOFF"; 35 36static 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_discarded() || !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 60std::map<std::string, std::string> AccountHelperData::GetAccountEventMap() 61{ 62 std::map<std::string, std::string> result = {}; 63 nlohmann::json jsonData; 64 if (!ParseJsonData(jsonData)) { 65 return result; 66 } 67 68 if ((jsonData.find(KEY_ACCOUNT_EVENT_MAP) != jsonData.end()) && (jsonData.at(KEY_ACCOUNT_EVENT_MAP).is_array())) { 69 result = jsonData.at(KEY_ACCOUNT_EVENT_MAP).get<std::map<std::string, std::string>>(); 70 } 71 72 return result; 73} 74} // namespace AccountSA 75} // namespace OHOS 76