ESP32/ESP8266 Firewall 1.0.0
a software firewall for ESP23 or ESP8266
fw::API Class Reference

The API to create, edit or remove Firewall rules. More...

#include <API.hpp>

Public Member Functions

 API (Firewall *, const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port=8080)
 Construct a new API object, assign ip and port to generate endpoint list, setup authentication, create WebServer, setup routing. More...
 
 ~API ()
 Destroy the API object.
 

Private Member Functions

String get_url_base ()
 Get the url base string e.g. http://0.0.0.0:8080/api. More...
 
ok_t setup_auth (const char *username, const char *password)
 Set up authentication. More...
 
auth_t check_auth ()
 check if request to API can proceed or needs to stop More...
 
void setup_routing (const char *cert, const char *key)
 Set up the routing/endpoints and encryption. More...
 
void add_endpoint_to_list (const String uri, const char *method, const char *description)
 add endpoint information to linked list that is used for the /api endpoint More...
 
void not_found_handler ()
 handling not found
 
void get_endpoint_list_handler ()
 GET handler to retrieve endpoint list.
 
void get_firewall_rule_handler ()
 GET handler to retrieve single firewall rule.
 
void get_firewall_rules_handler ()
 GET handler to retrieve firewall rules.
 
void post_firewall_handler ()
 POST handler to create firewall rule.
 
void put_firewall_handler ()
 PUT handler to update firewall rule.
 
void delete_firewall_handler ()
 DELETE handler to delete firewall rule.
 
bool request_has_all_firewall_parameter ()
 check if request to create/update firewall rule has all required parameter More...
 
String json_new_attribute (String key, String value, bool last=false)
 add another attribute to the json object More...
 
String json_new_attribute (String key, uint32_t value, bool last=false)
 add another attribute to the json object More...
 
void json_generic_response (String serialized_string, const uint16_t response_code)
 json response to send any string and response code More...
 
void json_array_response (String serialized_string, const uint16_t response_code)
 json response that wraps the message in array brackets More...
 
void json_message_response (String message, const uint16_t response_code)
 json response to send message as json object More...
 
String construct_json_firewall_rule (firewall_rule_t *rule_ptr)
 construct a firewall rule as json object More...
 
String construct_json_firewall ()
 construct array of all firewall rules as json object More...
 
String construct_json_api_endpoint (api_endpoint_t *api_ptr)
 construct an API endpoint as json object More...
 
String construct_json_api ()
 construct array of all endpoints as json object More...
 

Private Attributes

WebServer * server
 
Firewallfirewall
 
credential_t credentials
 
api_endpoint_tendpoint_head = NULL
 
String api_ip = "0.0.0.0"
 
uint16_t api_port
 
String json_response_type = "application/json; charset=utf-8"
 
const char * TAG = "[API]"
 

Detailed Description

The API to create, edit or remove Firewall rules.

Author
Florian Hoss

Constructor & Destructor Documentation

◆ API()

fw::API::API ( fw::Firewall firewall,
const char *  cert,
const char *  key,
const char *  username,
const char *  password,
const String  ip,
const uint16_t  port = 8080 
)

Construct a new API object, assign ip and port to generate endpoint list, setup authentication, create WebServer, setup routing.

Parameters
cert
key
username
password
ip
port

Member Function Documentation

◆ add_endpoint_to_list()

void fw::API::add_endpoint_to_list ( const String  uri,
const char *  method,
const char *  description 
)
private

add endpoint information to linked list that is used for the /api endpoint

Parameters
uri
method
description

◆ check_auth()

auth_t fw::API::check_auth ( )
private

check if request to API can proceed or needs to stop

Returns
auth_t

◆ construct_json_api()

String fw::API::construct_json_api ( )
private

construct array of all endpoints as json object

Parameters
api_ptr
Returns
String

◆ construct_json_api_endpoint()

String fw::API::construct_json_api_endpoint ( api_endpoint_t api_ptr)
private

construct an API endpoint as json object

Parameters
api_ptr
Returns
String

◆ construct_json_firewall()

String fw::API::construct_json_firewall ( )
private

construct array of all firewall rules as json object

Returns
String

◆ construct_json_firewall_rule()

String fw::API::construct_json_firewall_rule ( firewall_rule_t rule_ptr)
private

construct a firewall rule as json object

Parameters
rule_ptr
Returns
String

◆ get_url_base()

String fw::API::get_url_base ( )
private

Get the url base string e.g. http://0.0.0.0:8080/api.

Returns
String

◆ json_array_response()

void fw::API::json_array_response ( String  serialized_string,
const uint16_t  response_code 
)
private

json response that wraps the message in array brackets

Parameters
serialized_string
response_code

◆ json_generic_response()

void fw::API::json_generic_response ( String  serialized_string,
const uint16_t  response_code 
)
private

json response to send any string and response code

Parameters
serialized_string
response_code

◆ json_message_response()

void fw::API::json_message_response ( String  message,
const uint16_t  response_code 
)
private

json response to send message as json object

Parameters
message
response_code

◆ json_new_attribute() [1/2]

String fw::API::json_new_attribute ( String  key,
String  value,
bool  last = false 
)
private

add another attribute to the json object

Parameters
key
value
last
Returns
String

◆ json_new_attribute() [2/2]

String fw::API::json_new_attribute ( String  key,
uint32_t  value,
bool  last = false 
)
private

add another attribute to the json object

Parameters
key
value
last
Returns
String

◆ request_has_all_firewall_parameter()

bool fw::API::request_has_all_firewall_parameter ( )
private

check if request to create/update firewall rule has all required parameter

Returns
true
false

◆ setup_auth()

ok_t fw::API::setup_auth ( const char *  username,
const char *  password 
)
private

Set up authentication.

Parameters
username
password
Returns
ok_t

◆ setup_routing()

void fw::API::setup_routing ( const char *  cert,
const char *  key 
)
private

Set up the routing/endpoints and encryption.

Parameters
cert
key

The documentation for this class was generated from the following files: