Skip to content

Commit 9cf6020

Browse files
author
Dominik Frantisek Bucik
committed
style: 💄 files config.cpp and config.hpp
1 parent 15729c7 commit 9cf6020

File tree

3 files changed

+59
-39
lines changed

3 files changed

+59
-39
lines changed

‎examples/user_mappings.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
"mike"
99
]
1010
}
11-
}
11+
}

‎src/include/config.cpp‎

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,62 @@
55

66
#include "nlohmann/json.hpp"
77

8+
using namespace std;
89
using json = nlohmann::json;
910

1011
void Config::load(const char *path) {
11-
std::ifstream config_fstream(path);
12+
ifstream config_fstream(path);
1213
json j;
1314
config_fstream >> j;
15+
16+
json oauth_json = j.at("oauth");
1417

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");
2628
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);
3336
}
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>();
3942
}
43+
4044
json users_json;
4145
if (j.find("users") != j.end()) {
4246
users_json = j.at("users");
4347
Config::fill_user_map_from_json(usermap, j);
4448
} 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);
4751
users_fstream >> users_json;
4852
Config::fill_user_map_from_json(usermap, j);
4953
}
5054
}
5155

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) {
5357
for (auto const &element : j["users"].items()) {
5458
for (auto const &local_user : element.value()) {
5559
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;
5861
user_map[element.key()] = userset;
59-
} else {
60-
user_map[element.key()].insert((std::string)local_user);
6162
}
63+
user_map[element.key()].insert((string)local_user);
6264
}
6365
}
6466
}

‎src/include/config.hpp‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,42 @@
33

44
#include <map>
55
#include <set>
6-
#include <string>
76

87
#include "nlohmann/json.hpp"
98

109
using json = nlohmann::json;
1110

1211
class Config {
1312
public:
14-
void load(const char *path);
15-
std::string client_id, client_secret, scope, device_endpoint, token_endpoint,
16-
userinfo_endpoint, username_attribute, ldap_basedn, ldap_user,
17-
ldap_passwd, ldap_filter, ldap_attr;
18-
bool require_mfa, qr_show;
19-
std::set<std::string> ldap_hosts;
13+
// OAuth2
14+
std::string client_id;
15+
std::string client_secret;
16+
std::string scope;
17+
std::string device_endpoint;
18+
std::string token_endpoint;
19+
std::string userinfo_endpoint;
20+
std::string username_attribute;
21+
bool require_mfa;
22+
23+
// QR code
24+
bool qr_show;
2025
int qr_error_correction_level;
26+
27+
// LDAP
28+
std::string ldap_basedn;
29+
std::string ldap_user;
30+
std::string ldap_passwd;
31+
std::string ldap_filter;
32+
std::string ldap_attr;
33+
std::set<std::string> ldap_hosts;
34+
35+
// usermap
2136
std::map<std::string, std::set<std::string>> usermap;
37+
38+
// functions
39+
void load(const char *path);
2240
private:
23-
void fill_user_map_from_json(std::map<std::string, std::set<std::string>>& user_map, json& j);
41+
static void fill_user_map_from_json(std::map<std::string, std::set<std::string>>& user_map, json& j);
2442
};
2543

2644
#endif // PAM_OAUTH2_DEVICE_CONFIG_HPP

0 commit comments

Comments
 (0)