Resolve "Auth for API"
This commit is contained in:
parent
6aee231a4e
commit
b531b5b360
13 changed files with 226 additions and 132 deletions
|
@ -1,194 +0,0 @@
|
|||
#include "esp32API.hpp"
|
||||
|
||||
namespace firewall
|
||||
{
|
||||
API::API(const uint16_t port)
|
||||
{
|
||||
this->setup_certificate();
|
||||
this->server = new HTTPSServer(this->certificate, port, 5);
|
||||
this->setup_routing();
|
||||
log_i("Starting server...");
|
||||
this->server->start();
|
||||
if (this->server->isRunning())
|
||||
{
|
||||
log_i("Server ready.");
|
||||
}
|
||||
}
|
||||
|
||||
API::~API()
|
||||
{
|
||||
}
|
||||
|
||||
void API::handle_clients()
|
||||
{
|
||||
this->server->loop();
|
||||
}
|
||||
|
||||
void API::setup_certificate()
|
||||
{
|
||||
this->certificate = retrieve_certificate();
|
||||
if (certificate != NULL)
|
||||
return;
|
||||
log_i("Creating the certificate...");
|
||||
this->certificate = new SSLCert();
|
||||
int createCertResult = createSelfSignedCert(
|
||||
*this->certificate,
|
||||
KEYSIZE_2048,
|
||||
"CN=myesp32.local,O=Firewall,C=DE",
|
||||
"20220101000000",
|
||||
"20320101000000");
|
||||
if (createCertResult != 0)
|
||||
{
|
||||
log_e("Cerating certificate failed. Error Code = 0x%02X, check SSLCert.hpp for details", createCertResult);
|
||||
while (true)
|
||||
delay(500);
|
||||
}
|
||||
store_certificate(certificate);
|
||||
log_i("Creating the certificate was successful");
|
||||
}
|
||||
|
||||
void API::setup_routing()
|
||||
{
|
||||
ResourceNode *get_firewall_rule = new ResourceNode("/api/v1/firewall/*", "GET", std::bind(&API::get_firewall_rule_handler, this, std::placeholders::_1, std::placeholders::_2));
|
||||
ResourceNode *get_firewall_rules = new ResourceNode("/api/v1/firewall", "GET", std::bind(&API::get_firewall_rules_handler, this, std::placeholders::_1, std::placeholders::_2));
|
||||
ResourceNode *post_firewall = new ResourceNode("/api/v1/firewall", "POST", std::bind(&API::post_firewall_handler, this, std::placeholders::_1, std::placeholders::_2));
|
||||
ResourceNode *delete_firewall = new ResourceNode("/api/v1/firewall/*", "DELETE", std::bind(&API::delete_firewall_handler, this, std::placeholders::_1, std::placeholders::_2));
|
||||
ResourceNode *not_found = new ResourceNode("", "GET", std::bind(&API::not_found_handler, this, std::placeholders::_1, std::placeholders::_2));
|
||||
this->server->registerNode(get_firewall_rule);
|
||||
this->server->registerNode(get_firewall_rules);
|
||||
this->server->registerNode(post_firewall);
|
||||
this->server->registerNode(delete_firewall);
|
||||
this->server->setDefaultNode(not_found);
|
||||
}
|
||||
|
||||
void API::not_found_handler(HTTPRequest *request, HTTPResponse *response)
|
||||
{
|
||||
this->json_message_response(response, "not found", 404);
|
||||
}
|
||||
|
||||
void API::get_firewall_rule_handler(HTTPRequest *request, HTTPResponse *response)
|
||||
{
|
||||
ResourceParameters *params = request->getParams();
|
||||
int rule_number = atoi(params->getPathParameter(0).c_str());
|
||||
firewall_rule_t *rule_ptr = get_rule_from_firewall(rule_number);
|
||||
if (rule_ptr == NULL)
|
||||
{
|
||||
this->json_message_response(response, "rule not found", 404);
|
||||
}
|
||||
else
|
||||
{
|
||||
response->setHeader("Content-Type", "application/json");
|
||||
response->setStatusCode(200);
|
||||
response->print(this->construct_json_firewall_rule(rule_ptr));
|
||||
}
|
||||
}
|
||||
|
||||
void API::get_firewall_rules_handler(HTTPRequest *request, HTTPResponse *response)
|
||||
{
|
||||
this->json_generic_response(response, this->construct_json_firewall(), 200);
|
||||
}
|
||||
|
||||
bool API::request_has_firewall_parameter(ResourceParameters *params)
|
||||
{
|
||||
return params->isQueryParameterSet("source") || params->isQueryParameterSet("destination") || params->isQueryParameterSet("protocol") || params->isQueryParameterSet("target");
|
||||
}
|
||||
|
||||
void API::post_firewall_handler(HTTPRequest *request, HTTPResponse *response)
|
||||
{
|
||||
ResourceParameters *params = request->getParams();
|
||||
if (request_has_firewall_parameter(params))
|
||||
{
|
||||
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
||||
rule_ptr->key = ++amount_of_rules;
|
||||
|
||||
// carefully copying c-string that is shorter then the destination char-array length
|
||||
std::string source;
|
||||
params->getQueryParameter("source", source);
|
||||
strcpy(rule_ptr->source, source.length() <= IPV4ADDRESS_LENGTH ? source.c_str() : "");
|
||||
std::string destination;
|
||||
params->getQueryParameter("destination", destination);
|
||||
strcpy(rule_ptr->destination, destination.length() <= IPV4ADDRESS_LENGTH ? destination.c_str() : "");
|
||||
|
||||
std::string protocol;
|
||||
params->getQueryParameter("protocol", protocol);
|
||||
rule_ptr->protocol = string_to_protocol(protocol);
|
||||
std::string target;
|
||||
params->getQueryParameter("target", target);
|
||||
rule_ptr->target = string_to_target(target);
|
||||
|
||||
add_rule_to_firewall(rule_ptr);
|
||||
this->json_generic_response(response, this->construct_json_firewall_rule(rule_ptr), 200);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->json_message_response(response, "not enough parameter", 400);
|
||||
}
|
||||
}
|
||||
|
||||
void API::delete_firewall_handler(HTTPRequest *request, HTTPResponse *response)
|
||||
{
|
||||
ResourceParameters *params = request->getParams();
|
||||
int rule_number = atoi(params->getPathParameter(0).c_str());
|
||||
if (delete_rule_from_firewall(rule_number))
|
||||
{
|
||||
this->json_message_response(response, "firewall rule deleted", 200);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->json_message_response(response, "cannot delete firewall rule", 500);
|
||||
}
|
||||
}
|
||||
|
||||
void API::json_generic_response(HTTPResponse *response, String serialized, const uint16_t response_code)
|
||||
{
|
||||
response->setHeader("Content-Type", "application/json");
|
||||
response->setStatusCode(response_code);
|
||||
response->println(serialized);
|
||||
}
|
||||
|
||||
void API::json_message_response(HTTPResponse *response, String message, const uint16_t response_code)
|
||||
{
|
||||
response->setHeader("Content-Type", "application/json");
|
||||
response->setStatusCode(response_code);
|
||||
StaticJsonDocument<96> json;
|
||||
String serialized;
|
||||
json["message"] = message;
|
||||
serializeJson(json, serialized);
|
||||
response->println(serialized);
|
||||
}
|
||||
|
||||
String API::construct_json_firewall_rule(firewall_rule_t *rule_ptr)
|
||||
{
|
||||
StaticJsonDocument<256> doc;
|
||||
doc["key"] = rule_ptr->key;
|
||||
doc["source"] = rule_ptr->source;
|
||||
doc["destination"] = rule_ptr->destination;
|
||||
doc["protocol"] = protocol_to_string(rule_ptr->protocol);
|
||||
doc["target"] = target_to_string(rule_ptr->target);
|
||||
String response;
|
||||
serializeJson(doc, response);
|
||||
return response;
|
||||
}
|
||||
|
||||
String API::construct_json_firewall()
|
||||
{
|
||||
firewall_rule_t *rule_ptr = head;
|
||||
// Size for approx. 12 Rules
|
||||
StaticJsonDocument<2048> doc;
|
||||
String response;
|
||||
doc["amount_of_rules"] = amount_of_rules;
|
||||
JsonArray rules = doc.createNestedArray("rules");
|
||||
while (rule_ptr != NULL)
|
||||
{
|
||||
JsonObject rule = rules.createNestedObject();
|
||||
rule["key"] = rule_ptr->key;
|
||||
rule["source"] = rule_ptr->source;
|
||||
rule["destination"] = rule_ptr->destination;
|
||||
rule["protocol"] = protocol_to_string(rule_ptr->protocol);
|
||||
rule["target"] = target_to_string(rule_ptr->target);
|
||||
rule_ptr = rule_ptr->next;
|
||||
}
|
||||
serializeJson(doc, response);
|
||||
return response;
|
||||
}
|
||||
}
|
Reference in a new issue