fix POST requests to DELETE and PUT and fix double-free password in change_user.c

This commit is contained in:
wt committed 2025-08-23 13:05:40 +00:00
1 parent 9c3a8d796a
commit 7d04b0078f
12 files changed
+57 -35

No files matched your search

+2 -1
View File
@@ -5,4 +5,5 @@
build
builddir
fmongoose
users.db
users.db
*.swp
+4 -4
View File
@@ -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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 500, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("failed to create user"));
cJSON_Delete(payload);
return;
+4 -4
View File
@@ -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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token"));
return;
}
@@ -14,7 +14,7 @@ void change_password(struct mg_connection *c, struct mg_http_message *hm) {
cJSON *old_password = cJSON_GetObjectItem(jsonbody, "old_password");
cJSON *new_password = cJSON_GetObjectItem(jsonbody, "new_password");
if (old_password == nullptr || new_password == nullptr) {
mg_http_reply(c, 400, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("must have <old_password>, <new_password>"));
return;
}
@@ -24,7 +24,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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid old password"));
return;
}
@@ -33,7 +33,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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 500, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("failed to update user"));
return;
}
+6 -6
View File
@@ -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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token"));
return;
}
@@ -17,19 +17,19 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
cJSON *is_superuser = cJSON_GetObjectItem(jsonbody, "is_superuser");
User *old_user_data = db_get_user_by_username(username->valuestring);
if (!old_user_data) {
mg_http_reply(c, 404, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 404, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("user not found"));
return;
}
char *password = old_user_data->password;
if (!password) {
mg_http_reply(c, 400, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("must have <username>, <email>, <group, "
"optional>, <is_superuser, optional>"));
free_user(old_user_data);
@@ -44,7 +44,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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 500, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("failed to update user"));
cJSON_Delete(payload);
free_user(old_user_data);
@@ -63,5 +63,5 @@ void change_user(struct mg_connection *c, struct mg_http_message *hm) {
free_user(updated_user_data);
free_user(old_user_data);
cJSON_Delete(payload);
free(password);
//free(password);
}
+1 -1
View File
@@ -6,7 +6,7 @@ void delete_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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token"));
return;
}
+3 -3
View File
@@ -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, "", "{%m:%m}", MG_ESC("error"), MG_ESC(data->error));
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"), MG_ESC(data->error));
return;
}
if (!jwt_verifyJWT(data->token, JWT_DEFAULT_SECRET)) {
mg_http_reply(c, 403, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("user is not superuser"));
return;
}
+6 -6
View File
@@ -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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%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, "", "{%m:%m}", MG_ESC("error"), MG_ESC(error_ptr));
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"), MG_ESC(error_ptr));
return;
}
mg_http_reply(c, 400, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("couldn't find user"));
return;
}
if (strcmp(db_user->password, password->valuestring)) {
mg_http_reply(c, 400, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%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, "", "{%m:%m}", MG_ESC("token"), MG_ESC(token));
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("token"), MG_ESC(token));
free_user(db_user);
free(token);
}
+2 -2
View File
@@ -6,7 +6,7 @@ void get_user(struct mg_connection *c, struct mg_http_message *hm) {
request_with_token *requestdata = check_body(hm);
if (!jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET)) {
free_request_with_token(requestdata);
mg_http_reply(c, 403, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token"));
return;
}
@@ -18,7 +18,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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 404, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("user not found"));
return;
}
+2 -2
View File
@@ -15,9 +15,9 @@ static const struct route routes[] = {
{"POST", "/api/token/verify", verify_token},
{"POST", "/api/users", get_all_users},
{"POST", "/api/users/user", get_user},
{"POST", "/api/users/delete", delete_user},
{"DELETE", "/api/users", delete_user},
{"POST", "/api/users/password", change_password},
{"POST", "/api/users/change", change_user},
{"PUT", "/api/users", change_user},
{"POST", "/api/users/add", add_user},
{"POST", "/api/register", register_user},
{nullptr, nullptr, nullptr}};
+3 -3
View File
@@ -2,7 +2,7 @@
void register_user(struct mg_connection *c, struct mg_http_message *hm) {
if (hm->body.len == 0) {
mg_http_reply(c, 400, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("must have <username>, <password>, <email>, <group, "
"optional>, <is_superuser, optional>"));
return;
@@ -14,7 +14,7 @@ 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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 400, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("must have <username>, <password>, <email>, <group, "
"optional>, <is_superuser, optional>"));
return;
@@ -30,7 +30,7 @@ void register_user(struct mg_connection *c, struct mg_http_message *hm) {
username->valuestring, email->valuestring, password->valuestring,
group->valuestring, (const bool*)is_superuser_value);
if (new_user_data == nullptr) {
mg_http_reply(c, 500, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 500, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("failed to create user"));
return;
}
+3 -3
View File
@@ -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, "", "{%m:%m}", MG_ESC("error"), MG_ESC(data->error));
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%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, "", "{%m:%m}", MG_ESC("error"),
mg_http_reply(c, 403, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token"));
return;
}
mg_http_reply(c, 200, "", "{%m:%m}", MG_ESC("valid"), MG_ESC("true"));
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{%m:%m}", MG_ESC("valid"), MG_ESC("true"));
}
+21
View File
@@ -5,7 +5,28 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>fmongoose</title>
</head>
<style>
.login-form {
display: flex;
flex-direction: column;
max-width: 200px;
align-items: center;
}
</style>
<body>
<h1>it's work</h1>
<div class="login-form" style="display:flex;flex-direction:column;max-width:200px;">
<h2>Login</h2>
<label for="username_input">Username</label>
<input type="text" id="username_input">
<label for="password_input">Password</label>
<input type="password" id="password_input">
</div>
<script>
console.log("hello world");
const username_input = document.querySelector("#username_input");
const password_input = document.querySelector("#password_input");
console.log(username_input, password_input);
</script>
</body>
</html>