C++ Whitelister System with API
Below is a complete C++ implementation for a whitelisting system that uses API keys for authentication. This includes both the whitelisting functionality and a simple API interface.
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <random>
#include <cctype>
#include <fstream>
using namespace std;
// Whitelist entry structure
struct WhitelistEntry {
string key;
string identifier; // Could be username, email, hardware ID, etc.
time_t creationDate;
time_t expirationDate;
string additionalInfo;
};
// API key structure
struct APIKey {
string key;
vector<string> permissions; // "read", "write", "delete", etc.
time_t expirationDate;
};
class Whitelister {
private:
vector<WhitelistEntry> whitelist;
map<string, APIKey> apiKeys;
string whitelistFile = "whitelist.dat";
string apiKeysFile = "apikeys.dat";
// Helper function to generate random strings
string generateRandomString(size_t length) {
const string chars =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
random_device rd;
mt19937 generator(rd());
uniform_int_distribution<> dist(0, chars.size() - 1);
string result;
for (size_t i = 0; i < length; ++i) {
result += chars[dist(generator)];
}
return result;
}
// Helper function to convert time_t to string
string timeToString(time_t time) {
tm* tm = localtime(&time);
stringstream ss;
ss << put_time(tm, "%Y-%m-%d %H:%M:%S");
return ss.str();
}
// Helper function to parse string to time_t
time_t stringToTime(const string& timeStr) {
tm tm = {};
istringstream ss(timeStr);
ss >> get_time(&tm, "%Y-%m-%d %H:%M:%S");
return mktime(&tm);
}
// Load whitelist from file
void loadWhitelist() {
ifstream file(whitelistFile);
if (!file.is_open()) return;
whitelist.clear();
string line;
while (getline(file, line)) {
if (line.empty()) continue;
WhitelistEntry entry;
size_t pos1 = line.find('|');
size_t pos2 = line.find('|', pos1 + 1);
size_t pos3 = line.find('|', pos2 + 1);
size_t pos4 = line.find('|', pos3 + 1);
if (pos1 != string::npos && pos2 != string::npos &&
pos3 != string::npos && pos4 != string::npos) {
entry.key = line.substr(0, pos1);
entry.identifier = line.substr(pos1 + 1, pos2 - pos1 - 1);
entry.creationDate = stringToTime(line.substr(pos2 + 1, pos3 - pos2 - 1));
entry.expirationDate = stringToTime(line.substr(pos3 + 1, pos4 - pos3 - 1));
entry.additionalInfo = line.substr(pos4 + 1);
whitelist.push_back(entry);
}
}
file.close();
}
// Save whitelist to file
void saveWhitelist() {
ofstream file(whitelistFile);
if (!file.is_open()) return;
for (const auto& entry : whitelist) {
file << entry.key << "|"
<< entry.identifier << "|"
<< timeToString(entry.creationDate) << "|"
<< timeToString(entry.expirationDate) << "|"
<< entry.additionalInfo << "\n";
}
file.close();
}
// Load API keys from file
void loadAPIKeys() {
ifstream file(apiKeysFile);
if (!file.is_open()) return;
apiKeys.clear();
string line;
while (getline(file, line)) {
if (line.empty()) continue;
APIKey key;
size_t pos1 = line.find('|');
size_t pos2 = line.find('|', pos1 + 1);
if (pos1 != string::npos && pos2 != string::npos) {
key.key = line.substr(0, pos1);
// Parse permissions
string permsStr = line.substr(pos1 + 1, pos2 - pos1 - 1);
size_t start = 0;
size_t end = permsStr.find(',');
while (end != string::npos) {
key.permissions.push_back(permsStr.substr(start, end - start));
start = end + 1;
end = permsStr.find(',', start);
}
key.permissions.push_back(permsStr.substr(start));
key.expirationDate = stringToTime(line.substr(pos2 + 1));
apiKeys[key.key] = key;
}
}
file.close();
}
// Save API keys to file
void saveAPIKeys() {
ofstream file(apiKeysFile);
if (!file.is_open()) return;
for (const auto& pair : apiKeys) {
file << pair.second.key << "|";
// Save permissions as comma-separated list
for (size_t i = 0; i < pair.second.permissions.size(); ++i) {
if (i != 0) file << ",";
file << pair.second.permissions[i];
}
file << "|" << timeToString(pair.second.expirationDate) << "\n";
}
file.close();
}
// Check if API key is valid and has required permission
bool validateAPIKey(const string& apiKey, const string& requiredPermission = "") {
auto it = apiKeys.find(apiKey);
if (it == apiKeys.end()) return false;
// Check expiration
time_t now = time(nullptr);
if (now > it->second.expirationDate) {
apiKeys.erase(it); // Remove expired key
saveAPIKeys();
return false;
}
// Check permission if required
if (!requiredPermission.empty()) {
return find(it->second.permissions.begin(),
it->second.permissions.end(),
requiredPermission) != it->second.permissions.end();
}
return true;
}
public:
Whitelister() {
loadWhitelist();
loadAPIKeys();
}
~Whitelister() {
saveWhitelist();
saveAPIKeys();
}
// API: Add to whitelist
string addToWhitelist(const string& apiKey, const string& identifier,
time_t expirationDays = 30, const string& additionalInfo = "") {
if (!validateAPIKey(apiKey, "write")) {
return "ERROR: Invalid API key or insufficient permissions";
}
// Check if already whitelisted
for (const auto& entry : whitelist) {
if (entry.identifier == identifier) {
return "ERROR: Identifier already whitelisted";
}
}
WhitelistEntry entry;
entry.key = generateRandomString(16);
entry.identifier = identifier;
entry.creationDate = time(nullptr);
entry.expirationDate = entry.creationDate + (expirationDays * 24 * 60 * 60);
entry.additionalInfo = additionalInfo;
whitelist.push_back(entry);
saveWhitelist();
return "SUCCESS: Whitelist key generated - " + entry.key;
}
// API: Remove from whitelist
string removeFromWhitelist(const string& apiKey, const string& identifier) {
if (!validateAPIKey(apiKey, "delete")) {
return "ERROR: Invalid API key or insufficient permissions";
}
auto it = remove_if(whitelist.begin(), whitelist.end(),
[&identifier](const WhitelistEntry& entry) {
return entry.identifier == identifier;
});
if (it != whitelist.end()) {
whitelist.erase(it, whitelist.end());
saveWhitelist();
return "SUCCESS: Identifier removed from whitelist";
}
return "ERROR: Identifier not found in whitelist";
}
// API: Check whitelist status
string checkWhitelist(const string& apiKey, const string& identifier) {
if (!validateAPIKey(apiKey, "read")) {
return "ERROR: Invalid API key or insufficient permissions";
}
time_t now = time(nullptr);
for (const auto& entry : whitelist) {
if (entry.identifier == identifier) {
if (now > entry.expirationDate) {
return "EXPIRED: Whitelist entry expired on " + timeToString(entry.expirationDate);
}
return "WHITELISTED: Valid until " + timeToString(entry.expiration