From 8acc21db7c7b74f76af078a926b8ae521aaca6ed Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Sat, 10 Dec 2022 15:41:33 +0000 Subject: [PATCH] Add RouteLogin() --- src/Routes/RouteLogin.c | 69 +++++++++++++++++++++++++++++++++++++ src/Routes/RouteMatrix.c | 35 ++++++++++++++----- src/Routes/RouteWellKnown.c | 18 +++++----- src/include/Routes.h | 20 +++++++++-- 4 files changed, 121 insertions(+), 21 deletions(-) create mode 100644 src/Routes/RouteLogin.c diff --git a/src/Routes/RouteLogin.c b/src/Routes/RouteLogin.c new file mode 100644 index 0000000..6a8a26a --- /dev/null +++ b/src/Routes/RouteLogin.c @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net> + * + * 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 + +#include + +#include +#include +#include + +ROUTE_IMPL(RouteLogin, args) +{ + HashMap *response = NULL; + Array *enabledFlows; + HashMap *pwdFlow; + + Log(args->matrixArgs->lc, LOG_INFO, "Entered RouteLogin()"); + + if (MATRIX_PATH_PARTS(args->path) > 0) + { + HttpResponseStatus(args->context, HTTP_NOT_FOUND); + return MatrixErrorCreate(M_NOT_FOUND); + } + + switch (HttpRequestMethodGet(args->context)) + { + case HTTP_GET: + response = HashMapCreate(); + enabledFlows = ArrayCreate(); + pwdFlow = HashMapCreate(); + + HashMapSet(pwdFlow, "type", + JsonValueString(UtilStringDuplicate("m.login.password"))); + ArrayAdd(enabledFlows, JsonValueObject(pwdFlow)); + HashMapSet(response, "flows", JsonValueArray(enabledFlows)); + + break; + case HTTP_POST: + /* TODO */ + default: + HttpResponseStatus(args->context, HTTP_BAD_REQUEST); + response = MatrixErrorCreate(M_UNRECOGNIZED); + break; + } + + Log(args->matrixArgs->lc, LOG_INFO, "Exitting RouteLogin()"); + return response; +} diff --git a/src/Routes/RouteMatrix.c b/src/Routes/RouteMatrix.c index 5641463..a4e41c6 100644 --- a/src/Routes/RouteMatrix.c +++ b/src/Routes/RouteMatrix.c @@ -29,22 +29,20 @@ #include #include -ROUTE(RouteMatrix) +ROUTE_IMPL(RouteMatrix, args) { HashMap *response = NULL; - char *pathPart = MATRIX_PATH_POP(path); + char *pathPart = MATRIX_PATH_POP(args->path); - (void) args; - - if (!MATRIX_PATH_EQUALS(pathPart, "client") || MATRIX_PATH_PARTS(path) != 1) + if (!MATRIX_PATH_EQUALS(pathPart, "client") || MATRIX_PATH_PARTS(args->path) <= 1) { Free(pathPart); - HttpResponseStatus(context, HTTP_NOT_FOUND); + HttpResponseStatus(args->context, HTTP_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND); } Free(pathPart); - pathPart = MATRIX_PATH_POP(path); + pathPart = MATRIX_PATH_POP(args->path); if (MATRIX_PATH_EQUALS(pathPart, "versions")) { @@ -52,17 +50,36 @@ ROUTE(RouteMatrix) Free(pathPart); - ArrayAdd(versions, JsonValueString(UtilStringDuplicate("v1.4"))); + ArrayAdd(versions, JsonValueString(UtilStringDuplicate("v1.5"))); response = HashMapCreate(); HashMapSet(response, "versions", JsonValueArray(versions)); return response; } + else if (MATRIX_PATH_EQUALS(pathPart, "v3") || + MATRIX_PATH_EQUALS(pathPart, "r0")) + { + Free(pathPart); + pathPart = MATRIX_PATH_POP(args->path); + + if (MATRIX_PATH_EQUALS(pathPart, "login")) + { + response = RouteLogin(args); + } + else + { + HttpResponseStatus(args->context, HTTP_NOT_FOUND); + response = MatrixErrorCreate(M_NOT_FOUND); + } + + Free(pathPart); + return response; + } else { Free(pathPart); - HttpResponseStatus(context, HTTP_NOT_FOUND); + HttpResponseStatus(args->context, HTTP_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND); } } diff --git a/src/Routes/RouteWellKnown.c b/src/Routes/RouteWellKnown.c index a4d6d45..24b8362 100644 --- a/src/Routes/RouteWellKnown.c +++ b/src/Routes/RouteWellKnown.c @@ -29,20 +29,20 @@ #include #include -ROUTE(RouteWellKnown) +ROUTE_IMPL(RouteWellKnown, args) { HashMap *response = NULL; - char *pathPart = MATRIX_PATH_POP(path); + char *pathPart = MATRIX_PATH_POP(args->path); - if (!MATRIX_PATH_EQUALS(pathPart, "matrix") || MATRIX_PATH_PARTS(path) != 1) + if (!MATRIX_PATH_EQUALS(pathPart, "matrix") || MATRIX_PATH_PARTS(args->path) != 1) { Free(pathPart); - HttpResponseStatus(context, HTTP_NOT_FOUND); + HttpResponseStatus(args->context, HTTP_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND); } Free(pathPart); - pathPart = MATRIX_PATH_POP(path); + pathPart = MATRIX_PATH_POP(args->path); if (MATRIX_PATH_EQUALS(pathPart, "client")) { @@ -52,14 +52,14 @@ ROUTE(RouteWellKnown) response = HashMapCreate(); - HashMapSet(homeserver, "base_url", JsonValueString(UtilStringDuplicate(args->config->baseUrl))); + HashMapSet(homeserver, "base_url", JsonValueString(UtilStringDuplicate(args->matrixArgs->config->baseUrl))); HashMapSet(response, "m.homeserver", JsonValueObject(homeserver)); - if (args->config->identityServer) + if (args->matrixArgs->config->identityServer) { HashMap *identityServer = HashMapCreate(); - HashMapSet(identityServer, "base_url", JsonValueString(UtilStringDuplicate(args->config->identityServer))); + HashMapSet(identityServer, "base_url", JsonValueString(UtilStringDuplicate(args->matrixArgs->config->identityServer))); HashMapSet(response, "m.identity_server", identityServer); } @@ -68,7 +68,7 @@ ROUTE(RouteWellKnown) else { Free(pathPart); - HttpResponseStatus(context, HTTP_NOT_FOUND); + HttpResponseStatus(args->context, HTTP_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND); } } diff --git a/src/include/Routes.h b/src/include/Routes.h index dd5ad24..c84eee0 100644 --- a/src/include/Routes.h +++ b/src/include/Routes.h @@ -31,9 +31,23 @@ #include -#define ROUTE(name) HashMap * name(MatrixHttpHandlerArgs *args, HttpServerContext *context, Array *path) +typedef struct RouteArgs +{ + MatrixHttpHandlerArgs *matrixArgs; + HttpServerContext *context; + Array *path; +} RouteArgs; -extern ROUTE(RouteWellKnown); -extern ROUTE(RouteMatrix); +#define ROUTE(name) \ + extern HashMap * \ + name(RouteArgs *) + +#define ROUTE_IMPL(name, argsName) \ + HashMap * \ + name(RouteArgs * argsName) + +ROUTE(RouteWellKnown); +ROUTE(RouteMatrix); +ROUTE(RouteLogin); #endif