Improve json handling, validation, and multiple structure issues by ELF-Nigel · Pull Request #81 · KeyAuth/KeyAuth-CPP-Example · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 77 additions & 14 deletions x64/main.cpp
68 changes: 39 additions & 29 deletions x64/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,35 @@
#include "json.hpp"
using json = nlohmann::json;

std::string ReadFromJson(std::string path, std::string section)
{
if (!std::filesystem::exists(path))
return skCrypt("File Not Found").decrypt();
std::ifstream file(path);
json data = json::parse(file);
return data[section];
}

bool CheckIfJsonKeyExists(std::string path, std::string section)
{
if (!std::filesystem::exists(path))
return skCrypt("File Not Found").decrypt();
std::ifstream file(path);
json data = json::parse(file);
return data.contains(section);
}

bool WriteToJson(std::string path, std::string name, std::string value, bool userpass, std::string name2, std::string value2)
{
json file;
std::string ReadFromJson(std::string path, std::string section)
{
if (!std::filesystem::exists(path))
return ""; // missing file returns empty. -nigel
std::ifstream file(path);
if (!file.good())
return ""; // failed open returns empty. -nigel
json data = json::parse(file, nullptr, false);
if (data.is_discarded() || !data.contains(section))
return ""; // invalid or missing key returns empty. -nigel
return data[section];
}

bool CheckIfJsonKeyExists(std::string path, std::string section)
{
if (!std::filesystem::exists(path))
return false; // missing file means no key. -nigel
std::ifstream file(path);
if (!file.good())
return false; // failed open means no key. -nigel
json data = json::parse(file, nullptr, false);
if (data.is_discarded())
return false; // invalid json means no key. -nigel
return data.contains(section);
}

bool WriteToJson(std::string path, std::string name, std::string value, bool userpass, std::string name2, std::string value2)
{
json file;
if (!userpass)
{
file[name] = value;
Expand All @@ -37,14 +45,16 @@ bool WriteToJson(std::string path, std::string name, std::string value, bool use
file[name2] = value2;
}

std::ofstream jsonfile(path, std::ios::out);
jsonfile << file;
jsonfile.close();
if (!std::filesystem::exists(path))
return false;

return true;
}
std::ofstream jsonfile(path, std::ios::out | std::ios::trunc);
if (!jsonfile.good())
return false; // failed open means no write. -nigel
jsonfile << file;
jsonfile.flush();
if (!jsonfile.good() || !std::filesystem::exists(path))
return false;

return true;
}

void checkAuthenticated(std::string ownerid) {
while (true) {
Expand Down
8 changes: 5 additions & 3 deletions x86/lib/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ std::string utils::get_hwid() {
}

std::time_t utils::string_to_timet(std::string timestamp) {
auto cv = strtol(timestamp.c_str(), NULL, 10);

return (time_t)cv;
char* end = nullptr;
auto cv = strtol(timestamp.c_str(), &end, 10);
if (end == timestamp.c_str())
return 0; // invalid timestamp returns epoch. -nigel
return static_cast<time_t>(cv);
}

std::tm utils::timet_to_tm(time_t timestamp) {
Expand Down
38 changes: 35 additions & 3 deletions x86/main.cpp
Loading