added endpoints end refactor

This commit is contained in:
wt committed 2025-08-19 00:01:17 +07:00
1 parent 22dc118616
commit c4ea7a4293
15 files changed
+333 -87

No files matched your search

+13 -80
View File
@@ -4,86 +4,6 @@
#include <cJSON.h>
#include <mongoose.h>
#define JWT_SECRET_KEY "secret"
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));
return;
}
free_request_with_token(data);
User users[2] = {{.id = 1,
.username = "tola",
.email = "mail@example.com",
.password = "passsword",
.group = "root",
.is_superuser = 1},
{.id = 2,
.username = "lisa",
.email = "mail@example.com",
.password = "asdf",
.group = "users",
.is_superuser = 0}};
char msg[2048] = {0};
strncpy(msg, "[", sizeof(msg));
for (int i = 0; i < sizeof(users) / sizeof(User); i++) {
snprintf(msg + strlen(msg), sizeof(msg) - strlen(msg),
"{\"%s\":%d,\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%"
"s\",\"%s\":%d}",
"id", users[i].id, "username", users[i].username, "email",
users[i].email, "password", users[i].password, "group",
users[i].group, "is_superuser", users[i].is_superuser);
if (i < sizeof(users) / sizeof(User) - 1) {
strncat(msg, ",", strlen(msg));
}
}
strncat(msg, "]\n", strlen(msg));
mg_http_reply(c, 200, "Content-type: application/json\r\n", msg);
}
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_ESC("must have username and password"));
return;
}
cJSON *jsonbody = cJSON_Parse(hm->body.buf);
cJSON *username = cJSON_GetObjectItem(jsonbody, "username");
cJSON *password = cJSON_GetObjectItem(jsonbody, "password");
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));
return;
}
mg_http_reply(c, 400, "", "{%m:%m}", MG_ESC("error"),
MG_ESC("must have username and password"));
return;
}
const char *header = "{\"alg\":\"HS256\",\"typ\":\"JWT\"}";
const char *payload = "{\"sub\": \"1234567890\",\"username\": "
"\"tola\",\"is_superuser\": true,\"iat\": 1516239022}";
char *token = jwt_createJWT(header, payload, JWT_SECRET_KEY);
mg_http_reply(c, 200, "", "{%m:%m}", MG_ESC("token"), MG_ESC(token));
}
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));
return;
}
const bool valid = jwt_verifyJWT(data->token, JWT_SECRET_KEY);
if (!valid) {
mg_http_reply(c, 403, "", "{%m:%m}", MG_ESC("error"),
MG_ESC("invalid token"));
return;
}
mg_http_reply(c, 200, "", "{%m:%m}", MG_ESC("valid"), MG_ESC("true"));
}
request_with_token *new_request_with_token(char *token, cJSON *body,
char *error) {
request_with_token *data = malloc(sizeof(request_with_token));
@@ -105,3 +25,16 @@ void free_request_with_token(request_with_token *data) {
}
free(data);
}
User *new_user(const char *username, const char *password, const char *email, const char *group, bool is_superuser) {
User *user = malloc(sizeof(User));
if (user == NULL) {
return NULL;
}
user->username = strdup(username);
user->password = strdup(password);
user->email = strdup(email);
user->group = strdup(group);
user->is_superuser = is_superuser;
return user;
}