diff --git a/src/AuthService/AuthService.cpp b/src/AuthService/AuthService.cpp index 40acb0f..7e5ec9c 100644 --- a/src/AuthService/AuthService.cpp +++ b/src/AuthService/AuthService.cpp @@ -4,30 +4,33 @@ #include #include -AuthService::AuthService() +namespace 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); + AuthService::AuthService() + { + this->srv.Post("/api/token", Auth::auth); + this->srv.Post("/api/users/user", Users::get_user); + this->srv.Post("/api/users/all", Users::get_all_users); + this->srv.Post("/api/users/add", Users::add_user); + this->srv.Post("/api/users/user/password/change", Users::change_password); + this->srv.Post("/api/users/del", Users::delete_user); + this->srv.Post("/api/access", Users::user_access); + this->srv.Post("/api/users/user/change", Users::change_user); - this->srv.Options("/(.*)", set_cors_headers); + this->srv.Options("/(.*)", Security::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"); -} + 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; + 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; + 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 index 17f2a5b..e7202fc 100644 --- a/src/AuthService/AuthService.hpp +++ b/src/AuthService/AuthService.hpp @@ -4,13 +4,15 @@ #include #define CORS_ENABLE - -class AuthService +namespace AuthService { -private: - httplib::Server srv; -public: - AuthService(); - ~AuthService(); - int serve(const std::string& ip, int port, int socket_flags = 0); -}; + 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 1185f33..e52608b 100644 --- a/src/auth/auth.cpp +++ b/src/auth/auth.cpp @@ -4,63 +4,68 @@ #include #include #include -#include -// function for authorization users -void auth(const httplib::Request& request, httplib::Response& response) +namespace AuthService { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(AUTH_REQUIRED_STRING, JSON_TYPE); - syslog(LOG_ERR, "no body in auth request"); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); + namespace Auth + { + // function for authorization users + void auth(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(AUTH_REQUIRED_STRING, JSON_TYPE); + syslog(LOG_ERR, "no body in auth request"); + return; + } + nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["username"] == nullptr) { - response.set_content(AUTH_REQUIRED_STRING, JSON_TYPE); - syslog(LOG_ERR, "no username in auth request"); - return; - } - if (json_body["password"] == nullptr) { - response.set_content(AUTH_REQUIRED_STRING, JSON_TYPE); - syslog(LOG_ERR, "no password in auth request"); - return; - } - const std::string request_username = json_body["username"]; - const std::string request_password = json_body["password"]; + if (json_body["username"] == nullptr) { + response.set_content(AUTH_REQUIRED_STRING, JSON_TYPE); + syslog(LOG_ERR, "no username in auth request"); + return; + } + if (json_body["password"] == nullptr) { + response.set_content(AUTH_REQUIRED_STRING, JSON_TYPE); + syslog(LOG_ERR, "no password in auth request"); + return; + } + const std::string request_username = json_body["username"]; + const std::string request_password = json_body["password"]; - std::lock_guard lock(usersfilemutex); - std::ifstream usersfile(USERS_FILE); - nlohmann::json all_users = nlohmann::json::parse(usersfile); - usersfile.close(); + std::lock_guard lock(AuthService::Includes::usersfilemutex); + std::ifstream usersfile(USERS_FILE); + nlohmann::json all_users = nlohmann::json::parse(usersfile); + usersfile.close(); - std::string userid; - for (nlohmann::basic_json<> user : all_users) { - if ((user["username"] == request_username) && (user["password"] == request_password)) { - userid = user["id"]; - break; + std::string userid; + for (nlohmann::basic_json<> user : all_users) { + if ((user["username"] == request_username) && (user["password"] == request_password)) { + userid = user["id"]; + break; + } + } + if (userid.empty()) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "auth request rejected"); + return; + } + + nlohmann::json headers = { + {"alg", "HS256"}, + {"typ", "JWT"} + }; + + nlohmann::json payload{ + {"userId", userid} + }; + + std::string token = JWT::createJWT(headers.dump(), payload.dump(), JWT_SECRET_KEY); + + std::string response_json = R"({"token":")" + token + "\"}"; + syslog(LOG_INFO, "auth request success"); + response.set_content(response_json, JSON_TYPE); } } - if (userid.empty()) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "auth request rejected"); - return; - } - - nlohmann::json headers = { - {"alg", "HS256"}, - {"typ", "JWT"} - }; - - nlohmann::json payload{ - {"userId", userid} - }; - - std::string token = JWT::createJWT(headers.dump(), payload.dump(), JWT_SECRET_KEY); - - std::string response_json = R"({"token":")" + token + "\"}"; - syslog(LOG_INFO, "auth request success"); - response.set_content(response_json, JSON_TYPE); } diff --git a/src/auth/auth.hpp b/src/auth/auth.hpp index 74b5a59..b5bad8d 100644 --- a/src/auth/auth.hpp +++ b/src/auth/auth.hpp @@ -2,8 +2,14 @@ #include -// function for authorization users -void auth(const httplib::Request& request, httplib::Response& response); +namespace AuthService +{ + namespace Auth + { + // function for authorization users + void auth(const httplib::Request& request, httplib::Response& response); -// function for verify tokens -bool verify_auth(const std::string& token); + // function for verify tokens + bool verify_auth(const std::string& token); + } +} diff --git a/src/auth/verify_auth.cpp b/src/auth/verify_auth.cpp index c5e0780..7e5fdb0 100644 --- a/src/auth/verify_auth.cpp +++ b/src/auth/verify_auth.cpp @@ -3,19 +3,24 @@ #include #include #include - -// function for verify tokens -bool verify_auth(const std::string& token) +namespace AuthService { - try { - 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"); - return false; - } - catch (...) { - return false; + namespace Auth + { + // function for verify tokens + bool verify_auth(const std::string& token) + { + try { + 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"); + return false; + } + catch (...) { + return false; + } + } } } diff --git a/src/auth_service.cpp b/src/auth_service.cpp index d6be8da..fc3891e 100644 --- a/src/auth_service.cpp +++ b/src/auth_service.cpp @@ -2,10 +2,10 @@ int main() { - AuthService auth_service; #ifdef TEST return 0; #else + AuthService::AuthService auth_service; return auth_service.serve("0.0.0.0", 43243); #endif } diff --git a/src/includes.hpp b/src/includes.hpp index dd5dfe8..28fe0b9 100644 --- a/src/includes.hpp +++ b/src/includes.hpp @@ -1,6 +1,10 @@ #ifndef INCLUDES_HPP #define INCLUDES_HPP +namespace AuthService +{ + namespace Includes + { #define JWT_SECRET_KEY "secret" #define STRING403 R"({"status":"403"})" #define JSON_TYPE "application/json" @@ -11,6 +15,7 @@ #define CHANGE_USER_REQUIRED_STRING R"({"required":"[token] or if you superuser [token,userid]","optional":"[username,password,is_superuser,group]"})" #define DELETE_USER_REQUIRED_STRING R"({"required":"[token] or if you superuser [token,userid]"})" #define USERS_FILE "users.json" -static std::mutex usersfilemutex; - + static std::mutex usersfilemutex; + } +} #endif // INCLUDES_HPP diff --git a/src/security/HashPassword.hpp b/src/security/HashPassword.hpp deleted file mode 100644 index 731b4c8..0000000 --- a/src/security/HashPassword.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -// funcion for hashing password using sha256 -std::string hashPassword(const std::string& password) -{ - unsigned char hash[SHA256_DIGEST_LENGTH]; - - // calculating hash - SHA256(reinterpret_cast(password.c_str()), password.size(), hash); - - // reinterpreter as hex string - std::stringstream ss; - for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { - ss << std::setw(2) << std::setfill('0') << std::hex << static_cast(hash[i]); - } - - return ss.str(); -} diff --git a/src/security/cors.cpp b/src/security/cors.cpp index f79cbed..9036220 100644 --- a/src/security/cors.cpp +++ b/src/security/cors.cpp @@ -1,10 +1,16 @@ #include #include -void set_cors_headers(const httplib::Request& request, httplib::Response& response) +namespace AuthService { - std::cout << GREEN << request.path << " " << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - response.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS"); - response.set_header("Access-Control-Allow-Headers", "Authorization,X-Custom,Content-Type"); + namespace Security + { + void set_cors_headers(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << " " << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + response.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS"); + response.set_header("Access-Control-Allow-Headers", "Authorization,X-Custom,Content-Type"); + } + } } diff --git a/src/security/cors.hpp b/src/security/cors.hpp index a132184..02a8ebc 100644 --- a/src/security/cors.hpp +++ b/src/security/cors.hpp @@ -3,11 +3,14 @@ #include +namespace AuthService +{ + namespace Security + { #ifdef CORS_ENABLE - -// function to set cors headers -void set_cors_headers(const httplib::Request& request, httplib::Response& response); - + // function to set cors headers + void set_cors_headers(const httplib::Request& request, httplib::Response& response); #endif // CORS_ENABLE - + } +} #endif // CORS_HPP diff --git a/src/users/access_request.cpp b/src/users/access_request.cpp index baef5bc..8a35267 100644 --- a/src/users/access_request.cpp +++ b/src/users/access_request.cpp @@ -5,28 +5,34 @@ #include #include -// function for access or reject authorization -void user_access(const httplib::Request& request, httplib::Response& response) +namespace AuthService { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - syslog(LOG_ERR, "access request body is emty"); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); + namespace Users + { + // function for access or reject authorization + void user_access(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + syslog(LOG_ERR, "access request body is emty"); + return; + } + nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - syslog(LOG_ERR, "no token in acces request"); - return; + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + syslog(LOG_ERR, "no token in acces request"); + return; + } + if (!AuthService::Auth::verify_auth(json_body["token"])) { + response.set_content(R"({"access":"reject"})", JSON_TYPE); + syslog(LOG_ERR, "access rejected"); + return; + } + response.set_content(R"({"access":"success"})", JSON_TYPE); + syslog(LOG_INFO, "access success"); + } } - if (!verify_auth(json_body["token"])) { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - syslog(LOG_ERR, "access rejected"); - return; - } - response.set_content(R"({"access":"success"})", JSON_TYPE); - syslog(LOG_INFO, "access success"); } diff --git a/src/users/add_user.cpp b/src/users/add_user.cpp index 004a36e..c1b82a7 100644 --- a/src/users/add_user.cpp +++ b/src/users/add_user.cpp @@ -6,110 +6,115 @@ #include #define ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - -// function for generate random string -std::string get_random_string(const char* alphabet, int quantity) +namespace AuthService { - srand(time(nullptr)); - std::string str; - for (int i = 0; i < quantity; i++) { - str += alphabet[0 + std::rand() % strlen(alphabet)]; - } - return str; -} - -// function for add user -void add_user(const httplib::Request& request, httplib::Response& response) -{ - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ADD_USER_REQUIRED_STRING, - JSON_TYPE); - syslog(LOG_ERR, "add user request body is empty"); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["token"] == nullptr) { - response.set_content(ADD_USER_REQUIRED_STRING, - JSON_TYPE); - syslog(LOG_ERR, "no token in add user request"); - return; - } - if (!verify_auth(json_body["token"])) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in add user request"); - return; - } - if (json_body["username"] == nullptr) { - response.set_content(ADD_USER_REQUIRED_STRING, - JSON_TYPE); - syslog(LOG_ERR, "no username in add user request"); - return; - } - if (json_body["password"] == nullptr) { - response.set_content(ADD_USER_REQUIRED_STRING, - JSON_TYPE); - syslog(LOG_ERR, "no password in add user request"); - return; - } - - 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"); - return; - } - nlohmann::json all_users; + namespace Users { - std::lock_guard lock(usersfilemutex); - std::ifstream usersfile(USERS_FILE); - all_users = nlohmann::json::parse(usersfile); - usersfile.close(); - } + // function for generate random string + std::string get_random_string(const char* alphabet, int quantity) + { + srand(time(nullptr)); + std::string str; + for (int i = 0; i < quantity; i++) { + str += alphabet[0 + std::rand() % strlen(alphabet)]; + } + return str; + } - nlohmann::json response_user_data = nullptr; - for (nlohmann::json& user : all_users) { - if (user["id"] == userid) { - response_user_data = user; + // function for add user + void add_user(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ADD_USER_REQUIRED_STRING, + JSON_TYPE); + syslog(LOG_ERR, "add user request body is empty"); + return; + } + nlohmann::json json_body = nlohmann::json::parse(request.body); + + if (json_body["token"] == nullptr) { + response.set_content(ADD_USER_REQUIRED_STRING, + JSON_TYPE); + syslog(LOG_ERR, "no token in add user request"); + return; + } + if (!AuthService::Auth::verify_auth(json_body["token"])) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in add user request"); + return; + } + if (json_body["username"] == nullptr) { + response.set_content(ADD_USER_REQUIRED_STRING, + JSON_TYPE); + syslog(LOG_ERR, "no username in add user request"); + return; + } + if (json_body["password"] == nullptr) { + response.set_content(ADD_USER_REQUIRED_STRING, + JSON_TYPE); + syslog(LOG_ERR, "no password in add user request"); + return; + } + + 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"); + return; + } + nlohmann::json all_users; + { + std::lock_guard lock(AuthService::Includes::usersfilemutex); + std::ifstream usersfile(USERS_FILE); + all_users = nlohmann::json::parse(usersfile); + usersfile.close(); + } + + nlohmann::json response_user_data = nullptr; + for (nlohmann::json& user : all_users) { + if (user["id"] == userid) { + response_user_data = user; + } + } + if (response_user_data == nullptr) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in add user request"); + return; + } + if (response_user_data["is_superuser"] != "1") { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in add user request"); + return; + } + + const std::string username = json_body["username"]; + const std::string password = json_body["password"]; + + for (nlohmann::json user : all_users) { + if ((user["username"] == username)) { + response.set_content(R"({"status":"user already exists"})", JSON_TYPE); + syslog(LOG_ERR, "user already exists in add user request"); + return; + } + } + + std::string new_user_data; + new_user_data += R"({"id":")" + get_random_string(ALPHABET, 50) + R"(","username":")" + username + R"(","password":")" + password + R"(","group":)"; + (json_body["group"] != nullptr) ? new_user_data += R"(")" + std::string(json_body["group"]) + R"(")" : new_user_data += R"("users")"; + new_user_data += ",\"is_superuser\":"; + (json_body["is_superuser"] != nullptr) ? new_user_data += R"(")" + std::string(json_body["is_superuser"]) + R"(")" : new_user_data += "\"0\""; + new_user_data += "}"; + all_users.push_back(nlohmann::json::parse(new_user_data)); + + std::lock_guard lock(AuthService::Includes::usersfilemutex); + std::ofstream usersfilew(USERS_FILE); + usersfilew << all_users.dump(4); + usersfilew.close(); + std::string syslogstring = "user added [" + username + "]"; + syslog(LOG_INFO, syslogstring.c_str()); + response.set_content(R"({"status":"ok"})", JSON_TYPE); } } - if (response_user_data == nullptr) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in add user request"); - return; - } - if (response_user_data["is_superuser"] != "1") { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in add user request"); - return; - } - - const std::string username = json_body["username"]; - const std::string password = json_body["password"]; - - for (nlohmann::json user : all_users) { - if ((user["username"] == username)) { - response.set_content(R"({"status":"user already exists"})", JSON_TYPE); - syslog(LOG_ERR, "user already exists in add user request"); - return; - } - } - - std::string new_user_data; - new_user_data += R"({"id":")" + get_random_string(ALPHABET, 50) + R"(","username":")" + username + R"(","password":")" + password + R"(","group":)"; - (json_body["group"] != nullptr) ? new_user_data += R"(")" + std::string(json_body["group"]) + R"(")" : new_user_data += R"("users")"; - new_user_data += ",\"is_superuser\":"; - (json_body["is_superuser"] != nullptr) ? new_user_data += R"(")" + std::string(json_body["is_superuser"]) + R"(")" : new_user_data += "\"0\""; - new_user_data += "}"; - all_users.push_back(nlohmann::json::parse(new_user_data)); - - std::lock_guard lock(usersfilemutex); - std::ofstream usersfilew(USERS_FILE); - usersfilew << all_users.dump(4); - usersfilew.close(); - std::string syslogstring = "user added [" + username + "]"; - syslog(LOG_INFO, syslogstring.c_str()); - response.set_content(R"({"status":"ok"})", JSON_TYPE); } diff --git a/src/users/change_password.cpp b/src/users/change_password.cpp index 9924f72..63a29c2 100644 --- a/src/users/change_password.cpp +++ b/src/users/change_password.cpp @@ -5,119 +5,125 @@ #include #include -// functtion for change password for user -void change_password(const httplib::Request& request, httplib::Response& response) +namespace AuthService { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content( - CHANGE_PASSWORD_REQUIRED_STRING, - JSON_TYPE - ); - syslog(LOG_ERR, "body is empty in change password request"); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["token"] == nullptr) { - response.set_content( - CHANGE_PASSWORD_REQUIRED_STRING, - JSON_TYPE - ); - syslog(LOG_ERR, "no token in change password request"); - return; - } - if ((json_body["userid"] == nullptr) && (json_body["old_password"] == nullptr)) { - response.set_content( - CHANGE_PASSWORD_REQUIRED_STRING, - JSON_TYPE); - syslog(LOG_ERR, "no old password is change password request"); - return; - } - if (json_body["new_password"] == nullptr) { - response.set_content(CHANGE_PASSWORD_REQUIRED_STRING, - JSON_TYPE); - syslog(LOG_ERR, "no new password in change password request"); - return; - } - if (!verify_auth(json_body["token"])) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in change password request"); - return; - } - - std::string old_password; - const std::string new_password = json_body["new_password"]; - - if (json_body["userid"] == nullptr) { old_password = json_body["old_password"]; } - - 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"); - return; - } - nlohmann::json all_users; + namespace Users { - std::lock_guard lock(usersfilemutex); - std::ifstream usersfile(USERS_FILE); - all_users = nlohmann::json::parse(usersfile); - usersfile.close(); - } + // functtion for change password for user + void change_password(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content( + CHANGE_PASSWORD_REQUIRED_STRING, + JSON_TYPE + ); + syslog(LOG_ERR, "body is empty in change password request"); + return; + } + nlohmann::json json_body = nlohmann::json::parse(request.body); - std::string changepassuserid = userid; - if (json_body["userid"] != nullptr) { - changepassuserid = json_body["userid"]; - nlohmann::json response_user_data = nullptr; - for (nlohmann::json& user : all_users) { - if (user["id"] == userid) { - response_user_data = user; + if (json_body["token"] == nullptr) { + response.set_content( + CHANGE_PASSWORD_REQUIRED_STRING, + JSON_TYPE + ); + syslog(LOG_ERR, "no token in change password request"); + return; + } + if ((json_body["userid"] == nullptr) && (json_body["old_password"] == nullptr)) { + response.set_content( + CHANGE_PASSWORD_REQUIRED_STRING, + JSON_TYPE); + syslog(LOG_ERR, "no old password is change password request"); + return; + } + if (json_body["new_password"] == nullptr) { + response.set_content(CHANGE_PASSWORD_REQUIRED_STRING, + JSON_TYPE); + syslog(LOG_ERR, "no new password in change password request"); + return; + } + if (!AuthService::Auth::verify_auth(json_body["token"])) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in change password request"); + return; } - } - if (response_user_data == nullptr) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in change password request"); - return; - } - if (response_user_data["is_superuser"] != "1") { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in change password request"); - return; - } - } - bool found = false; - if (json_body["userid"] != nullptr) { - for (nlohmann::basic_json<>& all_user : all_users) { - if ((all_user["id"] == changepassuserid)) { - all_user["password"] = new_password; - found = true; - std::string syslogstring = "password changed for user [" + std::string(all_user["username"]) + "]"; - syslog(LOG_INFO, syslogstring.c_str()); - break; + std::string old_password; + const std::string new_password = json_body["new_password"]; + + if (json_body["userid"] == nullptr) { old_password = json_body["old_password"]; } + + 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"); + return; } + nlohmann::json all_users; + { + std::lock_guard lock(AuthService::Includes::usersfilemutex); + std::ifstream usersfile(USERS_FILE); + all_users = nlohmann::json::parse(usersfile); + usersfile.close(); + } + + std::string changepassuserid = userid; + if (json_body["userid"] != nullptr) { + changepassuserid = json_body["userid"]; + nlohmann::json response_user_data = nullptr; + for (nlohmann::json& user : all_users) { + if (user["id"] == userid) { + response_user_data = user; + } + } + if (response_user_data == nullptr) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in change password request"); + return; + } + if (response_user_data["is_superuser"] != "1") { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in change password request"); + return; + } + } + + bool found = false; + if (json_body["userid"] != nullptr) { + for (nlohmann::basic_json<>& all_user : all_users) { + if ((all_user["id"] == changepassuserid)) { + all_user["password"] = new_password; + found = true; + std::string syslogstring = "password changed for user [" + std::string(all_user["username"]) + "]"; + syslog(LOG_INFO, syslogstring.c_str()); + break; + } + } + } + else { + for (nlohmann::basic_json<>& all_user : all_users) { + if ((all_user["id"] == changepassuserid) && (all_user["password"] == old_password)) { + all_user["password"] = new_password; + found = true; + std::string syslogstring = "password changed for user [" + std::string(all_user["username"]) + "]"; + syslog(LOG_INFO, syslogstring.c_str()); + break; + } + } + } + if (!found) { + response.set_content(R"({"status":"can't find this user"})", JSON_TYPE); + syslog(LOG_ERR, "can't find this user in change password request"); + return; + } + std::lock_guard lock(AuthService::Includes::usersfilemutex); + std::ofstream usersfilew(USERS_FILE); + usersfilew << all_users.dump(4); + usersfilew.close(); + response.set_content(R"({"status":"ok"})", JSON_TYPE); } } - else { - for (nlohmann::basic_json<>& all_user : all_users) { - if ((all_user["id"] == changepassuserid) && (all_user["password"] == old_password)) { - all_user["password"] = new_password; - found = true; - std::string syslogstring = "password changed for user [" + std::string(all_user["username"]) + "]"; - syslog(LOG_INFO, syslogstring.c_str()); - break; - } - } - } - if (!found) { - response.set_content(R"({"status":"can't find this user"})", JSON_TYPE); - syslog(LOG_ERR, "can't find this user in change password request"); - return; - } - std::lock_guard lock(usersfilemutex); - std::ofstream usersfilew(USERS_FILE); - usersfilew << all_users.dump(4); - usersfilew.close(); - response.set_content(R"({"status":"ok"})", JSON_TYPE); } diff --git a/src/users/change_user.cpp b/src/users/change_user.cpp index 9fb75d2..eefab6c 100644 --- a/src/users/change_user.cpp +++ b/src/users/change_user.cpp @@ -5,109 +5,115 @@ #include #include -// function for change user data -void change_user(const httplib::Request& request, httplib::Response& response) +namespace AuthService { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content( - CHANGE_USER_REQUIRED_STRING, - JSON_TYPE); - syslog(LOG_ERR, "change user request body is empty"); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["token"] == nullptr) { - response.set_content( - CHANGE_USER_REQUIRED_STRING, - JSON_TYPE - ); - syslog(LOG_ERR, "no token in change user request"); - return; - } - if ((json_body["userid"] == nullptr) && (json_body["old_password"] == nullptr)) { - response.set_content( - CHANGE_USER_REQUIRED_STRING, - JSON_TYPE); - syslog(LOG_ERR, "no old_password in change user request"); - return; - } - if (!verify_auth(json_body["token"])) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in change user request"); - return; - } - - 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"); - return; - } - nlohmann::json all_users; + namespace Users { - std::lock_guard lock(usersfilemutex); - std::ifstream usersfile(USERS_FILE); - all_users = nlohmann::json::parse(usersfile); - usersfile.close(); - } - std::string changeusruserid = userid; - if (json_body["userid"] != nullptr) { - changeusruserid = json_body["userid"]; - nlohmann::json response_user_data = nullptr; - for (nlohmann::json& user : all_users) { - if (user["id"] == userid) { - response_user_data = user; + // function for change user data + void change_user(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content( + CHANGE_USER_REQUIRED_STRING, + JSON_TYPE); + syslog(LOG_ERR, "change user request body is empty"); + return; } - } - if (response_user_data == nullptr) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in change user request"); - return; - } - if (response_user_data["is_superuser"] != "1") { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in change user request"); - return; - } - } + nlohmann::json json_body = nlohmann::json::parse(request.body); - bool found = false; - if (json_body["userid"] != nullptr) { - for (nlohmann::basic_json<>& all_user : all_users) { - if ((all_user["id"] == changeusruserid)) { - if (json_body["username"] != nullptr) { all_user["username"] = json_body["username"]; } - if (json_body["password"] != nullptr) { all_user["password"] = json_body["password"]; } - if (json_body["group"] != nullptr) { all_user["group"] = json_body["group"]; } - if (json_body["is_superuser"] != nullptr) { all_user["is_superuser"] = json_body["is_superuser"]; } - found = true; - std::string syslogstring = "userdata changed for user [" + std::string(all_user["username"]) + "]"; - syslog(LOG_INFO, syslogstring.c_str()); - break; + if (json_body["token"] == nullptr) { + response.set_content( + CHANGE_USER_REQUIRED_STRING, + JSON_TYPE + ); + syslog(LOG_ERR, "no token in change user request"); + return; } + if ((json_body["userid"] == nullptr) && (json_body["old_password"] == nullptr)) { + response.set_content( + CHANGE_USER_REQUIRED_STRING, + JSON_TYPE); + syslog(LOG_ERR, "no old_password in change user request"); + return; + } + if (!AuthService::Auth::verify_auth(json_body["token"])) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in change user request"); + return; + } + + 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"); + return; + } + nlohmann::json all_users; + { + std::lock_guard lock(AuthService::Includes::usersfilemutex); + std::ifstream usersfile(USERS_FILE); + all_users = nlohmann::json::parse(usersfile); + usersfile.close(); + } + std::string changeusruserid = userid; + if (json_body["userid"] != nullptr) { + changeusruserid = json_body["userid"]; + nlohmann::json response_user_data = nullptr; + for (nlohmann::json& user : all_users) { + if (user["id"] == userid) { + response_user_data = user; + } + } + if (response_user_data == nullptr) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in change user request"); + return; + } + if (response_user_data["is_superuser"] != "1") { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in change user request"); + return; + } + } + + bool found = false; + if (json_body["userid"] != nullptr) { + for (nlohmann::basic_json<>& all_user : all_users) { + if ((all_user["id"] == changeusruserid)) { + if (json_body["username"] != nullptr) { all_user["username"] = json_body["username"]; } + if (json_body["password"] != nullptr) { all_user["password"] = json_body["password"]; } + if (json_body["group"] != nullptr) { all_user["group"] = json_body["group"]; } + if (json_body["is_superuser"] != nullptr) { all_user["is_superuser"] = json_body["is_superuser"]; } + found = true; + std::string syslogstring = "userdata changed for user [" + std::string(all_user["username"]) + "]"; + syslog(LOG_INFO, syslogstring.c_str()); + break; + } + } + } + else { + for (nlohmann::basic_json<>& all_user : all_users) { + if ((all_user["id"] == changeusruserid)) { + if (json_body["username"] != nullptr) { all_user["username"] = json_body["username"]; } + found = true; + std::string syslogstring = "userdata changed for user [" + std::string(all_user["username"]) + "]"; + syslog(LOG_INFO, syslogstring.c_str()); + break; + } + } + } + if (!found) { + response.set_content(R"({"status":"can't find this user"})", JSON_TYPE); + syslog(LOG_ERR, "can't find this user in change user request"); + return; + } + std::lock_guard lock(AuthService::Includes::usersfilemutex); + std::ofstream usersfilew(USERS_FILE); + usersfilew << all_users.dump(4); + usersfilew.close(); + response.set_content(R"({"status":"ok"})", JSON_TYPE); } } - else { - for (nlohmann::basic_json<>& all_user : all_users) { - if ((all_user["id"] == changeusruserid)) { - if (json_body["username"] != nullptr) { all_user["username"] = json_body["username"]; } - found = true; - std::string syslogstring = "userdata changed for user [" + std::string(all_user["username"]) + "]"; - syslog(LOG_INFO, syslogstring.c_str()); - break; - } - } - } - if (!found) { - response.set_content(R"({"status":"can't find this user"})", JSON_TYPE); - syslog(LOG_ERR, "can't find this user in change user request"); - return; - } - std::lock_guard lock(usersfilemutex); - std::ofstream usersfilew(USERS_FILE); - usersfilew << all_users.dump(4); - usersfilew.close(); - response.set_content(R"({"status":"ok"})", JSON_TYPE); } diff --git a/src/users/delete_user.cpp b/src/users/delete_user.cpp index e1449ca..3497bc8 100644 --- a/src/users/delete_user.cpp +++ b/src/users/delete_user.cpp @@ -5,85 +5,91 @@ #include #include -// function for delete user -void delete_user(const httplib::Request& request, httplib::Response& response) +namespace AuthService { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content( - DELETE_USER_REQUIRED_STRING, - JSON_TYPE); - syslog(LOG_ERR, "delete user request body is empty"); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["token"] == nullptr) { - response.set_content( - DELETE_USER_REQUIRED_STRING, - JSON_TYPE); - syslog(LOG_ERR, "no token in delete user request"); - return; - } - if (!verify_auth(json_body["token"])) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in delete user request"); - return; - } - - 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"); - return; - } - nlohmann::json all_users; + namespace Users { - std::lock_guard lock(usersfilemutex); - std::ifstream usersfile(USERS_FILE); - all_users = nlohmann::json::parse(usersfile); - usersfile.close(); - } - std::string deluserid = userid; - if (json_body["userid"] != nullptr) { - deluserid = json_body["userid"]; - nlohmann::json response_user_data = nullptr; - for (nlohmann::json& user : all_users) { - if (user["id"] == userid) { - response_user_data = user; + // function for delete user + void delete_user(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content( + DELETE_USER_REQUIRED_STRING, + JSON_TYPE); + syslog(LOG_ERR, "delete user request body is empty"); + return; } - } - if (response_user_data == nullptr) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in delete user request"); - return; - } - if (response_user_data["is_superuser"] != "1") { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in delete user request"); - return; - } - } + nlohmann::json json_body = nlohmann::json::parse(request.body); - bool found = false; - for (size_t i = 0; i < all_users.size(); i++) { - if (all_users[i]["id"] == deluserid) { - std::string syslogstring = "user deleted [" + std::string(all_users[i]["username"]) + "]"; - all_users.erase(i); - found = true; - syslog(LOG_INFO, syslogstring.c_str()); - break; + if (json_body["token"] == nullptr) { + response.set_content( + DELETE_USER_REQUIRED_STRING, + JSON_TYPE); + syslog(LOG_ERR, "no token in delete user request"); + return; + } + if (!AuthService::Auth::verify_auth(json_body["token"])) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in delete user request"); + return; + } + + 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"); + return; + } + nlohmann::json all_users; + { + std::lock_guard lock(AuthService::Includes::usersfilemutex); + std::ifstream usersfile(USERS_FILE); + all_users = nlohmann::json::parse(usersfile); + usersfile.close(); + } + std::string deluserid = userid; + if (json_body["userid"] != nullptr) { + deluserid = json_body["userid"]; + nlohmann::json response_user_data = nullptr; + for (nlohmann::json& user : all_users) { + if (user["id"] == userid) { + response_user_data = user; + } + } + if (response_user_data == nullptr) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in delete user request"); + return; + } + if (response_user_data["is_superuser"] != "1") { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in delete user request"); + return; + } + } + + bool found = false; + for (size_t i = 0; i < all_users.size(); i++) { + if (all_users[i]["id"] == deluserid) { + std::string syslogstring = "user deleted [" + std::string(all_users[i]["username"]) + "]"; + all_users.erase(i); + found = true; + syslog(LOG_INFO, syslogstring.c_str()); + break; + } + } + if (!found) { + response.set_content(R"({"status":"can't find this user"})", JSON_TYPE); + syslog(LOG_ERR, "can't find this user in delete user request"); + return; + } + std::lock_guard lock(AuthService::Includes::usersfilemutex); + std::ofstream usersfilew(USERS_FILE); + usersfilew << all_users.dump(4); + usersfilew.close(); + response.set_content(R"({"status":"ok"})", JSON_TYPE); } } - if (!found) { - response.set_content(R"({"status":"can't find this user"})", JSON_TYPE); - syslog(LOG_ERR, "can't find this user in delete user request"); - return; - } - std::lock_guard lock(usersfilemutex); - std::ofstream usersfilew(USERS_FILE); - usersfilew << all_users.dump(4); - usersfilew.close(); - response.set_content(R"({"status":"ok"})", JSON_TYPE); } diff --git a/src/users/get_all_users.cpp b/src/users/get_all_users.cpp index 63805cb..8195b53 100644 --- a/src/users/get_all_users.cpp +++ b/src/users/get_all_users.cpp @@ -5,54 +5,60 @@ #include #include -// function for get all users data without password -void get_all_users(const httplib::Request& request, httplib::Response& response) +namespace AuthService { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - syslog(LOG_ERR, "get all users request body is empty"); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); + namespace Users + { + // function for get all users data without password + void get_all_users(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + syslog(LOG_ERR, "get all users request body is empty"); + return; + } + nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - syslog(LOG_ERR, "no token in get all users"); - return; - } - if (!verify_auth(json_body["token"])) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in get all user request"); - return; - } - 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"); - return; - } - std::lock_guard lock(usersfilemutex); - std::ifstream usersfile(USERS_FILE); - nlohmann::json all_users = nlohmann::json::parse(usersfile); - usersfile.close(); - nlohmann::json response_user_data = nullptr; - for (nlohmann::json& user : all_users) { - if (user["id"] == userid) { - response_user_data = user; + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + syslog(LOG_ERR, "no token in get all users"); + return; + } + if (!AuthService::Auth::verify_auth(json_body["token"])) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in get all user request"); + return; + } + 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"); + return; + } + std::lock_guard lock(AuthService::Includes::usersfilemutex); + std::ifstream usersfile(USERS_FILE); + nlohmann::json all_users = nlohmann::json::parse(usersfile); + usersfile.close(); + nlohmann::json response_user_data = nullptr; + for (nlohmann::json& user : all_users) { + if (user["id"] == userid) { + response_user_data = user; + } + user.erase("password"); + } + if (response_user_data == nullptr) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in get all user request"); + return; + } + if (response_user_data["is_superuser"] != "1") { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in get all user request"); + return; + } + response.set_content(all_users.dump(), JSON_TYPE); } - user.erase("password"); } - if (response_user_data == nullptr) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in get all user request"); - return; - } - if (response_user_data["is_superuser"] != "1") { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in get all user request"); - return; - } - response.set_content(all_users.dump(), JSON_TYPE); } diff --git a/src/users/get_user.cpp b/src/users/get_user.cpp index 5ac932a..eb8939c 100644 --- a/src/users/get_user.cpp +++ b/src/users/get_user.cpp @@ -5,50 +5,56 @@ #include #include -// function for get user data without password -void get_user(const httplib::Request& request, httplib::Response& response) +namespace AuthService { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - syslog(LOG_ERR, "get user request body is empty"); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); + namespace Users + { + // function for get user data without password + void get_user(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + syslog(LOG_ERR, "get user request body is empty"); + return; + } + nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - syslog(LOG_ERR, "no token in get user request"); - return; - } - if (!verify_auth(json_body["token"])) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in get user request"); - return; - } - 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"); - return; - } - std::lock_guard lock(usersfilemutex); - std::ifstream usersfile(USERS_FILE); - nlohmann::json all_users = nlohmann::json::parse(usersfile); - usersfile.close(); - nlohmann::json response_user_data = nullptr; - for (nlohmann::json& user : all_users) { - if (user["id"] == userid) { - response_user_data = user; - break; + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + syslog(LOG_ERR, "no token in get user request"); + return; + } + if (!AuthService::Auth::verify_auth(json_body["token"])) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in get user request"); + return; + } + 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"); + return; + } + std::lock_guard lock(AuthService::Includes::usersfilemutex); + std::ifstream usersfile(USERS_FILE); + nlohmann::json all_users = nlohmann::json::parse(usersfile); + usersfile.close(); + nlohmann::json response_user_data = nullptr; + for (nlohmann::json& user : all_users) { + if (user["id"] == userid) { + response_user_data = user; + break; + } + } + if (response_user_data == nullptr) { + response.set_content(STRING403, JSON_TYPE); + syslog(LOG_ERR, "access reject in get user request"); + return; + } + response_user_data.erase("password"); + response.set_content(response_user_data.dump(), JSON_TYPE); } } - if (response_user_data == nullptr) { - response.set_content(STRING403, JSON_TYPE); - syslog(LOG_ERR, "access reject in get user request"); - return; - } - response_user_data.erase("password"); - response.set_content(response_user_data.dump(), JSON_TYPE); } diff --git a/src/users/users.hpp b/src/users/users.hpp index 5822045..2dbbe79 100644 --- a/src/users/users.hpp +++ b/src/users/users.hpp @@ -3,25 +3,30 @@ #include -// function for get user data without password -void get_user(const httplib::Request& request, httplib::Response& response); +namespace AuthService +{ + namespace Users + { + // function for get user data without password + void get_user(const httplib::Request& request, httplib::Response& response); -// function for get all users data without password -void get_all_users(const httplib::Request& request, httplib::Response& response); + // function for get all users data without password + void get_all_users(const httplib::Request& request, httplib::Response& response); -// function for access or reject authorization -void user_access(const httplib::Request& request, httplib::Response& response); + // function for access or reject authorization + void user_access(const httplib::Request& request, httplib::Response& response); -// function for add user -void add_user(const httplib::Request& request, httplib::Response& response); + // function for add user + void add_user(const httplib::Request& request, httplib::Response& response); -// function for delete user -void delete_user(const httplib::Request& request, httplib::Response& response); + // function for delete user + void delete_user(const httplib::Request& request, httplib::Response& response); -// function for change password for user -void change_password(const httplib::Request& request, httplib::Response& response); - -// function for change user data -void change_user(const httplib::Request& request, httplib::Response& response); + // function for change password for user + void change_password(const httplib::Request& request, httplib::Response& response); + // function for change user data + void change_user(const httplib::Request& request, httplib::Response& response); + } +} #endif // USERS_HPP