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
42 lines
1.4 KiB
C
42 lines
1.4 KiB
C
#include "jwt.h"
|
|
#include "user.h"
|
|
#include "utils.h"
|
|
|
|
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));
|
|
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);
|
|
}
|