[MOD] Use the j2s endpoint for POST /login

This seems to leak a bit of memory, but it seems like it's related
to Telodendria/Cytoplasm#17, hence
why I'm not fixing it there.
This commit is contained in:
lda 2023-11-18 19:38:25 +01:00
parent cfe5ae9e78
commit 69b1013654
Signed by: lda
GPG key ID: 6898757653ABE3E6

View file

@ -25,6 +25,8 @@
#include <string.h> #include <string.h>
#include <Schema/LoginRequest.h>
#include <Cytoplasm/Json.h> #include <Cytoplasm/Json.h>
#include <Cytoplasm/HashMap.h> #include <Cytoplasm/HashMap.h>
#include <Cytoplasm/Str.h> #include <Cytoplasm/Str.h>
@ -43,12 +45,9 @@ ROUTE_IMPL(RouteLogin, path, argp)
HashMap *identifier; HashMap *identifier;
char *deviceId = NULL; LoginRequest loginRequest;
char *initialDeviceDisplayName = NULL; LoginRequestUserIdentifier userIdentifier;
int refreshToken = 0;
char *password;
char *type;
UserId *userId = NULL; UserId *userId = NULL;
Db *db = args->matrixArgs->db; Db *db = args->matrixArgs->db;
@ -57,6 +56,14 @@ ROUTE_IMPL(RouteLogin, path, argp)
UserLoginInfo *loginInfo; UserLoginInfo *loginInfo;
char *fullUsername; char *fullUsername;
char *type;
char *initialDeviceDisplayName;
char *deviceId;
char *password;
int refreshToken;
char *msg;
Config *config = ConfigLock(db); Config *config = ConfigLock(db);
if (!config) if (!config)
@ -68,6 +75,18 @@ ROUTE_IMPL(RouteLogin, path, argp)
(void) path; (void) path;
loginRequest.user = NULL;
loginRequest.address = NULL;
loginRequest.token = NULL;
loginRequest.medium = NULL;
loginRequest.password = NULL;
loginRequest.device_id = NULL;
loginRequest.initial_device_display_name = NULL;
loginRequest.identifier = NULL;
userIdentifier.user = NULL;
userIdentifier.type = NULL;
switch (HttpRequestMethodGet(args->context)) switch (HttpRequestMethodGet(args->context))
{ {
case HTTP_GET: case HTTP_GET:
@ -88,45 +107,21 @@ ROUTE_IMPL(RouteLogin, path, argp)
break; break;
} }
val = HashMapGet(request, "type"); if (!LoginRequestFromJson(request, &loginRequest, &msg))
if (!val)
{ {
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_MISSING_PARAM, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
break; break;
} }
if (JsonValueType(val) != JSON_STRING) if (loginRequest.type != REQUEST_TYPE_PASSWORD)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL);
break;
}
type = JsonValueAsString(val);
if (!StrEquals(type, "m.login.password"))
{ {
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_UNRECOGNIZED, NULL); response = MatrixErrorCreate(M_UNRECOGNIZED, NULL);
break; break;
} }
val = HashMapGet(request, "identifier"); identifier = loginRequest.identifier;
if (!val)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_MISSING_PARAM, NULL);
break;
}
if (JsonValueType(val) != JSON_OBJECT)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL);
break;
}
identifier = JsonValueAsObject(val);
val = HashMapGet(identifier, "type"); val = HashMapGet(identifier, "type");
if (!val) if (!val)
@ -150,23 +145,16 @@ ROUTE_IMPL(RouteLogin, path, argp)
response = MatrixErrorCreate(M_UNRECOGNIZED, NULL); response = MatrixErrorCreate(M_UNRECOGNIZED, NULL);
break; break;
} }
if (!LoginRequestUserIdentifierFromJson(identifier,
val = HashMapGet(identifier, "user"); &userIdentifier, &msg))
if (!val)
{ {
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_MISSING_PARAM, NULL); response = MatrixErrorCreate(M_BAD_JSON, msg);
break; break;
} }
if (JsonValueType(val) != JSON_STRING)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL);
break;
}
userId = UserIdParse(JsonValueAsString(val), config->serverName); userId = UserIdParse(userIdentifier.user, config->serverName);
if (!userId) if (!userId)
{ {
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
@ -182,61 +170,11 @@ ROUTE_IMPL(RouteLogin, path, argp)
break; break;
} }
val = HashMapGet(request, "device_id"); deviceId = loginRequest.device_id;
if (val)
{
if (JsonValueType(val) != JSON_STRING)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL);
break;
}
deviceId = JsonValueAsString(val); initialDeviceDisplayName =loginRequest.initial_device_display_name;
} password = loginRequest.password;
refreshToken = loginRequest.refresh_token;
val = HashMapGet(request, "initial_device_display_name");
if (val)
{
if (JsonValueType(val) != JSON_STRING)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL);
break;
}
initialDeviceDisplayName = JsonValueAsString(val);
}
val = HashMapGet(request, "password");
if (!val)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_MISSING_PARAM, NULL);
break;
}
if (JsonValueType(val) != JSON_STRING)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL);
break;
}
password = JsonValueAsString(val);
val = HashMapGet(request, "refresh_token");
if (val)
{
if (JsonValueType(val) != JSON_BOOLEAN)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, NULL);
break;
}
refreshToken = JsonValueAsBoolean(val);
}
user = UserLock(db, userId->localpart); user = UserLock(db, userId->localpart);
@ -309,5 +247,8 @@ ROUTE_IMPL(RouteLogin, path, argp)
JsonFree(request); JsonFree(request);
ConfigUnlock(config); ConfigUnlock(config);
LoginRequestFree(&loginRequest);
LoginRequestUserIdentifierFree(&userIdentifier);
return response; return response;
} }