added editorconfig and formated code
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

This commit is contained in:
wt committed 2025-02-13 19:35:42 +07:00
1 parent 77a483a38a
commit e5abe4bb02
23 files changed
+22299 -21445

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
root = true
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4
tab_width = 8
trim_trailing_whitespace = true
insert_final_newline = true
[DEVELOPERS]
indent_style = tab
indent_size = tab
[Config*.in*]
indent_style = tab
indent_size = tab
[linux/Config.ext.in]
indent_style = tab
indent_size = tab
[Makefile*]
indent_style = tab
indent_size = tab
[*.mk]
indent_style = tab
indent_size = tab
[*.patch]
trim_trailing_whitespace = false
[S{0..9}{0..9}*]
indent_style = tab
indent_size = tab
[*.adoc]
trim_trailing_whitespace = false
[*.{c++,cc,cpp,cxx,h,h++,hh,hpp,hxx,inl,ipp,tlh,tli}]
cpp_indent_case_contents_when_block = true
cpp_new_line_before_open_brace_namespace = new_line
cpp_new_line_before_open_brace_function = new_line
+49 -49
View File
@@ -3,54 +3,54 @@ run-name: ${{ gitea.actor }}
on: [push] on: [push]
jobs: jobs:
Prepare-Build-Testing-With-Make: Prepare-Build-Testing-With-Make:
runs-on: alpine-sdk runs-on: alpine-sdk
steps: steps:
- name: prepare-container - name: prepare-container
run: | run: |
echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \ echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \
apk add openssl openssl-dev apk add openssl openssl-dev
- name: Check out repository code - name: Check out repository code
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Building binary with make - name: Building binary with make
run: | run: |
echo "#define TEST" >> src/AuthService/AuthService.hpp && \ echo "#define TEST" >> src/AuthService/AuthService.hpp && \
make -j$(nproc) make -j$(nproc)
- name: Testing to run binary builded with make - name: Testing to run binary builded with make
run: ./bin/auth_service run: ./bin/auth_service
Prepare-Build-Testing-With-CMake: Prepare-Build-Testing-With-CMake:
runs-on: alpine-sdk runs-on: alpine-sdk
steps: steps:
- name: prepare-container - name: prepare-container
run: | run: |
echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \ echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \
apk add openssl openssl-dev cmake apk add openssl openssl-dev cmake
- name: Check out repository code - name: Check out repository code
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Building binary with CMake - name: Building binary with CMake
run: | run: |
echo "#define TEST" >> src/AuthService/AuthService.hpp && \ echo "#define TEST" >> src/AuthService/AuthService.hpp && \
mkdir build && \ mkdir build && \
cd build && \ cd build && \
cmake .. && \ cmake .. && \
make -j$(nproc) make -j$(nproc)
- name: Testing to run binary builded with CMake - name: Testing to run binary builded with CMake
run: ./build/auth_service run: ./build/auth_service
Prepare-Build-Testing-With-Meson: Prepare-Build-Testing-With-Meson:
runs-on: alpine-sdk runs-on: alpine-sdk
steps: steps:
- name: prepare-container - name: prepare-container
run: | run: |
echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \ echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \
apk add openssl openssl-dev meson ninja cmake apk add openssl openssl-dev meson ninja cmake
- name: Check out repository code - name: Check out repository code
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Building binary with meson - name: Building binary with meson
run: | run: |
echo "#define TEST" >> src/AuthService/AuthService.hpp && \ echo "#define TEST" >> src/AuthService/AuthService.hpp && \
meson setup build && \ meson setup build && \
ninja -C build ninja -C build
- name: Testing to run binary builded with meson - name: Testing to run binary builded with meson
run: ./build/auth_service run: ./build/auth_service
+16 -5
View File
@@ -1,16 +1,27 @@
CXX=g++ CXX=g++
CXXFLAGS=-Wall -l ssl -l crypto -I src/libs -I src/auth -I src/security -I src/users -I src CXXFLAGS=-Wall -l ssl -l crypto -I src/libs -I src/auth -I src/security -I src/users -I src
SOURCES=src/auth_service.cpp src/auth/auth.cpp src/auth/verify_auth.cpp src/security/cors.cpp src/users/access_request.cpp src/users/add_user.cpp src/users/change_password.cpp src/users/change_user.cpp src/users/delete_user.cpp src/users/get_all_users.cpp src/users/get_user.cpp src/AuthService/AuthService.cpp SOURCES=src/auth_service.cpp \
TARGET=bin/auth_service src/auth/auth.cpp \
src/auth/verify_auth.cpp \
src/security/cors.cpp \
src/users/access_request.cpp \
src/users/add_user.cpp \
src/users/change_password.cpp \
src/users/change_user.cpp \
src/users/delete_user.cpp \
src/users/get_all_users.cpp \
src/users/get_user.cpp \
src/AuthService/AuthService.cpp
TARGET=build/auth_service
default: all default: all
all: build all: build
.PHONY: build
build: build:
@if [ ! -d bin ]; then \ @if [ ! -d build ]; then \
mkdir bin; \ mkdir build; \
fi fi
$(CXX) -o $(TARGET) $(SOURCES) $(CXXFLAGS) $(CXX) -o $(TARGET) $(SOURCES) $(CXXFLAGS)
clean: clean:
@if [ -f $(TARGET) ]; then \ @if [ -f $(TARGET) ]; then \
rm -v $(TARGET); \ rm -v $(TARGET); \
fi fi
+2 -2
View File
@@ -15,6 +15,6 @@ executable(
'src/AuthService/AuthService.cpp', 'src/AuthService/AuthService.cpp',
'src/auth/auth.cpp', 'src/auth/auth.cpp',
'src/auth/verify_auth.cpp', 'src/auth/verify_auth.cpp',
include_directories : include_dirs, include_directories: include_dirs,
dependencies : deps dependencies: deps,
) )
+2 -2
View File
@@ -24,10 +24,10 @@ AuthService::AuthService()
AuthService::~AuthService() = default; AuthService::~AuthService() = default;
int AuthService::serve(const std::string &ip, int port, int socket_flags) int AuthService::serve(const std::string& ip, int port, int socket_flags)
{ {
std::cout << GREEN << "Starting" << RESET << " server on [ " << YELLOW std::cout << GREEN << "Starting" << RESET << " server on [ " << YELLOW
<< ip << RESET << ":" << CYAN << port << RESET << " ]" << std::endl; << ip << RESET << ":" << CYAN << port << RESET << " ]" << std::endl;
this->srv.listen(ip, port, socket_flags); this->srv.listen(ip, port, socket_flags);
return 0; return 0;
} }
+1 -1
View File
@@ -12,5 +12,5 @@ private:
public: public:
AuthService(); AuthService();
~AuthService(); ~AuthService();
int serve(const std::string &ip, int port, int socket_flags = 0); int serve(const std::string& ip, int port, int socket_flags = 0);
}; };
+4 -3
View File
@@ -7,7 +7,8 @@
#include <security/HashPassword.hpp> #include <security/HashPassword.hpp>
// function for authorization users // function for authorization users
void auth(const httplib::Request &request, httplib::Response &response) { void auth(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) { if (request.body.empty()) {
@@ -35,7 +36,7 @@ void auth(const httplib::Request &request, httplib::Response &response) {
usersfile.close(); usersfile.close();
std::string userid; std::string userid;
for (nlohmann::basic_json<> user: all_users) { for (nlohmann::basic_json<> user : all_users) {
if ((user["username"] == request_username) && (user["password"] == request_password)) { if ((user["username"] == request_username) && (user["password"] == request_password)) {
userid = user["id"]; userid = user["id"];
break; break;
@@ -52,7 +53,7 @@ void auth(const httplib::Request &request, httplib::Response &response) {
{"typ", "JWT"} {"typ", "JWT"}
}; };
nlohmann::json payload { nlohmann::json payload{
{"userId", userid} {"userId", userid}
}; };
+2 -2
View File
@@ -3,7 +3,7 @@
#include <httplib.h> #include <httplib.h>
// function for authorization users // function for authorization users
void auth(const httplib::Request &request, httplib::Response &response); void auth(const httplib::Request& request, httplib::Response& response);
// function for verify tokens // function for verify tokens
bool verify_auth(const std::string &token); bool verify_auth(const std::string& token);
+4 -2
View File
@@ -5,10 +5,12 @@
#include <syslog.h> #include <syslog.h>
// function for verify tokens // function for verify tokens
bool verify_auth(const std::string &token) { bool verify_auth(const std::string& token)
{
try { try {
return JWT::verifyJWT(token, JWT_SECRET_KEY); return JWT::verifyJWT(token, JWT_SECRET_KEY);
} catch (const std::system_error &e) { }
catch (const std::system_error& e) {
std::cout << "Verification error: " << e.what() << std::endl; std::cout << "Verification error: " << e.what() << std::endl;
syslog(LOG_ERR, "token verification error"); syslog(LOG_ERR, "token verification error");
return false; return false;
+3 -2
View File
@@ -1,10 +1,11 @@
#include <AuthService/AuthService.hpp> #include <AuthService/AuthService.hpp>
int main() { int main()
{
AuthService auth_service; AuthService auth_service;
#ifdef TEST #ifdef TEST
return 0; return 0;
#else #else
return auth_service.serve("127.0.0.1", 43243); return auth_service.serve("0.0.0.0", 43243);
#endif #endif
} }
+9559 -8846
View File
File diff suppressed because it is too large. Load diff
+12529 -12469
View File
File diff suppressed because it is too large. Load diff
+28 -20
View File
@@ -14,18 +14,20 @@
#define JWT_DEFAULT_SECRET "secret" #define JWT_DEFAULT_SECRET "secret"
namespace JWT { namespace JWT
inline std::string base64_encode(const unsigned char *input, int length); {
inline std::string base64_decode(const std::string &in); inline std::string base64_encode(const unsigned char* input, int length);
inline std::string createJWT(const std::string &header_json, const std::string &payload_json, const std::string &secret); inline std::string base64_decode(const std::string& in);
inline std::string hmacSha256(const std::string &key, const std::string &data); inline std::string createJWT(const std::string& header_json, const std::string& payload_json, const std::string& secret);
inline bool verifyJWT(const std::string &token, const std::string &secret); inline std::string hmacSha256(const std::string& key, const std::string& data);
inline bool verifyJWT(const std::string& token, const std::string& secret);
inline nlohmann::json get_payload(std::string token, std::string secret); inline nlohmann::json get_payload(std::string token, std::string secret);
inline nlohmann::json get_header(std::string token, std::string secret); inline nlohmann::json get_header(std::string token, std::string secret);
// Function to Base64 Encode // Function to Base64 Encode
inline std::string base64_encode(const unsigned char *input, int length) { inline std::string base64_encode(const unsigned char* input, int length)
BIO *bio, *b64; {
BUF_MEM *bufferPtr; BIO* bio, * b64;
BUF_MEM* bufferPtr;
b64 = BIO_new(BIO_f_base64()); b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem()); bio = BIO_new(BIO_s_mem());
@@ -40,9 +42,10 @@ namespace JWT {
return result; return result;
} }
inline std::string base64_decode(const std::string &in) { inline std::string base64_decode(const std::string& in)
BIO *bio, *b64; {
BUF_MEM *bufferPtr; BIO* bio, * b64;
BUF_MEM* bufferPtr;
b64 = BIO_new(BIO_f_base64()); b64 = BIO_new(BIO_f_base64());
bio = BIO_new_mem_buf(in.data(), in.size()); bio = BIO_new_mem_buf(in.data(), in.size());
@@ -63,7 +66,8 @@ namespace JWT {
} }
// Function to encode JWT // Function to encode JWT
inline std::string createJWT(const std::string &header_json, const std::string &payload_json, const std::string &secret) { inline std::string createJWT(const std::string& header_json, const std::string& payload_json, const std::string& secret)
{
// Base64 URL Encode the header // Base64 URL Encode the header
auto encodedHeader = base64_encode(reinterpret_cast<const unsigned char*>(header_json.c_str()), header_json.length()); auto encodedHeader = base64_encode(reinterpret_cast<const unsigned char*>(header_json.c_str()), header_json.length());
// Base64 URL Encode the payload // Base64 URL Encode the payload
@@ -73,12 +77,12 @@ namespace JWT {
std::string unsignedToken = encodedHeader + "." + encodedPayload; std::string unsignedToken = encodedHeader + "." + encodedPayload;
// Sign the token // Sign the token
unsigned char *digest; unsigned char* digest;
unsigned int digestLength; unsigned int digestLength;
digest = HMAC(EVP_sha256(), secret.c_str(), secret.length(), digest = HMAC(EVP_sha256(), secret.c_str(), secret.length(),
reinterpret_cast<const unsigned char *>(unsignedToken.c_str()), unsignedToken.length(), reinterpret_cast<const unsigned char*>(unsignedToken.c_str()), unsignedToken.length(),
nullptr, &digestLength); nullptr, &digestLength);
// Base64 URL Encode the signature // Base64 URL Encode the signature
std::string encodedSignature = base64_encode(digest, digestLength); std::string encodedSignature = base64_encode(digest, digestLength);
@@ -89,7 +93,8 @@ namespace JWT {
return jwt; return jwt;
} }
inline std::string hmacSha256(const std::string &key, const std::string &data) { inline std::string hmacSha256(const std::string& key, const std::string& data)
{
unsigned char hash[EVP_MAX_MD_SIZE]; unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int length = 0; unsigned int length = 0;
@@ -100,7 +105,8 @@ namespace JWT {
return std::string(reinterpret_cast<char*>(hash), length); return std::string(reinterpret_cast<char*>(hash), length);
} }
inline bool verifyJWT(const std::string &token, const std::string &secret) { inline bool verifyJWT(const std::string& token, const std::string& secret)
{
// Split the token into its three parts // Split the token into its three parts
size_t pos1 = token.find('.'); size_t pos1 = token.find('.');
size_t pos2 = token.find('.', pos1 + 1); size_t pos2 = token.find('.', pos1 + 1);
@@ -151,7 +157,8 @@ namespace JWT {
return true; return true;
} }
inline nlohmann::json get_payload(std::string token, std::string secret) { inline nlohmann::json get_payload(std::string token, std::string secret)
{
size_t pos1 = token.find('.'); size_t pos1 = token.find('.');
size_t pos2 = token.find('.', pos1 + 1); size_t pos2 = token.find('.', pos1 + 1);
@@ -167,7 +174,8 @@ namespace JWT {
return jsonPayload; return jsonPayload;
} }
inline nlohmann::json get_header(std::string token, std::string secret) { inline nlohmann::json get_header(std::string token, std::string secret)
{
size_t pos1 = token.find('.'); size_t pos1 = token.find('.');
size_t pos2 = token.find('.', pos1 + 1); size_t pos2 = token.find('.', pos1 + 1);
+2 -1
View File
@@ -7,7 +7,8 @@
#include <sstream> #include <sstream>
// funcion for hashing password using sha256 // funcion for hashing password using sha256
std::string hashPassword(const std::string &password) { std::string hashPassword(const std::string& password)
{
unsigned char hash[SHA256_DIGEST_LENGTH]; unsigned char hash[SHA256_DIGEST_LENGTH];
// calculating hash // calculating hash
+2 -1
View File
@@ -1,7 +1,8 @@
#include <security/cors.hpp> #include <security/cors.hpp>
#include <colors.h> #include <colors.h>
void set_cors_headers(const httplib::Request &request, httplib::Response &response) { void set_cors_headers(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << " " << std::endl; std::cout << GREEN << request.path << " " << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
response.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS"); response.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
+2 -1
View File
@@ -6,7 +6,8 @@
#include <json.hpp> #include <json.hpp>
// function for access or reject authorization // function for access or reject authorization
void user_access(const httplib::Request &request, httplib::Response &response) { void user_access(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) { if (request.body.empty()) {
+12 -10
View File
@@ -8,7 +8,8 @@
#define ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" #define ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// function for generate random string // function for generate random string
std::string get_random_string(const char *alphabet, int quantity) { std::string get_random_string(const char* alphabet, int quantity)
{
srand(time(nullptr)); srand(time(nullptr));
std::string str; std::string str;
for (int i = 0; i < quantity; i++) { for (int i = 0; i < quantity; i++) {
@@ -18,12 +19,13 @@ std::string get_random_string(const char *alphabet, int quantity) {
} }
// function for add user // 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; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) { if (request.body.empty()) {
response.set_content(ADD_USER_REQUIRED_STRING, response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE); JSON_TYPE);
syslog(LOG_ERR, "add user request body is empty"); syslog(LOG_ERR, "add user request body is empty");
return; return;
} }
@@ -31,7 +33,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
if (json_body["token"] == nullptr) { if (json_body["token"] == nullptr) {
response.set_content(ADD_USER_REQUIRED_STRING, response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE); JSON_TYPE);
syslog(LOG_ERR, "no token in add user request"); syslog(LOG_ERR, "no token in add user request");
return; return;
} }
@@ -42,13 +44,13 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
} }
if (json_body["username"] == nullptr) { if (json_body["username"] == nullptr) {
response.set_content(ADD_USER_REQUIRED_STRING, response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE); JSON_TYPE);
syslog(LOG_ERR, "no username in add user request"); syslog(LOG_ERR, "no username in add user request");
return; return;
} }
if (json_body["password"] == nullptr) { if (json_body["password"] == nullptr) {
response.set_content(ADD_USER_REQUIRED_STRING, response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE); JSON_TYPE);
syslog(LOG_ERR, "no password in add user request"); syslog(LOG_ERR, "no password in add user request");
return; return;
} }
@@ -65,7 +67,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
usersfile.close(); usersfile.close();
nlohmann::json response_user_data = nullptr; nlohmann::json response_user_data = nullptr;
for (nlohmann::json &user: all_users) { for (nlohmann::json& user : all_users) {
if (user["id"] == userid) { if (user["id"] == userid) {
response_user_data = user; response_user_data = user;
} }
@@ -84,7 +86,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
const std::string username = json_body["username"]; const std::string username = json_body["username"];
const std::string password = json_body["password"]; const std::string password = json_body["password"];
for (nlohmann::json user: all_users) { for (nlohmann::json user : all_users) {
if ((user["username"] == username)) { if ((user["username"] == username)) {
response.set_content(R"({"status":"user already exists"})", JSON_TYPE); response.set_content(R"({"status":"user already exists"})", JSON_TYPE);
syslog(LOG_ERR, "user already exists in add user request"); syslog(LOG_ERR, "user already exists in add user request");
@@ -95,7 +97,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
std::stringstream new_user_data; std::stringstream new_user_data;
new_user_data << R"({"id":")" << get_random_string(ALPHABET, 50) << R"(","username":")" new_user_data << R"({"id":")" << get_random_string(ALPHABET, 50) << R"(","username":")"
<< username << R"(","password":")" << password << R"(","group":)"; << username << R"(","password":")" << password << R"(","group":)";
(json_body["group"] != nullptr) ? new_user_data << json_body["group"] : new_user_data << "\"users\""; (json_body["group"] != nullptr) ? new_user_data << json_body["group"] : new_user_data << "\"users\"";
new_user_data << ",\"is_superuser\":"; 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\"";
@@ -106,7 +108,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
usersfilew << all_users.dump(4); usersfilew << all_users.dump(4);
usersfilew.close(); usersfilew.close();
std::stringstream syslogstring; std::stringstream syslogstring;
syslogstring << "user added [" << username << "]"; syslogstring << "user added [" << username << "]";
syslog(LOG_INFO, syslogstring.str().c_str()); syslog(LOG_INFO, syslogstring.str().c_str());
response.set_content(R"({"status":"ok"})", JSON_TYPE); response.set_content(R"({"status":"ok"})", JSON_TYPE);
} }
+10 -8
View File
@@ -6,7 +6,8 @@
#include <syslog.h> #include <syslog.h>
// functtion for change password for user // functtion for change password for user
void change_password(const httplib::Request &request, httplib::Response &response) { void change_password(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) { if (request.body.empty()) {
@@ -36,7 +37,7 @@ void change_password(const httplib::Request &request, httplib::Response &respons
} }
if (json_body["new_password"] == nullptr) { if (json_body["new_password"] == nullptr) {
response.set_content(CHANGE_PASSWORD_REQUIRED_STRING, response.set_content(CHANGE_PASSWORD_REQUIRED_STRING,
JSON_TYPE); JSON_TYPE);
syslog(LOG_ERR, "no new password in change password request"); syslog(LOG_ERR, "no new password in change password request");
return; return;
} }
@@ -66,7 +67,7 @@ void change_password(const httplib::Request &request, httplib::Response &respons
if (json_body["userid"] != nullptr) { if (json_body["userid"] != nullptr) {
changepassuserid = json_body["userid"]; changepassuserid = json_body["userid"];
nlohmann::json response_user_data = nullptr; nlohmann::json response_user_data = nullptr;
for (nlohmann::json &user: all_users) { for (nlohmann::json& user : all_users) {
if (user["id"] == userid) { if (user["id"] == userid) {
response_user_data = user; response_user_data = user;
} }
@@ -85,23 +86,24 @@ void change_password(const httplib::Request &request, httplib::Response &respons
bool found = false; bool found = false;
if (json_body["userid"] != nullptr) { if (json_body["userid"] != nullptr) {
for (nlohmann::basic_json<> &all_user: all_users) { for (nlohmann::basic_json<>& all_user : all_users) {
if ((all_user["id"] == changepassuserid)) { if ((all_user["id"] == changepassuserid)) {
all_user["password"] = new_password; all_user["password"] = new_password;
found = true; found = true;
std::stringstream syslogstring; std::stringstream syslogstring;
syslogstring << "password changed for user [" << all_user["username"] << "]"; syslogstring << "password changed for user [" << all_user["username"] << "]";
syslog(LOG_INFO, syslogstring.str().c_str()); syslog(LOG_INFO, syslogstring.str().c_str());
break; break;
} }
} }
} else { }
for (nlohmann::basic_json<> &all_user: all_users) { else {
for (nlohmann::basic_json<>& all_user : all_users) {
if ((all_user["id"] == changepassuserid) && (all_user["password"] == old_password)) { if ((all_user["id"] == changepassuserid) && (all_user["password"] == old_password)) {
all_user["password"] = new_password; all_user["password"] = new_password;
found = true; found = true;
std::stringstream syslogstring; std::stringstream syslogstring;
syslogstring << "password changed for user [" << all_user["username"] << "]"; syslogstring << "password changed for user [" << all_user["username"] << "]";
syslog(LOG_INFO, syslogstring.str().c_str()); syslog(LOG_INFO, syslogstring.str().c_str());
break; break;
} }
+9 -7
View File
@@ -6,7 +6,8 @@
#include <syslog.h> #include <syslog.h>
// function for change user data // function for change user data
void change_user(const httplib::Request &request, httplib::Response &response) { void change_user(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) { if (request.body.empty()) {
@@ -54,7 +55,7 @@ void change_user(const httplib::Request &request, httplib::Response &response) {
if (json_body["userid"] != nullptr) { if (json_body["userid"] != nullptr) {
changeusruserid = json_body["userid"]; changeusruserid = json_body["userid"];
nlohmann::json response_user_data = nullptr; nlohmann::json response_user_data = nullptr;
for (nlohmann::json &user: all_users) { for (nlohmann::json& user : all_users) {
if (user["id"] == userid) { if (user["id"] == userid) {
response_user_data = user; response_user_data = user;
} }
@@ -73,7 +74,7 @@ void change_user(const httplib::Request &request, httplib::Response &response) {
bool found = false; bool found = false;
if (json_body["userid"] != nullptr) { if (json_body["userid"] != nullptr) {
for (nlohmann::basic_json<> &all_user: all_users) { for (nlohmann::basic_json<>& all_user : all_users) {
if ((all_user["id"] == changeusruserid)) { if ((all_user["id"] == changeusruserid)) {
if (json_body["username"] != nullptr) { all_user["username"] = json_body["username"]; } 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["password"] != nullptr) { all_user["password"] = json_body["password"]; }
@@ -81,18 +82,19 @@ void change_user(const httplib::Request &request, httplib::Response &response) {
if (json_body["is_superuser"] != nullptr) { all_user["is_superuser"] = json_body["is_superuser"]; } if (json_body["is_superuser"] != nullptr) { all_user["is_superuser"] = json_body["is_superuser"]; }
found = true; found = true;
std::stringstream syslogstring; std::stringstream syslogstring;
syslogstring << "userdata changed for user [" << all_user["username"] << "]"; syslogstring << "userdata changed for user [" << all_user["username"] << "]";
syslog(LOG_INFO, syslogstring.str().c_str()); syslog(LOG_INFO, syslogstring.str().c_str());
break; break;
} }
} }
} else { }
for (nlohmann::basic_json<> &all_user: all_users) { else {
for (nlohmann::basic_json<>& all_user : all_users) {
if ((all_user["id"] == changeusruserid)) { if ((all_user["id"] == changeusruserid)) {
if (json_body["username"] != nullptr) { all_user["username"] = json_body["username"]; } if (json_body["username"] != nullptr) { all_user["username"] = json_body["username"]; }
found = true; found = true;
std::stringstream syslogstring; std::stringstream syslogstring;
syslogstring << "userdata changed for user [" << all_user["username"] << "]"; syslogstring << "userdata changed for user [" << all_user["username"] << "]";
syslog(LOG_INFO, syslogstring.str().c_str()); syslog(LOG_INFO, syslogstring.str().c_str());
break; break;
} }
+4 -3
View File
@@ -6,7 +6,8 @@
#include <syslog.h> #include <syslog.h>
// function for delete user // function for delete user
void delete_user(const httplib::Request &request, httplib::Response &response) { void delete_user(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) { if (request.body.empty()) {
@@ -46,7 +47,7 @@ void delete_user(const httplib::Request &request, httplib::Response &response) {
if (json_body["userid"] != nullptr) { if (json_body["userid"] != nullptr) {
deluserid = json_body["userid"]; deluserid = json_body["userid"];
nlohmann::json response_user_data = nullptr; nlohmann::json response_user_data = nullptr;
for (nlohmann::json &user: all_users) { for (nlohmann::json& user : all_users) {
if (user["id"] == userid) { if (user["id"] == userid) {
response_user_data = user; response_user_data = user;
} }
@@ -67,7 +68,7 @@ void delete_user(const httplib::Request &request, httplib::Response &response) {
for (size_t i = 0; i < all_users.size(); i++) { for (size_t i = 0; i < all_users.size(); i++) {
if (all_users[i]["id"] == deluserid) { if (all_users[i]["id"] == deluserid) {
std::stringstream syslogstring; std::stringstream syslogstring;
syslogstring << "user deleted [" << all_users[i]["username"] << "]"; syslogstring << "user deleted [" << all_users[i]["username"] << "]";
all_users.erase(i); all_users.erase(i);
found = true; found = true;
syslog(LOG_INFO, syslogstring.str().c_str()); syslog(LOG_INFO, syslogstring.str().c_str());
+3 -2
View File
@@ -6,7 +6,8 @@
#include <syslog.h> #include <syslog.h>
// function for get all users data without password // function for get all users data without password
void get_all_users(const httplib::Request &request, httplib::Response &response) { void get_all_users(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) { if (request.body.empty()) {
@@ -36,7 +37,7 @@ void get_all_users(const httplib::Request &request, httplib::Response &response)
nlohmann::json all_users = nlohmann::json::parse(usersfile); nlohmann::json all_users = nlohmann::json::parse(usersfile);
usersfile.close(); usersfile.close();
nlohmann::json response_user_data = nullptr; nlohmann::json response_user_data = nullptr;
for (nlohmann::json &user: all_users) { for (nlohmann::json& user : all_users) {
if (user["id"] == userid) { if (user["id"] == userid) {
response_user_data = user; response_user_data = user;
} }
+3 -2
View File
@@ -6,7 +6,8 @@
#include <syslog.h> #include <syslog.h>
// function for get user data without password // function for get user data without password
void get_user(const httplib::Request &request, httplib::Response &response) { void get_user(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) { if (request.body.empty()) {
@@ -36,7 +37,7 @@ void get_user(const httplib::Request &request, httplib::Response &response) {
nlohmann::json all_users = nlohmann::json::parse(usersfile); nlohmann::json all_users = nlohmann::json::parse(usersfile);
usersfile.close(); usersfile.close();
nlohmann::json response_user_data = nullptr; nlohmann::json response_user_data = nullptr;
for (nlohmann::json &user: all_users) { for (nlohmann::json& user : all_users) {
if (user["id"] == userid) { if (user["id"] == userid) {
response_user_data = user; response_user_data = user;
break; break;
+7 -7
View File
@@ -4,24 +4,24 @@
#include <httplib.h> #include <httplib.h>
// function for get user data without password // function for get user data without password
void get_user(const httplib::Request &request, httplib::Response &response); void get_user(const httplib::Request& request, httplib::Response& response);
// function for get all users data without password // function for get all users data without password
void get_all_users(const httplib::Request &request, httplib::Response &response); void get_all_users(const httplib::Request& request, httplib::Response& response);
// function for access or reject authorization // function for access or reject authorization
void user_access(const httplib::Request &request, httplib::Response &response); void user_access(const httplib::Request& request, httplib::Response& response);
// function for add user // function for add user
void add_user(const httplib::Request &request, httplib::Response &response); void add_user(const httplib::Request& request, httplib::Response& response);
// function for delete user // function for delete user
void delete_user(const httplib::Request &request, httplib::Response &response); void delete_user(const httplib::Request& request, httplib::Response& response);
// function for change password for user // function for change password for user
void change_password(const httplib::Request &request, httplib::Response &response); void change_password(const httplib::Request& request, httplib::Response& response);
// function for change user data // function for change user data
void change_user(const httplib::Request &request, httplib::Response &response); void change_user(const httplib::Request& request, httplib::Response& response);
#endif // USERS_HPP #endif // USERS_HPP