mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-28 08:44:06 +02:00
add token
This commit is contained in:
parent
9b1784ba3d
commit
81ec1a6469
@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
void route_post_auth_login(HttpCtx* ctx)
|
void route_post_auth_login(HttpCtx* ctx)
|
||||||
{
|
{
|
||||||
|
|
||||||
Cx* cx = http_ctx_user_ctx(ctx);
|
Cx* cx = http_ctx_user_ctx(ctx);
|
||||||
|
|
||||||
const char* body_str = http_ctx_req_body(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");
|
RESPOND_BAD_REQUEST(ctx, "bad request");
|
||||||
goto l0_return;
|
goto l0_return;
|
||||||
}
|
}
|
||||||
if (strlen(req.email) == 0
|
if (strlen(req.email) == 0 || strlen(req.password) > MAX_HASH_INPUT_LEN) {
|
||||||
|| strlen(req.password) > MAX_HASH_INPUT_LEN) {
|
|
||||||
|
|
||||||
RESPOND_BAD_REQUEST(ctx, "bad request");
|
RESPOND_BAD_REQUEST(ctx, "bad request");
|
||||||
goto l0_return;
|
goto l0_return;
|
||||||
@ -33,8 +31,7 @@ void route_post_auth_login(HttpCtx* ctx)
|
|||||||
if (db_res == DbRes_NotFound) {
|
if (db_res == DbRes_NotFound) {
|
||||||
RESPOND_BAD_REQUEST(ctx, "user with email not found");
|
RESPOND_BAD_REQUEST(ctx, "user with email not found");
|
||||||
goto l0_return;
|
goto l0_return;
|
||||||
}
|
} else if (db_res == DbRes_Error) {
|
||||||
else if (db_res == DbRes_Error) {
|
|
||||||
RESPOND_SERVER_ERROR(ctx);
|
RESPOND_SERVER_ERROR(ctx);
|
||||||
goto l0_return;
|
goto l0_return;
|
||||||
}
|
}
|
||||||
@ -44,9 +41,11 @@ void route_post_auth_login(HttpCtx* ctx)
|
|||||||
goto l2_return;
|
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:
|
l2_return:
|
||||||
user_destroy(&user);
|
user_destroy(&user);
|
||||||
l0_return:
|
l0_return:
|
||||||
|
@ -8,15 +8,18 @@
|
|||||||
#include "str_util.h"
|
#include "str_util.h"
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
void test(void);
|
void test(void);
|
||||||
|
|
||||||
|
|
||||||
HttpServer* server;
|
HttpServer* server;
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
srand((unsigned int)time(NULL));
|
||||||
|
|
||||||
#ifdef RUN_TESTS
|
#ifdef RUN_TESTS
|
||||||
test();
|
test();
|
||||||
#endif
|
#endif
|
||||||
|
28
backend/src/session.c
Normal file
28
backend/src/session.c
Normal 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 });
|
||||||
|
}
|
@ -1,10 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include "collection.h"
|
#include "collection.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int64_t user_id;
|
int64_t user_id;
|
||||||
|
char* token;
|
||||||
} Session;
|
} Session;
|
||||||
|
|
||||||
|
void session_destroy(Session* session);
|
||||||
|
|
||||||
DEFINE_VEC(Session, SessionVec, session_vec, 16)
|
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);
|
||||||
|
@ -164,7 +164,26 @@ bool str_hash_equal(const char* hash, const char* input)
|
|||||||
return hashdata_is_equal(data, input);
|
return hashdata_is_equal(data, input);
|
||||||
}
|
}
|
||||||
|
|
||||||
void str_util_test(void) {
|
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");
|
char* hash = str_hash("1234");
|
||||||
if (!str_hash_equal(hash, "1234")) {
|
if (!str_hash_equal(hash, "1234")) {
|
||||||
PANIC("hash should be equal");
|
PANIC("hash should be equal");
|
||||||
@ -174,3 +193,13 @@ void str_util_test(void) {
|
|||||||
}
|
}
|
||||||
free(hash);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -38,4 +38,6 @@ DEFINE_VEC(char*, RawStrVec, rawstr_vec, 8)
|
|||||||
char* str_hash(const char* input);
|
char* str_hash(const char* input);
|
||||||
bool str_hash_equal(const char* 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);
|
void str_util_test(void);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user