2026-01-19 17:02:46 +01:00
|
|
|
package org.openautonomousconnection.oac2web.frontend;
|
|
|
|
|
|
|
|
|
|
import org.openautonomousconnection.oac2web.utils.Oac2WebApp;
|
|
|
|
|
import org.openautonomousconnection.oac2web.utils.Sha256;
|
|
|
|
|
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebResponsePacket;
|
|
|
|
|
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
|
|
|
|
|
import org.openautonomousconnection.protocol.side.web.managers.SessionManager;
|
|
|
|
|
import org.openautonomousconnection.protocol.versions.v1_0_0.beta.WebRequestMethod;
|
|
|
|
|
import org.openautonomousconnection.webserver.api.Route;
|
|
|
|
|
import org.openautonomousconnection.webserver.api.WebPage;
|
|
|
|
|
import org.openautonomousconnection.webserver.api.WebPageContext;
|
|
|
|
|
import org.openautonomousconnection.webserver.utils.Html;
|
|
|
|
|
import org.openautonomousconnection.webserver.utils.RequestParams;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Registration page.
|
|
|
|
|
*
|
|
|
|
|
* POST headers expected:
|
|
|
|
|
* - username
|
|
|
|
|
* - password
|
|
|
|
|
*
|
|
|
|
|
* Stores:
|
2026-01-19 17:49:05 +01:00
|
|
|
* - users.username = sha256(username) as HEX (64 chars)
|
2026-01-19 17:02:46 +01:00
|
|
|
* - users.password = PBKDF2$sha256$...
|
|
|
|
|
*/
|
2026-02-06 22:51:56 +01:00
|
|
|
@Route(path = "/ins/register")
|
2026-01-19 17:02:46 +01:00
|
|
|
public final class register implements WebPage {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public WebResponsePacket handle(WebPageContext ctx) throws Exception {
|
|
|
|
|
if (ctx.request.getMethod() != WebRequestMethod.POST) {
|
2026-01-19 17:49:05 +01:00
|
|
|
return renderForm(null);
|
2026-01-19 17:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RequestParams p = new RequestParams(ctx.request);
|
|
|
|
|
|
|
|
|
|
String username = p.get("username");
|
|
|
|
|
String password = p.get("password");
|
|
|
|
|
|
|
|
|
|
if (username == null || username.isBlank() || password == null || password.isBlank()) {
|
2026-01-19 17:49:05 +01:00
|
|
|
return renderForm("Missing username/password (send via headers).");
|
2026-01-19 17:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Oac2WebApp app = Oac2WebApp.get();
|
|
|
|
|
|
2026-01-19 17:49:05 +01:00
|
|
|
String usernameHashHex = Sha256.hex(username.trim());
|
2026-01-19 17:02:46 +01:00
|
|
|
String passwordEnc = app.passwordHasher().hash(password);
|
|
|
|
|
|
|
|
|
|
try {
|
2026-01-19 17:49:05 +01:00
|
|
|
int userId = app.dao().createUser(usernameHashHex, passwordEnc);
|
2026-01-19 17:02:46 +01:00
|
|
|
|
2026-02-06 22:51:56 +01:00
|
|
|
String ip = (ctx.client.getConnection().getTcpSocket() != null && ctx.client.getConnection().getTcpSocket().getInetAddress() != null)
|
|
|
|
|
? ctx.client.getConnection().getTcpSocket().getInetAddress().getHostAddress()
|
2026-01-19 17:02:46 +01:00
|
|
|
: "";
|
|
|
|
|
|
|
|
|
|
String ua = ctx.request.getHeaders() != null ? ctx.request.getHeaders().getOrDefault("user-agent", "") : "";
|
|
|
|
|
|
|
|
|
|
String session = SessionManager.create(String.valueOf(userId), ip, ua, (ProtocolWebServer) ctx.client.getServer());
|
|
|
|
|
|
|
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
|
|
headers.put("session", session);
|
2026-01-19 17:49:05 +01:00
|
|
|
headers.put("location", "/ins/dashboard");
|
2026-01-19 17:02:46 +01:00
|
|
|
|
|
|
|
|
return new WebResponsePacket(302, "text/plain", headers, new byte[0]);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
2026-01-19 17:49:05 +01:00
|
|
|
return renderForm("Register failed: " + e.getMessage());
|
2026-01-19 17:02:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-19 17:49:05 +01:00
|
|
|
private WebResponsePacket renderForm(String err) {
|
2026-01-19 17:02:46 +01:00
|
|
|
String body = """
|
|
|
|
|
<div class="card">
|
|
|
|
|
<h2>Register</h2>
|
|
|
|
|
%s
|
|
|
|
|
<p class="muted">Send a POST request with headers <code>username</code> and <code>password</code>.</p>
|
|
|
|
|
<div class="row">
|
2026-01-19 17:49:05 +01:00
|
|
|
<div class="col"><a href="/ins/login">Login</a></div>
|
|
|
|
|
<div class="col"><a href="/ins/index.html">Home</a></div>
|
2026-01-19 17:02:46 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-01-19 17:49:05 +01:00
|
|
|
""".formatted(err == null ? "" : "<p class='err'>" + Html.esc(err) + "</p>");
|
2026-01-19 17:02:46 +01:00
|
|
|
|
|
|
|
|
String html = Html.page("Register", body);
|
|
|
|
|
return new WebResponsePacket(200, "text/html", new HashMap<>(), Html.utf8(html));
|
|
|
|
|
}
|
|
|
|
|
}
|