fix POST requests to DELETE and PUT and fix double-free password in change_user.c
This commit is contained in:
1 parent
9c3a8d796a
commit
7d04b0078f
12 files changed
+57
-35
No files matched your search
+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, "", "{%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);
|
||||
}
|
||||
Reference in new issue
Block a user