telodendria/src/Routes/RouteStaticLogin.c

106 lines
3.9 KiB
C
Raw Normal View History

2023-02-28 17:52:09 +00:00
/*
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net>
2023-02-28 17:52:09 +00:00
*
* 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>
2023-02-28 17:52:09 +00:00
#include <Html.h>
ROUTE_IMPL(RouteStaticLogin, path, argp)
2023-02-28 17:52:09 +00:00
{
RouteArgs *args = argp;
Stream *stream = HttpServerStream(args->context);
(void) path;
HttpResponseHeader(args->context, "Content-Type", "text/html");
HttpSendHeaders(args->context);
2023-02-28 17:52:09 +00:00
HtmlBegin(stream, "Log In");
HtmlBeginForm(stream, "login-form");
StreamPuts(stream,
"<label for=\"user\">Username:</label>"
"<input type=\"text\" id=\"user\">"
"<label for=\"password\">Password:</label>"
"<input type=\"password\" id=\"password\">"
"<br>"
"<input type=\"submit\" value=\"Log In\">"
2023-02-28 17:52:09 +00:00
);
HtmlEndForm(stream);
2023-02-28 17:52:09 +00:00
2023-03-02 22:06:33 +00:00
HtmlBeginJs(stream);
StreamPuts(stream,
"function buildRequest(user, pass) {"
" var d = findGetParameter('device_id');"
2023-03-01 19:03:42 +00:00
" var i = findGetParameter('initial_device_display_name');"
" var r = findGetParameter('refresh_token') === 'true';"
" var request = {};"
" request['type'] = 'm.login.password';"
" request['identifier'] = {"
" type: 'm.id.user',"
" user: user"
" };"
" request['password'] = pass;"
" if (d) request['device_id'] = d;"
" if (i) request['initial_device_display_name'] = i;"
" if (r) request['refresh_token'] = r;"
" return request;"
"}"
2023-03-01 19:03:42 +00:00
);
StreamPuts(stream,
"function processResponse(xhr) {"
" if (xhr.readyState == 4) {"
" var r = JSON.parse(xhr.responseText);"
" if (!r.error) {"
" if (window.onLogin) {"
" window.onLogin(r);"
" } else {"
" setFormError('Client error.');"
" }"
" } else {"
" setFormError(r.errcode + ': ' + r.error);"
" }"
" }"
"}"
2023-03-01 19:03:42 +00:00
);
StreamPuts(stream,
"onFormSubmit('login-form', (frm) => {"
" var user = document.getElementById('user').value;"
" var pass = document.getElementById('password').value;"
" if (!user || !pass) {"
" setFormError('Please provide a username and password.');"
" return;"
" }"
" setFormError(null);"
" var request = buildRequest(user, pass);"
" jsonRequest('POST', '/_matrix/client/v3/login', request, processResponse);"
"});"
2023-02-28 17:52:09 +00:00
);
2023-03-02 22:06:33 +00:00
HtmlEndJs(stream);
2023-02-28 17:52:09 +00:00
HtmlEnd(stream);
return NULL;
2023-02-28 17:52:09 +00:00
}