working on user login

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2024-07-17 00:18:14 +02:00
parent 5627db16b5
commit add4209a1f
22 changed files with 173 additions and 401 deletions

View File

@@ -0,0 +1,23 @@
/* © SRSoftware 2024 */
package de.srsoftware.oidc.web;
import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.oidc.api.PathHandler;
import java.io.IOException;
public class Forward extends PathHandler {
private final int CODE = 302;
private final String toPath;
public Forward(String toPath) {
this.toPath = toPath;
}
@Override
public void handle(HttpExchange ex) throws IOException {
System.out.printf("Forwarding (%d) %s to %s…\n", CODE, ex.getRequestURI(), toPath);
ex.getResponseHeaders().add("Location", toPath);
ex.sendResponseHeaders(CODE, 0);
ex.getResponseBody().close();
}
}

View File

@@ -9,40 +9,42 @@ import java.io.OutputStream;
import java.util.Optional;
public class StaticPages extends PathHandler {
private static final String DEFAULT_LANG = "en";
private static final String DEFAULT_LANGUAGE = "en";
private ClassLoader loader;
private record Response(String contentType, byte[] content) {
}
private static final String INDEX = "de/index.html";
private static final String INDEX = "en/index.html";
@Override
public void handle(HttpExchange ex) throws IOException {
String path = relativePath(ex);
String path = relativePath(ex);
String lang = language(ex).orElse(DEFAULT_LANGUAGE);
String method = ex.getRequestMethod();
if (path.isBlank()) path = INDEX;
System.out.printf("%s %s: ", method, ex.getRequestURI());
try {
var response = loadTemplate(path).orElseThrow(() -> new FileNotFoundException());
System.out.printf("Loading %s for lagnuage %s…", path, lang);
var response = loadTemplate(lang, path).orElseThrow(() -> new FileNotFoundException());
ex.getResponseHeaders().add("Content-Type", response.contentType);
ex.sendResponseHeaders(200, response.content.length);
OutputStream os = ex.getResponseBody();
os.write(response.content);
os.close();
System.out.println("success.");
} catch (FileNotFoundException fnf) {
ex.sendResponseHeaders(404, 0);
ex.getResponseBody().close();
System.err.println("failed!");
}
}
private Optional<Response> loadTemplate(String path) throws IOException {
private Optional<Response> loadTemplate(String language, String path) throws IOException {
if (loader == null) loader = getClass().getClassLoader();
var resource = loader.getResource(path);
if (resource == null) {
var parts = path.split("/");
parts[0] = DEFAULT_LANG;
path = String.join("/", parts);
resource = loader.getResource(path);
}
var resource = loader.getResource(String.join("/", language, path));
if (resource == null) resource = loader.getResource(String.join("/", DEFAULT_LANGUAGE, path));
if (resource == null) return Optional.empty();
var connection = resource.openConnection();
var contentType = connection.getContentType();

View File

@@ -0,0 +1 @@
var api = "/api";

View File

@@ -1,9 +1,13 @@
<html>
<head>
<title>{title}</title>
<script src="lightoidc.js" />
<title>Light OIDC</title>
<script src="config.js"></script>
<script src="lightoidc.js"></script>
<script>
checkUser();
</script>
</head>
<body>
{body}
<h1>Welcome!</h1>
</body>
</html>

View File

@@ -0,0 +1,25 @@
const UNAUTHORIZED = 401;
function handleCheckUser(response){
console.log(window.location.href);
if (response.status == UNAUTHORIZED){
window.location.href = "login.html";
return;
}
}
function checkUser(){
fetch(api+"/user")
.then(handleCheckUser)
.catch((err) => console.log(err));
}
function submitForm(formId){
var data = Object.fromEntries(new FormData(document.getElementById(formId)));
fetch(api+"/login",{
headers: {
'login-username': data.user,
'login-password': data.pass, // TODO: send via body?
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
}

View File

@@ -0,0 +1,24 @@
<html>
<head>
<title>Light OIDC</title>
<script src="config.js"></script>
<script src="lightoidc.js"></script>
</head>
<body>
<h1>Login</h1>
<form id="form">
<fieldset>
<legend>User credentials</legend>
<label>
Username
<input type="text" name="user" />
</label>
<label>
Password
<input type="password" name="pass" />
</label>
<button type="button" onClick="submitForm('form')">Login</button>
</fieldset>
</form>
</body>
</html>