Skip to content

Commit

Permalink
v1.0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
pwaillette committed Dec 30, 2023
1 parent 8cbd63b commit 015a490
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "com.stmarygate"
version = "1.0.14"
version = "1.0.15"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.stmarygate.coral.network.codes;

import lombok.Getter;

@Getter
public enum LoginResultCode {
SUCCESS(1),
FAILURE_NO_ACCOUNT(2),
FAILURE_INCORRECT_PASSWORD(3);

private final int code;

LoginResultCode(int code) {
this.code = code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.stmarygate.coral.network.ClientSession;
import com.stmarygate.coral.network.packets.client.PacketLoginUsingCredentials;
import com.stmarygate.coral.network.packets.client.PacketVersion;
import com.stmarygate.coral.network.packets.server.PacketLoginResult;
import com.stmarygate.coral.network.packets.server.PacketVersionResult;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -48,13 +49,12 @@ public ClientSession getSession() {
}

public void handlePacketVersion(@NotNull PacketVersion packet) {}
;

public void handlePacketVersionResult(@NotNull PacketVersionResult packet) {}
;

public void handlePacketLoginUsingCredentials(@NotNull PacketLoginUsingCredentials packet) {}
;

public void handlePacketLoginResult(@NotNull PacketLoginResult packet) {}

/**
* Finds the handler method for the given packet class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.stmarygate.coral.network.packets.client.PacketLoginUsingCredentials;
import com.stmarygate.coral.network.packets.client.PacketVersion;
import com.stmarygate.coral.network.packets.server.PacketLoginResult;
import com.stmarygate.coral.network.packets.server.PacketVersionResult;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
Expand All @@ -25,6 +26,7 @@ private Protocol() {
register(1, PacketVersion.class);
register(2, PacketVersionResult.class);
register(3, PacketLoginUsingCredentials.class);
register(4, PacketLoginResult.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,16 @@
import com.stmarygate.coral.network.packets.PacketBuffer;
import com.stmarygate.coral.network.packets.PacketHandler;
import lombok.Getter;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;

@Getter
public class PacketLoginUsingCredentials extends Packet {
private String username;
private String password;
private String encodedPassword;

public PacketLoginUsingCredentials(String username, String password) {
if (username.isEmpty() && password.isEmpty()) return;
this.username = username;

Argon2PasswordEncoder argon2PasswordEncoder =
new Argon2PasswordEncoder(
16, // saltLength
64, // hashLength
8, // parallelism (e.g., number of CPU cores)
65536, // memory in kilobytes
4 // iterations
);

this.password = password;
this.encodedPassword = argon2PasswordEncoder.encode(password);
}

public PacketLoginUsingCredentials() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.stmarygate.coral.network.packets.server;

import com.stmarygate.coral.network.packets.Packet;
import com.stmarygate.coral.network.packets.PacketBuffer;
import com.stmarygate.coral.network.packets.PacketHandler;
import lombok.Getter;

@Getter
public class PacketLoginResult extends Packet {
private boolean accepted;
private int code;
private String token;

/**
* Constructs a new {@code PacketVersionResult} with specified login informations.
*
* @param accepted Whether the login is accepted.
* @param token The JWT token for the user if the login is accepted.
*/
public PacketLoginResult(boolean accepted, int code, String token) {
this.accepted = accepted;
this.code = code;
this.token = token;
}

/** Constructs a default {@code PacketLoginResult} with all version information set to zero. */
public PacketLoginResult() {
this(false, 2, "");
}

/**
* Decodes the packet data from the provided {@link PacketBuffer}.
*
* @param packet The {@link PacketBuffer} containing the packet data.
*/
@Override
public void decode(PacketBuffer packet) throws Exception {
this.accepted = packet.readUnsignedByte() == 1;
this.code = packet.readUnsignedByte();
this.token = this.accepted ? packet.readString() : "";
}

/**
* Encodes the packet data into the provided {@link PacketBuffer}.
*
* @param packet The {@link PacketBuffer} to which the packet data will be written.
*/
@Override
public void encode(PacketBuffer packet) throws Exception {
packet.writeByte(accepted ? 1 : 0);
packet.writeByte(code);
if (accepted) packet.writeString(token);
packet.finish();
}

/**
* Handles the packet using the specified {@link PacketHandler}.
*
* @param handler The {@link PacketHandler} responsible for handling the packet.
*/
@Override
public void handle(PacketHandler handler) throws Exception {
handler.handlePacket(this);
}

/**
* Returns a string representation of the {@code PacketLoginResult}.
*
* @return A string representation containing version information.
*/
@Override
public String toString() {
return "{ accepted: " + accepted + ", code: " + code + ", token: " + token + " }";
}
}

0 comments on commit 015a490

Please sign in to comment.