Clean up behavior of RouteAdminTokens().

This makes it accept keys with the same names as what is returned back to the caller,
allowing for a more consistent experience.
This commit is contained in:
Jordan Bancino 2023-11-08 12:36:01 -05:00
parent a6a9aeb4da
commit f87cce0f4c
3 changed files with 19 additions and 39 deletions

View file

@ -1,4 +1,5 @@
{ {
"guard": "TELODENDRIA_SCHEMA_FILTER_H",
"header": "Schema\/Filter.h", "header": "Schema\/Filter.h",
"types": { "types": {
"FilterRoom": { "FilterRoom": {
@ -116,6 +117,5 @@
}, },
"type": "struct" "type": "struct"
} }
}, }
"guard": "TELODENDRIA_SCHEMA_FILTER_H"
} }

View file

@ -1,23 +1,10 @@
{ {
"guard": "TELODENDRIA_SCHEMA_REGTOKEN_H",
"header": "Schema\/RegToken.h", "header": "Schema\/RegToken.h",
"include": [ "include": [
"Cytoplasm\/Db.h" "Cytoplasm\/Db.h"
], ],
"types": { "types": {
"RegTokenAdminRequest": {
"fields": {
"name": {
"type": "string"
},
"max_uses": {
"type": "integer"
},
"lifetime": {
"type": "integer"
}
},
"type": "struct"
},
"Db *": { "Db *": {
"type": "extern" "type": "extern"
}, },
@ -58,6 +45,5 @@
}, },
"type": "struct" "type": "struct"
} }
}, }
"guard": "TELODENDRIA_ADMINTOKEN_H"
} }

View file

@ -38,8 +38,6 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
HashMap *response = NULL; HashMap *response = NULL;
char *token; char *token;
char *username;
char *name;
char *msg; char *msg;
Db *db = args->matrixArgs->db; Db *db = args->matrixArgs->db;
@ -53,13 +51,10 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
RegTokenInfo *info; RegTokenInfo *info;
RegTokenAdminRequest *req; RegTokenInfo *req;
size_t i; size_t i;
Int64 maxuses;
Int64 lifetime;
if (method != HTTP_GET && method != HTTP_POST && method != HTTP_DELETE) if (method != HTTP_GET && method != HTTP_POST && method != HTTP_DELETE)
{ {
msg = "Route only supports GET, POST, and DELETE"; msg = "Route only supports GET, POST, and DELETE";
@ -120,6 +115,7 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
DbListFree(tokens); DbListFree(tokens);
break; break;
} }
info = RegTokenGetInfo(db, ArrayGet(path, 0)); info = RegTokenGetInfo(db, ArrayGet(path, 0));
if (!info) if (!info)
{ {
@ -142,14 +138,13 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
response = MatrixErrorCreate(M_NOT_JSON, NULL); response = MatrixErrorCreate(M_NOT_JSON, NULL);
goto finish; goto finish;
} }
req = Malloc(sizeof(RegTokenAdminRequest));
req->max_uses = Int64Neg(Int64Create(0, 1));
req->lifetime = Int64Create(0, 0);
req->name = NULL;
if (!RegTokenAdminRequestFromJson(request, req, &msg)) req = Malloc(sizeof(RegTokenInfo));
memset(req, 0, sizeof(RegTokenInfo));
if (!RegTokenInfoFromJson(request, req, &msg))
{ {
RegTokenAdminRequestFree(req); RegTokenInfoFree(req);
Free(req); Free(req);
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
@ -163,29 +158,28 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
req->name = StrRandom(16); req->name = StrRandom(16);
} }
username = UserGetName(user); /* Create the actual token that will be stored. */
name = req->name; info = RegTokenCreate(db, req->name, UserGetName(user),
maxuses = req->max_uses; req->expires_on, req->uses,
lifetime = req->lifetime; UserDecodePrivileges(req->grants));
info = RegTokenCreate(db, name, username, maxuses, lifetime, 0);
if (!info) if (!info)
{ {
RegTokenClose(info); RegTokenClose(info);
RegTokenFree(info); RegTokenFree(info);
RegTokenAdminRequestFree(req); RegTokenInfoFree(req);
Free(req); Free(req);
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
msg = "Cannot create token"; msg = "Cannot create token.";
response = MatrixErrorCreate(M_INVALID_PARAM, msg); response = MatrixErrorCreate(M_INVALID_PARAM, msg);
goto finish; goto finish;
} }
response = RegTokenInfoToJson(info); response = RegTokenInfoToJson(info);
RegTokenClose(info); RegTokenClose(info);
RegTokenFree(info); RegTokenFree(info);
RegTokenAdminRequestFree(req); RegTokenInfoFree(req);
Free(req); Free(req);
break; break;
case HTTP_DELETE: case HTTP_DELETE: