From 866343071a281bbae6a0536d4ae9c090906509ea Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Sat, 22 Apr 2023 00:59:03 +0000 Subject: [PATCH] Continued work on UIA fallback. --- src/Routes/RouteUiaFallback.c | 80 ++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 6 deletions(-) diff --git a/src/Routes/RouteUiaFallback.c b/src/Routes/RouteUiaFallback.c index d3d150b..b34c040 100644 --- a/src/Routes/RouteUiaFallback.c +++ b/src/Routes/RouteUiaFallback.c @@ -23,6 +23,10 @@ */ #include #include +#include +#include +#include +#include ROUTE_IMPL(RouteUiaFallback, path, argp) { @@ -39,6 +43,60 @@ ROUTE_IMPL(RouteUiaFallback, path, argp) 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"); if (!sessionId) { @@ -75,17 +133,26 @@ ROUTE_IMPL(RouteUiaFallback, path, argp) HtmlBeginForm(stream, "auth-form"); StreamPuts(stream, "" - "" + "" "
" ""); HtmlEndForm(stream); HtmlBeginJs(stream); - /* TODO */ - StreamPuts(stream, + StreamPrintf(stream, "function buildRequest() {" - " setFormError('Not implemented yet.');" - " return false;" - "}"); + " let token = document.getElementById('token').value;" + " if (!token) { " + " setFormError('Please specify a registration token.');" + " return false;" + " }" + " return {" + " auth: {" + " type: '%s'," + " session: '%s'," + " token: token" + " }" + " };" + "}", authType, sessionId); HtmlEndJs(stream); } /* @@ -112,6 +179,7 @@ ROUTE_IMPL(RouteUiaFallback, path, argp) " setFormError('Client error.');" " }" " } else {" + " console.log(xhr.responseText);" " let r = JSON.parse(xhr.responseText);" " setFormError(`${r.errcode}: ${r.error}`);" " }"