add token

This commit is contained in:
SimonFJ20 2025-03-12 11:01:09 +01:00
parent 9b1784ba3d
commit 81ec1a6469
6 changed files with 86 additions and 19 deletions

View File

@ -6,7 +6,6 @@
void route_post_auth_login(HttpCtx* ctx)
{
Cx* cx = http_ctx_user_ctx(ctx);
const char* body_str = http_ctx_req_body(ctx);
@ -21,8 +20,7 @@ void route_post_auth_login(HttpCtx* ctx)
RESPOND_BAD_REQUEST(ctx, "bad request");
goto l0_return;
}
if (strlen(req.email) == 0
|| strlen(req.password) > MAX_HASH_INPUT_LEN) {
if (strlen(req.email) == 0 || strlen(req.password) > MAX_HASH_INPUT_LEN) {
RESPOND_BAD_REQUEST(ctx, "bad request");
goto l0_return;
@ -33,8 +31,7 @@ void route_post_auth_login(HttpCtx* ctx)
if (db_res == DbRes_NotFound) {
RESPOND_BAD_REQUEST(ctx, "user with email not found");
goto l0_return;
}
else if (db_res == DbRes_Error) {
} else if (db_res == DbRes_Error) {
RESPOND_SERVER_ERROR(ctx);
goto l0_return;
}
@ -44,9 +41,11 @@ void route_post_auth_login(HttpCtx* ctx)
goto l2_return;
}
session_vec_push(&cx->sessions, (Session) {.user_id = user.id});
session_vec_remove_user_id(&cx->sessions, user.id);
char* token = str_random(64);
session_vec_add(&cx->sessions, user.id, token);
RESPOND_JSON(ctx, 200, "{\"ok\":true}");
RESPOND_JSON(ctx, 200, "{\"ok\":true,\"token\":\"%s\"}", token);
l2_return:
user_destroy(&user);
l0_return:

View File

@ -8,18 +8,21 @@
#include "str_util.h"
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void test(void);
HttpServer* server;
int main(void)
{
#ifdef RUN_TESTS
srand((unsigned int)time(NULL));
#ifdef RUN_TESTS
test();
#endif
#endif
Db* db = db_sqlite_new();

28
backend/src/session.c Normal file
View File

@ -0,0 +1,28 @@
#include "session.h"
#include <stdlib.h>
void session_destroy(Session* session)
{
free(session->token);
}
void session_vec_remove_user_id(SessionVec* vec, int64_t user_id)
{
for (size_t i = 0; i < vec->size; ++i) {
if (vec->data[i].user_id == user_id) {
session_destroy(&vec->data[i]);
vec->data[i] = (Session) { 0, NULL };
}
}
}
void session_vec_add(SessionVec* vec, int64_t user_id, char* token)
{
for (size_t i = 0; i < vec->size; ++i) {
if (vec->data[i].user_id == 0) {
vec->data[i] = (Session) { user_id, token };
return;
}
}
session_vec_push(vec, (Session) { user_id, token });
}

View File

@ -1,10 +1,16 @@
#pragma once
#include <stdint.h>
#include "collection.h"
#include <stdint.h>
typedef struct {
int64_t user_id;
char* token;
} Session;
void session_destroy(Session* session);
DEFINE_VEC(Session, SessionVec, session_vec, 16)
void session_vec_remove_user_id(SessionVec* vec, int64_t user_id);
void session_vec_add(SessionVec* vec, int64_t user_id, char* token);

View File

@ -164,13 +164,42 @@ bool str_hash_equal(const char* hash, const char* input)
return hashdata_is_equal(data, input);
}
void str_util_test(void) {
char* hash = str_hash("1234");
if (!str_hash_equal(hash, "1234")) {
PANIC("hash should be equal");
char* str_random(size_t length)
{
char* string = calloc(length + 1, sizeof(char));
size_t string_i = 0;
for (size_t i = 0; i < length; ++i) {
int r = rand() % (10 + 26 + 26);
if (r < 10) {
string[string_i++] = (char)r + '0';
} else if (r < 10 + 26) {
string[string_i++] = (char)(r - 10) + 'A';
} else {
string[string_i++] = (char)(r - 10 - 26) + 'a';
}
}
return string;
}
void str_util_test(void)
{
{
char* hash = str_hash("1234");
if (!str_hash_equal(hash, "1234")) {
PANIC("hash should be equal");
}
if (str_hash_equal(hash, "4321")) {
PANIC("hash should not be equal");
}
free(hash);
}
{
char* token_1 = str_random(16);
char* token_2 = str_random(16);
if (strcmp(token_1, token_2) == 0) {
PANIC("tokens should not be equal");
}
free(token_1);
free(token_2);
}
if (str_hash_equal(hash, "4321")) {
PANIC("hash should not be equal");
}
free(hash);
}

View File

@ -38,4 +38,6 @@ DEFINE_VEC(char*, RawStrVec, rawstr_vec, 8)
char* str_hash(const char* input);
bool str_hash_equal(const char* hash, const char* input);
char* str_random(size_t length);
void str_util_test(void);