diff --git a/CMakeLists.txt b/CMakeLists.txt index 22e18cc..15b9f89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,7 @@ set(CMAKE_CXX_FLAGS_RELEASE "-O3") find_package(Threads REQUIRED) add_executable(${PROJECT_NAME} main.cpp ) target_sources(${PROJECT_NAME} PRIVATE + src/admin_filter.cpp src/bank_api.cpp src/bank.cpp src/log.cpp diff --git a/include/bank_api.h b/include/bank_api.h index 9fe6f75..97784a8 100644 --- a/include/bank_api.h +++ b/include/bank_api.h @@ -1,5 +1,6 @@ #pragma once #include +#include "admin_filter.h" #include "user_filter.h" using namespace drogon; diff --git a/main.cpp b/main.cpp index c192ff0..3a18c95 100644 --- a/main.cpp +++ b/main.cpp @@ -87,6 +87,7 @@ int main(int argc, char **argv) //endpoints auto APIv1 = std::make_shared(bank); //v1 auto user_filter = std::make_shared(bank); + auto admin_filter = std::make_shared(bank); app().registerPostHandlingAdvice( [](const drogon::HttpRequestPtr &req, const drogon::HttpResponsePtr &resp) { @@ -95,6 +96,7 @@ int main(int argc, char **argv) app() .loadConfigFile(config_location) .registerFilter(user_filter) + .registerFilter(admin_filter) .registerController(APIv1) .setThreadNum(get_nprocs()) .run(); diff --git a/src/admin_filter.cpp b/src/admin_filter.cpp index 5c086c7..fa6b9e6 100644 --- a/src/admin_filter.cpp +++ b/src/admin_filter.cpp @@ -1,6 +1,6 @@ -#include "user_filter.h" +#include "admin_filter.h" -static char DecodeChar(const char ch) +static char DecodeChar2(const char ch) { if (ch >= 'A' && ch <= 'Z') { @@ -17,7 +17,7 @@ static char DecodeChar(const char ch) return 63 - (ch == '-'); } -char *DecodeBase64(const char *string) +char *DecodeBase642(const char *string) { char *output; size_t length = strlen(string); @@ -30,10 +30,10 @@ char *DecodeBase64(const char *string) uint32_t storage = 0; while (string[4]) { - storage |= DecodeChar(*string++) << 18; - storage |= DecodeChar(*string++) << 12; - storage |= DecodeChar(*string++) << 6; - storage |= DecodeChar(*string++); + storage |= DecodeChar2(*string++) << 18; + storage |= DecodeChar2(*string++) << 12; + storage |= DecodeChar2(*string++) << 6; + storage |= DecodeChar2(*string++); output[index++] = storage >> 16; output[index++] = (char)(storage >> 8); @@ -42,8 +42,8 @@ char *DecodeBase64(const char *string) storage = 0; } - storage |= DecodeChar(*string++) << 18; - storage |= DecodeChar(*string++) << 12; + storage |= DecodeChar2(*string++) << 18; + storage |= DecodeChar2(*string++) << 12; output[index++] = storage >> 16; if (*string == '=') @@ -51,7 +51,7 @@ char *DecodeBase64(const char *string) output[index] = '\0'; return output; } - storage |= DecodeChar(*string++) << 6; + storage |= DecodeChar2(*string++) << 6; output[index++] = (char)(storage >> 8); if (*string == '=') @@ -59,25 +59,25 @@ char *DecodeBase64(const char *string) output[index] = '\0'; return output; } - storage |= DecodeChar(*string); + storage |= DecodeChar2(*string); output[index++] = (char)storage; output[index] = '\0'; return output; } -UserFilter::UserFilter(Bank &b) : bank(b) {} +AdminFilter::AdminFilter(Bank &b) : bank(b) {} -void UserFilter::doFilter(const HttpRequestPtr &req, - FilterCallback &&fcb, - FilterChainCallback &&fccb) +void AdminFilter::doFilter(const HttpRequestPtr &req, + FilterCallback &&fcb, + FilterChainCallback &&fccb) { const std::string &auth_header = req->getHeader("Authorization"); if (auth_header.size() > 6) { if (auth_header.substr(0, 6) == "Basic ") { - std::stringstream ss(DecodeBase64(auth_header.substr(6).c_str())); + std::stringstream ss(DecodeBase642(auth_header.substr(6).c_str())); std::string username, password; std::getline(ss, username, ':'); std::getline(ss, password);