admin filter header

This commit is contained in:
EntireTwix 2021-06-27 17:10:51 -07:00
parent 371cd16e38
commit 7dec3bea72
4 changed files with 20 additions and 16 deletions

View file

@ -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

View file

@ -1,5 +1,6 @@
#pragma once
#include <drogon/HttpController.h>
#include "admin_filter.h"
#include "user_filter.h"
using namespace drogon;

View file

@ -87,6 +87,7 @@ int main(int argc, char **argv)
//endpoints
auto APIv1 = std::make_shared<v1::api>(bank); //v1
auto user_filter = std::make_shared<UserFilter>(bank);
auto admin_filter = std::make_shared<AdminFilter>(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();

View file

@ -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,16 +59,16 @@ 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,
void AdminFilter::doFilter(const HttpRequestPtr &req,
FilterCallback &&fcb,
FilterChainCallback &&fccb)
{
@ -77,7 +77,7 @@ void UserFilter::doFilter(const HttpRequestPtr &req,
{
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);