updated verifying token process
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 (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
This commit is contained in:
1 parent
8d6c5132b4
commit
1d616c3841
1 file changed
+24
-1
+24
-1
@@ -5,14 +5,37 @@
|
||||
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, HTTP_HEADERS, "{%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));
|
||||
free_request_with_token((request_with_token *) data);
|
||||
return;
|
||||
}
|
||||
const bool valid = jwt_verifyJWT(data->token, JWT_DEFAULT_SECRET);
|
||||
if (!valid) {
|
||||
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("invalid token"));
|
||||
free_request_with_token((request_with_token *) data);
|
||||
return;
|
||||
}
|
||||
cJSON *payload = jwt_get_payload(data->token);
|
||||
char *username =
|
||||
strdup(cJSON_GetObjectItem(payload, "username")->valuestring);
|
||||
if (!username) {
|
||||
free_request_with_token((request_with_token *) data);
|
||||
cJSON_Delete(payload);
|
||||
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:false}", MG_ESC("valid"));
|
||||
return;
|
||||
}
|
||||
User *user = db_get_user_by_username(username);
|
||||
if (!user) {
|
||||
free_request_with_token((request_with_token *) data);
|
||||
cJSON_Delete(payload);
|
||||
free(username);
|
||||
mg_http_reply(c, 404, HTTP_HEADERS, "{%m:false}", MG_ESC("valid"));
|
||||
return;
|
||||
}
|
||||
mg_http_reply(c, 200, HTTP_HEADERS, "{%m:true}", MG_ESC("valid"));
|
||||
free_request_with_token((request_with_token *) data);
|
||||
free(username);
|
||||
free_user(user);
|
||||
}
|
||||
Reference in new issue
Block a user