From a0de61a6c0f60a69017f70acd648008b9a7fe7da Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Mon, 2 Dec 2024 18:58:29 +0700 Subject: [PATCH] cross build --- CMakeLists.txt | 5 +++-- src/AuthService/AuthService.cpp | 33 ++++++++++++++++++++++++++++++ src/AuthService/AuthService.hpp | 16 +++++++++++++++ src/auth/auth.cpp | 9 +++++---- src/auth/auth.hpp | 1 - src/auth/verify_auth.cpp | 7 ++++--- src/auth_service.cpp | 36 +++------------------------------ src/{ => libs}/colors.h | 0 src/security/cors.cpp | 4 ++-- src/users/access_request.cpp | 9 +++++---- src/users/add_user.cpp | 10 ++++----- src/users/change_password.cpp | 10 ++++----- src/users/change_user.cpp | 10 ++++----- src/users/delete_user.cpp | 10 ++++----- src/users/get_all_users.cpp | 10 ++++----- src/users/get_user.cpp | 10 ++++----- toolchain_files/arm_uclibc | 1 + 17 files changed, 102 insertions(+), 79 deletions(-) create mode 100644 src/AuthService/AuthService.cpp create mode 100644 src/AuthService/AuthService.hpp rename src/{ => libs}/colors.h (100%) create mode 100644 toolchain_files/arm_uclibc diff --git a/CMakeLists.txt b/CMakeLists.txt index e566468..7398193 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,8 @@ cmake_minimum_required (VERSION 2.6) project(auth_service) -set(CMAKE_CXX_COMPILER /home/tola/Documents/linux/orangepipc/buildroot-2024.08.2/output/host/bin/arm-buildroot-linux-uclibcgnueabihf-g++) set(CMAKE_CXX_STANDARD 17) -include_directories(./src/libs/) +include_directories(./src/libs/ ./src) add_executable( auth_service @@ -21,6 +20,8 @@ add_executable( src/users/change_user.cpp src/security/cors.cpp + + src/AuthService/AuthService.cpp ) target_link_libraries(auth_service ssl crypto) diff --git a/src/AuthService/AuthService.cpp b/src/AuthService/AuthService.cpp new file mode 100644 index 0000000..ed40789 --- /dev/null +++ b/src/AuthService/AuthService.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +AuthService::AuthService() +{ + this->srv.Post("/api/token", auth); + this->srv.Post("/api/users/user", get_user); + this->srv.Post("/api/users/all", get_all_users); + this->srv.Post("/api/users/add", add_user); + this->srv.Post("/api/users/user/password/change", change_password); + this->srv.Post("/api/users/del", delete_user); + this->srv.Post("/api/access", user_access); + this->srv.Post("/api/users/user/change", change_user); + + this->srv.Options("/(.*)", set_cors_headers); + + this->srv.set_mount_point("/auth/admin", "../src/web"); + this->srv.set_mount_point("/auth/js", "../src/web/js"); + this->srv.set_mount_point("/auth/css", "../src/web/css"); +} + +AuthService::~AuthService() = default; + +int AuthService::serve(const std::string &ip, int port, int socket_flags) +{ + std::cout << GREEN << "Starting" << RESET << " server on [ " << YELLOW + << ip << RESET << ":" << CYAN << port << RESET << " ]" << std::endl; + this->srv.listen(ip, port, socket_flags); + return 0; +} diff --git a/src/AuthService/AuthService.hpp b/src/AuthService/AuthService.hpp new file mode 100644 index 0000000..7e872f5 --- /dev/null +++ b/src/AuthService/AuthService.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +#define CORS_ENABLE + +class AuthService +{ +private: + httplib::Server srv; +public: + AuthService(); + ~AuthService(); + int serve(const std::string &ip, int port, int socket_flags = 0); +}; diff --git a/src/auth/auth.cpp b/src/auth/auth.cpp index 585d6b9..71c1e93 100644 --- a/src/auth/auth.cpp +++ b/src/auth/auth.cpp @@ -1,6 +1,7 @@ -#include "auth.hpp" -#include "../colors.h" -#include "../includes.hpp" +#include +#include +#include +#include #include #include @@ -54,7 +55,7 @@ void auth(const httplib::Request &request, httplib::Response &response) { {"userId", userid} }; - std::string token = JWT::createJWT(headers.dump(), payload.dump(), JWT_DEFAULT_SECRET); + std::string token = JWT::createJWT(headers.dump(), payload.dump(), JWT_SECRET_KEY); std::stringstream response_json; response_json << R"({"token":")" << token.c_str() << "\"}"; diff --git a/src/auth/auth.hpp b/src/auth/auth.hpp index 0e0e183..808e63f 100644 --- a/src/auth/auth.hpp +++ b/src/auth/auth.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include // function for authorization users void auth(const httplib::Request &request, httplib::Response &response); diff --git a/src/auth/verify_auth.cpp b/src/auth/verify_auth.cpp index 80d8376..102fdc6 100644 --- a/src/auth/verify_auth.cpp +++ b/src/auth/verify_auth.cpp @@ -1,12 +1,13 @@ -#include "auth.hpp" +#include +#include +#include #include -#include "../includes.hpp" #include // function for verify tokens bool verify_auth(const std::string &token) { try { - return JWT::verifyJWT(token, JWT_DEFAULT_SECRET); + return JWT::verifyJWT(token, JWT_SECRET_KEY); } catch (const std::system_error &e) { std::cout << "Verification error: " << e.what() << std::endl; syslog(LOG_ERR, "token verification error"); diff --git a/src/auth_service.cpp b/src/auth_service.cpp index a630c8c..f03e430 100644 --- a/src/auth_service.cpp +++ b/src/auth_service.cpp @@ -1,36 +1,6 @@ -#include -#include -#include -#include "auth/auth.hpp" -#include "users/users.hpp" -#include "colors.h" -#include - -#define CORS_ENABLE - -#include "security/cors.hpp" +#include int main() { - httplib::Server srv; - - srv.Post("/api/token", auth); - srv.Post("/api/users/user", get_user); - srv.Post("/api/users/all", get_all_users); - srv.Post("/api/users/add", add_user); - srv.Post("/api/users/user/password/change", change_password); - srv.Post("/api/users/del", delete_user); - srv.Post("/api/access", user_access); - srv.Post("/api/users/user/change", change_user); - - srv.Options("/(.*)", set_cors_headers); - - srv.set_mount_point("/auth/admin", "../src/web"); - srv.set_mount_point("/auth/js", "../src/web/js"); - srv.set_mount_point("/auth/css", "../src/web/css"); - - std::cout << GREEN << "Starting" << RESET << " server on [ " << YELLOW - << "0.0.0.0" << RESET << ":" << CYAN << "43243" << RESET << " ]" << std::endl; - syslog(LOG_INFO, "Authorization service started"); - srv.listen("0.0.0.0", 43243); - return 0; + AuthService auth_service; + return auth_service.serve("127.0.0.1", 43243); } diff --git a/src/colors.h b/src/libs/colors.h similarity index 100% rename from src/colors.h rename to src/libs/colors.h diff --git a/src/security/cors.cpp b/src/security/cors.cpp index 41a6cf1..838e89f 100644 --- a/src/security/cors.cpp +++ b/src/security/cors.cpp @@ -1,5 +1,5 @@ -#include "cors.hpp" -#include "../colors.h" +#include +#include void set_cors_headers(const httplib::Request &request, httplib::Response &response) { std::cout << GREEN << request.path << " " << std::endl; diff --git a/src/users/access_request.cpp b/src/users/access_request.cpp index 65d44aa..462457f 100644 --- a/src/users/access_request.cpp +++ b/src/users/access_request.cpp @@ -1,8 +1,9 @@ -#include "users.hpp" -#include "../colors.h" -#include "../includes.hpp" -#include "../auth/auth.hpp" +#include +#include +#include +#include #include +#include // function for access or reject authorization void user_access(const httplib::Request &request, httplib::Response &response) { diff --git a/src/users/add_user.cpp b/src/users/add_user.cpp index 4f5160c..9be53db 100644 --- a/src/users/add_user.cpp +++ b/src/users/add_user.cpp @@ -1,7 +1,7 @@ -#include "users.hpp" -#include "../colors.h" -#include "../includes.hpp" -#include "../auth/auth.hpp" +#include +#include +#include +#include #include #include @@ -53,7 +53,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) { return; } - std::string userid = JWT::get_payload(json_body["token"], JWT_DEFAULT_SECRET)["userId"]; + std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"]; if (userid.empty()) { response.set_content(STRING403, JSON_TYPE); syslog(LOG_ERR, "access reject in add user request"); diff --git a/src/users/change_password.cpp b/src/users/change_password.cpp index 79da5e4..2af44f6 100644 --- a/src/users/change_password.cpp +++ b/src/users/change_password.cpp @@ -1,7 +1,7 @@ -#include "users.hpp" -#include "../colors.h" -#include "../includes.hpp" -#include "../auth/auth.hpp" +#include +#include +#include +#include #include #include @@ -51,7 +51,7 @@ void change_password(const httplib::Request &request, httplib::Response &respons if (json_body["userid"] == nullptr) { old_password = json_body["old_password"]; } - std::string userid = JWT::get_payload(json_body["token"], JWT_DEFAULT_SECRET)["userId"]; + std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"]; if (userid.empty()) { response.set_content(STRING403, JSON_TYPE); syslog(LOG_ERR, "access reject in change password request"); diff --git a/src/users/change_user.cpp b/src/users/change_user.cpp index e287173..770b8e8 100644 --- a/src/users/change_user.cpp +++ b/src/users/change_user.cpp @@ -1,7 +1,7 @@ -#include "users.hpp" -#include "../colors.h" -#include "../includes.hpp" -#include "../auth/auth.hpp" +#include +#include +#include +#include #include #include @@ -39,7 +39,7 @@ void change_user(const httplib::Request &request, httplib::Response &response) { return; } - std::string userid = JWT::get_payload(json_body["token"], JWT_DEFAULT_SECRET)["userId"]; + std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"]; if (userid.empty()) { response.set_content(STRING403, JSON_TYPE); syslog(LOG_ERR, "access reject in change user request"); diff --git a/src/users/delete_user.cpp b/src/users/delete_user.cpp index d38f651..ac5ba5b 100644 --- a/src/users/delete_user.cpp +++ b/src/users/delete_user.cpp @@ -1,7 +1,7 @@ -#include "users.hpp" -#include "../colors.h" -#include "../includes.hpp" -#include "../auth/auth.hpp" +#include +#include +#include +#include #include #include @@ -31,7 +31,7 @@ void delete_user(const httplib::Request &request, httplib::Response &response) { return; } - std::string userid = JWT::get_payload(json_body["token"], JWT_DEFAULT_SECRET)["userId"]; + std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"]; if (userid.empty()) { response.set_content(STRING403, JSON_TYPE); syslog(LOG_ERR, "access reject in delete user request"); diff --git a/src/users/get_all_users.cpp b/src/users/get_all_users.cpp index 73fe4da..15c7d80 100644 --- a/src/users/get_all_users.cpp +++ b/src/users/get_all_users.cpp @@ -1,7 +1,7 @@ -#include "users.hpp" -#include "../colors.h" -#include "../includes.hpp" -#include "../auth/auth.hpp" +#include +#include +#include +#include #include #include @@ -26,7 +26,7 @@ void get_all_users(const httplib::Request &request, httplib::Response &response) syslog(LOG_ERR, "access reject in get all user request"); return; } - std::string userid = JWT::get_payload(json_body["token"], JWT_DEFAULT_SECRET)["userId"]; + std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"]; if (userid.empty()) { response.set_content(STRING403, JSON_TYPE); syslog(LOG_ERR, "access reject in get all user request"); diff --git a/src/users/get_user.cpp b/src/users/get_user.cpp index 217459b..b9ced57 100644 --- a/src/users/get_user.cpp +++ b/src/users/get_user.cpp @@ -1,7 +1,7 @@ -#include "users.hpp" -#include "../colors.h" -#include "../includes.hpp" -#include "../auth/auth.hpp" +#include +#include +#include +#include #include #include @@ -26,7 +26,7 @@ void get_user(const httplib::Request &request, httplib::Response &response) { syslog(LOG_ERR, "access reject in get user request"); return; } - std::string userid = JWT::get_payload(json_body["token"], JWT_DEFAULT_SECRET)["userId"]; + std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"]; if (userid.empty()) { response.set_content(STRING403, JSON_TYPE); syslog(LOG_ERR, "access reject in get user request"); diff --git a/toolchain_files/arm_uclibc b/toolchain_files/arm_uclibc new file mode 100644 index 0000000..b352fae --- /dev/null +++ b/toolchain_files/arm_uclibc @@ -0,0 +1 @@ +set(CMAKE_CXX_COMPILER /home/tola/Documents/linux/orangepipc/buildroot-2024.08.2/output/host/bin/arm-buildroot-linux-uclibcgnueabihf-g++) \ No newline at end of file