diff --git a/include/db/auth.h b/include/db/auth.h index b696041..7db5f35 100644 --- a/include/db/auth.h +++ b/include/db/auth.h @@ -19,11 +19,17 @@ extern "C" int registe(const char* user_id, const char* password); int delete_user(const char* user_id); - + int get_user_permission(const char* user_id, int* result); - + int set_user_permission(const char* user_id, int permission); + int set_admin_password_hash(const char* hash); + + int get_admin_password_hash(char** result); + + int has_admin_password_hash(int* result); + #ifdef __cplusplus } #endif diff --git a/include/hash/hash.h b/include/hash/hash.h new file mode 100644 index 0000000..d6cf7da --- /dev/null +++ b/include/hash/hash.h @@ -0,0 +1,21 @@ +#ifndef JWT_H +#define JWT_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + char* kqm_generate_hash(const char* password, size_t rounds); + + int kqm_validate_password(const char* password, const char* hash); + + char* kqm_random_password(size_t length); + +#ifdef __cplusplus +} +#endif + +#endif // JWT_H diff --git a/include/hash/hash.hpp b/include/hash/hash.hpp index fcc029b..51d912e 100644 --- a/include/hash/hash.hpp +++ b/include/hash/hash.hpp @@ -5,6 +5,9 @@ #include auto generate_hash(const std::string_view password, std::size_t rounds) -> std::string; + auto validate_password(const std::string_view password, const std::string_view hash) -> bool; +auto random_password(std::size_t length) -> std::string; + #endif // HASH_HPP \ No newline at end of file diff --git a/src/db/auth.cpp b/src/db/auth.cpp index a855b3a..9f43e2b 100644 --- a/src/db/auth.cpp +++ b/src/db/auth.cpp @@ -149,4 +149,42 @@ extern "C" } return 1; } + + int set_admin_password_hash(const char* hash) + { + auto status = user_db->Put(leveldb::WriteOptions{}, "admin_password_hash", hash); + if (!status.ok()) { + std::println(stderr, "Failed to set admin password hash: {}", status.ToString()); + return 0; + } + return 1; + } + + int get_admin_password_hash(char** result) + { + auto value = std::string{}; + auto status = user_db->Get(leveldb::ReadOptions{}, "admin_password_hash", &value); + if (status.ok()) { + *result = strdup(value.c_str()); + return 1; + } else { + std::println(stderr, "Failed to get admin password hash: {}", status.ToString()); + return 0; + } + } + + int has_admin_password_hash(int* result) + { + auto value = std::string{}; + auto status = user_db->Get(leveldb::ReadOptions{}, "admin_password_hash", &value); + if (status.ok()) { + *result = 1; + } else if (status.IsNotFound()) { + *result = 0; + } else { + std::println(stderr, "Failed to check admin password hash existence: {}", status.ToString()); + return 0; + } + return 1; + } } \ No newline at end of file diff --git a/src/hash/hash.cpp b/src/hash/hash.cpp index 59510c2..53da492 100644 --- a/src/hash/hash.cpp +++ b/src/hash/hash.cpp @@ -1,4 +1,5 @@ -#include +#include "hash.hpp" +#include "hash.h" #include #include @@ -11,6 +12,7 @@ #include #include +#include #include using namespace CryptoPP; @@ -89,3 +91,37 @@ auto validate_password(const std::string_view password, const std::string_view h return entered_hashed_password == stored_hashed_password; } +auto random_password(std::size_t length) -> std::string +{ + static const auto chars = std::string_view{"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"}; + + auto rd = std::random_device{}; + auto gen = std::mt19937{rd()}; + auto dis = std::uniform_int_distribution{std::size_t{0}, chars.size() - 1}; + + std::string password; + for (size_t i = 0; i < length; ++i) { + password += chars[dis(gen)]; + } + + return password; +} + + +extern "C" +{ + char* kqm_generate_hash(const char* password, size_t rounds) + { + return strdup(generate_hash(password, rounds).c_str()); + } + + int kqm_validate_password(const char* password, const char* hash) + { + return validate_password(password, hash); + } + + char* kqm_random_password(size_t length) + { + return strdup(random_password(length).c_str()); + } +} diff --git a/src/server/auth/auth.c b/src/server/auth/auth.c index 74a21fe..25faddc 100644 --- a/src/server/auth/auth.c +++ b/src/server/auth/auth.c @@ -1,5 +1,6 @@ -#include +#include "server/auth.h" #include char* secret = NULL; +char* admin_password_hash = NULL;