refactor and review

This commit is contained in:
wt committed 2024-06-05 22:46:05 +07:00
1 parent 721e616207
commit 29df9728e2
16 files changed
+351 -174

No files matched your search

+52 -19
View File
@@ -1,68 +1,101 @@
#include "users.hpp"
#include "../colors.h"
#include "../includes.hpp"
#include "../auth/auth.hpp"
#include <jwt-cpp/jwt.h>
#define ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// function for generate random string
std::string get_random_string(const char *alphabet, int quantity) {
srand(time(NULL));
srand(time(nullptr));
std::string str;
for (int i = 0; i < quantity; i++) {
str += alphabet[0 + rand() % strlen(alphabet)];
str += alphabet[0 + std::rand() % strlen(alphabet)];
}
return str;
}
// function for add user
void add_user(const httplib::Request& request, httplib::Response& response) {
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 == "") {response.set_content("{\"required\":\"[token,username,password]\",\"optional\":\"[group,is_superuser]\"}", "application/json");return;}
if (request.body.empty()) {
response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {response.set_content("{\"required\":\"[token,username,password]\",\"optional\":\"[group,is_superuser]\"}", "application/json");return;}
if (!verify_auth(json_body["token"])) {response.set_content("{\"status\":\"403\"}", "application/json");return;}
if (json_body["username"] == nullptr) {response.set_content("{\"required\":\"[token,username,password]\",\"optional\":\"[group,is_superuser]\"}", "application/json");return;}
if (json_body["password"] == nullptr) {response.set_content("{\"required\":\"[token,username,password]\",\"optional\":\"[group,is_superuser]\"}", "application/json");return;}
if (json_body["token"] == nullptr) {
response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE);
return;
}
if (!verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE);
return;
}
if (json_body["username"] == nullptr) {
response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE);
return;
}
if (json_body["password"] == nullptr) {
response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE);
return;
}
jwt::decoded_jwt<jwt::traits::kazuho_picojson> decoded_token = jwt::decode(json_body["token"]);
std::string userid = "";
for (auto& e : decoded_token.get_payload_json()) {
std::string userid;
for (std::pair<const std::string, picojson::value> &e: decoded_token.get_payload_json()) {
if (e.first == "userId") {
userid = e.second.to_str();
break;
}
}
if (userid == "") {response.set_content("{\"status\":\"403\"}", "application/json");return;}
if (userid.empty()) {
response.set_content(STRING403, JSON_TYPE);
return;
}
std::ifstream usersfile("users.json");
nlohmann::json all_users = nlohmann::json::parse(usersfile);
usersfile.close();
nlohmann::json response_user_data = nullptr;
for (nlohmann::json& user : all_users) {
for (nlohmann::json &user: all_users) {
if (user["id"] == userid) {
response_user_data = user;
}
}
if (response_user_data == nullptr) {response.set_content("{\"status\":\"403\"}", "application/json");return;}
if (response_user_data["is_superuser"] != "1") {response.set_content("{\"status\":\"403\"}", "application/json");return;}
if (response_user_data == nullptr) {
response.set_content(STRING403, JSON_TYPE);
return;
}
if (response_user_data["is_superuser"] != "1") {
response.set_content(STRING403, JSON_TYPE);
return;
}
const std::string username = json_body["username"];
const std::string password = json_body["password"];
for (nlohmann::json user : all_users) {
for (nlohmann::json user: all_users) {
if ((user["username"] == username)) {
response.set_content("{\"status\":\"user already exists\"}", "application/json");return;
response.set_content(R"({"status":"user already exists"})", JSON_TYPE);
return;
}
}
std::stringstream new_user_data;
new_user_data << "{\"id\":\"" << get_random_string(ALPHABET, 50) << "\",\"username\":\"" << username << "\",\"password\":\"" << password << "\",\"group\":";
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 << json_body["group"] : new_user_data << "\"users\"";
new_user_data << ",\"is_superuser\":";
(json_body["is_superuser"] != nullptr) ? new_user_data << json_body["is_superuser"] : new_user_data << "\"0\"";
(json_body["is_superuser"] != nullptr) ? new_user_data << json_body["is_superuser"] : new_user_data << "\"0\"";
new_user_data << "}";
all_users.push_back(nlohmann::json::parse(new_user_data));
@@ -70,5 +103,5 @@ void add_user(const httplib::Request& request, httplib::Response& response) {
usersfilew << all_users.dump(4);
usersfilew.close();
response.set_content("{\"status\":\"ok\"}", "application/json");
response.set_content(R"({"status":"ok"})", JSON_TYPE);
}