added endpoints end refactor
This commit is contained in:
1 parent
22dc118616
commit
c4ea7a4293
15 files changed
+333
-87
No files matched your search
@@ -0,0 +1,28 @@
|
||||
#include "user.h"
|
||||
#include "jwt.h"
|
||||
|
||||
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_DEFAULT_SECRET);
|
||||
mg_http_reply(c, 200, "", "{%m:%m}", MG_ESC("token"), MG_ESC(token));
|
||||
}
|
||||
Reference in new issue
Block a user