• 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 "config_parser.h"
17 
18 #include <filesystem>
19 #include <fstream>
20 #include <sstream>
21 
22 #include "iam_logger.h"
23 
24 #define LOG_TAG "USER_AUTH_COMMON"
25 
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
Load(const std::string & configPath)29 bool ConfigParser::Load(const std::string &configPath)
30 {
31     std::filesystem::path fsPath(configPath);
32     std::error_code ec;
33     std::filesystem::path canonicalPath = std::filesystem::canonical(fsPath, ec);
34     if (ec) {
35         IAM_LOGE("Failed to get canonical path: %{public}s, error: %{public}s", configPath.c_str(),
36             ec.message().c_str());
37         return false;
38     }
39 
40     std::ifstream file(canonicalPath);
41     if (!file.is_open()) {
42         IAM_LOGE("Failed to open config file: %{public}s", canonicalPath.string().c_str());
43         return false;
44     }
45 
46     const std::filesystem::path allowedBasePath("/system/etc/useriam");
47     if (canonicalPath.string().find(allowedBasePath.string()) != 0) {
48         IAM_LOGE("Config file path not allowed: %{public}s", canonicalPath.string().c_str());
49         return false;
50     }
51 
52     try {
53         std::stringstream buffer;
54         buffer << file.rdbuf();
55         configJson_ = nlohmann::json::parse(buffer.str());
56         isLoaded_ = true;
57         return true;
58     } catch (const std::exception &e) {
59         IAM_LOGE("Failed to parse config file: %{public}s, error: %{public}s", canonicalPath.string().c_str(),
60             e.what());
61         return false;
62     }
63 }
64 
Get(const std::string & key,const std::string & defaultValue) const65 std::string ConfigParser::Get(const std::string &key, const std::string &defaultValue) const
66 {
67     if (!isLoaded_) {
68         IAM_LOGE("Config file not loaded");
69         return defaultValue;
70     }
71 
72     try {
73         if (!configJson_.contains(key)) {
74             return defaultValue;
75         }
76 
77         if (!configJson_[key].is_string()) {
78             IAM_LOGE("Config value is not string type for key: %{public}s", key.c_str());
79             return defaultValue;
80         }
81 
82         return configJson_[key].get<std::string>();
83     } catch (const std::exception &e) {
84         IAM_LOGE("Failed to get config value for key: %{public}s, error: %{public}s", key.c_str(), e.what());
85         return defaultValue;
86     }
87 }
88 } // namespace UserAuth
89 } // namespace UserIam
90 } // namespace OHOS