Skip to content

Commit

Permalink
Check for updated username on login
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Aug 28, 2024
1 parent 52911fc commit 08aaef9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ docker run --rm -it -v /path/to/config:/opt/app/config ghcr.io/rtm516/mcxboxbroa
8. Restart the tool
9. Check the friends tab ingame and you should see the server listed

### Manager
## Manager
There is a web manager available for donators. After joining the relevent [GitHub sponsors](https://github.com/sponsors/rtm516) tier you will be able to access its builds at https://github.com/MCXboxBroadcast/Manager/releases

Note: This also requires a MongoDB instance to be running
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void initialise() {
storageManager.cache(Constants.GSON.toJson(MinecraftAuth.BEDROCK_XBL_DEVICE_CODE_LOGIN.toJson(xboxToken)));

// Construct and store the Xbox token info
xboxTokenInfo = new XboxTokenInfo(xboxToken.getDisplayClaims().get("xid"), xboxToken.getUserHash(), xboxToken.getDisplayClaims().get("gtg"), xboxToken.getToken(), String.valueOf(xboxToken.getExpireTimeMs()));
xboxTokenInfo = new XboxTokenInfo(xboxToken);

playfabSessionTicket = fetchPlayfabSessionTicket(httpClient);
} catch (Exception e) {
Expand Down Expand Up @@ -165,4 +165,17 @@ public String getPlayfabSessionTicket() {
}
return playfabSessionTicket;
}

public void updateGamertag(String gamertag) {
try {
JsonObject xboxTokenJson = MinecraftAuth.BEDROCK_XBL_DEVICE_CODE_LOGIN.toJson(xboxToken);
xboxTokenJson.getAsJsonObject("xstsToken").getAsJsonObject("displayClaims").addProperty("gtg", gamertag);
xboxToken = MinecraftAuth.BEDROCK_XBL_DEVICE_CODE_LOGIN.fromJson(xboxTokenJson);

storageManager.cache(Constants.GSON.toJson(MinecraftAuth.BEDROCK_XBL_DEVICE_CODE_LOGIN.toJson(xboxToken)));
xboxTokenInfo = new XboxTokenInfo(xboxToken);
} catch (Exception e) {
logger.error("Failed to update gamertag", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Constants {
public static final URI SOCIAL = URI.create("https://peoplehub.xboxlive.com/users/me/people/social");
public static final URI SOCIAL_SUMMARY = URI.create("https://social.xboxlive.com/users/me/summary");
public static final URI BLOCK = URI.create("https://privacy.xboxlive.com/users/me/people/never");
public static final String PROFILE_SETTINGS = "https://profile.xboxlive.com/users/xuid(%s)/profile/settings?settings=Gamertag";

/**
* Gathered from scraped web requests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.rtm516.mcxboxbroadcast.core.exceptions.SessionUpdateException;
import com.rtm516.mcxboxbroadcast.core.models.auth.SessionStartBody;
import com.rtm516.mcxboxbroadcast.core.models.auth.SessionStartResponse;
import com.rtm516.mcxboxbroadcast.core.models.other.ProfileSettingsResponse;
import com.rtm516.mcxboxbroadcast.core.models.session.CreateHandleRequest;
import com.rtm516.mcxboxbroadcast.core.models.session.CreateHandleResponse;
import com.rtm516.mcxboxbroadcast.core.models.session.SessionRef;
Expand Down Expand Up @@ -130,6 +131,9 @@ public void init() throws SessionCreationException, SessionUpdateException {
XboxTokenInfo tokenInfo = getXboxToken();
logger.info("Successfully authenticated as " + tokenInfo.gamertag() + " (" + tokenInfo.userXUID() + ")");

// Check if the gamertag has been updated
checkGamertagUpdate(tokenInfo);

if (handleFriendship()) {
logger.info("Waiting for friendship to be processed...");
try {
Expand Down Expand Up @@ -464,4 +468,29 @@ public SocialSummaryResponse socialSummary() {

return new SocialSummaryResponse(-1, -1, false, false, false, false, "", -1, -1, "");
}

/**
* Check if the gamertag has been updated and update it if needed
*
* @param tokenInfo The token information to check the gamertag for
*/
private void checkGamertagUpdate(XboxTokenInfo tokenInfo) {
try {
ProfileSettingsResponse response = Constants.GSON.fromJson(httpClient.send(HttpRequest.newBuilder()
.uri(URI.create(Constants.PROFILE_SETTINGS.formatted(tokenInfo.userXUID())))
.header("Content-Type", "application/json")
.header("Authorization", tokenInfo.tokenHeader())
.header("x-xbl-contract-version", "3")
.GET()
.build(), HttpResponse.BodyHandlers.ofString()).body(), ProfileSettingsResponse.class);

String newGamertag = response.profileUsers().get(0).settings().get(0).value();
if (!newGamertag.equals(tokenInfo.gamertag())) {
logger.info("Gamertag changed from " + tokenInfo.gamertag() + " to " + newGamertag);
authManager.updateGamertag(newGamertag);
}
} catch (IOException | InterruptedException e) {
logger.error("Failed to check profile settings", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.rtm516.mcxboxbroadcast.core.models.auth;

import net.raphimc.minecraftauth.step.xbl.StepXblSisuAuthentication;

public record XboxTokenInfo(
String userXUID,
String userHash,
String gamertag,
String XSTSToken,
String expiresOn) {

public XboxTokenInfo(StepXblSisuAuthentication.XblSisuTokens xboxToken) {
this(xboxToken.getDisplayClaims().get("xid"), xboxToken.getUserHash(), xboxToken.getDisplayClaims().get("gtg"), xboxToken.getToken(), String.valueOf(xboxToken.getExpireTimeMs()));
}

public String tokenHeader() {
return "XBL3.0 x=" + this.userHash + ";" + this.XSTSToken;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.rtm516.mcxboxbroadcast.core.models.other;

import java.util.List;

public record ProfileSettingsResponse(List<ProfileUser> profileUsers) {
public record ProfileUser(
String hostId,
String id,
boolean isSponsoredUser,
List<ProfileSetting> settings
) { }

public record ProfileSetting(
String id,
String value
) { }
}

0 comments on commit 08aaef9

Please sign in to comment.