Skip to content

Commit

Permalink
Updated xbox live auth
Browse files Browse the repository at this point in the history
  • Loading branch information
xxAROX committed Nov 27, 2023
1 parent 548def0 commit 08c2838
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 62 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<dependency>
<groupId>com.github.RaphiMC</groupId>
<artifactId>MinecraftAuth</artifactId>
<version>991c6199ca</version>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xxAROX/PresenceMan/Application/AppInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.StringJoiner;

public final class AppInfo {
public final static int[] version = new int[]{ 1,1,2 };
public final static int[] version = new int[]{ 1,1,3 };
public final static String name = "Presence-Man";
public static String icon = "icon.png";
public static long discord_application_id = 1133823892486623344L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import net.raphimc.mcauth.step.bedrock.StepMCChain;
import net.raphimc.minecraftauth.MinecraftAuth;
import net.raphimc.minecraftauth.step.AbstractStep;
import net.raphimc.minecraftauth.step.bedrock.session.StepFullBedrockSession;
import net.raphimc.minecraftauth.util.MicrosoftConstants;

@Getter
@AllArgsConstructor
Expand All @@ -12,8 +15,15 @@ public final class XboxUserInfo {
private String xuid;
private String gamertag;

public XboxUserInfo(StepMCChain.MCChain fromInput) {
xuid = fromInput.xuid();
gamertag = fromInput.displayName();
public static final AbstractStep<?, StepFullBedrockSession.FullBedrockSession> DEVICE_CODE_LOGIN = MinecraftAuth.builder()
.withClientId(MicrosoftConstants.BEDROCK_ANDROID_TITLE_ID).withScope(MicrosoftConstants.SCOPE_TITLE_AUTH)
.deviceCode()
.withDeviceToken("Android")
.sisuTitleAuthentication(MicrosoftConstants.BEDROCK_XSTS_RELYING_PARTY)
.buildMinecraftBedrockChainStep(true, true);

public XboxUserInfo(StepFullBedrockSession.FullBedrockSession session) {
xuid = session.getMcChain().getXuid();
gamertag = session.getMcChain().getDisplayName();
}
}
5 changes: 4 additions & 1 deletion src/main/java/xxAROX/PresenceMan/Application/ui/AppUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.util.Objects;

public class AppUI extends JFrame {
public static final int BORDER_PADDING = 10;
public static final int BODY_BLOCK_PADDING = 10;

public final JTabbedPane contentPane = new JTabbedPane();
private final List<AUITab> tabs = new ArrayList<>();
private final Tray tray = new Tray();
Expand All @@ -35,7 +38,7 @@ public class AppUI extends JFrame {
public final FeaturedServersTab featured_servers_tab = new FeaturedServersTab(this);

public AppUI() {
Thread.setDefaultUncaughtExceptionHandler((t, e) -> this.showException(e));
Thread.setDefaultUncaughtExceptionHandler((t, e) -> showException(e));

this.setLookAndFeel();
this.initWindow();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package xxAROX.PresenceMan.Application.ui.popup;

import net.raphimc.mcauth.step.msa.StepMsaDeviceCode;
import net.raphimc.minecraftauth.step.msa.StepMsaDeviceCode;
import xxAROX.PresenceMan.Application.ui.AppUI;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
Expand Down Expand Up @@ -45,44 +43,24 @@ public void windowClosing(WindowEvent e) {
private void initComponents() {
JPanel contentPane = new JPanel();
contentPane.setLayout(null);
this.setContentPane(contentPane);
{
JLabel browserLabel = new JLabel("Please open the following URL in your browser:");
browserLabel.setBounds(10, 10, 380, 20);
contentPane.add(browserLabel);
JLabel browserLabel = new JLabel("Please open the following URL in your browser:");
browserLabel.setBounds(10, 10, 380, 20);
contentPane.add(browserLabel);

JLabel urlLabel = new JLabel("<html><a href=\"\">" + this.deviceCode.verificationUri() + "</a></html>");
urlLabel.setBounds(10, 30, 380, 20);
urlLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
parent.openURL(deviceCode.verificationUri());
}
});
contentPane.add(urlLabel);

JLabel enterCodeLabel = new JLabel("Enter the following code:");
enterCodeLabel.setBounds(10, 50, 380, 20);
contentPane.add(enterCodeLabel);

JLabel codeLabel = new JLabel(this.deviceCode.userCode());
codeLabel.setBounds(10, 70, 380, 20);
contentPane.add(codeLabel);
JLabel urlLabel = new JLabel("<html><a href=\"\">" + deviceCode.getDirectVerificationUri() + "</a></html>");
urlLabel.setBounds(10, 30, 380, 20);
urlLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
parent.openURL(deviceCode.getDirectVerificationUri());
}
});
contentPane.add(urlLabel);

JLabel closeInfo = new JLabel("The popup will close automatically after you have been logged in.");
closeInfo.setBounds(10, 100, 380, 20);
contentPane.add(closeInfo);
}
{
JButton copyCodeButton = new JButton("Copy Code");
copyCodeButton.setFocusPainted(false);
copyCodeButton.setBounds(this.getWidth() / 2 - 130 / 2, 130, 100, 20);
copyCodeButton.addActionListener(event -> {
StringSelection selection = new StringSelection(this.deviceCode.userCode());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
});
contentPane.add(copyCodeButton);
}
JLabel closeInfo = new JLabel("The popup will close automatically after you have been logged in.");
closeInfo.setBounds(10, 100, 380, 20);
contentPane.add(closeInfo);
setContentPane(contentPane);
}

public void markExternalClose() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ protected void update_presence(){
var game = (__Server) this.game.getSelectedItem();
if (network == null || game == null) {
App.getInstance().featuredServer = null;
App.setActivity(APIActivity.none());
return;
}
var mode = (__Server) this.mode.getSelectedItem();
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/xxAROX/PresenceMan/Application/ui/tabs/LoginTab.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package xxAROX.PresenceMan.Application.ui.tabs;

import net.raphimc.mcauth.MinecraftAuth;
import net.raphimc.mcauth.step.msa.StepMsaDeviceCode;
import net.raphimc.mcauth.util.MicrosoftConstants;
import net.raphimc.minecraftauth.step.msa.StepMsaDeviceCode;
import net.raphimc.minecraftauth.util.MicrosoftConstants;
import org.apache.http.impl.client.CloseableHttpClient;
import xxAROX.PresenceMan.Application.App;
import xxAROX.PresenceMan.Application.entity.XboxUserInfo;
Expand Down Expand Up @@ -34,27 +33,27 @@ public LoginTab(AppUI frame) {
}

private void reloadStateButton(){
boolean logged_in = App.getInstance().getXboxUserInfo() != null;
boolean logged_in = App.getInstance().getXboxUserInfo() == null;
stateButton.setText(logged_in ? LOGIN_TEXT : LOGOUT_TEXT);
stateButton.setBackground(logged_in ? LOGIN_COLOR : LOGOUT_COLOR);
App.ui.contentPane.setTitleAt(App.ui.contentPane.indexOfTab(name), TEXT);
}

@Override
protected void init(JPanel contentPane) {
boolean logged_in = App.getInstance().getXboxUserInfo() != null;
boolean logged_in = App.getInstance().getXboxUserInfo() == null;
contentPane.setLayout(new GridBagLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

{
stateButton = new JButton(logged_in ? "Login" : "Logout");
stateButton = new JButton(logged_in ? LOGIN_TEXT : LOGOUT_TEXT);
stateButton.setFocusPainted(false);
stateButton.setForeground(new Color(0, 0, 0));
stateButton.setFocusable(false);
stateButton.setBackground(logged_in ? LOGIN_COLOR : LOGOUT_COLOR);
stateButton.addActionListener(event -> {
if (stateButton.getText().equalsIgnoreCase("Login")) login();
else if (stateButton.getText().equalsIgnoreCase("Logout")) logout();
if (stateButton.getText().equalsIgnoreCase(LOGIN_TEXT)) login();
else if (stateButton.getText().equalsIgnoreCase(LOGOUT_TEXT)) logout();
});
stateButton.setVisible(false);
stateButton.setPreferredSize(new Dimension(315, 45));
Expand Down Expand Up @@ -89,7 +88,7 @@ private void closePopup() {
private void login(){
handleLogin(msaDeviceCodeConsumer -> {
try (CloseableHttpClient httpClient = MicrosoftConstants.createHttpClient()) {
return new XboxUserInfo(MinecraftAuth.BEDROCK_DEVICE_CODE_LOGIN.getFromInput(httpClient, new StepMsaDeviceCode.MsaDeviceCodeCallback(msaDeviceCodeConsumer)));
return new XboxUserInfo(XboxUserInfo.DEVICE_CODE_LOGIN.getFromInput(httpClient, new StepMsaDeviceCode.MsaDeviceCodeCallback(msaDeviceCodeConsumer)));
} catch (Exception e) {
if (e instanceof InterruptedException) return null;
else if (e instanceof TimeoutException) {
Expand All @@ -114,14 +113,15 @@ private void logout(){
private void handleLogin(Function<Consumer<StepMsaDeviceCode.MsaDeviceCode>, XboxUserInfo> requestHandler) {
this.addThread = new Thread(() -> {
try {
XboxUserInfo xboxUserInfo = requestHandler.apply(msaDeviceCode -> {
SwingUtilities.invokeLater(() -> new LoginPopup(frame, msaDeviceCode, popup -> login_popup = popup, () -> {
closePopup();
addThread.interrupt();
}));
});
XboxUserInfo xboxUserInfo = requestHandler.apply(msaDeviceCode -> SwingUtilities.invokeLater(() -> new LoginPopup(frame, msaDeviceCode, popup -> login_popup = popup, () -> {
closePopup();
addThread.interrupt();
})));
if (xboxUserInfo == null) {
frame.showError("Login failed, please try again!");
SwingUtilities.invokeLater(() -> {
this.closePopup();
this.frame.showError("Login failed, please try again!");
});
} else {
SwingUtilities.invokeLater(() -> {
closePopup();
Expand Down

0 comments on commit 08c2838

Please sign in to comment.