Skip to content

Commit

Permalink
Added bedrock realms token
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Nov 18, 2023
1 parent ac5167c commit 96aca18
Show file tree
Hide file tree
Showing 26 changed files with 469 additions and 292 deletions.
28 changes: 15 additions & 13 deletions src/main/java/net/raphimc/minecraftauth/MinecraftAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
package net.raphimc.minecraftauth;

import net.raphimc.minecraftauth.step.AbstractStep;
import net.raphimc.minecraftauth.step.OptionalMergeStep;
import net.raphimc.minecraftauth.step.SameInputOptionalMergeStep;
import net.raphimc.minecraftauth.step.BiMergeStep;
import net.raphimc.minecraftauth.step.SameInputBiMergeStep;
import net.raphimc.minecraftauth.step.SameInputTriMergeStep;
import net.raphimc.minecraftauth.step.bedrock.StepMCChain;
import net.raphimc.minecraftauth.step.bedrock.StepPlayFabToken;
import net.raphimc.minecraftauth.step.bedrock.session.StepFullBedrockSession;
Expand Down Expand Up @@ -63,14 +64,14 @@ public class MinecraftAuth {
.deviceCode()
.withDeviceToken("Android")
.sisuTitleAuthentication(MicrosoftConstants.BEDROCK_XSTS_RELYING_PARTY)
.buildMinecraftBedrockChainStep(true);
.buildMinecraftBedrockChainStep(true, false);

public static final AbstractStep<?, StepFullBedrockSession.FullBedrockSession> BEDROCK_CREDENTIALS_LOGIN = builder()
.withClientId(MicrosoftConstants.BEDROCK_ANDROID_TITLE_ID).withScope(MicrosoftConstants.SCOPE_TITLE_AUTH)
.credentials()
.withDeviceToken("Android")
.sisuTitleAuthentication(MicrosoftConstants.BEDROCK_XSTS_RELYING_PARTY)
.buildMinecraftBedrockChainStep(true);
.buildMinecraftBedrockChainStep(true, false);

public static MsaTokenBuilder builder() {
return new MsaTokenBuilder();
Expand Down Expand Up @@ -205,7 +206,7 @@ public AbstractStep<MsaCodeStep.MsaCode, StepMsaToken.MsaToken> build() {
public static class InitialXblSessionBuilder {

private final AbstractStep<MsaCodeStep.MsaCode, StepMsaToken.MsaToken> msaTokenStep;
private OptionalMergeStep<StepMsaToken.MsaToken, StepXblDeviceToken.XblDeviceToken, StepInitialXblSession.InitialXblSession> initialXblSessionStep;
private BiMergeStep<StepMsaToken.MsaToken, StepXblDeviceToken.XblDeviceToken, StepInitialXblSession.InitialXblSession> initialXblSessionStep;

private InitialXblSessionBuilder(final MsaTokenBuilder parent) {
this.msaTokenStep = parent.build();
Expand All @@ -223,16 +224,16 @@ public XblXstsTokenBuilder withoutDeviceToken() {
return new XblXstsTokenBuilder(this);
}

public OptionalMergeStep<StepMsaToken.MsaToken, StepXblDeviceToken.XblDeviceToken, StepInitialXblSession.InitialXblSession> build() {
public BiMergeStep<StepMsaToken.MsaToken, StepXblDeviceToken.XblDeviceToken, StepInitialXblSession.InitialXblSession> build() {
return this.initialXblSessionStep;
}

}

public static class XblXstsTokenBuilder {

private final OptionalMergeStep<StepMsaToken.MsaToken, StepXblDeviceToken.XblDeviceToken, StepInitialXblSession.InitialXblSession> initialXblSessionStep;
private AbstractStep<?, StepXblXstsToken.XblXsts<?>> xblXstsTokenStep;
private final BiMergeStep<StepMsaToken.MsaToken, StepXblDeviceToken.XblDeviceToken, StepInitialXblSession.InitialXblSession> initialXblSessionStep;
private AbstractStep<?, ? extends StepXblXstsToken.XblXsts<?>> xblXstsTokenStep;

private XblXstsTokenBuilder(final InitialXblSessionBuilder parent) {
this.initialXblSessionStep = parent.build();
Expand All @@ -256,29 +257,30 @@ public MinecraftBuilder regularAuthentication(final String relyingParty) {
return new MinecraftBuilder(this);
}

public AbstractStep<?, StepXblXstsToken.XblXsts<?>> build() {
public AbstractStep<?, ? extends StepXblXstsToken.XblXsts<?>> build() {
return this.xblXstsTokenStep;
}

}

public static class MinecraftBuilder {

private final AbstractStep<?, StepXblXstsToken.XblXsts<?>> xblXstsTokenStep;
private final AbstractStep<?, ? extends StepXblXstsToken.XblXsts<?>> xblXstsTokenStep;

private MinecraftBuilder(final XblXstsTokenBuilder parent) {
this.xblXstsTokenStep = parent.build();
}

public SameInputOptionalMergeStep<StepMCProfile.MCProfile, StepPlayerCertificates.PlayerCertificates, StepMCToken.MCToken, StepFullJavaSession.FullJavaSession> buildMinecraftJavaProfileStep(final boolean playerCertificates) {
public SameInputBiMergeStep<StepMCProfile.MCProfile, StepPlayerCertificates.PlayerCertificates, StepFullJavaSession.FullJavaSession> buildMinecraftJavaProfileStep(final boolean playerCertificates) {
final StepMCToken mcTokenStep = new StepMCToken(this.xblXstsTokenStep);
final StepPlayerCertificates playerCertificatesStep = playerCertificates ? new StepPlayerCertificates(mcTokenStep) : null;
return new StepFullJavaSession(new StepMCProfile(mcTokenStep), playerCertificatesStep);
}

public SameInputOptionalMergeStep<StepMCChain.MCChain, StepPlayFabToken.PlayFabToken, StepXblXstsToken.XblXsts<?>, StepFullBedrockSession.FullBedrockSession> buildMinecraftBedrockChainStep(final boolean playFabToken) {
public SameInputTriMergeStep<StepMCChain.MCChain, StepPlayFabToken.PlayFabToken, StepXblXstsToken.XblXsts<?>, StepFullBedrockSession.FullBedrockSession> buildMinecraftBedrockChainStep(final boolean playFabToken, final boolean realmsXsts) {
final StepPlayFabToken playFabTokenStep = new StepPlayFabToken(new StepXblXstsToken(new StepXblXstsToFullXblSession(this.xblXstsTokenStep), MicrosoftConstants.BEDROCK_PLAY_FAB_XSTS_RELYING_PARTY));
return new StepFullBedrockSession(new StepMCChain(this.xblXstsTokenStep), playFabToken ? playFabTokenStep : null);
final StepXblXstsToken realmsXstsStep = new StepXblXstsToken("realmsXsts", new StepXblXstsToFullXblSession(this.xblXstsTokenStep), MicrosoftConstants.BEDROCK_REALMS_XSTS_RELYING_PARTY);
return new StepFullBedrockSession(new StepMCChain(this.xblXstsTokenStep), playFabToken ? playFabTokenStep : null, realmsXsts ? realmsXstsStep : null);
}

}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/net/raphimc/minecraftauth/step/AbstractStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public AbstractStep(final String name, final AbstractStep<?, I> prevStep) {
public abstract O applyStep(final HttpClient httpClient, final I prevResult) throws Exception;

public O refresh(final HttpClient httpClient, final O result) throws Exception {
if (!result.isExpired()) {
return result;
}

return this.applyStep(httpClient, this.prevStep != null ? this.prevStep.refresh(httpClient, (I) result.prevResult()) : null);
}

Expand All @@ -54,6 +58,15 @@ public boolean isExpired() {

}

public abstract static class FirstStepResult extends StepResult<StepResult<?>> {

@Override
protected StepResult<?> prevResult() {
return null;
}

}

public abstract static class InitialInput extends StepResult<StepResult<?>> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,36 @@

import org.apache.http.client.HttpClient;

public abstract class OptionalMergeStep<I1 extends AbstractStep.StepResult<?>, I2 extends AbstractStep.StepResult<?>, O extends OptionalMergeStep.StepResult<?, ?>> extends AbstractStep<I1, O> {
public abstract class BiMergeStep<I1 extends AbstractStep.StepResult<?>, I2 extends AbstractStep.StepResult<?>, O extends BiMergeStep.StepResult<I1, I2>> extends AbstractStep<I1, O> {

protected final AbstractStep<?, I2> prevStep2;

public OptionalMergeStep(final String name, final AbstractStep<?, I1> prevStep1, final AbstractStep<?, I2> prevStep2) {
public BiMergeStep(final String name, final AbstractStep<?, I1> prevStep1, final AbstractStep<?, I2> prevStep2) {
super(name, prevStep1);

this.prevStep2 = prevStep2;
}

@Override
public O applyStep(HttpClient httpClient, I1 prevResult) throws Exception {
public O applyStep(final HttpClient httpClient, final I1 prevResult) throws Exception {
return this.applyStep(httpClient, prevResult, null);
}

public abstract O applyStep(final HttpClient httpClient, final I1 prevResult1, final I2 prevResult2) throws Exception;

@Override
public O refresh(final HttpClient httpClient, final O result) throws Exception {
final I1 prevResult1 = this.prevStep.refresh(httpClient, (I1) result.prevResult());
final I2 prevResult2 = this.prevStep2 != null ? this.prevStep2.refresh(httpClient, (I2) result.prevResult2()) : null;
if (!result.isExpired()) {
return result;
}

final I1 prevResult1 = this.prevStep.refresh(httpClient, result.prevResult());
final I2 prevResult2 = this.prevStep2 != null ? this.prevStep2.refresh(httpClient, result.prevResult2()) : null;
return this.applyStep(httpClient, prevResult1, prevResult2);
}

@Override
public O getFromInput(HttpClient httpClient, Object input) throws Exception {
public O getFromInput(final HttpClient httpClient, final Object input) throws Exception {
final I1 prevResult1 = this.prevStep.getFromInput(httpClient, input);
final I2 prevResult2 = this.prevStep2 != null ? this.prevStep2.getFromInput(httpClient, input) : null;
return this.applyStep(httpClient, prevResult1, prevResult2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* This file is part of MinecraftAuth - https://github.com/RaphiMC/MinecraftAuth
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.minecraftauth.step;

import com.google.gson.JsonObject;
import org.apache.http.client.HttpClient;

import java.util.ArrayList;
import java.util.List;

public abstract class SameInputBiMergeStep<I1 extends AbstractStep.StepResult<?>, I2 extends AbstractStep.StepResult<?>, O extends BiMergeStep.StepResult<I1, I2>> extends BiMergeStep<I1, I2, O> implements SameInputStep<I1, O> {

protected final List<AbstractStep<?, ?>> steps1UntilSameInput = new ArrayList<>();
protected final List<AbstractStep<?, ?>> steps2UntilSameInput = new ArrayList<>();

public SameInputBiMergeStep(final String name, final AbstractStep<?, I1> prevStep1, final AbstractStep<?, I2> prevStep2) {
super(name, prevStep1, prevStep2);

if (this.prevStep2 != null) {
this.steps1UntilSameInput.addAll(this.findCommonStep(this.prevStep, this.prevStep2));
this.steps2UntilSameInput.addAll(this.findCommonStep(this.prevStep2, this.prevStep));
}
}

@Override
public O refresh(final HttpClient httpClient, final O result) throws Exception {
if (!result.isExpired()) {
return result;
}

final I1 prevResult1 = this.prevStep.refresh(httpClient, result.prevResult());
final I2 prevResult2 = this.refreshSecondaryStepChain(httpClient, prevResult1, result.prevResult2(), this.steps1UntilSameInput, this.steps2UntilSameInput);
return this.applyStep(httpClient, prevResult1, prevResult2);
}

@Override
public O getFromInput(final HttpClient httpClient, final Object input) throws Exception {
final I1 prevResult1 = this.prevStep.getFromInput(httpClient, input);
final I2 prevResult2 = this.applySecondaryStepChain(httpClient, prevResult1, this.steps1UntilSameInput, this.steps2UntilSameInput);
return this.applyStep(httpClient, prevResult1, prevResult2);
}

@Override
public JsonObject toJson(final O result) {
final JsonObject json = this.toRawJson(result);
this.removeDuplicateStepResultsFromJson(json, this.steps2UntilSameInput);
return json;
}

@Override
public O fromJson(final JsonObject json) {
this.insertDuplicateStepResultsIntoJson(json, this.steps1UntilSameInput, this.steps2UntilSameInput);
return this.fromRawJson(json);
}

}

This file was deleted.

Loading

0 comments on commit 96aca18

Please sign in to comment.