diff --git a/common/src/main/java/com/alterdekim/xcraft/auth/SaltNic.java b/common/src/main/java/com/alterdekim/xcraft/auth/SaltNic.java index 74ebebf..c1d1d30 100644 --- a/common/src/main/java/com/alterdekim/xcraft/auth/SaltNic.java +++ b/common/src/main/java/com/alterdekim/xcraft/auth/SaltNic.java @@ -62,6 +62,8 @@ public class SaltNic extends NanoHTTPD { return handleHasJoinedRequest(session); } else if (uri.startsWith("/api/profile/") && method == Method.GET) { return handleProfileRequest(session, uri); + } else if (uri.startsWith("/api/login") && method == Method.POST) { + return handleLoginRequest(session); } else if (uri.startsWith("/api/register") && method == Method.POST) { return handleProfileRegistration(session); } else if (Method.POST == method && "/api/set_model".equals(uri)) { @@ -308,6 +310,40 @@ public class SaltNic extends NanoHTTPD { } } + private Response handleLoginRequest(IHTTPSession session) { + try { + Map files = new HashMap<>(); + session.parseBody(files); + SignUpRequest loginRequest = JsonIterator.deserialize(files.get("postData"), SignUpRequest.class); + + if (loginRequest == null) { + return newFixedLengthResponse(Response.Status.BAD_REQUEST, "text/plain", "Invalid JSON format"); + } + + String username = loginRequest.getUsername(); + String password = loginRequest.getPassword(); + + if (username == null || password == null) { + return newFixedLengthResponse(Response.Status.BAD_REQUEST, "text/plain", "Missing username or password"); + } + + if( this.storage.getUserPasswordByName(username) == null ) { + return newFixedLengthResponse(Response.Status.CONFLICT, "text/plain", "User doesn't exist"); + } + + boolean validSession = PasswordHasher.checkPassword(password, this.storage.getUserPasswordByName(username)); + + if (validSession) { + return newFixedLengthResponse(Response.Status.OK, "application/json", "{}"); + } else { + return invalidSession; + } + } catch (Exception e) { + logger.info("Error while processing join request from client: " + e.getMessage()); + return invalidSession; + } + } + private Response handleJoinRequest(IHTTPSession session) { try { Map files = new HashMap<>(); diff --git a/common/src/main/java/com/alterdekim/xcraft/auth/database/UserStorage.java b/common/src/main/java/com/alterdekim/xcraft/auth/database/UserStorage.java index dc43a64..32f9bfd 100644 --- a/common/src/main/java/com/alterdekim/xcraft/auth/database/UserStorage.java +++ b/common/src/main/java/com/alterdekim/xcraft/auth/database/UserStorage.java @@ -1,5 +1,6 @@ package com.alterdekim.xcraft.auth.database; +import com.alterdekim.xcraft.auth.UserId; import com.jsoniter.JsonIterator; import com.jsoniter.output.JsonStream; import org.mapdb.DB; @@ -7,6 +8,7 @@ import org.mapdb.DBMaker; import org.mapdb.Serializer; import java.io.File; +import java.security.NoSuchAlgorithmException; import java.util.concurrent.ConcurrentMap; public class UserStorage { @@ -42,6 +44,10 @@ public class UserStorage { return null; } + public String getUserPasswordByName(String username) throws NoSuchAlgorithmException { + return getUserPassword(UserId.generateUserId(username)); + } + public void close() { db.close(); }