2022-12-10 15:41:33 +00:00
|
|
|
/*
|
2022-12-26 15:52:52 +00:00
|
|
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
2022-12-10 15:41:33 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <Routes.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
Use `Makefile`s instead of a custom script (#38)
This pull request also requires the use of the external [Cytoplasm](/Telodendria/Cytoplasm) repository by removing the in-tree copy of Cytoplasm. The increased modularity requires a little more complex build process, but is overall better. Closes #19
The appropriate documentation has been updated. Closes #18
---
Please review the developer certificate of origin:
1. The contribution was created in whole or in part by me, and I have
the right to submit it under the open source licenses of the
Telodendria project; or
1. The contribution is based upon a previous work that, to the best of
my knowledge, is covered under an appropriate open source license and
I have the right under that license to submit that work with
modifications, whether created in whole or in part by me, under the
Telodendria project license; or
1. The contribution was provided directly to me by some other person
who certified (1), (2), or (3), and I have not modified it.
1. I understand and agree that this project and the contribution are
made public and that a record of the contribution—including all
personal information I submit with it—is maintained indefinitely
and may be redistributed consistent with this project or the open
source licenses involved.
- [x] I have read the Telodendria Project development certificate of
origin, and I certify that I have permission to submit this patch
under the conditions specified in it.
Reviewed-on: https://git.telodendria.io/Telodendria/Telodendria/pulls/38
2023-11-01 16:27:45 +00:00
|
|
|
#include <Cytoplasm/Json.h>
|
|
|
|
#include <Cytoplasm/HashMap.h>
|
|
|
|
#include <Cytoplasm/Str.h>
|
|
|
|
#include <Cytoplasm/Memory.h>
|
2023-01-17 00:02:50 +00:00
|
|
|
#include <User.h>
|
2022-12-10 15:41:33 +00:00
|
|
|
|
2023-04-14 21:20:56 +00:00
|
|
|
ROUTE_IMPL(RouteLogin, path, argp)
|
2022-12-10 15:41:33 +00:00
|
|
|
{
|
2023-04-14 21:20:56 +00:00
|
|
|
RouteArgs *args = argp;
|
2023-01-17 00:02:50 +00:00
|
|
|
HashMap *request = NULL;
|
2022-12-10 15:41:33 +00:00
|
|
|
HashMap *response = NULL;
|
|
|
|
Array *enabledFlows;
|
|
|
|
HashMap *pwdFlow;
|
|
|
|
|
2023-01-17 00:02:50 +00:00
|
|
|
JsonValue *val;
|
|
|
|
|
|
|
|
HashMap *identifier;
|
|
|
|
|
|
|
|
char *deviceId = NULL;
|
|
|
|
char *initialDeviceDisplayName = NULL;
|
|
|
|
int refreshToken = 0;
|
|
|
|
|
|
|
|
char *password;
|
|
|
|
char *type;
|
2023-02-28 13:44:34 +00:00
|
|
|
UserId *userId = NULL;
|
2023-01-17 00:02:50 +00:00
|
|
|
|
|
|
|
Db *db = args->matrixArgs->db;
|
|
|
|
|
|
|
|
User *user;
|
|
|
|
UserLoginInfo *loginInfo;
|
2023-02-24 00:17:56 +00:00
|
|
|
char *fullUsername;
|
2023-01-17 00:02:50 +00:00
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
Config *config = ConfigLock(db);
|
2023-04-19 02:07:38 +00:00
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
if (!config)
|
|
|
|
{
|
|
|
|
Log(LOG_ERR, "Login endpoint failed to lock configuration.");
|
|
|
|
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
|
2023-08-05 19:26:03 +00:00
|
|
|
return MatrixErrorCreate(M_UNKNOWN, NULL);
|
2023-04-19 00:33:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 21:20:56 +00:00
|
|
|
(void) path;
|
2022-12-10 15:41:33 +00:00
|
|
|
|
|
|
|
switch (HttpRequestMethodGet(args->context))
|
|
|
|
{
|
|
|
|
case HTTP_GET:
|
|
|
|
response = HashMapCreate();
|
|
|
|
enabledFlows = ArrayCreate();
|
|
|
|
pwdFlow = HashMapCreate();
|
|
|
|
|
2023-02-24 00:17:56 +00:00
|
|
|
HashMapSet(pwdFlow, "type", JsonValueString("m.login.password"));
|
2022-12-10 15:41:33 +00:00
|
|
|
ArrayAdd(enabledFlows, JsonValueObject(pwdFlow));
|
|
|
|
HashMapSet(response, "flows", JsonValueArray(enabledFlows));
|
|
|
|
break;
|
|
|
|
case HTTP_POST:
|
2023-03-16 02:17:48 +00:00
|
|
|
request = JsonDecode(HttpServerStream(args->context));
|
2023-01-17 00:02:50 +00:00
|
|
|
if (!request)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_NOT_JSON, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = HashMapGet(request, "type");
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_MISSING_PARAM, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JsonValueType(val) != JSON_STRING)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_BAD_JSON, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = JsonValueAsString(val);
|
2023-05-06 22:34:36 +00:00
|
|
|
if (!StrEquals(type, "m.login.password"))
|
2023-01-17 00:02:50 +00:00
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_UNRECOGNIZED, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = HashMapGet(request, "identifier");
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_MISSING_PARAM, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JsonValueType(val) != JSON_OBJECT)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_BAD_JSON, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
identifier = JsonValueAsObject(val);
|
|
|
|
|
|
|
|
val = HashMapGet(identifier, "type");
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_MISSING_PARAM, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JsonValueType(val) != JSON_STRING)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_BAD_JSON, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = JsonValueAsString(val);
|
2023-05-06 22:34:36 +00:00
|
|
|
if (!StrEquals(type, "m.id.user"))
|
2023-01-17 00:02:50 +00:00
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_UNRECOGNIZED, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = HashMapGet(identifier, "user");
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_MISSING_PARAM, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JsonValueType(val) != JSON_STRING)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_BAD_JSON, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-04-19 00:33:38 +00:00
|
|
|
userId = UserIdParse(JsonValueAsString(val), config->serverName);
|
2023-02-28 13:44:34 +00:00
|
|
|
if (!userId)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_BAD_JSON, NULL);
|
2023-02-28 13:44:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-01-17 00:02:50 +00:00
|
|
|
|
2023-05-06 22:34:36 +00:00
|
|
|
if (!StrEquals(userId->server, config->serverName)
|
2023-02-28 13:44:34 +00:00
|
|
|
|| !UserExists(db, userId->localpart))
|
2023-01-17 00:02:50 +00:00
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_FORBIDDEN);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_FORBIDDEN, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = HashMapGet(request, "device_id");
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
if (JsonValueType(val) != JSON_STRING)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_BAD_JSON, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
deviceId = JsonValueAsString(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
val = HashMapGet(request, "initial_device_display_name");
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
if (JsonValueType(val) != JSON_STRING)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_BAD_JSON, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
initialDeviceDisplayName = JsonValueAsString(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
val = HashMapGet(request, "password");
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_MISSING_PARAM, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JsonValueType(val) != JSON_STRING)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_BAD_JSON, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
password = JsonValueAsString(val);
|
|
|
|
|
|
|
|
val = HashMapGet(request, "refresh_token");
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
if (JsonValueType(val) != JSON_BOOLEAN)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_BAD_JSON, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshToken = JsonValueAsBoolean(val);
|
|
|
|
}
|
|
|
|
|
2023-02-28 13:44:34 +00:00
|
|
|
user = UserLock(db, userId->localpart);
|
2023-01-17 00:02:50 +00:00
|
|
|
|
|
|
|
if (!user)
|
|
|
|
{
|
|
|
|
HttpResponseStatus(args->context, HTTP_FORBIDDEN);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_FORBIDDEN, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-03-15 13:36:49 +00:00
|
|
|
if (UserDeactivated(user))
|
|
|
|
{
|
|
|
|
UserUnlock(user);
|
|
|
|
|
|
|
|
HttpResponseStatus(args->context, HTTP_FORBIDDEN);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_USER_DEACTIVATED, NULL);
|
2023-03-15 13:36:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-17 00:02:50 +00:00
|
|
|
loginInfo = UserLogin(user, password, deviceId,
|
|
|
|
initialDeviceDisplayName, refreshToken);
|
|
|
|
|
|
|
|
if (!loginInfo)
|
|
|
|
{
|
|
|
|
UserUnlock(user);
|
|
|
|
|
|
|
|
HttpResponseStatus(args->context, HTTP_FORBIDDEN);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_FORBIDDEN, NULL);
|
2023-01-17 00:02:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
response = HashMapCreate();
|
|
|
|
|
|
|
|
HashMapSet(response, "access_token",
|
2023-02-17 03:23:25 +00:00
|
|
|
JsonValueString(loginInfo->accessToken->string));
|
2023-01-17 00:02:50 +00:00
|
|
|
HashMapSet(response, "device_id",
|
2023-02-17 03:23:25 +00:00
|
|
|
JsonValueString(loginInfo->accessToken->deviceId));
|
2023-01-17 00:02:50 +00:00
|
|
|
|
|
|
|
if (refreshToken)
|
|
|
|
{
|
|
|
|
HashMapSet(response, "expires_in_ms",
|
2023-02-17 03:23:25 +00:00
|
|
|
JsonValueInteger(loginInfo->accessToken->lifetime));
|
2023-01-17 00:02:50 +00:00
|
|
|
HashMapSet(response, "refresh_token",
|
|
|
|
JsonValueString(loginInfo->refreshToken));
|
|
|
|
}
|
|
|
|
|
2023-02-24 00:17:56 +00:00
|
|
|
fullUsername = StrConcat(4, "@", UserGetName(user), ":",
|
2023-04-19 02:07:38 +00:00
|
|
|
config->serverName);
|
2023-02-24 00:17:56 +00:00
|
|
|
HashMapSet(response, "user_id", JsonValueString(fullUsername));
|
|
|
|
Free(fullUsername);
|
2023-01-17 00:02:50 +00:00
|
|
|
|
|
|
|
HashMapSet(response, "well_known",
|
|
|
|
JsonValueObject(
|
2023-04-19 02:07:38 +00:00
|
|
|
MatrixClientWellKnown(config->baseUrl, config->identityServer)));
|
2023-01-17 00:02:50 +00:00
|
|
|
|
2023-02-24 00:17:56 +00:00
|
|
|
UserAccessTokenFree(loginInfo->accessToken);
|
|
|
|
Free(loginInfo->refreshToken);
|
2023-01-17 00:02:50 +00:00
|
|
|
Free(loginInfo);
|
2023-02-23 15:13:39 +00:00
|
|
|
|
2023-01-17 00:02:50 +00:00
|
|
|
UserUnlock(user);
|
|
|
|
|
|
|
|
break;
|
2022-12-10 15:41:33 +00:00
|
|
|
default:
|
|
|
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
2023-08-05 19:26:03 +00:00
|
|
|
response = MatrixErrorCreate(M_UNRECOGNIZED, NULL);
|
2022-12-10 15:41:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-03-06 22:09:57 +00:00
|
|
|
UserIdFree(userId);
|
2023-01-17 00:02:50 +00:00
|
|
|
JsonFree(request);
|
2023-04-19 00:33:38 +00:00
|
|
|
ConfigUnlock(config);
|
2023-04-25 18:37:25 +00:00
|
|
|
|
2022-12-10 15:41:33 +00:00
|
|
|
return response;
|
|
|
|
}
|