[ADD] Add basic POST endpoint

This commit is contained in:
lda 2023-11-01 18:47:54 +01:00
parent 7ec10703cd
commit cdc056f9e9
Signed by: lda
GPG key ID: 6898757653ABE3E6
2 changed files with 88 additions and 4 deletions

14
Schema/TokenRequest.json Normal file
View file

@ -0,0 +1,14 @@
{
"header": "Schema\/TokenRequest.h",
"types": {
"TokenRequest": {
"fields": {
"name": { "type": "string" },
"max_uses": { "type": "integer" },
"lifetime": { "type": "integer" }
},
"type": "struct"
}
},
"guard": "TELODENDRIA_TOKENREQUEST_H"
}

View file

@ -26,16 +26,22 @@
#include <Cytoplasm/Json.h> #include <Cytoplasm/Json.h>
#include <Cytoplasm/HashMap.h> #include <Cytoplasm/HashMap.h>
#include <Cytoplasm/Str.h> #include <Cytoplasm/Str.h>
#include <Cytoplasm/Memory.h>
#include <Schema/TokenRequest.h>
#include <RegToken.h> #include <RegToken.h>
#include <User.h> #include <User.h>
ROUTE_IMPL(RouteAdminTokens, path, argp) ROUTE_IMPL(RouteAdminTokens, path, argp)
{ {
RouteArgs *args = argp; RouteArgs *args = argp;
HashMap *request = NULL;
HashMap *response = NULL; HashMap *response = NULL;
char *token; char *token;
char *username;
char *name;
char *msg;
Db *db = args->matrixArgs->db; Db *db = args->matrixArgs->db;
@ -48,11 +54,16 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
RegTokenInfo *info; RegTokenInfo *info;
TokenRequest *req;
size_t i; size_t i;
if (method != HTTP_GET) Int64 maxuses;
Int64 lifetime;
if (method != HTTP_GET && method != HTTP_POST)
{ {
char *msg = "Route only supports GET for now."; msg = "Route only supports GET and POST for now.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
return MatrixErrorCreate(M_UNRECOGNIZED, msg); return MatrixErrorCreate(M_UNRECOGNIZED, msg);
} }
@ -74,7 +85,7 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
if (!(UserGetPrivileges(user) & USER_ISSUE_TOKENS)) if (!(UserGetPrivileges(user) & USER_ISSUE_TOKENS))
{ {
char *msg = "User doesn't have the ISSUE_TOKENS privilege."; msg = "User doesn't have the ISSUE_TOKENS privilege.";
HttpResponseStatus(args->context, HTTP_FORBIDDEN); HttpResponseStatus(args->context, HTTP_FORBIDDEN);
response = MatrixErrorCreate(M_FORBIDDEN, msg); response = MatrixErrorCreate(M_FORBIDDEN, msg);
goto finish; goto finish;
@ -95,9 +106,10 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
for (i = 0; i < ArraySize(tokens); i++) for (i = 0; i < ArraySize(tokens); i++)
{ {
char *tokenname = ArrayGet(tokens, i); char *tokenname = ArrayGet(tokens, i);
HashMap *jsoninfo;
info = RegTokenGetInfo(db, tokenname); info = RegTokenGetInfo(db, tokenname);
HashMap *jsoninfo = RegTokenJSON(info); jsoninfo = RegTokenJSON(info);
RegTokenClose(info); RegTokenClose(info);
@ -117,8 +129,66 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
RegTokenClose(info); RegTokenClose(info);
RegTokenFree(info); RegTokenFree(info);
break; break;
case HTTP_POST:
request = JsonDecode(HttpServerStream(args->context));
if (!request)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_NOT_JSON, NULL);
goto finish;
}
req = Malloc(sizeof(TokenRequest));
req->max_uses = Int64Neg(Int64Create(0, 1));
req->lifetime = Int64Create(0, 0);
req->name = NULL;
if (!TokenRequestFromJson(request, req, &msg))
{
TokenRequestFree(req);
Free(req);
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish;
}
if (!req->name)
{
req->name = StrRandom(16);
}
username = UserGetName(user);
name = req->name;
maxuses = req->max_uses;
lifetime = req->lifetime;
info = RegTokenCreate(db, name, username, maxuses, lifetime, 0);
if (!info)
{
RegTokenClose(info);
RegTokenFree(info);
TokenRequestFree(req);
Free(req);
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
msg = "Cannot create token";
response = MatrixErrorCreate(M_INVALID_PARAM, msg);
goto finish;
}
response = RegTokenJSON(info);
RegTokenClose(info);
RegTokenFree(info);
TokenRequestFree(req);
Free(req);
break;
default:
/* Fallthrough, as those are naturally kept out beforehand */
break;
} }
finish: finish:
UserUnlock(user); UserUnlock(user);
JsonFree(request);
return response; return response;
} }