Added API key generation
This commit is contained in:
53
main.cpp
53
main.cpp
@@ -18,6 +18,56 @@ using nlohmann::json;
|
|||||||
|
|
||||||
using namespace std;
|
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)
|
void reload_container(TRMNLContainer& container, const string& filename)
|
||||||
{
|
{
|
||||||
json j;
|
json j;
|
||||||
@@ -157,8 +207,7 @@ int main(int argc, char **argv)
|
|||||||
if (trmnl->api_key().empty())
|
if (trmnl->api_key().empty())
|
||||||
{
|
{
|
||||||
should_dump = true;
|
should_dump = true;
|
||||||
// TODO: Randomly generate key here
|
trmnl->api_key(generate_api_key());
|
||||||
trmnl->api_key("nullptr");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
response["status"] = 200;
|
response["status"] = 200;
|
||||||
|
|||||||
Reference in New Issue
Block a user