From cc95c10f446a3f5b1c919b352a14b5df5a8ff606 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Mon, 16 Jan 2023 22:02:08 +0000 Subject: [PATCH] Move client well-known generation to MatrixClientWellKnown() function. We'll be using this for client login. --- TODO.txt | 4 ++-- src/Matrix.c | 28 ++++++++++++++++++++++++++++ src/Routes/RouteWellKnown.c | 20 ++------------------ src/include/Matrix.h | 3 ++- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/TODO.txt b/TODO.txt index ab5c04d..c9a269a 100644 --- a/TODO.txt +++ b/TODO.txt @@ -17,10 +17,10 @@ Milestone: v0.2.0 [x] Move String functions to a new String.h [x] Make StringConcat use varargs [x] Look into seeding random strings (possibly create Random.h?) -[~] User registration +[x] User registration [x] Username validation [x] Password hashing - [~] User API + [x] User API [x] Document MemoryHexDump() [x] Document DbExists() diff --git a/src/Matrix.c b/src/Matrix.c index 2eb2707..5a0ab2e 100644 --- a/src/Matrix.c +++ b/src/Matrix.c @@ -358,3 +358,31 @@ MatrixRateLimit(HttpServerContext * context, Db * db) (void) db; return NULL; } + +HashMap * +MatrixClientWellKnown(char *base, char *identity) +{ + HashMap *response; + HashMap *homeserver; + + if (!base) + { + return NULL; + } + + response = HashMapCreate(); + homeserver = HashMapCreate(); + + HashMapSet(homeserver, "base_url", JsonValueString(StrDuplicate(base))); + HashMapSet(response, "m.homeserver", JsonValueObject(homeserver)); + + if (identity) + { + HashMap *identityServer = HashMapCreate(); + + HashMapSet(identityServer, "base_url", JsonValueString(StrDuplicate(identity))); + HashMapSet(response, "m.identity_server", identityServer); + } + + return response; +} diff --git a/src/Routes/RouteWellKnown.c b/src/Routes/RouteWellKnown.c index 29f24fd..6d23b85 100644 --- a/src/Routes/RouteWellKnown.c +++ b/src/Routes/RouteWellKnown.c @@ -32,7 +32,6 @@ ROUTE_IMPL(RouteWellKnown, args) { - HashMap *response = NULL; char *pathPart = MATRIX_PATH_POP(args->path); if (!MATRIX_PATH_EQUALS(pathPart, "matrix") || MATRIX_PATH_PARTS(args->path) != 1) @@ -47,24 +46,9 @@ ROUTE_IMPL(RouteWellKnown, args) if (MATRIX_PATH_EQUALS(pathPart, "client")) { - HashMap *homeserver = HashMapCreate(); - Free(pathPart); - - response = HashMapCreate(); - - HashMapSet(homeserver, "base_url", JsonValueString(StrDuplicate(args->matrixArgs->config->baseUrl))); - HashMapSet(response, "m.homeserver", JsonValueObject(homeserver)); - - if (args->matrixArgs->config->identityServer) - { - HashMap *identityServer = HashMapCreate(); - - HashMapSet(identityServer, "base_url", JsonValueString(StrDuplicate(args->matrixArgs->config->identityServer))); - HashMapSet(response, "m.identity_server", identityServer); - } - - return response; + return MatrixClientWellKnown(args->matrixArgs->config->baseUrl, + args->matrixArgs->config->identityServer); } else { diff --git a/src/include/Matrix.h b/src/include/Matrix.h index ca519d6..3d5bae8 100644 --- a/src/include/Matrix.h +++ b/src/include/Matrix.h @@ -86,6 +86,7 @@ extern HashMap * extern HashMap * MatrixRateLimit(HttpServerContext *, Db *); - +extern HashMap * + MatrixClientWellKnown(char *, char *); #endif