Merge pull request 'updated and added namespaces' (#7) from oop into main
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Waiting to run
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Waiting to run
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Waiting to run
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Waiting to run
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Waiting to run
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Waiting to run
Reviewed-on: https://gitea.home.com:3000/TolaMironcenko/auth_service/pulls/7
This commit was merged in pull request #7.
This commit is contained in:
commit
6cfa8e7b1c
18 files changed
+698
-641
No files matched your search
@@ -4,30 +4,33 @@
|
||||
#include <security/cors.hpp>
|
||||
#include <colors.h>
|
||||
|
||||
AuthService::AuthService()
|
||||
namespace AuthService
|
||||
{
|
||||
this->srv.Post("/api/token", auth);
|
||||
this->srv.Post("/api/users/user", get_user);
|
||||
this->srv.Post("/api/users/all", get_all_users);
|
||||
this->srv.Post("/api/users/add", add_user);
|
||||
this->srv.Post("/api/users/user/password/change", change_password);
|
||||
this->srv.Post("/api/users/del", delete_user);
|
||||
this->srv.Post("/api/access", user_access);
|
||||
this->srv.Post("/api/users/user/change", change_user);
|
||||
AuthService::AuthService()
|
||||
{
|
||||
this->srv.Post("/api/token", Auth::auth);
|
||||
this->srv.Post("/api/users/user", Users::get_user);
|
||||
this->srv.Post("/api/users/all", Users::get_all_users);
|
||||
this->srv.Post("/api/users/add", Users::add_user);
|
||||
this->srv.Post("/api/users/user/password/change", Users::change_password);
|
||||
this->srv.Post("/api/users/del", Users::delete_user);
|
||||
this->srv.Post("/api/access", Users::user_access);
|
||||
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/js", "../src/web/js");
|
||||
this->srv.set_mount_point("/auth/css", "../src/web/css");
|
||||
}
|
||||
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/css", "../src/web/css");
|
||||
}
|
||||
|
||||
AuthService::~AuthService() = default;
|
||||
AuthService::~AuthService() = default;
|
||||
|
||||
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;
|
||||
this->srv.listen(ip, port, socket_flags);
|
||||
return 0;
|
||||
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;
|
||||
this->srv.listen(ip, port, socket_flags);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,15 @@
|
||||
#include <httplib.h>
|
||||
|
||||
#define CORS_ENABLE
|
||||
|
||||
class AuthService
|
||||
namespace AuthService
|
||||
{
|
||||
private:
|
||||
httplib::Server srv;
|
||||
public:
|
||||
AuthService();
|
||||
~AuthService();
|
||||
int serve(const std::string& ip, int port, int socket_flags = 0);
|
||||
};
|
||||
class AuthService
|
||||
{
|
||||
private:
|
||||
httplib::Server srv;
|
||||
public:
|
||||
AuthService();
|
||||
~AuthService();
|
||||
int serve(const std::string& ip, int port, int socket_flags = 0);
|
||||
};
|
||||
}
|
||||
+57
-52
@@ -4,63 +4,68 @@
|
||||
#include <includes.hpp>
|
||||
#include <jwt.hpp>
|
||||
#include <syslog.h>
|
||||
#include <security/HashPassword.hpp>
|
||||
|
||||
// function for authorization users
|
||||
void auth(const httplib::Request& request, httplib::Response& response)
|
||||
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;
|
||||
}
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
namespace Auth
|
||||
{
|
||||
// function for authorization users
|
||||
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()) {
|
||||
response.set_content(AUTH_REQUIRED_STRING, JSON_TYPE);
|
||||
syslog(LOG_ERR, "no body in auth request");
|
||||
return;
|
||||
}
|
||||
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;
|
||||
}
|
||||
const std::string request_username = json_body["username"];
|
||||
const std::string request_password = json_body["password"];
|
||||
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;
|
||||
}
|
||||
const std::string request_username = json_body["username"];
|
||||
const std::string request_password = json_body["password"];
|
||||
|
||||
std::lock_guard<std::mutex> lock(usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
nlohmann::json all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
nlohmann::json all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
|
||||
std::string userid;
|
||||
for (nlohmann::basic_json<> user : all_users) {
|
||||
if ((user["username"] == request_username) && (user["password"] == request_password)) {
|
||||
userid = user["id"];
|
||||
break;
|
||||
std::string userid;
|
||||
for (nlohmann::basic_json<> user : all_users) {
|
||||
if ((user["username"] == request_username) && (user["password"] == request_password)) {
|
||||
userid = user["id"];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (userid.empty()) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "auth request rejected");
|
||||
return;
|
||||
}
|
||||
|
||||
nlohmann::json headers = {
|
||||
{"alg", "HS256"},
|
||||
{"typ", "JWT"}
|
||||
};
|
||||
|
||||
nlohmann::json payload{
|
||||
{"userId", userid}
|
||||
};
|
||||
|
||||
std::string token = JWT::createJWT(headers.dump(), payload.dump(), JWT_SECRET_KEY);
|
||||
|
||||
std::string response_json = R"({"token":")" + token + "\"}";
|
||||
syslog(LOG_INFO, "auth request success");
|
||||
response.set_content(response_json, JSON_TYPE);
|
||||
}
|
||||
}
|
||||
if (userid.empty()) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "auth request rejected");
|
||||
return;
|
||||
}
|
||||
|
||||
nlohmann::json headers = {
|
||||
{"alg", "HS256"},
|
||||
{"typ", "JWT"}
|
||||
};
|
||||
|
||||
nlohmann::json payload{
|
||||
{"userId", userid}
|
||||
};
|
||||
|
||||
std::string token = JWT::createJWT(headers.dump(), payload.dump(), JWT_SECRET_KEY);
|
||||
|
||||
std::string response_json = R"({"token":")" + token + "\"}";
|
||||
syslog(LOG_INFO, "auth request success");
|
||||
response.set_content(response_json, JSON_TYPE);
|
||||
}
|
||||
+10
-4
@@ -2,8 +2,14 @@
|
||||
|
||||
#include <httplib.h>
|
||||
|
||||
// function for authorization users
|
||||
void auth(const httplib::Request& request, httplib::Response& response);
|
||||
namespace AuthService
|
||||
{
|
||||
namespace Auth
|
||||
{
|
||||
// function for authorization users
|
||||
void auth(const httplib::Request& request, httplib::Response& response);
|
||||
|
||||
// function for verify tokens
|
||||
bool verify_auth(const std::string& token);
|
||||
// function for verify tokens
|
||||
bool verify_auth(const std::string& token);
|
||||
}
|
||||
}
|
||||
+18
-13
@@ -3,19 +3,24 @@
|
||||
#include <includes.hpp>
|
||||
#include <jwt.hpp>
|
||||
#include <syslog.h>
|
||||
|
||||
// function for verify tokens
|
||||
bool verify_auth(const std::string& token)
|
||||
namespace AuthService
|
||||
{
|
||||
try {
|
||||
return JWT::verifyJWT(token, JWT_SECRET_KEY);
|
||||
}
|
||||
catch (const std::system_error& e) {
|
||||
std::cout << "Verification error: " << e.what() << std::endl;
|
||||
syslog(LOG_ERR, "token verification error");
|
||||
return false;
|
||||
}
|
||||
catch (...) {
|
||||
return false;
|
||||
namespace Auth
|
||||
{
|
||||
// function for verify tokens
|
||||
bool verify_auth(const std::string& token)
|
||||
{
|
||||
try {
|
||||
return JWT::verifyJWT(token, JWT_SECRET_KEY);
|
||||
}
|
||||
catch (const std::system_error& e) {
|
||||
std::cout << "Verification error: " << e.what() << std::endl;
|
||||
syslog(LOG_ERR, "token verification error");
|
||||
return false;
|
||||
}
|
||||
catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
AuthService auth_service;
|
||||
#ifdef TEST
|
||||
return 0;
|
||||
#else
|
||||
AuthService::AuthService auth_service;
|
||||
return auth_service.serve("0.0.0.0", 43243);
|
||||
#endif
|
||||
}
|
||||
+7
-2
@@ -1,6 +1,10 @@
|
||||
#ifndef INCLUDES_HPP
|
||||
#define INCLUDES_HPP
|
||||
|
||||
namespace AuthService
|
||||
{
|
||||
namespace Includes
|
||||
{
|
||||
#define JWT_SECRET_KEY "secret"
|
||||
#define STRING403 R"({"status":"403"})"
|
||||
#define JSON_TYPE "application/json"
|
||||
@@ -11,6 +15,7 @@
|
||||
#define CHANGE_USER_REQUIRED_STRING R"({"required":"[token] or if you superuser [token,userid]","optional":"[username,password,is_superuser,group]"})"
|
||||
#define DELETE_USER_REQUIRED_STRING R"({"required":"[token] or if you superuser [token,userid]"})"
|
||||
#define USERS_FILE "users.json"
|
||||
static std::mutex usersfilemutex;
|
||||
|
||||
static std::mutex usersfilemutex;
|
||||
}
|
||||
}
|
||||
#endif // INCLUDES_HPP
|
||||
@@ -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();
|
||||
}
|
||||
+11
-5
@@ -1,10 +1,16 @@
|
||||
#include <security/cors.hpp>
|
||||
#include <colors.h>
|
||||
|
||||
void set_cors_headers(const httplib::Request& request, httplib::Response& response)
|
||||
namespace AuthService
|
||||
{
|
||||
std::cout << GREEN << request.path << " " << std::endl;
|
||||
response.set_header("Access-Control-Allow-Origin", "*");
|
||||
response.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
|
||||
response.set_header("Access-Control-Allow-Headers", "Authorization,X-Custom,Content-Type");
|
||||
namespace Security
|
||||
{
|
||||
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");
|
||||
response.set_header("Access-Control-Allow-Headers", "Authorization,X-Custom,Content-Type");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,14 @@
|
||||
|
||||
#include <httplib.h>
|
||||
|
||||
namespace AuthService
|
||||
{
|
||||
namespace Security
|
||||
{
|
||||
#ifdef CORS_ENABLE
|
||||
|
||||
// function to set cors headers
|
||||
void set_cors_headers(const httplib::Request& request, httplib::Response& response);
|
||||
|
||||
// function to set cors headers
|
||||
void set_cors_headers(const httplib::Request& request, httplib::Response& response);
|
||||
#endif // CORS_ENABLE
|
||||
|
||||
}
|
||||
}
|
||||
#endif // CORS_HPP
|
||||
@@ -5,28 +5,34 @@
|
||||
#include <syslog.h>
|
||||
#include <json.hpp>
|
||||
|
||||
// function for access or reject authorization
|
||||
void user_access(const httplib::Request& request, httplib::Response& response)
|
||||
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;
|
||||
}
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
namespace Users
|
||||
{
|
||||
// function for access or reject authorization
|
||||
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()) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access request body is emty");
|
||||
return;
|
||||
}
|
||||
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 (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;
|
||||
}
|
||||
response.set_content(R"({"access":"success"})", JSON_TYPE);
|
||||
syslog(LOG_INFO, "access success");
|
||||
}
|
||||
}
|
||||
if (!verify_auth(json_body["token"])) {
|
||||
response.set_content(R"({"access":"reject"})", JSON_TYPE);
|
||||
syslog(LOG_ERR, "access rejected");
|
||||
return;
|
||||
}
|
||||
response.set_content(R"({"access":"success"})", JSON_TYPE);
|
||||
syslog(LOG_INFO, "access success");
|
||||
}
|
||||
+106
-101
@@ -6,110 +6,115 @@
|
||||
#include <syslog.h>
|
||||
|
||||
#define ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
|
||||
// function for generate random string
|
||||
std::string get_random_string(const char* alphabet, int quantity)
|
||||
namespace AuthService
|
||||
{
|
||||
srand(time(nullptr));
|
||||
std::string str;
|
||||
for (int i = 0; i < quantity; i++) {
|
||||
str += alphabet[0 + std::rand() % strlen(alphabet)];
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
// function for add user
|
||||
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);
|
||||
syslog(LOG_ERR, "add user request body is empty");
|
||||
return;
|
||||
}
|
||||
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 (!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;
|
||||
}
|
||||
|
||||
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 add user request");
|
||||
return;
|
||||
}
|
||||
nlohmann::json all_users;
|
||||
namespace Users
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
}
|
||||
// function for generate random string
|
||||
std::string get_random_string(const char* alphabet, int quantity)
|
||||
{
|
||||
srand(time(nullptr));
|
||||
std::string str;
|
||||
for (int i = 0; i < quantity; i++) {
|
||||
str += alphabet[0 + std::rand() % strlen(alphabet)];
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
// function for add user
|
||||
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);
|
||||
syslog(LOG_ERR, "add user request body is empty");
|
||||
return;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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 add user request");
|
||||
return;
|
||||
}
|
||||
nlohmann::json all_users;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
}
|
||||
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
}
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in add user request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in add user request");
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string username = json_body["username"];
|
||||
const std::string password = json_body["password"];
|
||||
|
||||
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");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::string new_user_data;
|
||||
new_user_data += R"({"id":")" + get_random_string(ALPHABET, 50) + R"(","username":")" + username + R"(","password":")" + password + R"(","group":)";
|
||||
(json_body["group"] != nullptr) ? new_user_data += R"(")" + std::string(json_body["group"]) + R"(")" : new_user_data += R"("users")";
|
||||
new_user_data += ",\"is_superuser\":";
|
||||
(json_body["is_superuser"] != nullptr) ? new_user_data += R"(")" + std::string(json_body["is_superuser"]) + R"(")" : new_user_data += "\"0\"";
|
||||
new_user_data += "}";
|
||||
all_users.push_back(nlohmann::json::parse(new_user_data));
|
||||
|
||||
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
||||
std::ofstream usersfilew(USERS_FILE);
|
||||
usersfilew << all_users.dump(4);
|
||||
usersfilew.close();
|
||||
std::string syslogstring = "user added [" + username + "]";
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
response.set_content(R"({"status":"ok"})", JSON_TYPE);
|
||||
}
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in add user request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in add user request");
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string username = json_body["username"];
|
||||
const std::string password = json_body["password"];
|
||||
|
||||
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");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::string new_user_data;
|
||||
new_user_data += R"({"id":")" + get_random_string(ALPHABET, 50) + R"(","username":")" + username + R"(","password":")" + password + R"(","group":)";
|
||||
(json_body["group"] != nullptr) ? new_user_data += R"(")" + std::string(json_body["group"]) + R"(")" : new_user_data += R"("users")";
|
||||
new_user_data += ",\"is_superuser\":";
|
||||
(json_body["is_superuser"] != nullptr) ? new_user_data += R"(")" + std::string(json_body["is_superuser"]) + R"(")" : new_user_data += "\"0\"";
|
||||
new_user_data += "}";
|
||||
all_users.push_back(nlohmann::json::parse(new_user_data));
|
||||
|
||||
std::lock_guard<std::mutex> lock(usersfilemutex);
|
||||
std::ofstream usersfilew(USERS_FILE);
|
||||
usersfilew << all_users.dump(4);
|
||||
usersfilew.close();
|
||||
std::string syslogstring = "user added [" + username + "]";
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
response.set_content(R"({"status":"ok"})", JSON_TYPE);
|
||||
}
|
||||
+113
-107
@@ -5,119 +5,125 @@
|
||||
#include <jwt.hpp>
|
||||
#include <syslog.h>
|
||||
|
||||
// functtion for change password for user
|
||||
void change_password(const httplib::Request& request, httplib::Response& response)
|
||||
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;
|
||||
}
|
||||
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 (!verify_auth(json_body["token"])) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in change password request");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string old_password;
|
||||
const std::string new_password = json_body["new_password"];
|
||||
|
||||
if (json_body["userid"] == nullptr) { old_password = json_body["old_password"]; }
|
||||
|
||||
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 change password request");
|
||||
return;
|
||||
}
|
||||
nlohmann::json all_users;
|
||||
namespace Users
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
}
|
||||
// functtion for change password for user
|
||||
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()) {
|
||||
response.set_content(
|
||||
CHANGE_PASSWORD_REQUIRED_STRING,
|
||||
JSON_TYPE
|
||||
);
|
||||
syslog(LOG_ERR, "body is empty in change password request");
|
||||
return;
|
||||
}
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
|
||||
std::string changepassuserid = userid;
|
||||
if (json_body["userid"] != nullptr) {
|
||||
changepassuserid = json_body["userid"];
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in change password request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in change password request");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
if (json_body["userid"] != nullptr) {
|
||||
for (nlohmann::basic_json<>& all_user : all_users) {
|
||||
if ((all_user["id"] == changepassuserid)) {
|
||||
all_user["password"] = new_password;
|
||||
found = true;
|
||||
std::string syslogstring = "password changed for user [" + std::string(all_user["username"]) + "]";
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
break;
|
||||
std::string old_password;
|
||||
const std::string new_password = json_body["new_password"];
|
||||
|
||||
if (json_body["userid"] == nullptr) { old_password = json_body["old_password"]; }
|
||||
|
||||
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 change password request");
|
||||
return;
|
||||
}
|
||||
nlohmann::json all_users;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
}
|
||||
|
||||
std::string changepassuserid = userid;
|
||||
if (json_body["userid"] != nullptr) {
|
||||
changepassuserid = json_body["userid"];
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
}
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in change password request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in change password request");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
if (json_body["userid"] != nullptr) {
|
||||
for (nlohmann::basic_json<>& all_user : all_users) {
|
||||
if ((all_user["id"] == changepassuserid)) {
|
||||
all_user["password"] = new_password;
|
||||
found = true;
|
||||
std::string syslogstring = "password changed for user [" + std::string(all_user["username"]) + "]";
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
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::string syslogstring = "password changed for user [" + std::string(all_user["username"]) + "]";
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
response.set_content(R"({"status":"can't find this user"})", JSON_TYPE);
|
||||
syslog(LOG_ERR, "can't find this user in change password request");
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
||||
std::ofstream usersfilew(USERS_FILE);
|
||||
usersfilew << all_users.dump(4);
|
||||
usersfilew.close();
|
||||
response.set_content(R"({"status":"ok"})", JSON_TYPE);
|
||||
}
|
||||
}
|
||||
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::string syslogstring = "password changed for user [" + std::string(all_user["username"]) + "]";
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
response.set_content(R"({"status":"can't find this user"})", JSON_TYPE);
|
||||
syslog(LOG_ERR, "can't find this user in change password request");
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(usersfilemutex);
|
||||
std::ofstream usersfilew(USERS_FILE);
|
||||
usersfilew << all_users.dump(4);
|
||||
usersfilew.close();
|
||||
response.set_content(R"({"status":"ok"})", JSON_TYPE);
|
||||
}
|
||||
+104
-98
@@ -5,109 +5,115 @@
|
||||
#include <jwt.hpp>
|
||||
#include <syslog.h>
|
||||
|
||||
// function for change user data
|
||||
void change_user(const httplib::Request& request, httplib::Response& response)
|
||||
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;
|
||||
}
|
||||
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 (!verify_auth(json_body["token"])) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in change user request");
|
||||
return;
|
||||
}
|
||||
|
||||
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 change user request");
|
||||
return;
|
||||
}
|
||||
nlohmann::json all_users;
|
||||
namespace Users
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
}
|
||||
std::string changeusruserid = userid;
|
||||
if (json_body["userid"] != nullptr) {
|
||||
changeusruserid = json_body["userid"];
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
// function for change user data
|
||||
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()) {
|
||||
response.set_content(
|
||||
CHANGE_USER_REQUIRED_STRING,
|
||||
JSON_TYPE);
|
||||
syslog(LOG_ERR, "change user request body is empty");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in change user request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in change user request");
|
||||
return;
|
||||
}
|
||||
}
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
|
||||
bool found = false;
|
||||
if (json_body["userid"] != nullptr) {
|
||||
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"]; }
|
||||
if (json_body["group"] != nullptr) { all_user["group"] = json_body["group"]; }
|
||||
if (json_body["is_superuser"] != nullptr) { all_user["is_superuser"] = json_body["is_superuser"]; }
|
||||
found = true;
|
||||
std::string syslogstring = "userdata changed for user [" + std::string(all_user["username"]) + "]";
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
break;
|
||||
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;
|
||||
}
|
||||
|
||||
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 change user request");
|
||||
return;
|
||||
}
|
||||
nlohmann::json all_users;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
}
|
||||
std::string changeusruserid = userid;
|
||||
if (json_body["userid"] != nullptr) {
|
||||
changeusruserid = json_body["userid"];
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
}
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in change user request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in change user request");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
if (json_body["userid"] != nullptr) {
|
||||
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"]; }
|
||||
if (json_body["group"] != nullptr) { all_user["group"] = json_body["group"]; }
|
||||
if (json_body["is_superuser"] != nullptr) { all_user["is_superuser"] = json_body["is_superuser"]; }
|
||||
found = true;
|
||||
std::string syslogstring = "userdata changed for user [" + std::string(all_user["username"]) + "]";
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
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::string syslogstring = "userdata changed for user [" + std::string(all_user["username"]) + "]";
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
response.set_content(R"({"status":"can't find this user"})", JSON_TYPE);
|
||||
syslog(LOG_ERR, "can't find this user in change user request");
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
||||
std::ofstream usersfilew(USERS_FILE);
|
||||
usersfilew << all_users.dump(4);
|
||||
usersfilew.close();
|
||||
response.set_content(R"({"status":"ok"})", JSON_TYPE);
|
||||
}
|
||||
}
|
||||
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::string syslogstring = "userdata changed for user [" + std::string(all_user["username"]) + "]";
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
response.set_content(R"({"status":"can't find this user"})", JSON_TYPE);
|
||||
syslog(LOG_ERR, "can't find this user in change user request");
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(usersfilemutex);
|
||||
std::ofstream usersfilew(USERS_FILE);
|
||||
usersfilew << all_users.dump(4);
|
||||
usersfilew.close();
|
||||
response.set_content(R"({"status":"ok"})", JSON_TYPE);
|
||||
}
|
||||
+81
-75
@@ -5,85 +5,91 @@
|
||||
#include <jwt.hpp>
|
||||
#include <syslog.h>
|
||||
|
||||
// function for delete user
|
||||
void delete_user(const httplib::Request& request, httplib::Response& response)
|
||||
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;
|
||||
}
|
||||
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 (!verify_auth(json_body["token"])) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in delete user request");
|
||||
return;
|
||||
}
|
||||
|
||||
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 delete user request");
|
||||
return;
|
||||
}
|
||||
nlohmann::json all_users;
|
||||
namespace Users
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
}
|
||||
std::string deluserid = userid;
|
||||
if (json_body["userid"] != nullptr) {
|
||||
deluserid = json_body["userid"];
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
// function for delete user
|
||||
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()) {
|
||||
response.set_content(
|
||||
DELETE_USER_REQUIRED_STRING,
|
||||
JSON_TYPE);
|
||||
syslog(LOG_ERR, "delete user request body is empty");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in delete user request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in delete user request");
|
||||
return;
|
||||
}
|
||||
}
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
|
||||
bool found = false;
|
||||
for (size_t i = 0; i < all_users.size(); i++) {
|
||||
if (all_users[i]["id"] == deluserid) {
|
||||
std::string syslogstring = "user deleted [" + std::string(all_users[i]["username"]) + "]";
|
||||
all_users.erase(i);
|
||||
found = true;
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
break;
|
||||
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;
|
||||
}
|
||||
|
||||
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 delete user request");
|
||||
return;
|
||||
}
|
||||
nlohmann::json all_users;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
}
|
||||
std::string deluserid = userid;
|
||||
if (json_body["userid"] != nullptr) {
|
||||
deluserid = json_body["userid"];
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
}
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in delete user request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in delete user request");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
for (size_t i = 0; i < all_users.size(); i++) {
|
||||
if (all_users[i]["id"] == deluserid) {
|
||||
std::string syslogstring = "user deleted [" + std::string(all_users[i]["username"]) + "]";
|
||||
all_users.erase(i);
|
||||
found = true;
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
response.set_content(R"({"status":"can't find this user"})", JSON_TYPE);
|
||||
syslog(LOG_ERR, "can't find this user in delete user request");
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
||||
std::ofstream usersfilew(USERS_FILE);
|
||||
usersfilew << all_users.dump(4);
|
||||
usersfilew.close();
|
||||
response.set_content(R"({"status":"ok"})", JSON_TYPE);
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
response.set_content(R"({"status":"can't find this user"})", JSON_TYPE);
|
||||
syslog(LOG_ERR, "can't find this user in delete user request");
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(usersfilemutex);
|
||||
std::ofstream usersfilew(USERS_FILE);
|
||||
usersfilew << all_users.dump(4);
|
||||
usersfilew.close();
|
||||
response.set_content(R"({"status":"ok"})", JSON_TYPE);
|
||||
}
|
||||
+52
-46
@@ -5,54 +5,60 @@
|
||||
#include <jwt.hpp>
|
||||
#include <syslog.h>
|
||||
|
||||
// function for get all users data without password
|
||||
void get_all_users(const httplib::Request& request, httplib::Response& response)
|
||||
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;
|
||||
}
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
namespace Users
|
||||
{
|
||||
// function for get all users data without password
|
||||
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()) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
syslog(LOG_ERR, "get all users request body is empty");
|
||||
return;
|
||||
}
|
||||
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 (!verify_auth(json_body["token"])) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get all user request");
|
||||
return;
|
||||
}
|
||||
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(usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
nlohmann::json all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
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;
|
||||
}
|
||||
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 response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
}
|
||||
user.erase("password");
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get all user request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get all user request");
|
||||
return;
|
||||
}
|
||||
response.set_content(all_users.dump(), JSON_TYPE);
|
||||
}
|
||||
user.erase("password");
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get all user request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get all user request");
|
||||
return;
|
||||
}
|
||||
response.set_content(all_users.dump(), JSON_TYPE);
|
||||
}
|
||||
+48
-42
@@ -5,50 +5,56 @@
|
||||
#include <jwt.hpp>
|
||||
#include <syslog.h>
|
||||
|
||||
// function for get user data without password
|
||||
void get_user(const httplib::Request& request, httplib::Response& response)
|
||||
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;
|
||||
}
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
namespace Users
|
||||
{
|
||||
// function for get user data without password
|
||||
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()) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
syslog(LOG_ERR, "get user request body is empty");
|
||||
return;
|
||||
}
|
||||
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 (!verify_auth(json_body["token"])) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get user request");
|
||||
return;
|
||||
}
|
||||
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(usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
nlohmann::json all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
break;
|
||||
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;
|
||||
}
|
||||
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 response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get user request");
|
||||
return;
|
||||
}
|
||||
response_user_data.erase("password");
|
||||
response.set_content(response_user_data.dump(), JSON_TYPE);
|
||||
}
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get user request");
|
||||
return;
|
||||
}
|
||||
response_user_data.erase("password");
|
||||
response.set_content(response_user_data.dump(), JSON_TYPE);
|
||||
}
|
||||
+20
-15
@@ -3,25 +3,30 @@
|
||||
|
||||
#include <httplib.h>
|
||||
|
||||
// function for get user data without password
|
||||
void get_user(const httplib::Request& request, httplib::Response& response);
|
||||
namespace AuthService
|
||||
{
|
||||
namespace Users
|
||||
{
|
||||
// function for get user data without password
|
||||
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);
|
||||
// function for get all users data without password
|
||||
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);
|
||||
// function for access or reject authorization
|
||||
void user_access(const httplib::Request& request, httplib::Response& response);
|
||||
|
||||
// function for add user
|
||||
void add_user(const httplib::Request& request, httplib::Response& response);
|
||||
// function for add user
|
||||
void add_user(const httplib::Request& request, httplib::Response& response);
|
||||
|
||||
// function for delete user
|
||||
void delete_user(const httplib::Request& request, httplib::Response& response);
|
||||
// function for delete user
|
||||
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);
|
||||
|
||||
// function for change user data
|
||||
void change_user(const httplib::Request& request, httplib::Response& response);
|
||||
// function for change password for user
|
||||
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);
|
||||
}
|
||||
}
|
||||
#endif // USERS_HPP
|
||||
Reference in new issue
Block a user