Skip to content
API Documentation
BastionGuard™
HibpProvider Class Referencefinal

#include <HibpProvider.hpp>

Inheritance diagram for HibpProvider:
ILeakProvider

Public Member Functions

 HibpProvider (const std::string &apiKey)
std::vector< LeakRecord > checkEmail (const std::string &email) override
std::string name () const override
Public Member Functions inherited from ILeakProvider
virtual ~ILeakProvider ()=default

Private Member Functions

LeakRecord::Severity computeSeverity (const std::vector< std::string > &dataClasses) const

Private Attributes

std::string apiKey

Detailed Description

Definition at line 29 of file HibpProvider.hpp.

Constructor & Destructor Documentation

◆ HibpProvider()

HibpProvider::HibpProvider ( const std::string & apiKey)
explicit

Definition at line 31 of file HibpProvider.cpp.

32: apiKey(key)
33{
34}
std::string apiKey

References apiKey.

Member Function Documentation

◆ checkEmail()

std::vector< LeakRecord > HibpProvider::checkEmail ( const std::string & email)
overridevirtual

Implements ILeakProvider.

Definition at line 37 of file HibpProvider.cpp.

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}
nlohmann::json json
Definition Backend.cpp:2908
#define _(s)
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

References _, apiKey, HttpResponse::body, LeakRecord::breachDate, LeakRecord::breachName, computeSeverity(), LeakRecord::dataClasses, LeakRecord::description, HttpClient::get(), LeakRecord::provider, LeakRecord::severity, HttpResponse::status, and HttpClient::urlEncode().

◆ computeSeverity()

LeakRecord::Severity HibpProvider::computeSeverity ( const std::vector< std::string > & dataClasses) const
private

Definition at line 115 of file HibpProvider.cpp.

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}

References LeakRecord::CRITICAL, LeakRecord::HIGH, LeakRecord::INFO, LeakRecord::LOW, and LeakRecord::MEDIUM.

Referenced by checkEmail().

◆ name()

std::string HibpProvider::name ( ) const
inlineoverridevirtual

Implements ILeakProvider.

Definition at line 36 of file HibpProvider.hpp.

36{ return "HIBP"; }

Member Data Documentation

◆ apiKey

std::string HibpProvider::apiKey
private

Definition at line 39 of file HibpProvider.hpp.

Referenced by checkEmail(), and HibpProvider().


The documentation for this class was generated from the following files: