updated and added more funcs to be small
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
2025-04-19 16:41:19 +07:00
parent 55ccf5b2f6
commit 794ada3d10
11 changed files with 72 additions and 197 deletions
+4 -21
View File
@@ -14,30 +14,13 @@ namespace AuthService
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(AUTH_REQUIRED_STRING, JSON_TYPE);
syslog(LOG_ERR, "no body in auth request");
return;
}
AuthService::Includes::check_body_empty(request, response, AUTH_REQUIRED_STRING);
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["username"] == nullptr) {
response.set_content(AUTH_REQUIRED_STRING, JSON_TYPE);
syslog(LOG_ERR, "no username in auth request");
return;
}
if (json_body["password"] == nullptr) {
response.set_content(AUTH_REQUIRED_STRING, JSON_TYPE);
syslog(LOG_ERR, "no password in auth request");
return;
}
AuthService::Includes::check_body_data(json_body, "username", response, AUTH_REQUIRED_STRING);
AuthService::Includes::check_body_data(json_body, "password", response, AUTH_REQUIRED_STRING);
const std::string request_username = json_body["username"];
const std::string request_password = json_body["password"];
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ifstream usersfile(USERS_FILE);
nlohmann::json all_users = nlohmann::json::parse(usersfile);
usersfile.close();
nlohmann::json all_users = AuthService::Includes::get_all_users_from_file();
std::string userid;
for (nlohmann::basic_json<> user : all_users) {
+1 -1
View File
@@ -10,6 +10,6 @@ namespace AuthService
void auth(const httplib::Request& request, httplib::Response& response);
// function for verify tokens
bool verify_auth(const std::string& token);
void verify_auth(const std::string& token, httplib::Response& response);
}
}
+13 -5
View File
@@ -7,19 +7,27 @@ namespace AuthService
{
namespace Auth
{
// function for verify tokens
bool verify_auth(const std::string& token)
void send_reject(httplib::Response& response) {
response.set_content(R"({"access":"reject"})", JSON_TYPE);
syslog(LOG_ERR, "access rejected");
}
void verify_auth(const std::string& token, httplib::Response& response)
{
try {
return JWT::verifyJWT(token, JWT_SECRET_KEY);
if (!JWT::verifyJWT(token, JWT_SECRET_KEY)) {
send_reject(response);
return;
}
}
catch (const std::system_error& e) {
std::cout << "Verification error: " << e.what() << std::endl;
syslog(LOG_ERR, "token verification error");
return false;
send_reject(response);
return;
}
catch (...) {
return false;
send_reject(response);
return;
}
}
}
+24
View File
@@ -1,6 +1,9 @@
#ifndef INCLUDES_HPP
#define INCLUDES_HPP
#include <json.hpp>
#include <syslog.h>
namespace AuthService
{
namespace Includes
@@ -16,6 +19,27 @@ namespace AuthService
#define DELETE_USER_REQUIRED_STRING R"({"required":"[token] or if you superuser [token,userid]"})"
#define USERS_FILE "users.json"
static std::mutex usersfilemutex;
inline void check_body_empty(const httplib::Request& request, httplib::Response& response, std::string required_string) {
if (request.body.empty()) {
response.set_content(required_string, JSON_TYPE);
syslog(LOG_ERR, "no body in request");
return;
}
}
inline void check_body_data(nlohmann::json body, std::string data, httplib::Response& response, std::string required_string) {
if (body[data] == nullptr) {
response.set_content(required_string, JSON_TYPE);
syslog(LOG_ERR, "Error request");
return;
}
}
inline nlohmann::json get_all_users_from_file() {
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ifstream usersfile(USERS_FILE);
nlohmann::json all_users = nlohmann::json::parse(usersfile);
usersfile.close();
return all_users;
}
}
}
#endif // INCLUDES_HPP
+3 -16
View File
@@ -14,23 +14,10 @@ namespace AuthService
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
syslog(LOG_ERR, "access request body is emty");
return;
}
AuthService::Includes::check_body_empty(request, response, ACCESS_REQUIRED_STRING);
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
syslog(LOG_ERR, "no token in acces request");
return;
}
if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(R"({"access":"reject"})", JSON_TYPE);
syslog(LOG_ERR, "access rejected");
return;
}
AuthService::Includes::check_body_data(json_body, "token", response, ACCESS_REQUIRED_STRING);
AuthService::Auth::verify_auth(json_body["token"], response);
response.set_content(R"({"access":"success"})", JSON_TYPE);
syslog(LOG_INFO, "access success");
}
+5 -31
View File
@@ -26,38 +26,12 @@ namespace AuthService
{
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);
syslog(LOG_ERR, "add user request body is empty");
return;
}
AuthService::Includes::check_body_empty(request, response, ADD_USER_REQUIRED_STRING);
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE);
syslog(LOG_ERR, "no token in add user request");
return;
}
if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in add user request");
return;
}
if (json_body["username"] == nullptr) {
response.set_content(ADD_USER_REQUIRED_STRING,
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);
syslog(LOG_ERR, "no password in add user request");
return;
}
AuthService::Includes::check_body_data(json_body, "token", response, ADD_USER_REQUIRED_STRING);
AuthService::Auth::verify_auth(json_body["token"], response);
AuthService::Includes::check_body_data(json_body, "username", response, ADD_USER_REQUIRED_STRING);
AuthService::Includes::check_body_data(json_body, "password", response, ADD_USER_REQUIRED_STRING);
std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"];
if (userid.empty()) {
response.set_content(STRING403, JSON_TYPE);
+6 -35
View File
@@ -14,42 +14,13 @@ namespace AuthService
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(
CHANGE_PASSWORD_REQUIRED_STRING,
JSON_TYPE
);
syslog(LOG_ERR, "body is empty in change password request");
return;
}
AuthService::Includes::check_body_empty(request, response, CHANGE_PASSWORD_REQUIRED_STRING);
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(
CHANGE_PASSWORD_REQUIRED_STRING,
JSON_TYPE
);
syslog(LOG_ERR, "no token in change password request");
return;
}
if ((json_body["userid"] == nullptr) && (json_body["old_password"] == nullptr)) {
response.set_content(
CHANGE_PASSWORD_REQUIRED_STRING,
JSON_TYPE);
syslog(LOG_ERR, "no old password is change password request");
return;
}
if (json_body["new_password"] == nullptr) {
response.set_content(CHANGE_PASSWORD_REQUIRED_STRING,
JSON_TYPE);
syslog(LOG_ERR, "no new password in change password request");
return;
}
if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in change password request");
return;
}
AuthService::Includes::check_body_data(json_body, "token", response, CHANGE_PASSWORD_REQUIRED_STRING);
AuthService::Includes::check_body_data(json_body, "userid", response, CHANGE_PASSWORD_REQUIRED_STRING);
AuthService::Includes::check_body_data(json_body, "old_password", response, CHANGE_PASSWORD_REQUIRED_STRING);
AuthService::Includes::check_body_data(json_body, "new_password", response, CHANGE_PASSWORD_REQUIRED_STRING);
AuthService::Auth::verify_auth(json_body["token"], response);
std::string old_password;
const std::string new_password = json_body["new_password"];
+5 -28
View File
@@ -14,35 +14,12 @@ namespace AuthService
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(
CHANGE_USER_REQUIRED_STRING,
JSON_TYPE);
syslog(LOG_ERR, "change user request body is empty");
return;
}
AuthService::Includes::check_body_empty(request, response, CHANGE_USER_REQUIRED_STRING);
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(
CHANGE_USER_REQUIRED_STRING,
JSON_TYPE
);
syslog(LOG_ERR, "no token in change user request");
return;
}
if ((json_body["userid"] == nullptr) && (json_body["old_password"] == nullptr)) {
response.set_content(
CHANGE_USER_REQUIRED_STRING,
JSON_TYPE);
syslog(LOG_ERR, "no old_password in change user request");
return;
}
if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in change user request");
return;
}
AuthService::Includes::check_body_data(json_body, "token", response, CHANGE_USER_REQUIRED_STRING);
AuthService::Includes::check_body_data(json_body, "userid", response, CHANGE_USER_REQUIRED_STRING);
AuthService::Includes::check_body_data(json_body, "old_password", response, CHANGE_USER_REQUIRED_STRING);
AuthService::Auth::verify_auth(json_body["token"], response);
std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"];
if (userid.empty()) {
+3 -20
View File
@@ -14,27 +14,10 @@ namespace AuthService
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(
DELETE_USER_REQUIRED_STRING,
JSON_TYPE);
syslog(LOG_ERR, "delete user request body is empty");
return;
}
AuthService::Includes::check_body_empty(request, response, DELETE_USER_REQUIRED_STRING);
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(
DELETE_USER_REQUIRED_STRING,
JSON_TYPE);
syslog(LOG_ERR, "no token in delete user request");
return;
}
if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in delete user request");
return;
}
AuthService::Includes::check_body_data(json_body, "token", response, DELETE_USER_REQUIRED_STRING);
AuthService::Auth::verify_auth(json_body["token"], response);
std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"];
if (userid.empty()) {
+4 -20
View File
@@ -14,33 +14,17 @@ namespace AuthService
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
syslog(LOG_ERR, "get all users request body is empty");
return;
}
AuthService::Includes::check_body_empty(request, response, ACCESS_REQUIRED_STRING);
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
syslog(LOG_ERR, "no token in get all users");
return;
}
if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in get all user request");
return;
}
AuthService::Includes::check_body_data(json_body, "token", response, ACCESS_REQUIRED_STRING);
AuthService::Auth::verify_auth(json_body["token"], response);
std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"];
if (userid.empty()) {
response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in get all user request");
return;
}
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ifstream usersfile(USERS_FILE);
nlohmann::json all_users = nlohmann::json::parse(usersfile);
usersfile.close();
nlohmann::json all_users = AuthService::Includes::get_all_users_from_file();
nlohmann::json response_user_data = nullptr;
for (nlohmann::json& user : all_users) {
if (user["id"] == userid) {
+4 -20
View File
@@ -14,33 +14,17 @@ namespace AuthService
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
syslog(LOG_ERR, "get user request body is empty");
return;
}
AuthService::Includes::check_body_empty(request, response, ACCESS_REQUIRED_STRING);
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
syslog(LOG_ERR, "no token in get user request");
return;
}
if (!AuthService::Auth::verify_auth(json_body["token"])) {
response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in get user request");
return;
}
AuthService::Includes::check_body_data(json_body, "token", response, ACCESS_REQUIRED_STRING);
AuthService::Auth::verify_auth(json_body["token"], response);
std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"];
if (userid.empty()) {
response.set_content(STRING403, JSON_TYPE);
syslog(LOG_ERR, "access reject in get user request");
return;
}
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
std::ifstream usersfile(USERS_FILE);
nlohmann::json all_users = nlohmann::json::parse(usersfile);
usersfile.close();
nlohmann::json all_users = AuthService::Includes::get_all_users_from_file();
nlohmann::json response_user_data = nullptr;
for (nlohmann::json& user : all_users) {
if (user["id"] == userid) {