implemented cookies, implemented local file delivery option (--base /path/to/static/content), refactoring static files

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2024-07-19 23:39:32 +02:00
parent 59075db1ad
commit 9ee963924d
13 changed files with 124 additions and 77 deletions

View File

@@ -12,6 +12,7 @@ repositories {
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation project(':de.srsoftware.cookies')
implementation project(':de.srsoftware.oidc.api')
implementation 'org.json:json:20240303'
}

View File

@@ -8,6 +8,7 @@ import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.cookies.SessionToken;
import de.srsoftware.oidc.api.*;
import java.io.IOException;
import java.util.Optional;
@@ -43,19 +44,23 @@ public class Backend extends PathHandler {
String method = ex.getRequestMethod();
System.out.printf("%s %s…", method, path);
var user = getSession(ex).map(Session::user);
var session = getSession(ex);
if ("login".equals(path) && POST.equals(method)) {
doLogin(ex); // TODO: prevent brute force
return;
}
if (user.isEmpty()) {
if (session.isEmpty()) {
sendEmptyResponse(HTTP_UNAUTHORIZED, ex);
System.err.println("unauthorized");
return;
}
switch (path) {
case "user":
sendUserAndCookie(ex, session.get());
return;
}
System.err.println("not implemented");
ex.sendResponseHeaders(HTTP_NOT_FOUND, 0);
ex.getResponseBody().close();
sendEmptyResponse(HTTP_NOT_FOUND, ex);
}
private Optional<Session> getSession(HttpExchange ex) {