forked from GeyserMC/Geyser
commit
a8f8de8613
4 changed files with 47 additions and 23 deletions
|
@ -37,6 +37,10 @@ public class FormButton {
|
||||||
@Getter
|
@Getter
|
||||||
private FormImage image;
|
private FormImage image;
|
||||||
|
|
||||||
|
public FormButton(String text) {
|
||||||
|
this.text = text;
|
||||||
|
}
|
||||||
|
|
||||||
public FormButton(String text, FormImage image) {
|
public FormButton(String text, FormImage image) {
|
||||||
this.text = text;
|
this.text = text;
|
||||||
|
|
||||||
|
|
|
@ -86,7 +86,7 @@ public class UpstreamPacketHandler extends LoggingPacketHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean handle(ModalFormResponsePacket packet) {
|
public boolean handle(ModalFormResponsePacket packet) {
|
||||||
return LoginEncryptionUtils.authenticateFromForm(session, connector, packet.getFormData());
|
return LoginEncryptionUtils.authenticateFromForm(session, connector, packet.getFormId(), packet.getFormData());
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean couldLoginUserByName(String bedrockUsername) {
|
private boolean couldLoginUserByName(String bedrockUsername) {
|
||||||
|
|
|
@ -300,7 +300,7 @@ public class GeyserSession implements CommandSender {
|
||||||
|
|
||||||
downstream.getSession().connect();
|
downstream.getSession().connect();
|
||||||
connector.addPlayer(this);
|
connector.addPlayer(this);
|
||||||
} catch (InvalidCredentialsException e) {
|
} catch (InvalidCredentialsException | IllegalArgumentException e) {
|
||||||
connector.getLogger().info("User '" + username + "' entered invalid login info, kicking.");
|
connector.getLogger().info("User '" + username + "' entered invalid login info, kicking.");
|
||||||
disconnect("Invalid/incorrect login info");
|
disconnect("Invalid/incorrect login info");
|
||||||
} catch (RequestException ex) {
|
} catch (RequestException ex) {
|
||||||
|
|
|
@ -40,9 +40,12 @@ import net.minidev.json.JSONObject;
|
||||||
import org.geysermc.common.window.CustomFormBuilder;
|
import org.geysermc.common.window.CustomFormBuilder;
|
||||||
import org.geysermc.common.window.CustomFormWindow;
|
import org.geysermc.common.window.CustomFormWindow;
|
||||||
import org.geysermc.common.window.FormWindow;
|
import org.geysermc.common.window.FormWindow;
|
||||||
|
import org.geysermc.common.window.SimpleFormWindow;
|
||||||
|
import org.geysermc.common.window.button.FormButton;
|
||||||
import org.geysermc.common.window.component.InputComponent;
|
import org.geysermc.common.window.component.InputComponent;
|
||||||
import org.geysermc.common.window.component.LabelComponent;
|
import org.geysermc.common.window.component.LabelComponent;
|
||||||
import org.geysermc.common.window.response.CustomFormResponse;
|
import org.geysermc.common.window.response.CustomFormResponse;
|
||||||
|
import org.geysermc.common.window.response.SimpleFormResponse;
|
||||||
import org.geysermc.connector.GeyserConnector;
|
import org.geysermc.connector.GeyserConnector;
|
||||||
import org.geysermc.connector.network.session.GeyserSession;
|
import org.geysermc.connector.network.session.GeyserSession;
|
||||||
import org.geysermc.connector.network.session.auth.AuthData;
|
import org.geysermc.connector.network.session.auth.AuthData;
|
||||||
|
@ -152,43 +155,60 @@ public class LoginEncryptionUtils {
|
||||||
session.getUpstream().sendPacketImmediately(packet);
|
session.getUpstream().sendPacketImmediately(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int AUTH_FORM_ID = 1337;
|
private static int AUTH_FORM_ID = 1336;
|
||||||
|
private static int AUTH_DETAILS_FORM_ID = 1337;
|
||||||
|
|
||||||
public static void showLoginWindow(GeyserSession session) {
|
public static void showLoginWindow(GeyserSession session) {
|
||||||
CustomFormWindow window = new CustomFormBuilder("Login")
|
SimpleFormWindow window = new SimpleFormWindow("Login", "You need a Java Edition account to play on this server.");
|
||||||
.addComponent(new LabelComponent("Minecraft: Java Edition account authentication."))
|
window.getButtons().add(new FormButton("Login with Minecraft"));
|
||||||
|
window.getButtons().add(new FormButton("Disconnect"));
|
||||||
|
|
||||||
|
session.sendForm(window, AUTH_FORM_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void showLoginDetailsWindow(GeyserSession session) {
|
||||||
|
CustomFormWindow window = new CustomFormBuilder("Login Details")
|
||||||
.addComponent(new LabelComponent("Enter the credentials for your Minecraft: Java Edition account below."))
|
.addComponent(new LabelComponent("Enter the credentials for your Minecraft: Java Edition account below."))
|
||||||
.addComponent(new InputComponent("Email/Username", "account@geysermc.org", ""))
|
.addComponent(new InputComponent("Email/Username", "account@geysermc.org", ""))
|
||||||
.addComponent(new InputComponent("Password", "123456", ""))
|
.addComponent(new InputComponent("Password", "123456", ""))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
session.sendForm(window, AUTH_FORM_ID);
|
session.sendForm(window, AUTH_DETAILS_FORM_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean authenticateFromForm(GeyserSession session, GeyserConnector connector, String formData) {
|
public static boolean authenticateFromForm(GeyserSession session, GeyserConnector connector, int formId, String formData) {
|
||||||
WindowCache windowCache = session.getWindowCache();
|
WindowCache windowCache = session.getWindowCache();
|
||||||
if (!windowCache.getWindows().containsKey(AUTH_FORM_ID))
|
if (!windowCache.getWindows().containsKey(formId))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
FormWindow window = windowCache.getWindows().remove(AUTH_FORM_ID);
|
if(formId == AUTH_FORM_ID || formId == AUTH_DETAILS_FORM_ID) {
|
||||||
|
FormWindow window = windowCache.getWindows().remove(formId);
|
||||||
window.setResponse(formData.trim());
|
window.setResponse(formData.trim());
|
||||||
|
|
||||||
if (!session.isLoggedIn()) {
|
if (!session.isLoggedIn()) {
|
||||||
if (window instanceof CustomFormWindow) {
|
if (formId == AUTH_DETAILS_FORM_ID && window instanceof CustomFormWindow) {
|
||||||
CustomFormWindow customFormWindow = (CustomFormWindow) window;
|
CustomFormWindow customFormWindow = (CustomFormWindow) window;
|
||||||
if (!customFormWindow.getTitle().equals("Login"))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
CustomFormResponse response = (CustomFormResponse) customFormWindow.getResponse();
|
CustomFormResponse response = (CustomFormResponse) customFormWindow.getResponse();
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
String email = response.getInputResponses().get(2);
|
String email = response.getInputResponses().get(1);
|
||||||
String password = response.getInputResponses().get(3);
|
String password = response.getInputResponses().get(2);
|
||||||
|
|
||||||
session.authenticate(email, password);
|
session.authenticate(email, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear windows so authentication data isn't accidentally cached
|
// Clear windows so authentication data isn't accidentally cached
|
||||||
windowCache.getWindows().clear();
|
windowCache.getWindows().clear();
|
||||||
|
} else if (formId == AUTH_FORM_ID && window instanceof SimpleFormWindow) {
|
||||||
|
SimpleFormResponse response = (SimpleFormResponse) window.getResponse();
|
||||||
|
if(response != null) {
|
||||||
|
if(response.getClickedButtonId() == 0) {
|
||||||
|
showLoginDetailsWindow(session);
|
||||||
|
} else if(response.getClickedButtonId() == 1) {
|
||||||
|
session.disconnect("Login is required");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Reference in a new issue