|
5 | 5 |
|
6 | 6 | #include "nlohmann/json.hpp"
|
7 | 7 |
|
| 8 | +using namespace std; |
8 | 9 | using json = nlohmann::json;
|
9 | 10 |
|
10 | 11 | void Config::load(const char *path) {
|
11 |
| - std::ifstream config_fstream(path); |
| 12 | + ifstream config_fstream(path); |
12 | 13 | json j;
|
13 | 14 | config_fstream >> j;
|
| 15 | + |
| 16 | + json oauth_json = j.at("oauth_token"); |
14 | 17 |
|
15 |
| - client_id = j.at("oauth").at("client").at("id").get<std::string>(); |
16 |
| - client_secret = j.at("oauth").at("client").at("secret").get<std::string>(); |
17 |
| - scope = j.at("oauth").at("scope").get<std::string>(); |
18 |
| - device_endpoint = j.at("oauth").at("device_endpoint").get<std::string>(); |
19 |
| - token_endpoint = j.at("oauth").at("token_endpoint").get<std::string>(); |
20 |
| - userinfo_endpoint = j.at("oauth").at("userinfo_endpoint").get<std::string>(); |
21 |
| - username_attribute = |
22 |
| - j.at("oauth").at("username_attribute").get<std::string>(); |
23 |
| - require_mfa = j["oauth"].contains("require_mfa") |
24 |
| - ? j.at("oauth").at("require_mfa").get<bool>() |
25 |
| - : false; |
| 18 | + client_id = oauth_json.at("client").at("id").get<string>(); |
| 19 | + client_secret = oauth_json.at("client").at("secret").get<string>(); |
| 20 | + scope = oauth_json.at("scope").get<string>(); |
| 21 | + device_endpoint = oauth_json.at("device_endpoint").get<string>(); |
| 22 | + token_endpoint = oauth_json.at("token_endpoint").get<string>(); |
| 23 | + userinfo_endpoint = oauth_json.at("userinfo_endpoint").get<string>(); |
| 24 | + username_attribute = oauth_json.at("username_attribute").get<string>(); |
| 25 | + require_mfa = oauth_json.contains("require_mfa") && oauth_json.at("require_mfa").get<bool>(); |
| 26 | + |
| 27 | + json qr_json = j.at("qr"); |
26 | 28 | qr_error_correction_level =
|
27 |
| - j.at("qr").at("error_correction_level").get<int>(); |
28 |
| - qr_show = |
29 |
| - (j["qr"].contains("show")) ? j.at("qr").at("show").get<bool>() : true; |
30 |
| - if (j.find("ldap") != j.end() && j["ldap"].find("hosts") != j["ldap"].end()) { |
31 |
| - for (auto &host : j["ldap"]["hosts"]) { |
32 |
| - ldap_hosts.insert((std::string)host); |
| 29 | + qr_json.at("error_correction_level").get<int>(); |
| 30 | + qr_show = qr_json.contains("show") && qr_json.at("show").get<bool>(); |
| 31 | + |
| 32 | + if (j.find("ldap") != j.end() && j.at("ldap").find("hosts") != j.at("ldap").end()) { |
| 33 | + json ldap_json = j.at("ldap"); |
| 34 | + for (auto const &host : ldap_json.at("hosts")) { |
| 35 | + ldap_hosts.insert((string)host); |
33 | 36 | }
|
34 |
| - ldap_basedn = j.at("ldap").at("basedn").get<std::string>(); |
35 |
| - ldap_user = j.at("ldap").at("user").get<std::string>(); |
36 |
| - ldap_passwd = j.at("ldap").at("passwd").get<std::string>(); |
37 |
| - ldap_filter = j.at("ldap").at("filter").get<std::string>(); |
38 |
| - ldap_attr = j.at("ldap").at("attr").get<std::string>(); |
| 37 | + ldap_basedn = ldap_json.at("basedn").get<string>(); |
| 38 | + ldap_user = ldap_json.at("user").get<string>(); |
| 39 | + ldap_passwd = ldap_json.at("passwd").get<string>(); |
| 40 | + ldap_filter = ldap_json.at("filter").get<string>(); |
| 41 | + ldap_attr = ldap_json.at("attr").get<string>(); |
39 | 42 | }
|
| 43 | + |
40 | 44 | json users_json;
|
41 | 45 | if (j.find("users") != j.end()) {
|
42 | 46 | users_json = j.at("users");
|
43 | 47 | Config::fill_user_map_from_json(usermap, j);
|
44 | 48 | } else if (j.find("usersFilePath") != j.end()) {
|
45 |
| - std::string users_path = j.at("usersFilePath").get<std::string>(); |
46 |
| - std::ifstream users_fstream(users_path); |
| 49 | + string users_path = j.at("usersFilePath").get<string>(); |
| 50 | + ifstream users_fstream(users_path); |
47 | 51 | users_fstream >> users_json;
|
48 | 52 | Config::fill_user_map_from_json(usermap, j);
|
49 | 53 | }
|
50 | 54 | }
|
51 | 55 |
|
52 |
| -void Config::fill_user_map_from_json(std::map<std::string, std::set<std::string>>& user_map, json& j) { |
| 56 | +void Config::fill_user_map_from_json(map<string, set<string>>& user_map, json& j) { |
53 | 57 | for (auto const &element : j["users"].items()) {
|
54 | 58 | for (auto const &local_user : element.value()) {
|
55 | 59 | if (user_map.find(element.key()) == user_map.end()) {
|
56 |
| - std::set<std::string> userset; |
57 |
| - userset.insert((std::string)local_user); |
| 60 | + set<string> userset; |
58 | 61 | user_map[element.key()] = userset;
|
59 |
| - } else { |
60 |
| - user_map[element.key()].insert((std::string)local_user); |
61 | 62 | }
|
| 63 | + user_map[element.key()].insert((string)local_user); |
62 | 64 | }
|
63 | 65 | }
|
64 | 66 | }
|
0 commit comments