Implement static login page.

This commit is contained in:
Jordan Bancino 2023-03-01 19:03:42 +00:00
parent 55b3728e7e
commit ce6d483135
5 changed files with 92 additions and 5 deletions

View file

@ -23,7 +23,7 @@ Milestone: v0.2.0
[x] Logout [x] Logout
[x] Delete refresh token if present [x] Delete refresh token if present
[ ] Logout all [ ] Logout all
[~] Login fallback (static HTML page) [x] Login fallback (static HTML page)
[~] User Interactive [~] User Interactive
[x] Passwords [x] Passwords
[x] Caller builds flows [x] Caller builds flows

View file

@ -39,15 +39,101 @@ StaticLogin(FILE * stream)
"<br>" "<br>"
"<input type=\"submit\" value=\"Log In\">" "<input type=\"submit\" value=\"Log In\">"
"</form>" "</form>"
"<style>"
"#error-msg {"
" display: none;"
" color: red;"
" text-align: center;"
" font-weight: bold;"
" font-size: larger;"
"}"
"</style>"
"<p id=\"error-msg\"></p>"
"</div>" "</div>"
); );
fprintf(stream, fprintf(stream,
"<script>" "<script>"
"function findGetParameter(parameterName) {"
" var result = null;"
" var tmp = [];"
" var items = location.search.substr(1).split(\"&\");"
" for (var index = 0; index < items.length; index++) {"
" tmp = items[index].split(\"=\");"
" if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);"
" }"
" return result;"
"}"
"function setError(msg) {"
" var err = document.getElementById('error-msg');"
" if (msg) {"
" err.style.display = 'block';"
" err.innerHTML = msg;"
" } else {"
" err.style.display = 'none';"
" }"
"}"
);
fprintf(stream,
"function buildRequest(user, pass) {"
" var d = findGetParameter('device_id');"
" 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;"
"}"
);
fprintf(stream,
"function processResponse(xhr) {"
" if (xhr.readyState == 4) {"
" var r = JSON.parse(xhr.responseText);"
" if (!r.error) {"
" if (window.onLogin) {"
" window.onLogin(r);"
" } else {"
" setError('Client malfunction. Your client should have defined window.onLogin()');"
" }"
" } else {"
" setError(r.errcode + ': ' + r.error);"
" }"
" }"
"}"
);
fprintf(stream,
"function sendRequest(request) {"
" var xhr = new XMLHttpRequest();"
" xhr.open('POST', '/_matrix/client/v3/login');"
" xhr.setRequestHeader('Content-Type', 'application/json');"
" xhr.onreadystatechange = () => processResponse(xhr);"
" xhr.send(JSON.stringify(request));"
"}"
);
fprintf(stream,
"window.addEventListener('load', () => {" "window.addEventListener('load', () => {"
" document.getElementById('login-form').addEventListener('submit', (e) => {" " document.getElementById('login-form').addEventListener('submit', (e) => {"
" e.preventDefault();" " e.preventDefault();"
" /* TODO: Submit form here*/" " var user = document.getElementById('user').value;"
" var pass = document.getElementById('password').value;"
" if (!user || !pass) {"
" setError('Please provide a username and password.');"
" return;"
" }"
" setError(null);"
" var request = buildRequest(user, pass);"
" sendRequest(request);"
" });" " });"
"});" "});"
"</script>" "</script>"

View file

@ -411,7 +411,8 @@ UiaComplete(Array * flows, HttpServerContext * context, Db * db,
ArrayAdd(completed, JsonValueString(authType)); ArrayAdd(completed, JsonValueString(authType));
ret = 1; /* TODO: Only return 1 if there are remaining stages */ ret = 1; /* TODO: Only return 1 if there are
* remaining stages */
finish: finish:
ArrayFree(possibleNext); ArrayFree(possibleNext);