Throw if api key looks wrong

This commit is contained in:
King_DuckZ 2020-09-06 17:10:57 +01:00
parent abc5441a50
commit 2221aef77c

View file

@ -28,6 +28,7 @@
#if defined(OROTOOL_WITH_RESTCCPP) #if defined(OROTOOL_WITH_RESTCCPP)
# include <restc-cpp/error.h> # include <restc-cpp/error.h>
#endif #endif
#include <regex>
namespace { namespace {
void print_ping(oro::Api& oro_api) { void print_ping(oro::Api& oro_api) {
@ -67,6 +68,17 @@ namespace {
constexpr const auto us = dhandy::bt::make_string("_"); constexpr const auto us = dhandy::bt::make_string("_");
return (name + us + app_version()); return (name + us + app_version());
} }
void validate_key(std::string_view key) {
using std::string;
using std::logic_error;
std::regex key_reg("^[a-z0-9]{32}$");
if (not std::regex_match(key.begin(), key.end(), key_reg))
throw logic_error("API key \"" + string(key) + "\" doesn't look valid");
return;
}
} //unnamed namespace } //unnamed namespace
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@ -81,6 +93,7 @@ int main(int argc, char* argv[]) {
); );
print_ping(*oro_api); print_ping(*oro_api);
validate_key(app_conf.api_key());
std::unique_ptr<oro::OriginsDB> db(oro::OriginsDB::make(app_conf.backend(), app_conf.db_path())); std::unique_ptr<oro::OriginsDB> db(oro::OriginsDB::make(app_conf.backend(), app_conf.db_path()));