updated format
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 10s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 12s
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 10s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 12s
This commit is contained in:
1 parent
d6409140f1
commit
6c556624bd
23 files changed
+147
-113
No files matched your search
@@ -1,9 +1,11 @@
|
||||
#include "ConfigManager.h"
|
||||
|
||||
#include <cJSON.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "file.h"
|
||||
|
||||
typedef struct ConfigManager {
|
||||
|
||||
+3
-5
@@ -33,10 +33,9 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
return;
|
||||
}
|
||||
const bool user_is_superuser = is_superuser->valueint ? true : false;
|
||||
User *new_db_user = db_insert_user(
|
||||
username->valuestring, email->valuestring, password->valuestring,
|
||||
group->valuestring,
|
||||
&user_is_superuser);
|
||||
User *new_db_user = db_insert_user(username->valuestring, email->valuestring,
|
||||
password->valuestring, group->valuestring,
|
||||
&user_is_superuser);
|
||||
if (!new_db_user) {
|
||||
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("failed to create user"));
|
||||
@@ -50,7 +49,6 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
username->valuestring, email->valuestring, group->valuestring,
|
||||
is_superuser->valueint ? "true" : "false");
|
||||
|
||||
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
||||
cJSON_Delete(payload);
|
||||
}
|
||||
+1
-1
@@ -3,5 +3,5 @@
|
||||
ConfigManager *config = nullptr;
|
||||
|
||||
void init_config(const char *filename) {
|
||||
config = cm_new_config_from_file(filename);
|
||||
config = cm_new_config_from_file(filename);
|
||||
}
|
||||
+6
-1
@@ -1,5 +1,10 @@
|
||||
#include "cors.h"
|
||||
|
||||
void set_cors_headers(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
mg_http_reply(c, 200, "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: GET,POST,OPTIONS,PUT,DELETE\r\nAccess-Control-Allow-Headers: Authorization,X-Custom,Content-Type", "");
|
||||
mg_http_reply(
|
||||
c, 200,
|
||||
"Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: "
|
||||
"GET,POST,OPTIONS,PUT,DELETE\r\nAccess-Control-Allow-Headers: "
|
||||
"Authorization,X-Custom,Content-Type",
|
||||
"");
|
||||
}
|
||||
+54
-30
@@ -1,15 +1,18 @@
|
||||
#include "database.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "sql.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "sql.h"
|
||||
|
||||
sqlite3* db = nullptr;
|
||||
pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
void init_db() {
|
||||
if (sqlite3_open(cm_get_parameter_as_string(config, "database"), &db) != SQLITE_OK) {
|
||||
if (sqlite3_open(cm_get_parameter_as_string(config, "database"), &db) !=
|
||||
SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_close(db);
|
||||
exit(1);
|
||||
@@ -17,7 +20,7 @@ void init_db() {
|
||||
}
|
||||
|
||||
void migrate_db() {
|
||||
char *err_msg = 0;
|
||||
char* err_msg = 0;
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (sqlite3_exec(db, CREATE_TABLE_USERS, nullptr, nullptr, &err_msg) !=
|
||||
SQLITE_OK) {
|
||||
@@ -31,33 +34,54 @@ void migrate_db() {
|
||||
|
||||
// Вспомогательная функция для экранирования специальных символов в JSON
|
||||
char* json_escape(const char* str) {
|
||||
if (!str) return NULL;
|
||||
|
||||
size_t len = strlen(str);
|
||||
// Выделяем память с запасом (каждый символ может быть экранирован)
|
||||
char* escaped = malloc(len * 6 + 1);
|
||||
if (!escaped) return NULL;
|
||||
|
||||
char* p = escaped;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
switch (str[i]) {
|
||||
case '\"': strcpy(p, "\\\""); p += 2; break;
|
||||
case '\\': strcpy(p, "\\\\"); p += 2; break;
|
||||
case '\b': strcpy(p, "\\b"); p += 2; break;
|
||||
case '\f': strcpy(p, "\\f"); p += 2; break;
|
||||
case '\n': strcpy(p, "\\n"); p += 2; break;
|
||||
case '\r': strcpy(p, "\\r"); p += 2; break;
|
||||
case '\t': strcpy(p, "\\t"); p += 2; break;
|
||||
default:
|
||||
if (str[i] < 0x20) {
|
||||
sprintf(p, "\\u%04x", (unsigned char)str[i]);
|
||||
p += 6;
|
||||
} else {
|
||||
*p++ = str[i];
|
||||
}
|
||||
if (!str) return NULL;
|
||||
|
||||
size_t len = strlen(str);
|
||||
// Выделяем память с запасом (каждый символ может быть экранирован)
|
||||
char* escaped = malloc(len * 6 + 1);
|
||||
if (!escaped) return NULL;
|
||||
|
||||
char* p = escaped;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
switch (str[i]) {
|
||||
case '\"':
|
||||
strcpy(p, "\\\"");
|
||||
p += 2;
|
||||
break;
|
||||
case '\\':
|
||||
strcpy(p, "\\\\");
|
||||
p += 2;
|
||||
break;
|
||||
case '\b':
|
||||
strcpy(p, "\\b");
|
||||
p += 2;
|
||||
break;
|
||||
case '\f':
|
||||
strcpy(p, "\\f");
|
||||
p += 2;
|
||||
break;
|
||||
case '\n':
|
||||
strcpy(p, "\\n");
|
||||
p += 2;
|
||||
break;
|
||||
case '\r':
|
||||
strcpy(p, "\\r");
|
||||
p += 2;
|
||||
break;
|
||||
case '\t':
|
||||
strcpy(p, "\\t");
|
||||
p += 2;
|
||||
break;
|
||||
default:
|
||||
if (str[i] < 0x20) {
|
||||
sprintf(p, "\\u%04x", (unsigned char)str[i]);
|
||||
p += 6;
|
||||
} else {
|
||||
*p++ = str[i];
|
||||
}
|
||||
}
|
||||
*p = '\0';
|
||||
|
||||
return escaped;
|
||||
}
|
||||
*p = '\0';
|
||||
|
||||
return escaped;
|
||||
}
|
||||
+2
-2
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <sqlite3.h>
|
||||
#include <pthread.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
extern sqlite3 *db;
|
||||
extern sqlite3* db;
|
||||
extern pthread_mutex_t db_mutex;
|
||||
|
||||
void init_db();
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "database.h"
|
||||
#include "sql.h"
|
||||
#include "user.h"
|
||||
#include <string.h>
|
||||
|
||||
char *db_get_all_users() {
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
@@ -40,9 +41,9 @@ char *db_get_all_users() {
|
||||
const unsigned char *group = sqlite3_column_text(stmt, 4);
|
||||
int is_superuser = sqlite3_column_int(stmt, 5);
|
||||
|
||||
char *escaped_username = json_escape((const char *) username);
|
||||
char *escaped_email = json_escape((const char *) email);
|
||||
char *escaped_group = json_escape((const char *) group);
|
||||
char *escaped_username = json_escape((const char *)username);
|
||||
char *escaped_email = json_escape((const char *)email);
|
||||
char *escaped_group = json_escape((const char *)group);
|
||||
if (!escaped_username || !escaped_email || !escaped_group) {
|
||||
free(escaped_username);
|
||||
free(escaped_email);
|
||||
|
||||
@@ -27,8 +27,8 @@ User *db_get_user_by_id(const int id) {
|
||||
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
|
||||
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
|
||||
int db_is_superuser = sqlite3_column_int(stmt, 5);
|
||||
user = new_user(id, (char *) db_username, (char *) db_password,
|
||||
(char *) db_email, (char *) db_group, db_is_superuser);
|
||||
user = new_user(id, (char *)db_username, (char *)db_password,
|
||||
(char *)db_email, (char *)db_group, db_is_superuser);
|
||||
}
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
|
||||
@@ -28,8 +28,8 @@ User *db_get_user_by_username(const char *username) {
|
||||
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
|
||||
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
|
||||
int db_is_superuser = sqlite3_column_int(stmt, 5);
|
||||
user = new_user(id, (char *) db_username, (char *) db_password,
|
||||
(char *) db_email, (char *) db_group, db_is_superuser);
|
||||
user = new_user(id, (char *)db_username, (char *)db_password,
|
||||
(char *)db_email, (char *)db_group, db_is_superuser);
|
||||
}
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <pthread.h>
|
||||
|
||||
#include "database.h"
|
||||
#include "sql.h"
|
||||
#include "user.h"
|
||||
@@ -24,9 +25,9 @@ char *db_get_user_field_by_id(const int id, const char *field) {
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc == SQLITE_ROW) {
|
||||
const unsigned char *db_password = sqlite3_column_text(stmt, 0);
|
||||
password = malloc(strlen((const char *) db_password) + 1);
|
||||
password = malloc(strlen((const char *)db_password) + 1);
|
||||
if (password) {
|
||||
strcpy(password, (const char *) db_password);
|
||||
strcpy(password, (const char *)db_password);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
} else if (rc == SQLITE_DONE) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <cJSON.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include "file.h"
|
||||
#include "user.h"
|
||||
|
||||
|
||||
@@ -26,9 +26,9 @@ bool db_update_user_field(const int id, const char *field, const char *value) {
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "execution failed: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
fprintf(stderr, "execution failed: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return sqlite3_changes(db) > 0;
|
||||
|
||||
+8
-8
@@ -4,20 +4,20 @@
|
||||
|
||||
void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("must have token"));
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("must have token"));
|
||||
return;
|
||||
}
|
||||
request_with_token *requestdata = check_body(hm);
|
||||
if (requestdata->error != NULL) {
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC(requestdata->error));
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC(requestdata->error));
|
||||
return;
|
||||
}
|
||||
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
|
||||
if (!valid) {
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("invalid token"));
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("invalid token"));
|
||||
free_request_with_token(requestdata);
|
||||
return;
|
||||
}
|
||||
@@ -38,8 +38,8 @@ void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
}
|
||||
bool is_superuser = cJSON_GetObjectItem(payload, "is_superuser")->valueint;
|
||||
if (!is_superuser) {
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("user is not superuser"));
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("user is not superuser"));
|
||||
return;
|
||||
}
|
||||
bool user_deleted = db_delete_user(useridfromjson->valueint);
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
#include "file.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -13,7 +14,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);
|
||||
|
||||
+2
-1
@@ -5,7 +5,8 @@
|
||||
void get_all_users(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
request_with_token *data = check_body(hm);
|
||||
if (data->error != NULL) {
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"), MG_ESC(data->error));
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC(data->error));
|
||||
return;
|
||||
}
|
||||
if (!jwt_verifyJWT(data->token, JWT_DEFAULT_SECRET)) {
|
||||
|
||||
+4
-2
@@ -13,7 +13,8 @@ void get_token(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (username == NULL || password == NULL) {
|
||||
const char *error_ptr = cJSON_GetErrorPtr();
|
||||
if (error_ptr != NULL) {
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"), MG_ESC(error_ptr));
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC(error_ptr));
|
||||
return;
|
||||
}
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
@@ -41,7 +42,8 @@ void get_token(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
db_user->id, db_user->username, db_user->email, db_user->group,
|
||||
db_user->is_superuser ? "true" : "false");
|
||||
char *token = jwt_createJWT(header, payload, JWT_DEFAULT_SECRET);
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, "{%m:%m}", MG_ESC("token"), MG_ESC(token));
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, "{%m:%m}", MG_ESC("token"),
|
||||
MG_ESC(token));
|
||||
free_user(db_user);
|
||||
free(token);
|
||||
}
|
||||
+2
-1
@@ -10,7 +10,8 @@ void get_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
}
|
||||
request_with_token *requestdata = check_body(hm);
|
||||
if (requestdata->error != NULL) {
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"), MG_ESC(requestdata->error));
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC(requestdata->error));
|
||||
return;
|
||||
}
|
||||
if (!jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET)) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "jwt.h"
|
||||
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/buffer.h>
|
||||
#include <openssl/evp.h>
|
||||
@@ -57,7 +58,7 @@ char *jwt_base64url_encode(const unsigned char *input, const int length) {
|
||||
BUF_MEM *bufferPtr;
|
||||
BIO_get_mem_ptr(bio, &bufferPtr);
|
||||
|
||||
const auto result = (char *) malloc(bufferPtr->length + 1);
|
||||
const auto result = (char *)malloc(bufferPtr->length + 1);
|
||||
memcpy(result, bufferPtr->data, bufferPtr->length);
|
||||
result[bufferPtr->length] = '\0';
|
||||
|
||||
@@ -73,7 +74,7 @@ unsigned char *jwt_base64url_decode(const char *input, int input_len,
|
||||
if (input_len == 0) input_len = strlen(input);
|
||||
|
||||
// Создаем копию для преобразования
|
||||
const auto input_copy = (char *) malloc(input_len + 1);
|
||||
const auto input_copy = (char *)malloc(input_len + 1);
|
||||
strncpy(input_copy, input, input_len);
|
||||
input_copy[input_len] = '\0';
|
||||
|
||||
@@ -85,7 +86,7 @@ unsigned char *jwt_base64url_decode(const char *input, int input_len,
|
||||
|
||||
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
|
||||
|
||||
const auto buffer = (unsigned char *) malloc(input_len);
|
||||
const auto buffer = (unsigned char *)malloc(input_len);
|
||||
const int len = BIO_read(bio, buffer, input_len);
|
||||
|
||||
BIO_free_all(bio);
|
||||
@@ -104,13 +105,13 @@ unsigned char *jwt_base64url_decode(const char *input, int input_len,
|
||||
// Создание JWT токена
|
||||
char *jwt_createJWT(const char *header_json, const char *payload_json,
|
||||
const char *secret) {
|
||||
char *encodedHeader = jwt_base64url_encode(
|
||||
(const unsigned char *) header_json, strlen(header_json));
|
||||
char *encodedHeader = jwt_base64url_encode((const unsigned char *)header_json,
|
||||
strlen(header_json));
|
||||
char *encodedPayload = jwt_base64url_encode(
|
||||
(const unsigned char *) payload_json, strlen(payload_json));
|
||||
(const unsigned char *)payload_json, strlen(payload_json));
|
||||
|
||||
char *unsignedToken =
|
||||
(char *) malloc(strlen(encodedHeader) + strlen(encodedPayload) + 2);
|
||||
(char *)malloc(strlen(encodedHeader) + strlen(encodedPayload) + 2);
|
||||
sprintf(unsignedToken, "%s.%s", encodedHeader, encodedPayload);
|
||||
|
||||
unsigned int digest_len;
|
||||
@@ -119,7 +120,7 @@ char *jwt_createJWT(const char *header_json, const char *payload_json,
|
||||
char *encodedSignature = jwt_base64url_encode(digest, digest_len);
|
||||
|
||||
char *jwt =
|
||||
(char *) malloc(strlen(unsignedToken) + strlen(encodedSignature) + 2);
|
||||
(char *)malloc(strlen(unsignedToken) + strlen(encodedSignature) + 2);
|
||||
sprintf(jwt, "%s.%s", unsignedToken, encodedSignature);
|
||||
|
||||
free(encodedHeader);
|
||||
@@ -134,8 +135,8 @@ char *jwt_createJWT(const char *header_json, const char *payload_json,
|
||||
// Генерация HMAC-SHA256
|
||||
unsigned char *jwt_hmac_sha256(const char *key, int key_len, const char *data,
|
||||
int data_len, unsigned int *digest_len) {
|
||||
unsigned char *digest = (unsigned char *) malloc(EVP_MAX_MD_SIZE);
|
||||
HMAC(EVP_sha256(), key, key_len, (const unsigned char *) data, data_len,
|
||||
unsigned char *digest = (unsigned char *)malloc(EVP_MAX_MD_SIZE);
|
||||
HMAC(EVP_sha256(), key, key_len, (const unsigned char *)data, data_len,
|
||||
digest, digest_len);
|
||||
return digest;
|
||||
}
|
||||
@@ -150,17 +151,17 @@ bool jwt_verifyJWT(const char *token, const char *secret) {
|
||||
size_t payload_len = dot2 - dot1 - 1;
|
||||
size_t sig_len = strlen(dot2 + 1);
|
||||
|
||||
char *header = (char *) malloc(header_len + 1);
|
||||
char *header = (char *)malloc(header_len + 1);
|
||||
strncpy(header, token, header_len);
|
||||
header[header_len] = '\0';
|
||||
|
||||
char *payload = (char *) malloc(payload_len + 1);
|
||||
char *payload = (char *)malloc(payload_len + 1);
|
||||
strncpy(payload, dot1 + 1, payload_len);
|
||||
payload[payload_len] = '\0';
|
||||
|
||||
char *signature = strndup(dot2 + 1, sig_len);
|
||||
|
||||
char *signing_input = (char *) malloc(header_len + payload_len + 2);
|
||||
char *signing_input = (char *)malloc(header_len + payload_len + 2);
|
||||
sprintf(signing_input, "%s.%s", header, payload);
|
||||
|
||||
int expected_sig_len;
|
||||
@@ -181,7 +182,7 @@ bool jwt_verifyJWT(const char *token, const char *secret) {
|
||||
int payload_bin_len;
|
||||
unsigned char *payload_bin =
|
||||
jwt_base64url_decode(payload, payload_len, &payload_bin_len);
|
||||
char *payload_str = (char *) malloc(payload_bin_len + 1);
|
||||
char *payload_str = (char *)malloc(payload_bin_len + 1);
|
||||
memcpy(payload_str, payload_bin, payload_bin_len);
|
||||
payload_str[payload_bin_len] = '\0';
|
||||
|
||||
@@ -190,7 +191,7 @@ bool jwt_verifyJWT(const char *token, const char *secret) {
|
||||
cJSON *exp = cJSON_GetObjectItemCaseSensitive(json_payload, "exp");
|
||||
if (cJSON_IsNumber(exp)) {
|
||||
time_t now = time(nullptr);
|
||||
if (now >= (time_t) exp->valuedouble) {
|
||||
if (now >= (time_t)exp->valuedouble) {
|
||||
exp_valid = false;
|
||||
}
|
||||
}
|
||||
@@ -217,14 +218,14 @@ cJSON *jwt_get_payload(const char *token) {
|
||||
if (!dot1 || !dot2) return nullptr;
|
||||
|
||||
size_t payload_len = dot2 - dot1 - 1;
|
||||
char *payload = (char *) malloc(payload_len + 1);
|
||||
char *payload = (char *)malloc(payload_len + 1);
|
||||
strncpy(payload, dot1 + 1, payload_len);
|
||||
payload[payload_len] = '\0';
|
||||
|
||||
int payload_bin_len;
|
||||
unsigned char *payload_bin =
|
||||
jwt_base64url_decode(payload, payload_len, &payload_bin_len);
|
||||
char *payload_str = (char *) malloc(payload_bin_len + 1);
|
||||
char *payload_str = (char *)malloc(payload_bin_len + 1);
|
||||
memcpy(payload_str, payload_bin, payload_bin_len);
|
||||
payload_str[payload_bin_len] = '\0';
|
||||
|
||||
@@ -243,14 +244,14 @@ cJSON *jwt_get_header(const char *token) {
|
||||
if (!dot1) return nullptr;
|
||||
|
||||
size_t header_len = dot1 - token;
|
||||
char *header = (char *) malloc(header_len + 1);
|
||||
char *header = (char *)malloc(header_len + 1);
|
||||
strncpy(header, token, header_len);
|
||||
header[header_len] = '\0';
|
||||
|
||||
int header_bin_len;
|
||||
unsigned char *header_bin =
|
||||
jwt_base64url_decode(header, header_len, &header_bin_len);
|
||||
char *header_str = (char *) malloc(header_bin_len + 1);
|
||||
char *header_str = (char *)malloc(header_bin_len + 1);
|
||||
memcpy(header_str, header_bin, header_bin_len);
|
||||
header_str[header_bin_len] = '\0';
|
||||
|
||||
|
||||
+6
-4
@@ -1,6 +1,7 @@
|
||||
#include <colors.h>
|
||||
#include <mongoose.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "cors.h"
|
||||
#include "database.h"
|
||||
@@ -25,7 +26,7 @@ static const struct route routes[] = {
|
||||
|
||||
void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_ERROR) {
|
||||
MG_LOG(MG_LL_ERROR, ("Ошибка соединения: %s", (char *) ev_data));
|
||||
MG_LOG(MG_LL_ERROR, ("Ошибка соединения: %s", (char *)ev_data));
|
||||
}
|
||||
const bool tls_enabled = cm_get_parameter_as_bool(config, "tls_enabled");
|
||||
if (tls_enabled) {
|
||||
@@ -40,7 +41,7 @@ void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
}
|
||||
}
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
const auto hm = (struct mg_http_message *) ev_data;
|
||||
const auto hm = (struct mg_http_message *)ev_data;
|
||||
bool route_found = false;
|
||||
|
||||
for (int i = 0; routes[i].path != nullptr; i++) {
|
||||
@@ -80,8 +81,9 @@ int main(void) {
|
||||
const char *tls_address = cm_get_parameter_as_string(config, "tls_address");
|
||||
char https_address[64];
|
||||
snprintf(https_address, sizeof(https_address), "https://%s", tls_address);
|
||||
tls_address ? mg_http_listen(&mgr, https_address, ev_handler, nullptr)
|
||||
: mg_http_listen(&mgr, "https://0.0.0.0:8223", ev_handler, nullptr);
|
||||
tls_address
|
||||
? mg_http_listen(&mgr, https_address, ev_handler, nullptr)
|
||||
: mg_http_listen(&mgr, "https://0.0.0.0:8223", ev_handler, nullptr);
|
||||
}
|
||||
for (;;) {
|
||||
mg_mgr_poll(&mgr, 1000);
|
||||
|
||||
+10
-21
@@ -2,12 +2,9 @@
|
||||
|
||||
void register_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
mg_http_reply(
|
||||
c, 400,
|
||||
HTTP_HEADERS,
|
||||
"{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("must have <username>, <password>, <email>, <group, "
|
||||
"optional>, <is_superuser, optional>"));
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("must have <username>, <password>, <email>, <group, "
|
||||
"optional>, <is_superuser, optional>"));
|
||||
return;
|
||||
}
|
||||
cJSON *jsonbody = cJSON_Parse(hm->body.buf);
|
||||
@@ -17,12 +14,9 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
cJSON *group = cJSON_GetObjectItem(jsonbody, "group");
|
||||
cJSON *is_superuser = cJSON_GetObjectItem(jsonbody, "is_superuser");
|
||||
if (username == nullptr || password == nullptr || email == nullptr) {
|
||||
mg_http_reply(
|
||||
c, 400,
|
||||
HTTP_HEADERS,
|
||||
"{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("must have <username>, <password>, <email>, <group, "
|
||||
"optional>, <is_superuser, optional>"));
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("must have <username>, <password>, <email>, <group, "
|
||||
"optional>, <is_superuser, optional>"));
|
||||
return;
|
||||
}
|
||||
if (group == nullptr) {
|
||||
@@ -34,12 +28,10 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
const bool is_superuser_value = is_superuser->valueint ? true : false;
|
||||
User *new_user_data = db_insert_user(
|
||||
username->valuestring, email->valuestring, password->valuestring,
|
||||
group->valuestring, (const bool *) is_superuser_value);
|
||||
group->valuestring, (const bool *)is_superuser_value);
|
||||
if (new_user_data == nullptr) {
|
||||
mg_http_reply(
|
||||
c, 500,
|
||||
HTTP_HEADERS,
|
||||
"{%m:%m}", MG_ESC("error"), MG_ESC("failed to create user"));
|
||||
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("failed to create user"));
|
||||
return;
|
||||
}
|
||||
char responsedata[2048] = {0};
|
||||
@@ -49,9 +41,6 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
new_user_data->id, new_user_data->username, new_user_data->email,
|
||||
new_user_data->group, new_user_data->is_superuser);
|
||||
|
||||
mg_http_reply(
|
||||
c, 200,
|
||||
HTTP_HEADERS,
|
||||
responsedata);
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
||||
free_user(new_user_data);
|
||||
}
|
||||
+3
-1
@@ -1,7 +1,9 @@
|
||||
#include "static.h"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
void root_handler(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
struct mg_http_serve_opts opts = {.root_dir = cm_get_parameter_as_string(config, "web")};
|
||||
struct mg_http_serve_opts opts = {
|
||||
.root_dir = cm_get_parameter_as_string(config, "web")};
|
||||
mg_http_serve_dir(c, hm, &opts);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "user.h"
|
||||
|
||||
#include <cJSON.h>
|
||||
#include <mongoose.h>
|
||||
|
||||
|
||||
+5
-3
@@ -1,11 +1,13 @@
|
||||
#include "utils.h"
|
||||
|
||||
#include <cJSON.h>
|
||||
#include <mongoose.h>
|
||||
|
||||
#include "user.h"
|
||||
|
||||
request_with_token *check_body(const struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
return new_request_with_token(nullptr, nullptr, (char *) "must have token");
|
||||
return new_request_with_token(nullptr, nullptr, (char *)"must have token");
|
||||
}
|
||||
|
||||
cJSON *json_body = cJSON_Parse(hm->body.buf);
|
||||
@@ -13,9 +15,9 @@ request_with_token *check_body(const struct mg_http_message *hm) {
|
||||
if (token == NULL) {
|
||||
const char *error_ptr = cJSON_GetErrorPtr();
|
||||
if (error_ptr != NULL) {
|
||||
return new_request_with_token(nullptr, nullptr, (char *) error_ptr);
|
||||
return new_request_with_token(nullptr, nullptr, (char *)error_ptr);
|
||||
}
|
||||
return new_request_with_token(nullptr, nullptr, (char *) "must have token");
|
||||
return new_request_with_token(nullptr, nullptr, (char *)"must have token");
|
||||
}
|
||||
return new_request_with_token(strdup(token->valuestring), json_body, nullptr);
|
||||
}
|
||||
Reference in new issue
Block a user