Continued work on UIA fallback.

This commit is contained in:
Jordan Bancino 2023-04-22 00:59:03 +00:00
parent 6a5d89e14b
commit 866343071a

View file

@ -23,6 +23,10 @@
*/ */
#include <Routes.h> #include <Routes.h>
#include <Html.h> #include <Html.h>
#include <Json.h>
#include <Config.h>
#include <Uia.h>
#include <Str.h>
ROUTE_IMPL(RouteUiaFallback, path, argp) ROUTE_IMPL(RouteUiaFallback, path, argp)
{ {
@ -39,6 +43,60 @@ ROUTE_IMPL(RouteUiaFallback, path, argp)
return MatrixErrorCreate(M_UNKNOWN); return MatrixErrorCreate(M_UNKNOWN);
} }
if (HttpRequestMethodGet(args->context) == HTTP_POST)
{
HashMap *request;
HashMap *response;
int uiaResult;
Config *config;
Array *flows;
config = ConfigLock(args->matrixArgs->db);
if (!config)
{
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
return MatrixErrorCreate(M_UNKNOWN);
}
request = JsonDecode(HttpServerStream(args->context));
if (!request)
{
ConfigUnlock(config);
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
return MatrixErrorCreate(M_NOT_JSON);
}
Log(LOG_DEBUG, "Building flows...");
flows = ArrayCreate();
ArrayAdd(flows, UiaStageBuild(authType, NULL));
Log(LOG_DEBUG, "about to UiaComplete()...");
uiaResult = UiaComplete(flows, args->context,
args->matrixArgs->db, request, &response, config);
Log(LOG_DEBUG, "Freeing flows...");
UiaFlowsFree(flows);
Log(LOG_DEBUG, "Completed UIA.");
if (uiaResult < 0)
{
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
response = MatrixErrorCreate(M_UNKNOWN);
}
else if (uiaResult)
{
response = HashMapCreate();
}
JsonFree(request);
ConfigUnlock(config);
return response;
}
else if (HttpRequestMethodGet(args->context) != HTTP_GET)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
return MatrixErrorCreate(M_UNRECOGNIZED);
}
sessionId = HashMapGet(requestParams, "session"); sessionId = HashMapGet(requestParams, "session");
if (!sessionId) if (!sessionId)
{ {
@ -75,17 +133,26 @@ ROUTE_IMPL(RouteUiaFallback, path, argp)
HtmlBeginForm(stream, "auth-form"); HtmlBeginForm(stream, "auth-form");
StreamPuts(stream, StreamPuts(stream,
"<label for=\"token\">Registration Token:</label>" "<label for=\"token\">Registration Token:</label>"
"<input type=\"text\" id=\"token\">" "<input type=\"password\" id=\"token\">"
"<br>" "<br>"
"<input type=\"submit\" value=\"Authenticate\">"); "<input type=\"submit\" value=\"Authenticate\">");
HtmlEndForm(stream); HtmlEndForm(stream);
HtmlBeginJs(stream); HtmlBeginJs(stream);
/* TODO */ StreamPrintf(stream,
StreamPuts(stream,
"function buildRequest() {" "function buildRequest() {"
" setFormError('Not implemented yet.');" " let token = document.getElementById('token').value;"
" return false;" " if (!token) { "
"}"); " setFormError('Please specify a registration token.');"
" return false;"
" }"
" return {"
" auth: {"
" type: '%s',"
" session: '%s',"
" token: token"
" }"
" };"
"}", authType, sessionId);
HtmlEndJs(stream); HtmlEndJs(stream);
} }
/* /*
@ -112,6 +179,7 @@ ROUTE_IMPL(RouteUiaFallback, path, argp)
" setFormError('Client error.');" " setFormError('Client error.');"
" }" " }"
" } else {" " } else {"
" console.log(xhr.responseText);"
" let r = JSON.parse(xhr.responseText);" " let r = JSON.parse(xhr.responseText);"
" setFormError(`${r.errcode}: ${r.error}`);" " setFormError(`${r.errcode}: ${r.error}`);"
" }" " }"