[MOD] Refactor code out for #6

This commit is contained in:
lda 2023-11-02 12:42:45 +01:00
parent 42a901b7f5
commit 1e097ff895
Signed by: lda
GPG key ID: 6898757653ABE3E6
4 changed files with 100 additions and 38 deletions

View file

@ -0,0 +1,14 @@
{
"header": "Schema\/AdminDeactivate.h",
"types": {
"DeactivateRequest": {
"fields": {
"reason": {
"type": "string"
}
},
"type": "struct"
}
},
"guard": "TELODENDRIA_SCHEMA_ADMINDEACTIVATE_H"
}

21
Schema/RequestToken.json Normal file
View file

@ -0,0 +1,21 @@
{
"header": "Schema\/RequestToken.h",
"types": {
"RequestToken": {
"fields": {
"client_secret": { "type": "string" },
"send_attempt": { "type": "integer" },
"next_link": { "type": "string" },
"id_access_token": { "type": "string" },
"id_server": { "type": "string" },
"email": { "type": "string" },
"country": { "type": "string" },
"phone_number": { "type": "string" }
},
"type": "struct"
}
},
"guard": "TELODENDRIA_SCHEMA_REQUESTTOKEN_H"
}

View file

@ -28,7 +28,8 @@
#include <Cytoplasm/Str.h> #include <Cytoplasm/Str.h>
#include <User.h> #include <User.h>
#include <Cytoplasm/Log.h>
#include <Schema/AdminDeactivate.h>
ROUTE_IMPL(RouteAdminDeactivate, path, argp) ROUTE_IMPL(RouteAdminDeactivate, path, argp)
{ {
@ -38,6 +39,7 @@ ROUTE_IMPL(RouteAdminDeactivate, path, argp)
JsonValue *val; JsonValue *val;
char *reason = "Deactivated by admin"; char *reason = "Deactivated by admin";
char *err;
char *removedLocalpart = ArrayGet(path, 0); char *removedLocalpart = ArrayGet(path, 0);
char *token; char *token;
@ -48,6 +50,8 @@ ROUTE_IMPL(RouteAdminDeactivate, path, argp)
HttpRequestMethod method = HttpRequestMethodGet(args->context); HttpRequestMethod method = HttpRequestMethodGet(args->context);
DeactivateRequest deactReq;
if ((method != HTTP_DELETE) && (method != HTTP_PUT)) if ((method != HTTP_DELETE) && (method != HTTP_PUT))
{ {
char * msg = "Route only supports DELETE and PUT as for now."; char * msg = "Route only supports DELETE and PUT as for now.";
@ -63,10 +67,10 @@ ROUTE_IMPL(RouteAdminDeactivate, path, argp)
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
return MatrixErrorCreate(M_NOT_JSON, NULL); return MatrixErrorCreate(M_NOT_JSON, NULL);
} }
val = HashMapGet(request, "reason"); if (!DeactivateRequestFromJson(request, &deactReq, &err))
if (val && JsonValueType(val) == JSON_STRING)
{ {
reason = JsonValueAsString(val); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
return MatrixErrorCreate(M_BAD_JSON, err);
} }
} }
@ -100,7 +104,7 @@ ROUTE_IMPL(RouteAdminDeactivate, path, argp)
response = HashMapCreate(); response = HashMapCreate();
JsonSet(response, JsonValueString(removedLocalpart), 1, "user"); JsonSet(response, JsonValueString(removedLocalpart), 1, "user");
JsonSet(response, JsonValueString(reason), 1, "reason"); JsonSet(response, JsonValueString(deactReq.reason), 1, "reason");
JsonSet(response, JsonValueString(UserGetName(user)), 1, "banned_by"); JsonSet(response, JsonValueString(UserGetName(user)), 1, "banned_by");
} }
else else
@ -112,6 +116,7 @@ ROUTE_IMPL(RouteAdminDeactivate, path, argp)
finish: finish:
UserUnlock(user); UserUnlock(user);
UserUnlock(removed); UserUnlock(removed);
DeactivateRequestFree(&deactReq);
JsonFree(request); JsonFree(request);
return response; return response;
} }

View file

@ -26,14 +26,31 @@
#include <Cytoplasm/Str.h> #include <Cytoplasm/Str.h>
#include <Cytoplasm/Json.h> #include <Cytoplasm/Json.h>
#include <Schema/RequestToken.h>
ROUTE_IMPL(RouteRequestToken, path, argp) ROUTE_IMPL(RouteRequestToken, path, argp)
{ {
RouteArgs *args = argp; RouteArgs *args = argp;
char *type = ArrayGet(path, 0); char *type = ArrayGet(path, 0);
HashMap *request; HashMap *request;
HashMap *response; HashMap *response;
JsonValue *val;
char *str; char *msg;
RequestToken reqTok;
Int64 minusOne = Int64Neg(Int64Create(0, 1));
reqTok.client_secret = NULL;
reqTok.next_link = NULL;
reqTok.id_access_token = NULL;
reqTok.id_server = NULL;
reqTok.email = NULL;
reqTok.country = NULL;
reqTok.phone_number = NULL;
reqTok.send_attempt = minusOne;
if (HttpRequestMethodGet(args->context) != HTTP_POST) if (HttpRequestMethodGet(args->context) != HTTP_POST)
{ {
@ -48,87 +65,92 @@ ROUTE_IMPL(RouteRequestToken, path, argp)
return MatrixErrorCreate(M_NOT_JSON, NULL); return MatrixErrorCreate(M_NOT_JSON, NULL);
} }
val = HashMapGet(request, "client_secret"); if (!RequestTokenFromJson(request, &reqTok, &msg))
if (!val || JsonValueType(val) != JSON_STRING)
{ {
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish; goto finish;
} }
str = JsonValueAsString(val); if (!reqTok.client_secret)
if (strlen(str) > 255 || StrBlank(str))
{ {
msg = "'client_secret' is not set";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish; goto finish;
} }
val = HashMapGet(request, "send_attempt"); if (strlen(reqTok.client_secret) > 255 || StrBlank(reqTok.client_secret))
if (!val || JsonValueType(val) != JSON_INTEGER)
{ {
msg = "'client_secret' is blank or too long";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish; goto finish;
} }
val = HashMapGet(request, "next_link"); if (Int64Eq(reqTok.send_attempt, minusOne))
if (val && JsonValueType(val) != JSON_STRING)
{ {
msg = "Invalid or inexistent 'send_attempt'";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish; goto finish;
} }
val = HashMapGet(request, "id_access_token"); if (!reqTok.next_link)
if (val && JsonValueType(val) != JSON_STRING)
{ {
msg = "'next_link' is not set";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish; goto finish;
} }
if (!reqTok.id_access_token)
val = HashMapGet(request, "id_server");
if (val && JsonValueType(val) != JSON_STRING)
{ {
msg = "'id_access_token' is not set";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish;
}
if (!reqTok.id_server)
{
msg = "'id_server' is not set";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish; goto finish;
} }
if (StrEquals(type, "email")) if (StrEquals(type, "email"))
{ {
val = HashMapGet(request, "email"); if (!reqTok.email)
if (val && JsonValueType(val) != JSON_STRING)
{ {
msg = "Type is set to 'email' yet none was set";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish; goto finish;
} }
} }
else if (StrEquals(type, "msisdn")) else if (StrEquals(type, "msisdn"))
{ {
val = HashMapGet(request, "country"); if (!reqTok.country)
if (val && JsonValueType(val) != JSON_STRING)
{ {
msg = "Type is set to 'msisdn' yet no country is set";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish; goto finish;
} }
str = JsonValueAsString(val); if (strlen(reqTok.country) != 2)
if (strlen(str) != 2)
{ {
msg = "Invalid country tag, length must be 2";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish; goto finish;
} }
val = HashMapGet(request, "phone_number"); if (!reqTok.phone_number)
if (val && JsonValueType(val) != JSON_STRING)
{ {
msg = "Type is set to 'msisdn' yet phone_number is unset";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish; goto finish;
} }
} }