Add RouteLogin()

This commit is contained in:
Jordan Bancino 2022-12-10 15:41:33 +00:00
parent 3b636a9d22
commit 8acc21db7c
4 changed files with 121 additions and 21 deletions

69
src/Routes/RouteLogin.c Normal file
View file

@ -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 <Routes.h>
#include <string.h>
#include <Memory.h>
#include <Json.h>
#include <HashMap.h>
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;
}

View file

@ -29,22 +29,20 @@
#include <Json.h> #include <Json.h>
#include <HashMap.h> #include <HashMap.h>
ROUTE(RouteMatrix) ROUTE_IMPL(RouteMatrix, args)
{ {
HashMap *response = NULL; 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(args->path) <= 1)
if (!MATRIX_PATH_EQUALS(pathPart, "client") || MATRIX_PATH_PARTS(path) != 1)
{ {
Free(pathPart); Free(pathPart);
HttpResponseStatus(context, HTTP_NOT_FOUND); HttpResponseStatus(args->context, HTTP_NOT_FOUND);
return MatrixErrorCreate(M_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND);
} }
Free(pathPart); Free(pathPart);
pathPart = MATRIX_PATH_POP(path); pathPart = MATRIX_PATH_POP(args->path);
if (MATRIX_PATH_EQUALS(pathPart, "versions")) if (MATRIX_PATH_EQUALS(pathPart, "versions"))
{ {
@ -52,17 +50,36 @@ ROUTE(RouteMatrix)
Free(pathPart); Free(pathPart);
ArrayAdd(versions, JsonValueString(UtilStringDuplicate("v1.4"))); ArrayAdd(versions, JsonValueString(UtilStringDuplicate("v1.5")));
response = HashMapCreate(); response = HashMapCreate();
HashMapSet(response, "versions", JsonValueArray(versions)); HashMapSet(response, "versions", JsonValueArray(versions));
return response; 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 else
{ {
Free(pathPart); Free(pathPart);
HttpResponseStatus(context, HTTP_NOT_FOUND); HttpResponseStatus(args->context, HTTP_NOT_FOUND);
return MatrixErrorCreate(M_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND);
} }
} }

View file

@ -29,20 +29,20 @@
#include <Json.h> #include <Json.h>
#include <HashMap.h> #include <HashMap.h>
ROUTE(RouteWellKnown) ROUTE_IMPL(RouteWellKnown, args)
{ {
HashMap *response = NULL; 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); Free(pathPart);
HttpResponseStatus(context, HTTP_NOT_FOUND); HttpResponseStatus(args->context, HTTP_NOT_FOUND);
return MatrixErrorCreate(M_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND);
} }
Free(pathPart); Free(pathPart);
pathPart = MATRIX_PATH_POP(path); pathPart = MATRIX_PATH_POP(args->path);
if (MATRIX_PATH_EQUALS(pathPart, "client")) if (MATRIX_PATH_EQUALS(pathPart, "client"))
{ {
@ -52,14 +52,14 @@ ROUTE(RouteWellKnown)
response = HashMapCreate(); 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)); HashMapSet(response, "m.homeserver", JsonValueObject(homeserver));
if (args->config->identityServer) if (args->matrixArgs->config->identityServer)
{ {
HashMap *identityServer = HashMapCreate(); 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); HashMapSet(response, "m.identity_server", identityServer);
} }
@ -68,7 +68,7 @@ ROUTE(RouteWellKnown)
else else
{ {
Free(pathPart); Free(pathPart);
HttpResponseStatus(context, HTTP_NOT_FOUND); HttpResponseStatus(args->context, HTTP_NOT_FOUND);
return MatrixErrorCreate(M_NOT_FOUND); return MatrixErrorCreate(M_NOT_FOUND);
} }
} }

View file

@ -31,9 +31,23 @@
#include <Matrix.h> #include <Matrix.h>
#define ROUTE(name) HashMap * name(MatrixHttpHandlerArgs *args, HttpServerContext *context, Array *path) typedef struct RouteArgs
{
MatrixHttpHandlerArgs *matrixArgs;
HttpServerContext *context;
Array *path;
} RouteArgs;
extern ROUTE(RouteWellKnown); #define ROUTE(name) \
extern ROUTE(RouteMatrix); extern HashMap * \
name(RouteArgs *)
#define ROUTE_IMPL(name, argsName) \
HashMap * \
name(RouteArgs * argsName)
ROUTE(RouteWellKnown);
ROUTE(RouteMatrix);
ROUTE(RouteLogin);
#endif #endif