updated and added namespaces #7

Merged
wt merged 1 commits from oop into main 2025-04-18 09:39:42 +00:00
18 changed files with 118 additions and 61 deletions

No files matched your search

+12 -9
View File
@@ -4,18 +4,20 @@
#include <security/cors.hpp> #include <security/cors.hpp>
#include <colors.h> #include <colors.h>
namespace AuthService
{
AuthService::AuthService() AuthService::AuthService()
{ {
this->srv.Post("/api/token", auth); this->srv.Post("/api/token", Auth::auth);
this->srv.Post("/api/users/user", get_user); this->srv.Post("/api/users/user", Users::get_user);
this->srv.Post("/api/users/all", get_all_users); this->srv.Post("/api/users/all", Users::get_all_users);
this->srv.Post("/api/users/add", add_user); this->srv.Post("/api/users/add", Users::add_user);
this->srv.Post("/api/users/user/password/change", change_password); this->srv.Post("/api/users/user/password/change", Users::change_password);
this->srv.Post("/api/users/del", delete_user); this->srv.Post("/api/users/del", Users::delete_user);
this->srv.Post("/api/access", user_access); this->srv.Post("/api/access", Users::user_access);
this->srv.Post("/api/users/user/change", change_user); 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/admin", "../src/web");
this->srv.set_mount_point("/auth/js", "../src/web/js"); this->srv.set_mount_point("/auth/js", "../src/web/js");
@@ -31,3 +33,4 @@ int AuthService::serve(const std::string& ip, int port, int socket_flags)
this->srv.listen(ip, port, socket_flags); this->srv.listen(ip, port, socket_flags);
return 0; return 0;
} }
}
+3 -1
View File
@@ -4,7 +4,8 @@
#include <httplib.h> #include <httplib.h>
#define CORS_ENABLE #define CORS_ENABLE
namespace AuthService
{
class AuthService class AuthService
{ {
private: private:
@@ -14,3 +15,4 @@ 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);
}; };
}
+7 -2
View File
@@ -4,8 +4,11 @@
#include <includes.hpp> #include <includes.hpp>
#include <jwt.hpp> #include <jwt.hpp>
#include <syslog.h> #include <syslog.h>
#include <security/HashPassword.hpp>
namespace AuthService
{
namespace Auth
{
// 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)
{ {
@@ -31,7 +34,7 @@ void auth(const httplib::Request& request, httplib::Response& response)
const std::string request_username = json_body["username"]; const std::string request_username = json_body["username"];
const std::string request_password = json_body["password"]; const std::string request_password = json_body["password"];
std::lock_guard<std::mutex> lock(usersfilemutex); std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ifstream usersfile(USERS_FILE); std::ifstream usersfile(USERS_FILE);
nlohmann::json all_users = nlohmann::json::parse(usersfile); nlohmann::json all_users = nlohmann::json::parse(usersfile);
usersfile.close(); usersfile.close();
@@ -64,3 +67,5 @@ void auth(const httplib::Request& request, httplib::Response& response)
syslog(LOG_INFO, "auth request success"); syslog(LOG_INFO, "auth request success");
response.set_content(response_json, JSON_TYPE); response.set_content(response_json, JSON_TYPE);
} }
}
}
+6
View File
@@ -2,8 +2,14 @@
#include <httplib.h> #include <httplib.h>
namespace AuthService
{
namespace Auth
{
// 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);
}
}
+6 -1
View File
@@ -3,7 +3,10 @@
#include <includes.hpp> #include <includes.hpp>
#include <jwt.hpp> #include <jwt.hpp>
#include <syslog.h> #include <syslog.h>
namespace AuthService
{
namespace Auth
{
// function for verify tokens // function for verify tokens
bool verify_auth(const std::string& token) bool verify_auth(const std::string& token)
{ {
@@ -19,3 +22,5 @@ bool verify_auth(const std::string& token)
return false; return false;
} }
} }
}
}
+1 -1
View File
@@ -2,10 +2,10 @@
int main() int main()
{ {
AuthService auth_service;
#ifdef TEST #ifdef TEST
return 0; return 0;
#else #else
AuthService::AuthService auth_service;
return auth_service.serve("0.0.0.0", 43243); return auth_service.serve("0.0.0.0", 43243);
#endif #endif
} }
+6 -1
View File
@@ -1,6 +1,10 @@
#ifndef INCLUDES_HPP #ifndef INCLUDES_HPP
#define INCLUDES_HPP #define INCLUDES_HPP
namespace AuthService
{
namespace Includes
{
#define JWT_SECRET_KEY "secret" #define JWT_SECRET_KEY "secret"
#define STRING403 R"({"status":"403"})" #define STRING403 R"({"status":"403"})"
#define JSON_TYPE "application/json" #define JSON_TYPE "application/json"
@@ -12,5 +16,6 @@
#define DELETE_USER_REQUIRED_STRING R"({"required":"[token] or if you superuser [token,userid]"})" #define DELETE_USER_REQUIRED_STRING R"({"required":"[token] or if you superuser [token,userid]"})"
#define USERS_FILE "users.json" #define USERS_FILE "users.json"
static std::mutex usersfilemutex; static std::mutex usersfilemutex;
}
}
#endif // INCLUDES_HPP #endif // INCLUDES_HPP
-24
View File
@@ -1,24 +0,0 @@
#pragma once
#include <iostream>
#include <string>
#include <openssl/sha.h>
#include <iomanip>
#include <sstream>
// funcion for hashing password using sha256
std::string hashPassword(const std::string& password)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
// calculating hash
SHA256(reinterpret_cast<const unsigned char*>(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<int>(hash[i]);
}
return ss.str();
}
+6
View File
@@ -1,6 +1,10 @@
#include <security/cors.hpp> #include <security/cors.hpp>
#include <colors.h> #include <colors.h>
namespace AuthService
{
namespace Security
{
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;
@@ -8,3 +12,5 @@ void set_cors_headers(const httplib::Request& request, httplib::Response& respon
response.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS"); response.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
response.set_header("Access-Control-Allow-Headers", "Authorization,X-Custom,Content-Type"); response.set_header("Access-Control-Allow-Headers", "Authorization,X-Custom,Content-Type");
} }
}
}
+6 -3
View File
@@ -3,11 +3,14 @@
#include <httplib.h> #include <httplib.h>
namespace AuthService
{
namespace Security
{
#ifdef CORS_ENABLE #ifdef CORS_ENABLE
// function to set cors headers // function to set cors headers
void set_cors_headers(const httplib::Request& request, httplib::Response& response); void set_cors_headers(const httplib::Request& request, httplib::Response& response);
#endif // CORS_ENABLE #endif // CORS_ENABLE
}
}
#endif // CORS_HPP #endif // CORS_HPP
+7 -1
View File
@@ -5,6 +5,10 @@
#include <syslog.h> #include <syslog.h>
#include <json.hpp> #include <json.hpp>
namespace AuthService
{
namespace Users
{
// 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)
{ {
@@ -22,7 +26,7 @@ void user_access(const httplib::Request& request, httplib::Response& response)
syslog(LOG_ERR, "no token in acces request"); syslog(LOG_ERR, "no token in acces request");
return; return;
} }
if (!verify_auth(json_body["token"])) { if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(R"({"access":"reject"})", JSON_TYPE); response.set_content(R"({"access":"reject"})", JSON_TYPE);
syslog(LOG_ERR, "access rejected"); syslog(LOG_ERR, "access rejected");
return; return;
@@ -30,3 +34,5 @@ void user_access(const httplib::Request& request, httplib::Response& response)
response.set_content(R"({"access":"success"})", JSON_TYPE); response.set_content(R"({"access":"success"})", JSON_TYPE);
syslog(LOG_INFO, "access success"); syslog(LOG_INFO, "access success");
} }
}
}
+9 -4
View File
@@ -6,7 +6,10 @@
#include <syslog.h> #include <syslog.h>
#define ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" #define ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
namespace AuthService
{
namespace Users
{
// 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)
{ {
@@ -37,7 +40,7 @@ void add_user(const httplib::Request& request, httplib::Response& response)
syslog(LOG_ERR, "no token in add user request"); syslog(LOG_ERR, "no token in add user request");
return; return;
} }
if (!verify_auth(json_body["token"])) { if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE); response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in add user request"); syslog(LOG_ERR, "access reject in add user request");
return; return;
@@ -63,7 +66,7 @@ void add_user(const httplib::Request& request, httplib::Response& response)
} }
nlohmann::json all_users; nlohmann::json all_users;
{ {
std::lock_guard<std::mutex> lock(usersfilemutex); std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ifstream usersfile(USERS_FILE); std::ifstream usersfile(USERS_FILE);
all_users = nlohmann::json::parse(usersfile); all_users = nlohmann::json::parse(usersfile);
usersfile.close(); usersfile.close();
@@ -105,7 +108,7 @@ void add_user(const httplib::Request& request, httplib::Response& response)
new_user_data += "}"; new_user_data += "}";
all_users.push_back(nlohmann::json::parse(new_user_data)); all_users.push_back(nlohmann::json::parse(new_user_data));
std::lock_guard<std::mutex> lock(usersfilemutex); std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ofstream usersfilew(USERS_FILE); std::ofstream usersfilew(USERS_FILE);
usersfilew << all_users.dump(4); usersfilew << all_users.dump(4);
usersfilew.close(); usersfilew.close();
@@ -113,3 +116,5 @@ void add_user(const httplib::Request& request, httplib::Response& response)
syslog(LOG_INFO, syslogstring.c_str()); syslog(LOG_INFO, syslogstring.c_str());
response.set_content(R"({"status":"ok"})", JSON_TYPE); response.set_content(R"({"status":"ok"})", JSON_TYPE);
} }
}
}
+9 -3
View File
@@ -5,6 +5,10 @@
#include <jwt.hpp> #include <jwt.hpp>
#include <syslog.h> #include <syslog.h>
namespace AuthService
{
namespace Users
{
// 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)
{ {
@@ -41,7 +45,7 @@ void change_password(const httplib::Request& request, httplib::Response& respons
syslog(LOG_ERR, "no new password in change password request"); syslog(LOG_ERR, "no new password in change password request");
return; return;
} }
if (!verify_auth(json_body["token"])) { if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE); response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in change password request"); syslog(LOG_ERR, "access reject in change password request");
return; return;
@@ -60,7 +64,7 @@ void change_password(const httplib::Request& request, httplib::Response& respons
} }
nlohmann::json all_users; nlohmann::json all_users;
{ {
std::lock_guard<std::mutex> lock(usersfilemutex); std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ifstream usersfile(USERS_FILE); std::ifstream usersfile(USERS_FILE);
all_users = nlohmann::json::parse(usersfile); all_users = nlohmann::json::parse(usersfile);
usersfile.close(); usersfile.close();
@@ -115,9 +119,11 @@ void change_password(const httplib::Request& request, httplib::Response& respons
syslog(LOG_ERR, "can't find this user in change password request"); syslog(LOG_ERR, "can't find this user in change password request");
return; return;
} }
std::lock_guard<std::mutex> lock(usersfilemutex); std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ofstream usersfilew(USERS_FILE); std::ofstream usersfilew(USERS_FILE);
usersfilew << all_users.dump(4); usersfilew << all_users.dump(4);
usersfilew.close(); usersfilew.close();
response.set_content(R"({"status":"ok"})", JSON_TYPE); response.set_content(R"({"status":"ok"})", JSON_TYPE);
} }
}
}
+9 -3
View File
@@ -5,6 +5,10 @@
#include <jwt.hpp> #include <jwt.hpp>
#include <syslog.h> #include <syslog.h>
namespace AuthService
{
namespace Users
{
// 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)
{ {
@@ -34,7 +38,7 @@ void change_user(const httplib::Request& request, httplib::Response& response)
syslog(LOG_ERR, "no old_password in change user request"); syslog(LOG_ERR, "no old_password in change user request");
return; return;
} }
if (!verify_auth(json_body["token"])) { if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE); response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in change user request"); syslog(LOG_ERR, "access reject in change user request");
return; return;
@@ -48,7 +52,7 @@ void change_user(const httplib::Request& request, httplib::Response& response)
} }
nlohmann::json all_users; nlohmann::json all_users;
{ {
std::lock_guard<std::mutex> lock(usersfilemutex); std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ifstream usersfile(USERS_FILE); std::ifstream usersfile(USERS_FILE);
all_users = nlohmann::json::parse(usersfile); all_users = nlohmann::json::parse(usersfile);
usersfile.close(); usersfile.close();
@@ -105,9 +109,11 @@ void change_user(const httplib::Request& request, httplib::Response& response)
syslog(LOG_ERR, "can't find this user in change user request"); syslog(LOG_ERR, "can't find this user in change user request");
return; return;
} }
std::lock_guard<std::mutex> lock(usersfilemutex); std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ofstream usersfilew(USERS_FILE); std::ofstream usersfilew(USERS_FILE);
usersfilew << all_users.dump(4); usersfilew << all_users.dump(4);
usersfilew.close(); usersfilew.close();
response.set_content(R"({"status":"ok"})", JSON_TYPE); response.set_content(R"({"status":"ok"})", JSON_TYPE);
} }
}
}
+9 -3
View File
@@ -5,6 +5,10 @@
#include <jwt.hpp> #include <jwt.hpp>
#include <syslog.h> #include <syslog.h>
namespace AuthService
{
namespace Users
{
// 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)
{ {
@@ -26,7 +30,7 @@ void delete_user(const httplib::Request& request, httplib::Response& response)
syslog(LOG_ERR, "no token in delete user request"); syslog(LOG_ERR, "no token in delete user request");
return; return;
} }
if (!verify_auth(json_body["token"])) { if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE); response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in delete user request"); syslog(LOG_ERR, "access reject in delete user request");
return; return;
@@ -40,7 +44,7 @@ void delete_user(const httplib::Request& request, httplib::Response& response)
} }
nlohmann::json all_users; nlohmann::json all_users;
{ {
std::lock_guard<std::mutex> lock(usersfilemutex); std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ifstream usersfile(USERS_FILE); std::ifstream usersfile(USERS_FILE);
all_users = nlohmann::json::parse(usersfile); all_users = nlohmann::json::parse(usersfile);
usersfile.close(); usersfile.close();
@@ -81,9 +85,11 @@ void delete_user(const httplib::Request& request, httplib::Response& response)
syslog(LOG_ERR, "can't find this user in delete user request"); syslog(LOG_ERR, "can't find this user in delete user request");
return; return;
} }
std::lock_guard<std::mutex> lock(usersfilemutex); std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ofstream usersfilew(USERS_FILE); std::ofstream usersfilew(USERS_FILE);
usersfilew << all_users.dump(4); usersfilew << all_users.dump(4);
usersfilew.close(); usersfilew.close();
response.set_content(R"({"status":"ok"})", JSON_TYPE); response.set_content(R"({"status":"ok"})", JSON_TYPE);
} }
}
}
+8 -2
View File
@@ -5,6 +5,10 @@
#include <jwt.hpp> #include <jwt.hpp>
#include <syslog.h> #include <syslog.h>
namespace AuthService
{
namespace Users
{
// 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)
{ {
@@ -22,7 +26,7 @@ void get_all_users(const httplib::Request& request, httplib::Response& response)
syslog(LOG_ERR, "no token in get all users"); syslog(LOG_ERR, "no token in get all users");
return; return;
} }
if (!verify_auth(json_body["token"])) { if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE); response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in get all user request"); syslog(LOG_ERR, "access reject in get all user request");
return; return;
@@ -33,7 +37,7 @@ void get_all_users(const httplib::Request& request, httplib::Response& response)
syslog(LOG_ERR, "access reject in get all user request"); syslog(LOG_ERR, "access reject in get all user request");
return; return;
} }
std::lock_guard<std::mutex> lock(usersfilemutex); std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ifstream usersfile(USERS_FILE); std::ifstream usersfile(USERS_FILE);
nlohmann::json all_users = nlohmann::json::parse(usersfile); nlohmann::json all_users = nlohmann::json::parse(usersfile);
usersfile.close(); usersfile.close();
@@ -56,3 +60,5 @@ void get_all_users(const httplib::Request& request, httplib::Response& response)
} }
response.set_content(all_users.dump(), JSON_TYPE); response.set_content(all_users.dump(), JSON_TYPE);
} }
}
}
+8 -2
View File
@@ -5,6 +5,10 @@
#include <jwt.hpp> #include <jwt.hpp>
#include <syslog.h> #include <syslog.h>
namespace AuthService
{
namespace Users
{
// 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)
{ {
@@ -22,7 +26,7 @@ void get_user(const httplib::Request& request, httplib::Response& response)
syslog(LOG_ERR, "no token in get user request"); syslog(LOG_ERR, "no token in get user request");
return; return;
} }
if (!verify_auth(json_body["token"])) { if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE); response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in get user request"); syslog(LOG_ERR, "access reject in get user request");
return; return;
@@ -33,7 +37,7 @@ void get_user(const httplib::Request& request, httplib::Response& response)
syslog(LOG_ERR, "access reject in get user request"); syslog(LOG_ERR, "access reject in get user request");
return; return;
} }
std::lock_guard<std::mutex> lock(usersfilemutex); std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ifstream usersfile(USERS_FILE); std::ifstream usersfile(USERS_FILE);
nlohmann::json all_users = nlohmann::json::parse(usersfile); nlohmann::json all_users = nlohmann::json::parse(usersfile);
usersfile.close(); usersfile.close();
@@ -52,3 +56,5 @@ void get_user(const httplib::Request& request, httplib::Response& response)
response_user_data.erase("password"); response_user_data.erase("password");
response.set_content(response_user_data.dump(), JSON_TYPE); response.set_content(response_user_data.dump(), JSON_TYPE);
} }
}
}
+6 -1
View File
@@ -3,6 +3,10 @@
#include <httplib.h> #include <httplib.h>
namespace AuthService
{
namespace Users
{
// 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);
@@ -23,5 +27,6 @@ void change_password(const httplib::Request& request, httplib::Response& respons
// 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