diff --git a/src/API.hpp b/src/API.hpp index a53ca3c..20e9e41 100644 --- a/src/API.hpp +++ b/src/API.hpp @@ -12,10 +12,34 @@ namespace fw { + /** + * @brief The API to create, edit or remove firewall rules + * @author Florian Hoss + * + */ class API { public: + /** + * @brief Construct a new API object + * assign ip and port to generate endpoint list, + * setup authentication, + * create WebServer, + * setup routing + * + * @param cert + * @param key + * @param username + * @param password + * @param ip + * @param port + */ API(Firewall *, const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port = 8080); + + /** + * @brief Destroy the API object + * + */ ~API(); #ifdef ESP8266 void handle_client(); @@ -36,30 +60,164 @@ namespace fw String json_response_type = "application/json; charset=utf-8"; const char *TAG = "[API]"; + /** + * @brief Get the url base string + * e.g. http://0.0.0.0:8080/api + * + * @return String + */ String get_url_base(); + + /** + * @brief Set up authentication + * + * @param username + * @param password + * @return ok_t + */ ok_t setup_auth(const char *username, const char *password); + + /** + * @brief check if request to API can proceed or needs to stop + * + * @return auth_t + */ auth_t check_auth(); + /** + * @brief Set up the routing/endpoints and encryption + * + * @param cert + * @param key + */ void setup_routing(const char *cert, const char *key); + + /** + * @brief add endpoint information to linked list + * that is used for the /api endpoint + * + * @param uri + * @param method + * @param description + */ void add_endpoint_to_list(const String uri, const char *method, const char *description); + + /** + * @brief handling not found + */ void not_found_handler(); + + /** + * @brief GET handler to retrieve endpoint list + */ void get_endpoint_list_handler(); + + /** + * @brief GET handler to retrieve single firewall rule + */ void get_firewall_rule_handler(); + + /** + * @brief GET handler to retrieve firewall rules + */ void get_firewall_rules_handler(); + + /** + * @brief POST handler to create firewall rule + */ void post_firewall_handler(); + + /** + * @brief PUT handler to update firewall rule + */ void put_firewall_handler(); + /** + * @brief handling not found + */ void delete_firewall_handler(); + /** + * @brief check if request to create/update firewall rule + * has all required parameter + * + * @return true + * @return false + */ bool request_has_all_firewall_parameter(); + + /** + * @brief add another attribute to the json object + * + * @param key + * @param value + * @param last + * @return String + */ String json_new_attribute(String key, String value, bool last = false); + + /** + * @brief add another attribute to the json object + * + * @param key + * @param value + * @param last + * @return String + */ String json_new_attribute(String key, uint32_t value, bool last = false); + + /** + * @brief json response to send any string and response code + * + * @param serialized_string + * @param response_code + */ void json_generic_response(String serialized_string, const uint16_t response_code); + + /** + * @brief json response that wraps the message in array brackets + * + * @param serialized_string + * @param response_code + */ void json_array_response(String serialized_string, const uint16_t response_code); + + /** + * @brief json response to send message as json object + * + * @param message + * @param response_code + */ void json_message_response(String message, const uint16_t response_code); + /** + * @brief construct a firewall rule as json object + * + * @param rule_ptr + * @return String + */ String construct_json_firewall_rule(firewall_rule_t *rule_ptr); + + /** + * @brief construct array of all firewall rules as json object + * + * @return String + */ String construct_json_firewall(); + + /** + * @brief construct an API endpoint as json object + * + * @param api_ptr + * @return String + */ String construct_json_api_endpoint(api_endpoint_t *api_ptr); + + /** + * @brief construct array of all endpoints as json object + * + * @param api_ptr + * @return String + */ String construct_json_api(); }; } diff --git a/src/Utils.hpp b/src/Utils.hpp index ffe0970..2604b8b 100644 --- a/src/Utils.hpp +++ b/src/Utils.hpp @@ -78,12 +78,53 @@ namespace fw struct api_endpoints *next; } api_endpoint_t; + /** + * @brief converting protocol type to string + * + * @param protocol + * @return String + */ String protocol_to_string(firewall_protocol_t &protocol); + + /** + * @brief converting string to protocol type + * + * @param protocol + * @return firewall_protocol_t + */ firewall_protocol_t string_to_protocol(String &protocol); + + /** + * @brief converting target type to string + * + * @param target + * @return String + */ String target_to_string(firewall_target_t &target); + + /** + * @brief converting string to target type + * + * @param target + * @return firewall_target_t + */ firewall_target_t string_to_target(String &target); - String response_code_to_string(const uint16_t response_code); + + /** + * @brief running an endless loop to prevent further damage + * + */ void endless_loop(); + + /** + * @brief returns if number is between or equal to lower and upper + * + * @param number + * @param lower + * @param upper + * @return true + * @return false + */ bool is_in_range(const uint16_t number, const uint16_t lower, const uint16_t upper); }