added HTTP_HEADERS constant and tls support
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 13s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 15s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Failing after 8s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 13s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 15s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Failing after 8s
This commit is contained in:
1 parent
4c7a156723
commit
e0bd40e47f
13 files changed
+78
-64
No files matched your search
@@ -1,7 +1,7 @@
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -g -std=gnu2x -DMG_TLS=MG_TLS_BUILTIN
|
||||
CFLAGS = -Wall -g -std=gnu2x -DMG_TLS=MG_TLS_OPENSSL
|
||||
LDFLAGS =
|
||||
LIBS = -lpthread -lcrypto -lsqlite3
|
||||
LIBS = -lpthread -lcrypto -lssl -lsqlite3
|
||||
INCLUDES = -I./src/lib -I./src
|
||||
SRCS = $(wildcard src/*.c src/lib/*.c)
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"database": "users.db",
|
||||
"address": "localhost:8222",
|
||||
"tls_address": "localhost:8223",
|
||||
"default_users_file": "users.json",
|
||||
"web": "./web",
|
||||
"tls_enabled": true,
|
||||
|
||||
+5
-5
@@ -6,7 +6,7 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
request_with_token *requestdata = check_body(hm);
|
||||
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
|
||||
if (!valid) {
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("invalid token"));
|
||||
return;
|
||||
}
|
||||
@@ -18,7 +18,7 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
cJSON *is_superuser = cJSON_GetObjectItem(jsonbody, "is_superuser");
|
||||
if (username == nullptr || password == nullptr || email == nullptr ||
|
||||
is_superuser == nullptr || group == nullptr) {
|
||||
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
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;
|
||||
@@ -27,7 +27,7 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
bool jwt_is_superuser =
|
||||
cJSON_GetObjectItem(payload, "is_superuser")->valueint;
|
||||
if (!jwt_is_superuser) {
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("not authorized"));
|
||||
cJSON_Delete(payload);
|
||||
return;
|
||||
@@ -38,7 +38,7 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
group->valuestring,
|
||||
&user_is_superuser);
|
||||
if (!new_db_user) {
|
||||
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("failed to create user"));
|
||||
cJSON_Delete(payload);
|
||||
return;
|
||||
@@ -51,6 +51,6 @@ void add_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
is_superuser->valueint ? "true" : "false");
|
||||
|
||||
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
||||
cJSON_Delete(payload);
|
||||
}
|
||||
+10
-10
@@ -6,7 +6,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
request_with_token *requestdata = check_body(hm);
|
||||
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
|
||||
if (!valid) {
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("invalid token"));
|
||||
return;
|
||||
}
|
||||
@@ -16,7 +16,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
cJSON *userid = cJSON_GetObjectItem(jsonbody, "userid");
|
||||
if ((old_password == nullptr || new_password == nullptr) &&
|
||||
userid == nullptr) {
|
||||
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"),
|
||||
MG_ESC("must have <old_password>, <new_password>"));
|
||||
return;
|
||||
@@ -27,7 +27,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
char *username = cJSON_GetObjectItem(payload, "username")->valuestring;
|
||||
User *old_user_data = db_get_user_by_username(username);
|
||||
if (!old_user_data) {
|
||||
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"),
|
||||
MG_ESC("failed to update user can't get old_user_data"));
|
||||
cJSON_Delete(payload);
|
||||
@@ -35,7 +35,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
char *password = old_user_data->password;
|
||||
|
||||
if (strcmp(password, old_password->valuestring)) {
|
||||
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("invalid old password"));
|
||||
return;
|
||||
}
|
||||
@@ -44,7 +44,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
old_user_data->id, old_user_data->username, old_user_data->email,
|
||||
new_password->valuestring, old_user_data->group, &old_is_superuser);
|
||||
if (!new_user_data) {
|
||||
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("failed to update user can't update database"));
|
||||
free_user(old_user_data);
|
||||
cJSON_Delete(payload);
|
||||
@@ -54,21 +54,21 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
char responsedata[64] = {0};
|
||||
snprintf(responsedata, sizeof(responsedata), "{\"success\": true}");
|
||||
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
||||
free_user(old_user_data);
|
||||
cJSON_Delete(payload);
|
||||
return;
|
||||
}
|
||||
bool is_superuser = cJSON_GetObjectItem(payload, "is_superuser")->valueint;
|
||||
if (!is_superuser) {
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("user is not superuser"));
|
||||
cJSON_Delete(payload);
|
||||
return;
|
||||
}
|
||||
User *old_user_data = db_get_user_by_id(userid->valueint);
|
||||
if (!old_user_data) {
|
||||
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"),
|
||||
MG_ESC("failed to update user can't get old_user_data"));
|
||||
cJSON_Delete(payload);
|
||||
@@ -79,7 +79,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
old_user_data->id, old_user_data->username, old_user_data->email,
|
||||
new_password->valuestring, old_user_data->group, &old_is_superuser);
|
||||
if (!new_user_data) {
|
||||
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("failed to update user can't create new_user_data from root"));
|
||||
free_user(old_user_data);
|
||||
cJSON_Delete(payload);
|
||||
@@ -88,7 +88,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
char responsedata[64] = {0};
|
||||
snprintf(responsedata, sizeof(responsedata), "{\"success\": true}");
|
||||
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
||||
free_user(old_user_data);
|
||||
free_user(new_user_data);
|
||||
cJSON_Delete(payload);
|
||||
|
||||
+8
-8
@@ -6,7 +6,7 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
request_with_token *requestdata = check_body(hm);
|
||||
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
|
||||
if (!valid) {
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("invalid token"));
|
||||
return;
|
||||
}
|
||||
@@ -23,19 +23,19 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
old_user_data = db_get_user_by_id(userid->valueint);
|
||||
}
|
||||
if (!old_user_data) {
|
||||
mg_http_reply(c, 404, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 404, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("user not found"));
|
||||
return;
|
||||
}
|
||||
char *password = old_user_data->password;
|
||||
if (!password) {
|
||||
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("password is required"));
|
||||
return;
|
||||
}
|
||||
if (username == nullptr || email == nullptr || is_superuser == nullptr ||
|
||||
group == nullptr) {
|
||||
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"),
|
||||
MG_ESC("must have <username>, <email>, <group, "
|
||||
"optional>, <is_superuser, optional>"));
|
||||
@@ -52,7 +52,7 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
group->valuestring, &user_is_superuser);
|
||||
|
||||
if (!updated_user_data) {
|
||||
mg_http_reply(c, 500, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("failed to update user change user request"));
|
||||
cJSON_Delete(payload);
|
||||
free_user(old_user_data);
|
||||
@@ -66,7 +66,7 @@ void change_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, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
||||
free_user(updated_user_data);
|
||||
free_user(old_user_data);
|
||||
cJSON_Delete(payload);
|
||||
@@ -75,7 +75,7 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
bool req_is_superuser =
|
||||
cJSON_GetObjectItem(payload, "is_superuser")->valueint;
|
||||
if (!req_is_superuser) {
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("user is not superuser"));
|
||||
cJSON_Delete(payload);
|
||||
free_user(old_user_data);
|
||||
@@ -99,7 +99,7 @@ void change_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, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
||||
free_user(updated_user_data);
|
||||
free_user(old_user_data);
|
||||
cJSON_Delete(payload);
|
||||
|
||||
+6
-6
@@ -4,19 +4,19 @@
|
||||
|
||||
void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
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, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
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, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("invalid token"));
|
||||
free_request_with_token(requestdata);
|
||||
return;
|
||||
@@ -31,14 +31,14 @@ void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
snprintf(responsedata, sizeof(responsedata), "{\"success\": %s}",
|
||||
user_deleted ? "true" : "false");
|
||||
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
||||
free_request_with_token(requestdata);
|
||||
cJSON_Delete(payload);
|
||||
return;
|
||||
}
|
||||
bool is_superuser = cJSON_GetObjectItem(payload, "is_superuser")->valueint;
|
||||
if (!is_superuser) {
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}",
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}",
|
||||
MG_ESC("error"), MG_ESC("user is not superuser"));
|
||||
return;
|
||||
}
|
||||
@@ -47,7 +47,7 @@ void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
snprintf(responsedata, sizeof(responsedata), "{\"success\": %s}",
|
||||
user_deleted ? "true" : "false");
|
||||
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
||||
free_request_with_token(requestdata);
|
||||
cJSON_Delete(payload);
|
||||
}
|
||||
+3
-3
@@ -5,18 +5,18 @@
|
||||
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, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%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)) {
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("invalid token"));
|
||||
return;
|
||||
}
|
||||
cJSON *payload = jwt_get_payload(data->token);
|
||||
bool is_superuser = cJSON_GetObjectItem(payload, "is_superuser")->valueint;
|
||||
if (!is_superuser) {
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("user is not superuser"));
|
||||
return;
|
||||
}
|
||||
|
||||
+6
-6
@@ -3,7 +3,7 @@
|
||||
|
||||
void get_token(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("must have username and password"));
|
||||
return;
|
||||
}
|
||||
@@ -13,21 +13,21 @@ 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, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%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, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("must have username and password"));
|
||||
return;
|
||||
}
|
||||
User *db_user = db_get_user_by_username(username->valuestring);
|
||||
if (!db_user) {
|
||||
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("couldn't find user"));
|
||||
return;
|
||||
}
|
||||
if (strcmp(db_user->password, password->valuestring)) {
|
||||
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("wrong password"));
|
||||
return;
|
||||
}
|
||||
@@ -41,7 +41,7 @@ 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, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%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);
|
||||
}
|
||||
+5
-5
@@ -4,18 +4,18 @@
|
||||
|
||||
void get_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
mg_http_reply(c, 400, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
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, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%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)) {
|
||||
free_request_with_token(requestdata);
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("invalid token"));
|
||||
return;
|
||||
}
|
||||
@@ -27,7 +27,7 @@ void get_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
free_request_with_token(requestdata);
|
||||
cJSON_Delete(payload);
|
||||
free(username);
|
||||
mg_http_reply(c, 404, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 404, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("user not found"));
|
||||
return;
|
||||
}
|
||||
@@ -38,7 +38,7 @@ void get_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
user->id, user->username, user->email, user->group,
|
||||
user->is_superuser ? "true" : "false");
|
||||
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", responsedata);
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
||||
free_request_with_token(requestdata);
|
||||
free(username);
|
||||
free_user(user);
|
||||
|
||||
+23
-12
@@ -4,10 +4,10 @@
|
||||
#include "config.h"
|
||||
#include "cors.h"
|
||||
#include "database.h"
|
||||
#include "file.h"
|
||||
#include "route.h"
|
||||
#include "static.h"
|
||||
#include "user.h"
|
||||
#include "file.h"
|
||||
|
||||
static const struct route routes[] = {
|
||||
{"GET", "/#", root_handler},
|
||||
@@ -25,15 +25,19 @@ 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));
|
||||
}
|
||||
if (ev == MG_EV_ACCEPT && c->is_tls) {
|
||||
struct mg_tls_opts opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.cert = mg_str(read_file("/home/tola/certs/localhost/cert.pem"));
|
||||
opts.key = mg_str(read_file("/home/tola/certs/localhost/key.pem"));
|
||||
mg_tls_init(c, &opts);
|
||||
const bool tls_enabled = cm_get_parameter_as_bool(config, "tls_enabled");
|
||||
if (tls_enabled) {
|
||||
if (ev == MG_EV_ACCEPT && c->is_tls) {
|
||||
struct mg_tls_opts opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.cert =
|
||||
mg_str(read_file(cm_get_parameter_as_string(config, "tls_cert")));
|
||||
opts.key =
|
||||
mg_str(read_file(cm_get_parameter_as_string(config, "tls_key")));
|
||||
mg_tls_init(c, &opts);
|
||||
}
|
||||
}
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
const auto hm = (struct mg_http_message *) ev_data;
|
||||
@@ -68,10 +72,17 @@ int main(void) {
|
||||
mg_log_set(MG_LL_DEBUG);
|
||||
mg_mgr_init(&mgr);
|
||||
const char *address = cm_get_parameter_as_string(config, "address");
|
||||
// address ? mg_http_listen(&mgr, cm_get_parameter_as_string(config, "address"),
|
||||
// ev_handler, nullptr)
|
||||
// : mg_http_listen(&mgr, "http://0.0.0.0:8222", ev_handler, nullptr);
|
||||
mg_http_listen(&mgr, "https://0.0.0.0:8222", ev_handler, nullptr);
|
||||
char http_address[64];
|
||||
snprintf(http_address, sizeof(http_address), "http://%s", address);
|
||||
address ? mg_http_listen(&mgr, http_address, ev_handler, nullptr)
|
||||
: mg_http_listen(&mgr, "http://0.0.0.0:8222", ev_handler, nullptr);
|
||||
if (cm_get_parameter_as_bool(config, "tls_enabled")) {
|
||||
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);
|
||||
}
|
||||
for (;;) {
|
||||
mg_mgr_poll(&mgr, 1000);
|
||||
}
|
||||
|
||||
+4
-4
@@ -4,7 +4,7 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
mg_http_reply(
|
||||
c, 400,
|
||||
"Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
|
||||
HTTP_HEADERS,
|
||||
"{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("must have <username>, <password>, <email>, <group, "
|
||||
"optional>, <is_superuser, optional>"));
|
||||
@@ -19,7 +19,7 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (username == nullptr || password == nullptr || email == nullptr) {
|
||||
mg_http_reply(
|
||||
c, 400,
|
||||
"Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
|
||||
HTTP_HEADERS,
|
||||
"{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("must have <username>, <password>, <email>, <group, "
|
||||
"optional>, <is_superuser, optional>"));
|
||||
@@ -38,7 +38,7 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (new_user_data == nullptr) {
|
||||
mg_http_reply(
|
||||
c, 500,
|
||||
"Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
|
||||
HTTP_HEADERS,
|
||||
"{%m:%m}", MG_ESC("error"), MG_ESC("failed to create user"));
|
||||
return;
|
||||
}
|
||||
@@ -51,7 +51,7 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
|
||||
mg_http_reply(
|
||||
c, 200,
|
||||
"Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n",
|
||||
HTTP_HEADERS,
|
||||
responsedata);
|
||||
free_user(new_user_data);
|
||||
}
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <mongoose.h>
|
||||
#include <sqlite3.h>
|
||||
|
||||
#define HTTP_HEADERS "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n"
|
||||
|
||||
typedef struct User {
|
||||
int id;
|
||||
char *username;
|
||||
|
||||
+3
-3
@@ -5,14 +5,14 @@
|
||||
void verify_token(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
const request_with_token *data = check_body(hm);
|
||||
if (data->error != nullptr) {
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%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;
|
||||
}
|
||||
const bool valid = jwt_verifyJWT(data->token, JWT_DEFAULT_SECRET);
|
||||
if (!valid) {
|
||||
mg_http_reply(c, 403, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:%m}", MG_ESC("error"),
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("invalid token"));
|
||||
return;
|
||||
}
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\nAccess-Control-Allow-Origin: *\r\n", "{%m:true}", MG_ESC("valid"));
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, "{%m:true}", MG_ESC("valid"));
|
||||
}
|
||||
Reference in new issue
Block a user