telodendria/src/Routes/RouteLogin.c

255 lines
8.2 KiB
C
Raw Normal View History

2022-12-10 15:41:33 +00:00
/*
* Copyright (C) 2022-2024 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>
#include <Schema/LoginRequest.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&mdash;including all personal information I submit with it&mdash;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
ROUTE_IMPL(RouteLogin, path, argp)
2022-12-10 15:41:33 +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;
LoginRequest loginRequest;
LoginRequestUserIdentifier userIdentifier;
2023-01-17 00:02:50 +00:00
UserId *userId = NULL;
2023-01-17 00:02:50 +00:00
Db *db = args->matrixArgs->db;
User *user;
UserLoginInfo *loginInfo;
char *fullUsername;
2023-01-17 00:02:50 +00:00
char *type;
char *initialDeviceDisplayName;
char *deviceId;
char *password;
int refreshToken;
char *msg;
Config *config = ConfigLock(db);
if (!config)
{
Log(LOG_ERR, "Login endpoint failed to lock configuration.");
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
return MatrixErrorCreate(M_UNKNOWN, NULL);
}
(void) path;
2022-12-10 15:41:33 +00:00
memset(&loginRequest, 0, sizeof(LoginRequest));
memset(&userIdentifier, 0, sizeof(LoginRequestUserIdentifier));
2022-12-10 15:41:33 +00:00
switch (HttpRequestMethodGet(args->context))
{
case HTTP_GET:
response = HashMapCreate();
enabledFlows = ArrayCreate();
pwdFlow = HashMapCreate();
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:
request = JsonDecode(HttpServerStream(args->context));
2023-01-17 00:02:50 +00:00
if (!request)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_NOT_JSON, NULL);
2023-01-17 00:02:50 +00:00
break;
}
if (!LoginRequestFromJson(request, &loginRequest, &msg))
2023-01-17 00:02:50 +00:00
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, msg);
2023-01-17 00:02:50 +00:00
break;
}
if (loginRequest.type != REQUEST_TYPE_PASSWORD)
2023-01-17 00:02:50 +00:00
{
msg = "Unsupported login type.";
2023-01-17 00:02:50 +00:00
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_UNRECOGNIZED, msg);
2023-01-17 00:02:50 +00:00
break;
}
identifier = loginRequest.identifier;
2023-01-17 00:02:50 +00:00
val = HashMapGet(identifier, "type");
if (!val)
{
msg = "No login identifier type set.";
2023-01-17 00:02:50 +00:00
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_MISSING_PARAM, NULL);
2023-01-17 00:02:50 +00:00
break;
}
if (JsonValueType(val) != JSON_STRING)
{
msg = "Invalid login identifier type.";
2023-01-17 00:02:50 +00:00
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, msg);
2023-01-17 00:02:50 +00:00
break;
}
type = JsonValueAsString(val);
if (!StrEquals(type, "m.id.user"))
2023-01-17 00:02:50 +00:00
{
msg = "Invalid login identifier type.";
2023-01-17 00:02:50 +00:00
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_UNRECOGNIZED, msg);
2023-01-17 00:02:50 +00:00
break;
}
if (!LoginRequestUserIdentifierFromJson(identifier,
&userIdentifier, &msg))
2023-01-17 00:02:50 +00:00
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, msg);
2023-01-17 00:02:50 +00:00
break;
}
userId = UserIdParse(userIdentifier.user, config->serverName);
if (!userId)
{
msg = "Invalid user ID.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, msg);
break;
}
2023-01-17 00:02:50 +00:00
if (!StrEquals(userId->server, config->serverName)
|| !UserExists(db, userId->localpart))
2023-01-17 00:02:50 +00:00
{
msg = "Unknown user ID.";
2023-01-17 00:02:50 +00:00
HttpResponseStatus(args->context, HTTP_FORBIDDEN);
response = MatrixErrorCreate(M_FORBIDDEN, msg);
2023-01-17 00:02:50 +00:00
break;
}
deviceId = loginRequest.device_id;
2023-01-17 00:02:50 +00:00
initialDeviceDisplayName =loginRequest.initial_device_display_name;
password = loginRequest.password;
refreshToken = loginRequest.refresh_token;
2023-01-17 00:02:50 +00:00
user = UserLock(db, userId->localpart);
2023-01-17 00:02:50 +00:00
if (!user)
{
msg = "Couldn't lock user.";
2023-01-17 00:02:50 +00:00
HttpResponseStatus(args->context, HTTP_FORBIDDEN);
response = MatrixErrorCreate(M_FORBIDDEN, msg);
2023-01-17 00:02:50 +00:00
break;
}
if (UserDeactivated(user))
{
UserUnlock(user);
HttpResponseStatus(args->context, HTTP_FORBIDDEN);
response = MatrixErrorCreate(M_USER_DEACTIVATED, NULL);
break;
}
2023-01-17 00:02:50 +00:00
loginInfo = UserLogin(user, password, deviceId,
initialDeviceDisplayName, refreshToken);
if (!loginInfo)
{
msg = "Invalid creditentials for user.";
2023-01-17 00:02:50 +00:00
UserUnlock(user);
HttpResponseStatus(args->context, HTTP_FORBIDDEN);
response = MatrixErrorCreate(M_FORBIDDEN, msg);
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));
}
fullUsername = StrConcat(4, "@", UserGetName(user), ":",
config->serverName);
HashMapSet(response, "user_id", JsonValueString(fullUsername));
Free(fullUsername);
2023-01-17 00:02:50 +00:00
HashMapSet(response, "well_known",
JsonValueObject(
MatrixClientWellKnown(config->baseUrl, config->identityServer)));
2023-01-17 00:02:50 +00:00
UserAccessTokenFree(loginInfo->accessToken);
Free(loginInfo->refreshToken);
2023-01-17 00:02:50 +00:00
Free(loginInfo);
2023-01-17 00:02:50 +00:00
UserUnlock(user);
break;
2022-12-10 15:41:33 +00:00
default:
msg = "Route only accepts GET and POST.";
2022-12-10 15:41:33 +00:00
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_UNRECOGNIZED, msg);
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);
ConfigUnlock(config);
LoginRequestFree(&loginRequest);
LoginRequestUserIdentifierFree(&userIdentifier);
2022-12-10 15:41:33 +00:00
return response;
}