mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-27 16:24:07 +02:00
rename auth -> session
This commit is contained in:
parent
3978ed8204
commit
f98cce2747
@ -2,10 +2,24 @@
|
||||
|
||||
#include "db.h"
|
||||
#include "http_server.h"
|
||||
#include "session.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
int64_t user_id;
|
||||
char* token;
|
||||
size_t token_hash;
|
||||
} Session;
|
||||
|
||||
void session_construct(Session* session, int64_t user_id);
|
||||
void session_destroy(Session* session);
|
||||
|
||||
DEFINE_VEC(Session, SessionVec, session_vec, 16)
|
||||
|
||||
void sessions_remove(SessionVec* vec, int64_t user_id);
|
||||
Session* sessions_add(SessionVec* vec, int64_t user_id);
|
||||
const Session* sessions_find(SessionVec* vec, const char* token);
|
||||
|
||||
typedef struct {
|
||||
int number;
|
||||
SessionVec sessions;
|
||||
@ -18,10 +32,10 @@ void route_get_not_found(HttpCtx* ctx);
|
||||
|
||||
void route_get_products_all(HttpCtx* ctx);
|
||||
|
||||
void route_post_user_register(HttpCtx* ctx);
|
||||
void route_post_users_register(HttpCtx* ctx);
|
||||
|
||||
void route_post_auth_login(HttpCtx* ctx);
|
||||
void route_post_auth_logout(HttpCtx* ctx);
|
||||
void route_post_sessions_login(HttpCtx* ctx);
|
||||
void route_post_sessions_logout(HttpCtx* ctx);
|
||||
|
||||
const Session* header_session(HttpCtx* ctx);
|
||||
const Session* middleware_session(HttpCtx* ctx);
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "../str_util.h"
|
||||
#include <string.h>
|
||||
|
||||
void route_post_auth_login(HttpCtx* ctx)
|
||||
void route_post_sessions_login(HttpCtx* ctx)
|
||||
{
|
||||
Cx* cx = http_ctx_user_ctx(ctx);
|
||||
|
||||
@ -51,7 +51,7 @@ l0_return:
|
||||
auth_login_req_destroy(&req);
|
||||
}
|
||||
|
||||
void route_post_auth_logout(HttpCtx* ctx)
|
||||
void route_post_sessions_logout(HttpCtx* ctx)
|
||||
{
|
||||
Cx* cx = http_ctx_user_ctx(ctx);
|
||||
const Session* session = header_session(ctx);
|
||||
@ -84,3 +84,54 @@ const Session* middleware_session(HttpCtx* ctx)
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
void session_construct(Session* session, int64_t user_id)
|
||||
{
|
||||
char* token = str_random(64);
|
||||
size_t token_hash = str_fast_hash(token);
|
||||
*session = (Session) { user_id, token, token_hash };
|
||||
}
|
||||
|
||||
void session_destroy(Session* session)
|
||||
{
|
||||
free(session->token);
|
||||
*session = (Session) {
|
||||
.user_id = 0,
|
||||
.token = NULL,
|
||||
.token_hash = 0,
|
||||
};
|
||||
}
|
||||
|
||||
void sessions_remove(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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Session* sessions_add(SessionVec* vec, int64_t user_id)
|
||||
{
|
||||
for (size_t i = 0; i < vec->size; ++i) {
|
||||
if (vec->data[i].user_id == 0) {
|
||||
session_construct(&vec->data[i], user_id);
|
||||
return &vec->data[i];
|
||||
}
|
||||
}
|
||||
Session session;
|
||||
session_construct(&session, user_id);
|
||||
session_vec_push(vec, session);
|
||||
return &vec->data[vec->size - 1];
|
||||
}
|
||||
|
||||
const Session* sessions_find(SessionVec* vec, const char* token)
|
||||
{
|
||||
size_t token_hash = str_fast_hash(token);
|
||||
for (size_t i = 0; i < vec->size; ++i) {
|
||||
if (vec->data[i].token_hash == token_hash) {
|
||||
return &vec->data[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
#include "../str_util.h"
|
||||
#include <string.h>
|
||||
|
||||
void route_post_user_register(HttpCtx* ctx)
|
||||
void route_post_users_register(HttpCtx* ctx)
|
||||
{
|
||||
Cx* cx = http_ctx_user_ctx(ctx);
|
||||
|
||||
|
@ -4,7 +4,6 @@
|
||||
#include "json.h"
|
||||
#include "models.h"
|
||||
#include "models_json.h"
|
||||
#include "session.h"
|
||||
#include "str_util.h"
|
||||
#include <sqlite3.h>
|
||||
#include <stdio.h>
|
||||
@ -44,9 +43,10 @@ int main(void)
|
||||
|
||||
http_server_get(server, "/api/products/all", route_get_products_all);
|
||||
|
||||
http_server_post(server, "/api/users/register", route_post_user_register);
|
||||
http_server_post(server, "/api/auth/login", route_post_auth_login);
|
||||
http_server_post(server, "/api/auth/logout", route_post_auth_logout);
|
||||
http_server_post(server, "/api/users/register", route_post_users_register);
|
||||
http_server_post(server, "/api/sessions/login", route_post_sessions_login);
|
||||
http_server_post(
|
||||
server, "/api/sessions/logout", route_post_sessions_logout);
|
||||
|
||||
http_server_get(server, "/", route_get_index);
|
||||
http_server_post(server, "/set_number", route_post_set_number);
|
||||
|
@ -1,54 +0,0 @@
|
||||
#include "session.h"
|
||||
#include "str_util.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void session_construct(Session* session, int64_t user_id)
|
||||
{
|
||||
char* token = str_random(64);
|
||||
size_t token_hash = str_fast_hash(token);
|
||||
*session = (Session) { user_id, token, token_hash };
|
||||
}
|
||||
|
||||
void session_destroy(Session* session)
|
||||
{
|
||||
free(session->token);
|
||||
*session = (Session) {
|
||||
.user_id = 0,
|
||||
.token = NULL,
|
||||
.token_hash = 0,
|
||||
};
|
||||
}
|
||||
|
||||
void sessions_remove(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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Session* sessions_add(SessionVec* vec, int64_t user_id)
|
||||
{
|
||||
for (size_t i = 0; i < vec->size; ++i) {
|
||||
if (vec->data[i].user_id == 0) {
|
||||
session_construct(&vec->data[i], user_id);
|
||||
return &vec->data[i];
|
||||
}
|
||||
}
|
||||
Session session;
|
||||
session_construct(&session, user_id);
|
||||
session_vec_push(vec, session);
|
||||
return &vec->data[vec->size - 1];
|
||||
}
|
||||
|
||||
const Session* sessions_find(SessionVec* vec, const char* token)
|
||||
{
|
||||
size_t token_hash = str_fast_hash(token);
|
||||
for (size_t i = 0; i < vec->size; ++i) {
|
||||
if (vec->data[i].token_hash == token_hash) {
|
||||
return &vec->data[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "collection.h"
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
int64_t user_id;
|
||||
char* token;
|
||||
size_t token_hash;
|
||||
} Session;
|
||||
|
||||
void session_construct(Session* session, int64_t user_id);
|
||||
void session_destroy(Session* session);
|
||||
|
||||
DEFINE_VEC(Session, SessionVec, session_vec, 16)
|
||||
|
||||
void sessions_remove(SessionVec* vec, int64_t user_id);
|
||||
Session* sessions_add(SessionVec* vec, int64_t user_id);
|
||||
const Session* sessions_find(SessionVec* vec, const char* token);
|
Loading…
x
Reference in New Issue
Block a user