Pull out libusers

This commit is contained in:
2026-06-17 17:34:07 +02:00
parent 8aab365e14
commit 05f318233b
5 changed files with 46 additions and 30 deletions

View File

@@ -1,14 +1,13 @@
#include <microhttpd.h>
#include <sha256.h>
#include <uuid/uuid.h>
#include <dirent.h>
#include "path.h"
#include "users.h"
#include "noh.h"
Noh_Arena temp;
#include "users.c"
#define SV Noh_String_View
#define sv noh_sv_from_cstr
@@ -67,7 +66,7 @@ static bool authorize_user(struct MHD_Connection *connection, SV *username, Sett
}
// Hash the provided password.
hashed = hash_password(provided_username, provided_password);
hashed = hash_password(&temp, provided_username, provided_password);
// Check the hash.
if (!noh_sv_eq(noh_sv_from_string(hashed), user->hash)) {

View File

@@ -2,12 +2,11 @@
#include <dirent.h>
#include "path.h"
#include "users.h"
#include "noh.h"
Noh_Arena temp;
#include "users.c"
#define SV Noh_String_View
#define sv noh_sv_from_cstr
@@ -96,7 +95,7 @@ bool register_user(Noh_String_View library_path, Noh_String_View username, Noh_S
if (!read_users_file(users_file_path.elems, &users_file, &users)) noh_return_defer(false);
// Hash the password.
hashed = hash_password(username, password);
hashed = hash_password(&temp, username, password);
// Find the user in the map of users.
for (size_t i = 0; i < users.count; i++) {

View File

@@ -1,17 +1,8 @@
#include "users.h"
#include <sha256.h>
typedef struct {
Noh_String_View username;
Noh_String_View hash;
} User;
typedef struct {
User *elems;
size_t count;
size_t capacity;
} Users;
Noh_String hash_password(Noh_String_View username, Noh_String_View password) {
Noh_String hash_password(Noh_Arena *temp, Noh_String_View username, Noh_String_View password) {
// Build the sha input string.
Noh_String input = {0};
noh_string_append_sv(&input, username);
@@ -26,19 +17,16 @@ Noh_String hash_password(Noh_String_View username, Noh_String_View password) {
sha256_final(&ctx, hash);
// Build the result string.
noh_arena_save(&temp);
noh_arena_save(temp);
Noh_String result = {0};
for (int i = 0; i < 32; i++) {
noh_string_append_cstr(&result, noh_arena_sprintf(&temp, "%02x", hash[i]));
noh_string_append_cstr(&result, noh_arena_sprintf(temp, "%02x", hash[i]));
}
noh_arena_reset(&temp);
noh_arena_reset(temp);
return result;
}
// Reads the users file into users_file. Free this string when the data is no longer needed.
// Note that the string needs to be initialized and empty before calling this function.
// The users struct will contain string views referencing the data in this string.
bool read_users_file(char *file_path, Noh_String *users_file, Users *users) {
noh_log(NOH_INFO, "Reading users file: %s", file_path);
if (!noh_file_exists(file_path)) return true;

26
src/users.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef USERS_H
#define USERS_H
#include "noh.h"
typedef struct {
Noh_String_View username;
Noh_String_View hash;
} User;
typedef struct {
User *elems;
size_t count;
size_t capacity;
} Users;
Noh_String hash_password(Noh_Arena *temp, Noh_String_View username, Noh_String_View password);
// Reads the users file into users_file. Free this string when the data is no longer needed.
// Note that the string needs to be initialized and empty before calling this function.
// The users struct will contain string views referencing the data in this string.
bool read_users_file(char *file_path, Noh_String *users_file, Users *users);
bool write_users_file(char *file_path, Users *users, User *user_to_skip);
#endif // USERS_H