Skip to content
API Documentation
BastionGuard™
IdentityLeakConfig.cpp
Go to the documentation of this file.
1/*
2 * BastionGuard™
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * BastionGuard™ is a trademark of Calogero Scarnà.
18 * The BastionGuard™ name and branding are not licensed under the GPL.
19 */
20
22
23#include <filesystem>
24#include <fstream>
25#include <cstdlib>
26
27using json = nlohmann::json;
28
29static const char* CONFIG_DIR = ".config/BastionGuard";
30static const char* CONFIG_FILE = "identity_leak.json";
31
32/* ================= PATH ================= */
33
35{
36 const char* home = std::getenv("HOME");
37 if (!home)
38 return {};
39
40 std::filesystem::path dir =
41 std::filesystem::path(home) / CONFIG_DIR;
42
43 std::filesystem::create_directories(dir);
44
45 return (dir / CONFIG_FILE).string();
46}
47
49{
50 std::ifstream in(configPath());
51 if (!in)
52 return json::object();
53
54 json j;
55 try {
56 in >> j;
57 } catch (...) {
58 return json::object();
59 }
60 return j;
61}
62
64{
65 std::ofstream out(configPath());
66 out << j.dump(4);
67}
68
70{
71 auto j = loadJson();
72 return j.value("hibp_api_key", "");
73}
74
75void IdentityLeakConfig::setHibpApiKey(const std::string& key)
76{
77 auto j = loadJson();
78 j["hibp_api_key"] = key;
79 saveJson(j);
80}
81
83{
84 auto j = loadJson();
85 return j.value("leakcheck_api_key", "");
86}
87
88void IdentityLeakConfig::setLeakCheckApiKey(const std::string& key)
89{
90 auto j = loadJson();
91 j["leakcheck_api_key"] = key;
92 saveJson(j);
93}
94
95
97{
98 auto j = loadJson();
99 return j.value("monitor_enabled", false);
100}
101
103{
104 auto j = loadJson();
105 j["monitor_enabled"] = enabled;
106 saveJson(j);
107}
nlohmann::json json
Definition Backend.cpp:2908
static const char * CONFIG_FILE
static const char * CONFIG_DIR
static std::string configPath()
static std::string hibpApiKey()
static nlohmann::json loadJson()
static void setLeakCheckApiKey(const std::string &)
static void saveJson(const nlohmann::json &j)
static std::string leakCheckApiKey()
static void setHibpApiKey(const std::string &)
static void setMonitorEnabled(bool)