Skip to content
API Documentation
BastionGuard™
HibpProvider.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
21#include "HibpProvider.hpp"
22
23#include "../net/HttpClient.hpp"
24#include <nlohmann/json.hpp>
25#include <glib/gi18n.h>
26#include <map>
27#include <stdexcept>
28
29using json = nlohmann::json;
30
31HibpProvider::HibpProvider(const std::string& key)
32: apiKey(key)
33{
34}
35
36std::vector<LeakRecord>
37HibpProvider::checkEmail(const std::string& email)
38{
39 std::vector<LeakRecord> results;
40
41 if (email.empty() || apiKey.empty())
42 return results;
43
44 const std::string encodedEmail =
46
47 const std::string url =
48 "https://haveibeenpwned.com/api/v3/breachedaccount/" +
49 encodedEmail +
50 "?truncateResponse=false";
51
52 std::map<std::string, std::string> headers;
53 headers["hibp-api-key"] = apiKey;
54 headers["User-Agent"] = "BastionGuard/IdentityLeak";
55 headers["Accept"] = "application/json";
56
57 HttpResponse resp = HttpClient::get(url, headers, 20);
58
59 if (resp.status == 404) {
60 return results;
61 }
62
63 if (resp.status == 401) {
64 throw std::runtime_error(_("HIBP: API key non valida (401)"));
65 }
66
67 if (resp.status == 429) {
68 throw std::runtime_error(_("HIBP: rate limit superato (429)"));
69 }
70
71 if (resp.status != 200) {
72 throw std::runtime_error(
73 "HIBP: errore HTTP " + std::to_string(resp.status));
74 }
75
76
77 json root;
78 try {
79 root = json::parse(resp.body);
80 } catch (...) {
81 throw std::runtime_error(_("HIBP: JSON non valido"));
82 }
83
84 if (!root.is_array())
85 return results;
86
87 for (const auto& item : root) {
88
89 LeakRecord rec;
90 rec.provider = "HIBP";
91 rec.breachName = item.value("Name", "Unknown");
92 rec.breachDate = item.value("BreachDate", "");
93 rec.description = item.value("Description", "");
94
95 // DataClasses
96 std::vector<std::string> dataClasses;
97 if (item.contains("DataClasses") &&
98 item["DataClasses"].is_array())
99 {
100 for (const auto& dc : item["DataClasses"]) {
101 dataClasses.push_back(dc.get<std::string>());
102 }
103 }
104
105 rec.dataClasses = dataClasses;
106 rec.severity = computeSeverity(dataClasses);
107
108 results.push_back(std::move(rec));
109 }
110
111 return results;
112}
113
116 const std::vector<std::string>& dataClasses) const
117{
118 bool hasPassword = false;
119 bool hasEmail = false;
120 bool hasPII = false;
121
122 for (const auto& dc : dataClasses) {
123 if (dc == "Passwords" || dc == "Password hints")
124 hasPassword = true;
125 else if (dc == "Email addresses")
126 hasEmail = true;
127 else
128 hasPII = true;
129 }
130
131 if (hasPassword && hasEmail)
133
134 if (hasPassword)
136
137 if (hasEmail && hasPII)
139
140 if (hasEmail)
142
144}
nlohmann::json json
Definition Backend.cpp:2908
#define _(s)
HibpProvider(const std::string &apiKey)
std::vector< LeakRecord > checkEmail(const std::string &email) override
std::string apiKey
LeakRecord::Severity computeSeverity(const std::vector< std::string > &dataClasses) const
static HttpResponse get(const std::string &url, const std::map< std::string, std::string > &headers={}, long timeoutSeconds=20)
static std::string urlEncode(const std::string &value)
std::string body
std::string breachDate
Severity severity
std::vector< std::string > dataClasses
std::string provider
std::string description
std::string breachName