Files
auth_service/src/users/add_user.cpp
T
wt 40ccbd6ad2
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Has been cancelled
updated for classes and small fix web
2025-04-24 14:07:57 +07:00

95 lines
4.2 KiB
C++

#include <users/users.hpp>
#include <colors.h>
#include <includes.hpp>
#include <auth/auth.hpp>
#include <jwt.hpp>
#include <syslog.h>
#define ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
namespace AuthService
{
namespace Users
{
// 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;
}
// function for add user
void Users::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", "*");
AuthService::Includes::check_body_empty(request, response, ADD_USER_REQUIRED_STRING);
nlohmann::json json_body = nlohmann::json::parse(request.body);
AuthService::Includes::check_body_data(json_body, "token", response, ADD_USER_REQUIRED_STRING);
AuthService::Auth::Auth::verify_auth(json_body["token"], response);
AuthService::Includes::check_body_data(json_body, "username", response, ADD_USER_REQUIRED_STRING);
AuthService::Includes::check_body_data(json_body, "password", response, ADD_USER_REQUIRED_STRING);
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<std::mutex> 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<std::mutex> 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);
}
}
}