implemented CommonQueries

This commit is contained in:
2025-12-15 23:26:32 +05:00
parent 72696dea70
commit 49793e2929
29 changed files with 540 additions and 495 deletions

View File

@@ -22,8 +22,8 @@ declare_RequestHandler(Login)
}
// validate username
str username_str = str_null;
str name_error_str = validateUsername_cstr(req.username, &username_str);
str username = str_null;
str name_error_str = validateUsername_cstr(req.username, &username);
if(name_error_str.data){
Defer(str_destroy(name_error_str));
try_void(sendErrorMessage(log_ctx, conn, res_head,
@@ -31,42 +31,28 @@ declare_RequestHandler(Login)
Return RESULT_VOID;
}
// lock users cache
idb_lockTable(srv->users.table);
bool unlocked_users_cache_mutex = false;
Defer(
if(!unlocked_users_cache_mutex)
idb_unlockTable(srv->users.table)
);
// try get id from name cache
u64* id_ptr = HashMap_tryGetPtr(&srv->users.name_id_map, username_str);
if(id_ptr == NULL){
// get user by id
try(u64 user_id, i, User_getIdForUsername(conn->queries.common, username));
if(user_id == 0){
try_void(sendErrorMessage(log_ctx, conn, res_head,
LogSeverity_Warn, STR("Username is not registered") ));
Return RESULT_VOID;
}
u64 user_id = *id_ptr;
// get user by id
try_assert(user_id < srv->users.list.len);
UserInfo* u = srv->users.list.data + user_id;
// TODO: get user token
Array(u8) token = Array_u8_construct(req.token, sizeof(req.token));
try(bool authorized, i, User_tryAuthorize(conn->queries.common, user_id, token));
// validate token hash
if(memcmp(req.token, u->token, sizeof(req.token)) != 0){
if(!authorized){
try_void(sendErrorMessage(log_ctx, conn, res_head,
LogSeverity_Warn, STR("wrong password") ));
Return RESULT_VOID;
}
// manually unlock mutex
idb_unlockTable(srv->users.table);
unlocked_users_cache_mutex = true;
// authorize
conn->authorized = true;
conn->user_id = user_id;
logInfo("authorized user '%s'", username_str.data);
logInfo("authorized user '%s'", username.data);
// send response
LoginResponse res;