update added to config.json jwt_secret
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Failing after 10s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Failing after 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Failing after 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Failing after 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Failing after 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Failing after 13s

This commit is contained in:
wt committed 2026-03-10 16:40:33 +07:00
1 parent e4efaa6165
commit e0c807bb35
9 files changed
+80 -9

No files matched your search

+2 -1
View File
@@ -7,5 +7,6 @@
"web": "./web", "web": "./web",
"tls_enabled": true, "tls_enabled": true,
"tls_cert": "/home/tola/certs/localhost/localhost.crt", "tls_cert": "/home/tola/certs/localhost/localhost.crt",
"tls_key": "/home/tola/certs/localhost/localhost.key" "tls_key": "/home/tola/certs/localhost/localhost.key",
"jwt_secret": "super_secret"
} }
+9 -1
View File
@@ -1,10 +1,18 @@
#include "security/jwt.h" #include "security/jwt.h"
#include "user.h" #include "user.h"
#include "utils/utils.h" #include "utils/utils.h"
#include "config/config.h"
void add_user(struct mg_connection *c, struct mg_http_message *hm) { void add_user(struct mg_connection *c, struct mg_http_message *hm) {
request_with_token *requestdata = check_body(hm); request_with_token *requestdata = check_body(hm);
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET); char *jwt_secret = cm_get_parameter_as_string(config, "jwt_secret");
bool valid = false;
if (jwt_secret) {
printf("jwt_secret is not null %s\n", jwt_secret);
valid = jwt_verifyJWT(requestdata->token, jwt_secret);
} else {
valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
}
if (!valid) { if (!valid) {
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"), mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token")); MG_ESC("invalid token"));
+9 -1
View File
@@ -2,6 +2,7 @@
#include "security/jwt.h" #include "security/jwt.h"
#include "user.h" #include "user.h"
#include "utils/utils.h" #include "utils/utils.h"
#include "config/config.h"
void change_password(struct mg_connection *c, struct mg_http_message *hm) { void change_password(struct mg_connection *c, struct mg_http_message *hm) {
if (hm->body.len == 0) { if (hm->body.len == 0) {
@@ -15,7 +16,14 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
MG_ESC(requestdata->error)); MG_ESC(requestdata->error));
return; return;
} }
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET); char *jwt_secret = cm_get_parameter_as_string(config, "jwt_secret");
bool valid = false;
if (jwt_secret) {
printf("jwt_secret is not null %s\n", jwt_secret);
valid = jwt_verifyJWT(requestdata->token, jwt_secret);
} else {
valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
}
if (!valid) { if (!valid) {
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"), mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token")); MG_ESC("invalid token"));
+9 -1
View File
@@ -1,6 +1,7 @@
#include "security/jwt.h" #include "security/jwt.h"
#include "user.h" #include "user.h"
#include "utils/utils.h" #include "utils/utils.h"
#include "config/config.h"
void change_user(struct mg_connection *c, struct mg_http_message *hm) { void change_user(struct mg_connection *c, struct mg_http_message *hm) {
if (hm->body.len == 0) { if (hm->body.len == 0) {
@@ -14,7 +15,14 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
MG_ESC(requestdata->error)); MG_ESC(requestdata->error));
return; return;
} }
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET); char *jwt_secret = cm_get_parameter_as_string(config, "jwt_secret");
bool valid = false;
if (jwt_secret) {
printf("jwt_secret is not null %s\n", jwt_secret);
valid = jwt_verifyJWT(requestdata->token, jwt_secret);
} else {
valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
}
if (!valid) { if (!valid) {
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"), mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token")); MG_ESC("invalid token"));
+9 -1
View File
@@ -1,6 +1,7 @@
#include "security/jwt.h" #include "security/jwt.h"
#include "user.h" #include "user.h"
#include "utils/utils.h" #include "utils/utils.h"
#include "config/config.h"
void delete_user(struct mg_connection *c, struct mg_http_message *hm) { void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
if (hm->body.len == 0) { if (hm->body.len == 0) {
@@ -14,7 +15,14 @@ void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
MG_ESC(requestdata->error)); MG_ESC(requestdata->error));
return; return;
} }
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET); char *jwt_secret = cm_get_parameter_as_string(config, "jwt_secret");
bool valid = false;
if (jwt_secret) {
printf("jwt_secret is not null %s\n", jwt_secret);
valid = jwt_verifyJWT(requestdata->token, jwt_secret);
} else {
valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
}
if (!valid) { if (!valid) {
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"), mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token")); MG_ESC("invalid token"));
+10 -1
View File
@@ -1,6 +1,7 @@
#include "security/jwt.h" #include "security/jwt.h"
#include "user.h" #include "user.h"
#include "utils/utils.h" #include "utils/utils.h"
#include "config/config.h"
void get_all_users(struct mg_connection *c, struct mg_http_message *hm) { void get_all_users(struct mg_connection *c, struct mg_http_message *hm) {
request_with_token *data = check_body(hm); request_with_token *data = check_body(hm);
@@ -9,7 +10,15 @@ void get_all_users(struct mg_connection *c, struct mg_http_message *hm) {
MG_ESC(data->error)); MG_ESC(data->error));
return; return;
} }
if (!jwt_verifyJWT(data->token, JWT_DEFAULT_SECRET)) { char *jwt_secret = cm_get_parameter_as_string(config, "jwt_secret");
bool valid = false;
if (jwt_secret) {
printf("jwt_secret is not null %s\n", jwt_secret);
valid = jwt_verifyJWT(data->token, jwt_secret);
} else {
valid = jwt_verifyJWT(data->token, JWT_DEFAULT_SECRET);
}
if (!valid) {
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"), mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token")); MG_ESC("invalid token"));
return; return;
+11 -1
View File
@@ -1,5 +1,7 @@
#include "config/ConfigManager.h"
#include "security/jwt.h" #include "security/jwt.h"
#include "user.h" #include "user.h"
#include "config/config.h"
void get_token(struct mg_connection *c, struct mg_http_message *hm) { void get_token(struct mg_connection *c, struct mg_http_message *hm) {
if (hm->body.len == 0) { if (hm->body.len == 0) {
@@ -41,9 +43,17 @@ void get_token(struct mg_connection *c, struct mg_http_message *hm) {
"\"%s\",\"is_superuser\": %s}", "\"%s\",\"is_superuser\": %s}",
db_user->id, db_user->username, db_user->email, db_user->group, db_user->id, db_user->username, db_user->email, db_user->group,
db_user->is_superuser ? "true" : "false"); db_user->is_superuser ? "true" : "false");
char *token = jwt_createJWT(header, payload, JWT_DEFAULT_SECRET); char *jwt_secret = cm_get_parameter_as_string(config, "jwt_secret");
char *token = nullptr;
if (jwt_secret) {
printf("jwt_secret is not null %s\n", jwt_secret);
token = jwt_createJWT(header, payload, jwt_secret);
} else {
token = jwt_createJWT(header, payload, JWT_DEFAULT_SECRET);
}
mg_http_reply(c, 200, HTTP_HEADERS, "{%m:%m}", MG_ESC("token"), mg_http_reply(c, 200, HTTP_HEADERS, "{%m:%m}", MG_ESC("token"),
MG_ESC(token)); MG_ESC(token));
free_user(db_user); free_user(db_user);
free(token); free(token);
free(jwt_secret);
} }
+10 -1
View File
@@ -1,6 +1,7 @@
#include "security/jwt.h" #include "security/jwt.h"
#include "user.h" #include "user.h"
#include "utils/utils.h" #include "utils/utils.h"
#include "config/config.h"
void get_user(struct mg_connection *c, struct mg_http_message *hm) { void get_user(struct mg_connection *c, struct mg_http_message *hm) {
if (hm->body.len == 0) { if (hm->body.len == 0) {
@@ -14,7 +15,15 @@ void get_user(struct mg_connection *c, struct mg_http_message *hm) {
MG_ESC(requestdata->error)); MG_ESC(requestdata->error));
return; return;
} }
if (!jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET)) { char *jwt_secret = cm_get_parameter_as_string(config, "jwt_secret");
bool valid = false;
if (jwt_secret) {
printf("jwt_secret is not null %s\n", jwt_secret);
valid = jwt_verifyJWT(requestdata->token, jwt_secret);
} else {
valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
}
if (!valid) {
free_request_with_token(requestdata); free_request_with_token(requestdata);
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"), mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token")); MG_ESC("invalid token"));
+11 -1
View File
@@ -1,3 +1,5 @@
#include "config/ConfigManager.h"
#include "config/config.h"
#include "security/jwt.h" #include "security/jwt.h"
#include "user.h" #include "user.h"
#include "utils/utils.h" #include "utils/utils.h"
@@ -8,7 +10,14 @@ void verify_token(struct mg_connection *c, struct mg_http_message *hm) {
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:false}", MG_ESC("valid")); mg_http_reply(c, 403, HTTP_HEADERS, "{%m:false}", MG_ESC("valid"));
return; return;
} }
const bool valid = jwt_verifyJWT(data->token, JWT_DEFAULT_SECRET); char *jwt_secret = cm_get_parameter_as_string(config, "jwt_secret");
bool valid = false;
if (jwt_secret) {
printf("jwt_secret is not null %s\n", jwt_secret);
valid = jwt_verifyJWT(data->token, jwt_secret);
} else {
valid = jwt_verifyJWT(data->token, JWT_DEFAULT_SECRET);
}
if (!valid) { if (!valid) {
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:false}", MG_ESC("valid")); mg_http_reply(c, 403, HTTP_HEADERS, "{%m:false}", MG_ESC("valid"));
free_request_with_token(data); free_request_with_token(data);
@@ -35,4 +44,5 @@ void verify_token(struct mg_connection *c, struct mg_http_message *hm) {
free_request_with_token(data); free_request_with_token(data);
free(username); free(username);
free_user(user); free_user(user);
free(jwt_secret);
} }