Skip to content

Commit

Permalink
style: 💄 files config.cpp and config.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
dBucik committed Jan 23, 2024
1 parent 15729c7 commit 9cf6020
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
2 changes: 1 addition & 1 deletion examples/user_mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"mike"
]
}
}
}
62 changes: 32 additions & 30 deletions src/include/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,62 @@

#include "nlohmann/json.hpp"

using namespace std;
using json = nlohmann::json;

void Config::load(const char *path) {
std::ifstream config_fstream(path);
ifstream config_fstream(path);
json j;
config_fstream >> j;

json oauth_json = j.at("oauth");

client_id = j.at("oauth").at("client").at("id").get<std::string>();
client_secret = j.at("oauth").at("client").at("secret").get<std::string>();
scope = j.at("oauth").at("scope").get<std::string>();
device_endpoint = j.at("oauth").at("device_endpoint").get<std::string>();
token_endpoint = j.at("oauth").at("token_endpoint").get<std::string>();
userinfo_endpoint = j.at("oauth").at("userinfo_endpoint").get<std::string>();
username_attribute =
j.at("oauth").at("username_attribute").get<std::string>();
require_mfa = j["oauth"].contains("require_mfa")
? j.at("oauth").at("require_mfa").get<bool>()
: false;
client_id = oauth_json.at("client").at("id").get<string>();
client_secret = oauth_json.at("client").at("secret").get<string>();
scope = oauth_json.at("scope").get<string>();
device_endpoint = oauth_json.at("device_endpoint").get<string>();
token_endpoint = oauth_json.at("token_endpoint").get<string>();
userinfo_endpoint = oauth_json.at("userinfo_endpoint").get<string>();
username_attribute = oauth_json.at("username_attribute").get<string>();
require_mfa = oauth_json.contains("require_mfa") && oauth_json.at("require_mfa").get<bool>();

json qr_json = j.at("qr");
qr_error_correction_level =
j.at("qr").at("error_correction_level").get<int>();
qr_show =
(j["qr"].contains("show")) ? j.at("qr").at("show").get<bool>() : true;
if (j.find("ldap") != j.end() && j["ldap"].find("hosts") != j["ldap"].end()) {
for (auto &host : j["ldap"]["hosts"]) {
ldap_hosts.insert((std::string)host);
qr_json.at("error_correction_level").get<int>();
qr_show = qr_json.contains("show") && qr_json.at("show").get<bool>();

if (j.find("ldap") != j.end() && j.at("ldap").find("hosts") != j.at("ldap").end()) {
json ldap_json = j.at("ldap");
for (auto const &host : ldap_json.at("hosts")) {
ldap_hosts.insert((string)host);
}
ldap_basedn = j.at("ldap").at("basedn").get<std::string>();
ldap_user = j.at("ldap").at("user").get<std::string>();
ldap_passwd = j.at("ldap").at("passwd").get<std::string>();
ldap_filter = j.at("ldap").at("filter").get<std::string>();
ldap_attr = j.at("ldap").at("attr").get<std::string>();
ldap_basedn = ldap_json.at("basedn").get<string>();
ldap_user = ldap_json.at("user").get<string>();
ldap_passwd = ldap_json.at("passwd").get<string>();
ldap_filter = ldap_json.at("filter").get<string>();
ldap_attr = ldap_json.at("attr").get<string>();
}

json users_json;
if (j.find("users") != j.end()) {
users_json = j.at("users");
Config::fill_user_map_from_json(usermap, j);
} else if (j.find("usersFilePath") != j.end()) {
std::string users_path = j.at("usersFilePath").get<std::string>();
std::ifstream users_fstream(users_path);
string users_path = j.at("usersFilePath").get<string>();
ifstream users_fstream(users_path);
users_fstream >> users_json;
Config::fill_user_map_from_json(usermap, j);
}
}

void Config::fill_user_map_from_json(std::map<std::string, std::set<std::string>>& user_map, json& j) {
void Config::fill_user_map_from_json(map<string, set<string>>& user_map, json& j) {
for (auto const &element : j["users"].items()) {
for (auto const &local_user : element.value()) {
if (user_map.find(element.key()) == user_map.end()) {
std::set<std::string> userset;
userset.insert((std::string)local_user);
set<string> userset;
user_map[element.key()] = userset;
} else {
user_map[element.key()].insert((std::string)local_user);
}
user_map[element.key()].insert((string)local_user);
}
}
}
34 changes: 26 additions & 8 deletions src/include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,42 @@

#include <map>
#include <set>
#include <string>

#include "nlohmann/json.hpp"

using json = nlohmann::json;

class Config {
public:
void load(const char *path);
std::string client_id, client_secret, scope, device_endpoint, token_endpoint,
userinfo_endpoint, username_attribute, ldap_basedn, ldap_user,
ldap_passwd, ldap_filter, ldap_attr;
bool require_mfa, qr_show;
std::set<std::string> ldap_hosts;
// OAuth2
std::string client_id;
std::string client_secret;
std::string scope;
std::string device_endpoint;
std::string token_endpoint;
std::string userinfo_endpoint;
std::string username_attribute;
bool require_mfa;

// QR code
bool qr_show;
int qr_error_correction_level;

// LDAP
std::string ldap_basedn;
std::string ldap_user;
std::string ldap_passwd;
std::string ldap_filter;
std::string ldap_attr;
std::set<std::string> ldap_hosts;

// usermap
std::map<std::string, std::set<std::string>> usermap;

// functions
void load(const char *path);
private:
void fill_user_map_from_json(std::map<std::string, std::set<std::string>>& user_map, json& j);
static void fill_user_map_from_json(std::map<std::string, std::set<std::string>>& user_map, json& j);
};

#endif // PAM_OAUTH2_DEVICE_CONFIG_HPP

0 comments on commit 9cf6020

Please sign in to comment.