added editorconfig and formated code #3
No files matched your search
@@ -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
|
||||
@@ -3,54 +3,54 @@ run-name: ${{ gitea.actor }}
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
Prepare-Build-Testing-With-Make:
|
||||
runs-on: alpine-sdk
|
||||
steps:
|
||||
- name: prepare-container
|
||||
run: |
|
||||
echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \
|
||||
apk add openssl openssl-dev
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v3
|
||||
- name: Building binary with make
|
||||
run: |
|
||||
echo "#define TEST" >> src/AuthService/AuthService.hpp && \
|
||||
make -j$(nproc)
|
||||
- name: Testing to run binary builded with make
|
||||
run: ./bin/auth_service
|
||||
|
||||
Prepare-Build-Testing-With-CMake:
|
||||
runs-on: alpine-sdk
|
||||
steps:
|
||||
- name: prepare-container
|
||||
run: |
|
||||
echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \
|
||||
apk add openssl openssl-dev cmake
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v3
|
||||
- name: Building binary with CMake
|
||||
run: |
|
||||
echo "#define TEST" >> src/AuthService/AuthService.hpp && \
|
||||
mkdir build && \
|
||||
cd build && \
|
||||
cmake .. && \
|
||||
make -j$(nproc)
|
||||
- name: Testing to run binary builded with CMake
|
||||
run: ./build/auth_service
|
||||
|
||||
Prepare-Build-Testing-With-Meson:
|
||||
runs-on: alpine-sdk
|
||||
steps:
|
||||
- name: prepare-container
|
||||
run: |
|
||||
echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \
|
||||
apk add openssl openssl-dev meson ninja cmake
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v3
|
||||
- name: Building binary with meson
|
||||
run: |
|
||||
echo "#define TEST" >> src/AuthService/AuthService.hpp && \
|
||||
meson setup build && \
|
||||
ninja -C build
|
||||
- name: Testing to run binary builded with meson
|
||||
run: ./build/auth_service
|
||||
Prepare-Build-Testing-With-Make:
|
||||
runs-on: alpine-sdk
|
||||
steps:
|
||||
- name: prepare-container
|
||||
run: |
|
||||
echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \
|
||||
apk add openssl openssl-dev
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v3
|
||||
- name: Building binary with make
|
||||
run: |
|
||||
echo "#define TEST" >> src/AuthService/AuthService.hpp && \
|
||||
make -j$(nproc)
|
||||
- name: Testing to run binary builded with make
|
||||
run: ./bin/auth_service
|
||||
|
||||
Prepare-Build-Testing-With-CMake:
|
||||
runs-on: alpine-sdk
|
||||
steps:
|
||||
- name: prepare-container
|
||||
run: |
|
||||
echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \
|
||||
apk add openssl openssl-dev cmake
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v3
|
||||
- name: Building binary with CMake
|
||||
run: |
|
||||
echo "#define TEST" >> src/AuthService/AuthService.hpp && \
|
||||
mkdir build && \
|
||||
cd build && \
|
||||
cmake .. && \
|
||||
make -j$(nproc)
|
||||
- name: Testing to run binary builded with CMake
|
||||
run: ./build/auth_service
|
||||
|
||||
Prepare-Build-Testing-With-Meson:
|
||||
runs-on: alpine-sdk
|
||||
steps:
|
||||
- name: prepare-container
|
||||
run: |
|
||||
echo "83.172.39.220 gitea.home.com" >> /etc/hosts && \
|
||||
apk add openssl openssl-dev meson ninja cmake
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v3
|
||||
- name: Building binary with meson
|
||||
run: |
|
||||
echo "#define TEST" >> src/AuthService/AuthService.hpp && \
|
||||
meson setup build && \
|
||||
ninja -C build
|
||||
- name: Testing to run binary builded with meson
|
||||
run: ./build/auth_service
|
||||
@@ -1,16 +1,27 @@
|
||||
CXX=g++
|
||||
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
|
||||
TARGET=bin/auth_service
|
||||
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
|
||||
TARGET=build/auth_service
|
||||
default: all
|
||||
all: build
|
||||
.PHONY: build
|
||||
build:
|
||||
@if [ ! -d bin ]; then \
|
||||
mkdir bin; \
|
||||
@if [ ! -d build ]; then \
|
||||
mkdir build; \
|
||||
fi
|
||||
$(CXX) -o $(TARGET) $(SOURCES) $(CXXFLAGS)
|
||||
clean:
|
||||
@if [ -f $(TARGET) ]; then \
|
||||
rm -v $(TARGET); \
|
||||
fi
|
||||
|
||||
+15
-15
@@ -2,19 +2,19 @@ project('auth_service', 'cpp')
|
||||
include_dirs = include_directories('src', 'src/libs')
|
||||
deps = dependency('openssl')
|
||||
executable(
|
||||
'auth_service',
|
||||
'src/auth_service.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/security/cors.cpp',
|
||||
'src/AuthService/AuthService.cpp',
|
||||
'src/auth/auth.cpp',
|
||||
'src/auth/verify_auth.cpp',
|
||||
include_directories : include_dirs,
|
||||
dependencies : deps
|
||||
'auth_service',
|
||||
'src/auth_service.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/security/cors.cpp',
|
||||
'src/AuthService/AuthService.cpp',
|
||||
'src/auth/auth.cpp',
|
||||
'src/auth/verify_auth.cpp',
|
||||
include_directories: include_dirs,
|
||||
dependencies: deps,
|
||||
)
|
||||
@@ -24,10 +24,10 @@ AuthService::AuthService()
|
||||
|
||||
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
|
||||
<< ip << RESET << ":" << CYAN << port << RESET << " ]" << std::endl;
|
||||
<< ip << RESET << ":" << CYAN << port << RESET << " ]" << std::endl;
|
||||
this->srv.listen(ip, port, socket_flags);
|
||||
return 0;
|
||||
}
|
||||
@@ -5,12 +5,12 @@
|
||||
|
||||
#define CORS_ENABLE
|
||||
|
||||
class AuthService
|
||||
class AuthService
|
||||
{
|
||||
private:
|
||||
httplib::Server srv;
|
||||
public:
|
||||
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);
|
||||
};
|
||||
+5
-4
@@ -7,7 +7,8 @@
|
||||
#include <security/HashPassword.hpp>
|
||||
|
||||
// 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;
|
||||
response.set_header("Access-Control-Allow-Origin", "*");
|
||||
if (request.body.empty()) {
|
||||
@@ -35,7 +36,7 @@ void auth(const httplib::Request &request, httplib::Response &response) {
|
||||
usersfile.close();
|
||||
|
||||
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)) {
|
||||
userid = user["id"];
|
||||
break;
|
||||
@@ -46,13 +47,13 @@ void auth(const httplib::Request &request, httplib::Response &response) {
|
||||
syslog(LOG_ERR, "auth request rejected");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
nlohmann::json headers = {
|
||||
{"alg", "HS256"},
|
||||
{"typ", "JWT"}
|
||||
};
|
||||
|
||||
nlohmann::json payload {
|
||||
nlohmann::json payload{
|
||||
{"userId", userid}
|
||||
};
|
||||
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@
|
||||
#include <httplib.h>
|
||||
|
||||
// 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
|
||||
bool verify_auth(const std::string &token);
|
||||
bool verify_auth(const std::string& token);
|
||||
@@ -5,10 +5,12 @@
|
||||
#include <syslog.h>
|
||||
|
||||
// function for verify tokens
|
||||
bool verify_auth(const std::string &token) {
|
||||
bool verify_auth(const std::string& token)
|
||||
{
|
||||
try {
|
||||
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;
|
||||
syslog(LOG_ERR, "token verification error");
|
||||
return false;
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#include <AuthService/AuthService.hpp>
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
AuthService auth_service;
|
||||
#ifdef TEST
|
||||
return 0;
|
||||
#else
|
||||
return auth_service.serve("127.0.0.1", 43243);
|
||||
return auth_service.serve("0.0.0.0", 43243);
|
||||
#endif
|
||||
}
|
||||
+9559
-8846
File diff suppressed because it is too large.
Load diff
+12529
-12469
File diff suppressed because it is too large.
Load diff
+44
-36
@@ -14,19 +14,21 @@
|
||||
|
||||
#define JWT_DEFAULT_SECRET "secret"
|
||||
|
||||
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 createJWT(const std::string &header_json, const std::string &payload_json, 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);
|
||||
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 createJWT(const std::string& header_json, const std::string& payload_json, 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_header(std::string token, std::string secret);
|
||||
// Function to Base64 Encode
|
||||
inline std::string base64_encode(const unsigned char *input, int length) {
|
||||
BIO *bio, *b64;
|
||||
BUF_MEM *bufferPtr;
|
||||
|
||||
inline std::string base64_encode(const unsigned char* input, int length)
|
||||
{
|
||||
BIO* bio, * b64;
|
||||
BUF_MEM* bufferPtr;
|
||||
|
||||
b64 = BIO_new(BIO_f_base64());
|
||||
bio = BIO_new(BIO_s_mem());
|
||||
bio = BIO_push(b64, bio);
|
||||
@@ -34,16 +36,17 @@ namespace JWT {
|
||||
BIO_write(bio, input, length);
|
||||
BIO_flush(bio);
|
||||
BIO_get_mem_ptr(bio, &bufferPtr);
|
||||
|
||||
|
||||
std::string result(bufferPtr->data, bufferPtr->length);
|
||||
BIO_free_all(bio);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline std::string base64_decode(const std::string &in) {
|
||||
BIO *bio, *b64;
|
||||
BUF_MEM *bufferPtr;
|
||||
|
||||
inline std::string base64_decode(const std::string& in)
|
||||
{
|
||||
BIO* bio, * b64;
|
||||
BUF_MEM* bufferPtr;
|
||||
|
||||
b64 = BIO_new(BIO_f_base64());
|
||||
bio = BIO_new_mem_buf(in.data(), in.size());
|
||||
bio = BIO_push(b64, bio);
|
||||
@@ -51,45 +54,47 @@ namespace JWT {
|
||||
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
|
||||
BIO_get_mem_ptr(bio, &bufferPtr);
|
||||
BIO_set_close(bio, BIO_NOCLOSE);
|
||||
|
||||
|
||||
std::string out(bufferPtr->data, bufferPtr->length);
|
||||
BIO_reset(bio);
|
||||
|
||||
|
||||
std::vector<char> buffer(out.size());
|
||||
int decodedLength = BIO_read(bio, buffer.data(), buffer.size());
|
||||
BIO_free_all(bio);
|
||||
|
||||
|
||||
return std::string(buffer.data(), decodedLength);
|
||||
}
|
||||
|
||||
// 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
|
||||
auto encodedHeader = base64_encode(reinterpret_cast<const unsigned char*>(header_json.c_str()), header_json.length());
|
||||
// Base64 URL Encode the payload
|
||||
auto encodedPayload = base64_encode(reinterpret_cast<const unsigned char*>(payload_json.c_str()), payload_json.length());
|
||||
|
||||
|
||||
// Prepare the signing input
|
||||
std::string unsignedToken = encodedHeader + "." + encodedPayload;
|
||||
|
||||
|
||||
// Sign the token
|
||||
unsigned char *digest;
|
||||
unsigned char* digest;
|
||||
unsigned int digestLength;
|
||||
|
||||
digest = HMAC(EVP_sha256(), secret.c_str(), secret.length(),
|
||||
reinterpret_cast<const unsigned char *>(unsignedToken.c_str()), unsignedToken.length(),
|
||||
nullptr, &digestLength);
|
||||
|
||||
|
||||
digest = HMAC(EVP_sha256(), secret.c_str(), secret.length(),
|
||||
reinterpret_cast<const unsigned char*>(unsignedToken.c_str()), unsignedToken.length(),
|
||||
nullptr, &digestLength);
|
||||
|
||||
// Base64 URL Encode the signature
|
||||
std::string encodedSignature = base64_encode(digest, digestLength);
|
||||
|
||||
|
||||
// Form the final JWT token
|
||||
std::string jwt = unsignedToken + "." + encodedSignature;
|
||||
|
||||
|
||||
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 int length = 0;
|
||||
|
||||
@@ -100,11 +105,12 @@ namespace JWT {
|
||||
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
|
||||
size_t pos1 = token.find('.');
|
||||
size_t pos2 = token.find('.', pos1 + 1);
|
||||
|
||||
|
||||
if (pos1 == std::string::npos || pos2 == std::string::npos) {
|
||||
std::cerr << "Invalid token format." << std::endl;
|
||||
return false;
|
||||
@@ -151,10 +157,11 @@ namespace JWT {
|
||||
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 pos2 = token.find('.', pos1 + 1);
|
||||
|
||||
|
||||
if (pos1 == std::string::npos || pos2 == std::string::npos) {
|
||||
std::cerr << "Invalid token format." << std::endl;
|
||||
return false;
|
||||
@@ -167,10 +174,11 @@ namespace JWT {
|
||||
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 pos2 = token.find('.', pos1 + 1);
|
||||
|
||||
|
||||
if (pos1 == std::string::npos || pos2 == std::string::npos) {
|
||||
std::cerr << "Invalid token format." << std::endl;
|
||||
return false;
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
#include <sstream>
|
||||
|
||||
// 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];
|
||||
|
||||
// calculating hash
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#include <security/cors.hpp>
|
||||
#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;
|
||||
response.set_header("Access-Control-Allow-Origin", "*");
|
||||
response.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
#include <json.hpp>
|
||||
|
||||
// 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;
|
||||
response.set_header("Access-Control-Allow-Origin", "*");
|
||||
if (request.body.empty()) {
|
||||
|
||||
+12
-10
@@ -8,7 +8,8 @@
|
||||
#define ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
|
||||
// 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));
|
||||
std::string str;
|
||||
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
|
||||
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.empty()) {
|
||||
response.set_content(ADD_USER_REQUIRED_STRING,
|
||||
JSON_TYPE);
|
||||
JSON_TYPE);
|
||||
syslog(LOG_ERR, "add user request body is empty");
|
||||
return;
|
||||
}
|
||||
@@ -31,7 +33,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
|
||||
|
||||
if (json_body["token"] == nullptr) {
|
||||
response.set_content(ADD_USER_REQUIRED_STRING,
|
||||
JSON_TYPE);
|
||||
JSON_TYPE);
|
||||
syslog(LOG_ERR, "no token in add user request");
|
||||
return;
|
||||
}
|
||||
@@ -42,13 +44,13 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
|
||||
}
|
||||
if (json_body["username"] == nullptr) {
|
||||
response.set_content(ADD_USER_REQUIRED_STRING,
|
||||
JSON_TYPE);
|
||||
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);
|
||||
JSON_TYPE);
|
||||
syslog(LOG_ERR, "no password in add user request");
|
||||
return;
|
||||
}
|
||||
@@ -65,7 +67,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
|
||||
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;
|
||||
}
|
||||
@@ -84,7 +86,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
|
||||
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(R"({"status":"user already exists"})", JSON_TYPE);
|
||||
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;
|
||||
|
||||
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\"";
|
||||
new_user_data << ",\"is_superuser\":";
|
||||
(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.close();
|
||||
std::stringstream syslogstring;
|
||||
syslogstring << "user added [" << username << "]";
|
||||
syslogstring << "user added [" << username << "]";
|
||||
syslog(LOG_INFO, syslogstring.str().c_str());
|
||||
response.set_content(R"({"status":"ok"})", JSON_TYPE);
|
||||
}
|
||||
@@ -6,7 +6,8 @@
|
||||
#include <syslog.h>
|
||||
|
||||
// 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;
|
||||
response.set_header("Access-Control-Allow-Origin", "*");
|
||||
if (request.body.empty()) {
|
||||
@@ -36,7 +37,7 @@ void change_password(const httplib::Request &request, httplib::Response &respons
|
||||
}
|
||||
if (json_body["new_password"] == nullptr) {
|
||||
response.set_content(CHANGE_PASSWORD_REQUIRED_STRING,
|
||||
JSON_TYPE);
|
||||
JSON_TYPE);
|
||||
syslog(LOG_ERR, "no new password in change password request");
|
||||
return;
|
||||
}
|
||||
@@ -66,7 +67,7 @@ void change_password(const httplib::Request &request, httplib::Response &respons
|
||||
if (json_body["userid"] != nullptr) {
|
||||
changepassuserid = json_body["userid"];
|
||||
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;
|
||||
}
|
||||
@@ -85,23 +86,24 @@ void change_password(const httplib::Request &request, httplib::Response &respons
|
||||
|
||||
bool found = false;
|
||||
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)) {
|
||||
all_user["password"] = new_password;
|
||||
found = true;
|
||||
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());
|
||||
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)) {
|
||||
all_user["password"] = new_password;
|
||||
found = true;
|
||||
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());
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
#include <syslog.h>
|
||||
|
||||
// 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;
|
||||
response.set_header("Access-Control-Allow-Origin", "*");
|
||||
if (request.body.empty()) {
|
||||
@@ -54,7 +55,7 @@ void change_user(const httplib::Request &request, httplib::Response &response) {
|
||||
if (json_body["userid"] != nullptr) {
|
||||
changeusruserid = json_body["userid"];
|
||||
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;
|
||||
}
|
||||
@@ -73,7 +74,7 @@ void change_user(const httplib::Request &request, httplib::Response &response) {
|
||||
|
||||
bool found = false;
|
||||
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 (json_body["username"] != nullptr) { all_user["username"] = json_body["username"]; }
|
||||
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"]; }
|
||||
found = true;
|
||||
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());
|
||||
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 (json_body["username"] != nullptr) { all_user["username"] = json_body["username"]; }
|
||||
found = true;
|
||||
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());
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
#include <syslog.h>
|
||||
|
||||
// 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;
|
||||
response.set_header("Access-Control-Allow-Origin", "*");
|
||||
if (request.body.empty()) {
|
||||
@@ -46,7 +47,7 @@ void delete_user(const httplib::Request &request, httplib::Response &response) {
|
||||
if (json_body["userid"] != nullptr) {
|
||||
deluserid = json_body["userid"];
|
||||
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;
|
||||
}
|
||||
@@ -67,7 +68,7 @@ void delete_user(const httplib::Request &request, httplib::Response &response) {
|
||||
for (size_t i = 0; i < all_users.size(); i++) {
|
||||
if (all_users[i]["id"] == deluserid) {
|
||||
std::stringstream syslogstring;
|
||||
syslogstring << "user deleted [" << all_users[i]["username"] << "]";
|
||||
syslogstring << "user deleted [" << all_users[i]["username"] << "]";
|
||||
all_users.erase(i);
|
||||
found = true;
|
||||
syslog(LOG_INFO, syslogstring.str().c_str());
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
#include <syslog.h>
|
||||
|
||||
// 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;
|
||||
response.set_header("Access-Control-Allow-Origin", "*");
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
#include <syslog.h>
|
||||
|
||||
// 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;
|
||||
response.set_header("Access-Control-Allow-Origin", "*");
|
||||
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);
|
||||
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;
|
||||
break;
|
||||
|
||||
+7
-7
@@ -4,24 +4,24 @@
|
||||
#include <httplib.h>
|
||||
|
||||
// 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
|
||||
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
|
||||
void user_access(const httplib::Request &request, httplib::Response &response);
|
||||
void user_access(const httplib::Request& request, httplib::Response& response);
|
||||
|
||||
// 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
|
||||
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
|
||||
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
|
||||
void change_user(const httplib::Request &request, httplib::Response &response);
|
||||
void change_user(const httplib::Request& request, httplib::Response& response);
|
||||
|
||||
#endif // USERS_HPP
|
||||
Reference in new issue
Block a user