updated file database added get_all_users get_user_by_id
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Failing after 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Failing after 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 12s
get_user_by_username get_user_field_by_id
This commit is contained in:
1 parent
73a73976fe
commit
d14db43e9b
13 files changed
+158
-9
No files matched your search
@@ -11,6 +11,7 @@ include_directories(./src/lib/ ${SQLite3_INCLUDE_DIRS})
|
||||
FILE(GLOB_RECURSE SOURCES src/*.c)
|
||||
|
||||
option(USE_SQLITE3 "Enable SQLite3 support" OFF)
|
||||
option(USE_WERROR "Enable SQLite3 support" OFF)
|
||||
|
||||
if(USE_SQLITE3)
|
||||
find_package(SQLite3 REQUIRED)
|
||||
@@ -18,6 +19,10 @@ if(USE_SQLITE3)
|
||||
add_definitions(-DUSE_SQLITE3)
|
||||
endif()
|
||||
|
||||
if(USE_WERROR)
|
||||
add_definitions(-Werror)
|
||||
endif()
|
||||
|
||||
add_executable(
|
||||
auth_service
|
||||
${SOURCES}
|
||||
|
||||
@@ -9,12 +9,17 @@ BUILD_DIR=./build
|
||||
CROSS_COMPILE =
|
||||
LIBS = -lpthread -lcrypto -lssl
|
||||
USE_SQLITE3 ?= 0
|
||||
WERROR ?= 0
|
||||
|
||||
ifeq ($(USE_SQLITE3), 1)
|
||||
CFLAGS += -DUSE_SQLITE3
|
||||
LIBS += -lsqlite3
|
||||
endif
|
||||
|
||||
ifeq ($(WERROR), 1)
|
||||
CFLAGS += -Werror
|
||||
endif
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
|
||||
@@ -6,6 +6,7 @@ extra_c_link_args = ['-Wl,--gc-sections']
|
||||
|
||||
sqlite3_dep = dependency('sqlite3', required: false)
|
||||
use_sqlite3 = get_option('use_sqlite3')
|
||||
use_werror = get_option('use_werror')
|
||||
|
||||
if use_sqlite3
|
||||
if not sqlite3_dep.found()
|
||||
@@ -15,6 +16,10 @@ if use_sqlite3
|
||||
add_project_arguments('-DUSE_SQLITE3', language: 'c')
|
||||
endif
|
||||
|
||||
if use_werror
|
||||
add_project_arguments('-Werror', language: 'c')
|
||||
endif
|
||||
|
||||
executable(
|
||||
'auth_service',
|
||||
'src/main.c',
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
option('use_sqlite3', type: 'boolean', value: false, description: 'Enable SQLite3 support')
|
||||
option('use_werror', type: 'boolean', value: false, description: 'Build with warning as errors')
|
||||
@@ -10,6 +10,9 @@
|
||||
#ifdef USE_SQLITE3
|
||||
sqlite3 *db = nullptr;
|
||||
pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
#else
|
||||
FILE *users_file = nullptr;
|
||||
pthread_mutex_t users_file_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
#endif
|
||||
|
||||
void init_db() {
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
#include <sqlite3.h>
|
||||
extern sqlite3 *db;
|
||||
extern pthread_mutex_t db_mutex;
|
||||
#else
|
||||
#include <stdio.h>
|
||||
extern FILE *users_file;
|
||||
extern pthread_mutex_t users_file_mutex;
|
||||
#endif
|
||||
|
||||
void init_db();
|
||||
|
||||
@@ -4,6 +4,10 @@
|
||||
#include "sql.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifndef USE_SQLITE3
|
||||
#include "file.h"
|
||||
#endif
|
||||
|
||||
char *db_get_all_users() {
|
||||
#ifdef USE_SQLITE3
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
@@ -99,6 +103,9 @@ char *db_get_all_users() {
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return result;
|
||||
#else
|
||||
return "";
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return data;
|
||||
#endif
|
||||
}
|
||||
+29
-2
@@ -2,6 +2,10 @@
|
||||
#include "sql.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifndef USE_SQLITE3
|
||||
#include "file.h"
|
||||
#endif
|
||||
|
||||
User *db_get_user_by_id(const int id) {
|
||||
#ifdef USE_SQLITE3
|
||||
User *user = nullptr;
|
||||
@@ -43,7 +47,30 @@ User *db_get_user_by_id(const int id) {
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return user;
|
||||
#else
|
||||
User *asdf = new_user(0, "asdf", "asdf", "asdf", "asdf", false);
|
||||
return asdf;
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
User *found = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser");
|
||||
if (user_id->valueint == id) {
|
||||
found = new_user(user_id->valueint, user_username->valuestring,
|
||||
user_password->valuestring, user_email->valuestring,
|
||||
user_group->valuestring, user_is_superuser->valueint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return found;
|
||||
#endif
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
#include "cJSON.h"
|
||||
#include "database.h"
|
||||
#include "sql.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifndef USE_SQLITE3
|
||||
#include "file.h"
|
||||
#endif
|
||||
|
||||
User *db_get_user_by_username(const char *username) {
|
||||
#ifdef USE_SQLITE3
|
||||
User *user = nullptr;
|
||||
@@ -43,7 +48,30 @@ User *db_get_user_by_username(const char *username) {
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return user;
|
||||
#else
|
||||
User *asdf = new_user(0, "asdf", "asdf", "asdf", "asdf", false);
|
||||
return asdf;
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
User *found = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser");
|
||||
if (!strcmp(user_username->valuestring, username)) {
|
||||
found = new_user(user_id->valueint, user_username->valuestring,
|
||||
user_password->valuestring, user_email->valuestring,
|
||||
user_group->valuestring, user_is_superuser->valueint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return found;
|
||||
#endif
|
||||
}
|
||||
@@ -4,6 +4,10 @@
|
||||
#include "sql.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifndef USE_SQLITE3
|
||||
#include "file.h"
|
||||
#endif
|
||||
|
||||
char *db_get_user_field_by_id(const int id, const char *field) {
|
||||
#ifdef USE_SQLITE3
|
||||
char *password = nullptr;
|
||||
@@ -45,6 +49,30 @@ char *db_get_user_field_by_id(const int id, const char *field) {
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return password;
|
||||
#else
|
||||
return "";
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
User *found = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser");
|
||||
if (user_id->valueint == id) {
|
||||
found = new_user(user_id->valueint, user_username->valuestring,
|
||||
user_password->valuestring, user_email->valuestring,
|
||||
user_group->valuestring, user_is_superuser->valueint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return found->password;
|
||||
#endif
|
||||
}
|
||||
+19
-2
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *read_file(const char *path) {
|
||||
FILE *fp = fopen(path, "rb");
|
||||
@@ -14,7 +15,7 @@ char *read_file(const char *path) {
|
||||
long file_size = ftell(fp);
|
||||
rewind(fp);
|
||||
|
||||
char *buffer = (char *)malloc(file_size + 1);
|
||||
char *buffer = (char *) malloc(file_size + 1);
|
||||
if (!buffer) {
|
||||
perror("Error allocating memory");
|
||||
fclose(fp);
|
||||
@@ -32,4 +33,20 @@ char *read_file(const char *path) {
|
||||
buffer[file_size] = '\0';
|
||||
fclose(fp);
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
char *write_file(const char *path, const char *data) {
|
||||
FILE *fp = fopen(path, "w");
|
||||
if (fp == NULL) {
|
||||
printf("%s\n", path);
|
||||
perror("Error opening file");
|
||||
return nullptr;
|
||||
}
|
||||
if (fprintf(fp, "%s", data) < 0) {
|
||||
perror("Error write file");
|
||||
return nullptr;
|
||||
}
|
||||
fclose(fp);
|
||||
char *ret = strdup(data);
|
||||
return ret;
|
||||
}
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
char *read_file(const char *path);
|
||||
char *read_file(const char *path);
|
||||
char *write_file(const char *path, const char *data);
|
||||
@@ -0,0 +1,18 @@
|
||||
[
|
||||
{
|
||||
"id": 0,
|
||||
"username": "tola",
|
||||
"email": "mail@example.com",
|
||||
"password": "e0c7bfa5b8e4ae0cecfc1f14093a71e05c27abd93a3850fc35f008c3baf866b4",
|
||||
"group": "root",
|
||||
"is_superuser": true
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"username": "lisa",
|
||||
"email": "lisa@example.com",
|
||||
"password": "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b",
|
||||
"group": "users",
|
||||
"is_superuser": false
|
||||
}
|
||||
]
|
||||
Reference in new issue
Block a user