[ADD] Finish off a test version of #26, fix a short NULL guard

This commit is contained in:
lda 2023-11-01 21:24:25 +01:00
parent 6edacc8b32
commit 30c3f837d4
Signed by: lda
GPG key ID: 6898757653ABE3E6
2 changed files with 27 additions and 2 deletions

View file

@ -205,6 +205,11 @@ RegTokenJSON(RegTokenInfo * info)
{
TokenInfo tokinfo;
if (!info)
{
return NULL;
}
tokinfo.name = info->name;
tokinfo.created_on = info->created;
tokinfo.expires_on = info->expires;

View file

@ -61,9 +61,9 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
Int64 maxuses;
Int64 lifetime;
if (method != HTTP_GET && method != HTTP_POST)
if (method != HTTP_GET && method != HTTP_POST && method != HTTP_DELETE)
{
msg = "Route only supports GET and POST for now.";
msg = "Route only supports GET, POST, and DELETE";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
return MatrixErrorCreate(M_UNRECOGNIZED, msg);
}
@ -123,6 +123,13 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
break;
}
info = RegTokenGetInfo(db, ArrayGet(path, 0));
if (!info)
{
msg = "Token doesn't exist.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_INVALID_PARAM, msg);
goto finish;
}
response = RegTokenJSON(info);
@ -183,6 +190,19 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
TokenRequestFree(req);
Free(req);
break;
case HTTP_DELETE:
if (ArraySize(path) == 0)
{
msg = "No registration token given to DELETE /tokens/[token].";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_INVALID_PARAM, msg);
goto finish;
}
info = RegTokenGetInfo(db, ArrayGet(path, 0));
RegTokenDelete(info);
/* As this is a No Content, let's not set any data in the
* response */
HttpResponseStatus(args->context, HTTP_NO_CONTENT);
default:
/* Fallthrough, as those are naturally kept out beforehand */
break;