Added API key generation

This commit is contained in:
2026-06-25 16:06:13 +03:00
parent eca47d1596
commit 147fa762c8

View File

@@ -18,6 +18,56 @@ using nlohmann::json;
using namespace std;
constexpr int API_KEY_LENGHT = 16;
string generate_api_key(int len = API_KEY_LENGHT)
{
string result = "";
std::random_device rand;
std::uniform_int_distribution distribution(0, 63);
int single;
char symbol;
if (len < 0)
{
len = API_KEY_LENGHT;
}
for (int i = 0; i < len; ++i)
{
single = distribution(rand);
if (single <= 9)
{
symbol = '0' + single;
}
else if ((single >= 10) && (single <= 35))
{
symbol = 'A' + single - 10;
}
else if ((single >= 36) && (single <= 61))
{
symbol = 'a' + single - 36;
}
else if (62 == single)
{
symbol = '+';
}
else if (63 == single)
{
symbol = '/';
}
// This should never happen
else
{
symbol = '?';
}
result += symbol;
}
return result;
}
void reload_container(TRMNLContainer& container, const string& filename)
{
json j;
@@ -157,8 +207,7 @@ int main(int argc, char **argv)
if (trmnl->api_key().empty())
{
should_dump = true;
// TODO: Randomly generate key here
trmnl->api_key("nullptr");
trmnl->api_key(generate_api_key());
}
response["status"] = 200;