Class MicrosoftUtils
For more information refer to: https://wiki.vg/Microsoft_Authentication_Scheme
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumIndicates the type of user interaction that is required when requesting Microsoft authorization codes. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe default authorization url used in the configuration.static final StringThe default client id used in the configuration.static final StringThe default Minecraft authentication url used in the configuration.static final StringThe default Minecraft profile url used in the configuration.static final org.apache.http.client.config.RequestConfigA reusable Apache HTTP request configstatic final StringThe default token url used in the configuration.static final StringThe default Xbox authentication url used in the configuration.static final StringThe default Xbox XSTS url used in the configuration. -
Method Summary
Modifier and TypeMethodDescriptionstatic CompletableFuture<String> acquireMCAccessToken(String xstsToken, String userHash, Executor executor) Exchanges an Xbox Live XSTS token for a Minecraft access token.static CompletableFuture<String> acquireMSAccessToken(String authCode, Executor executor) Exchanges a Microsoft auth code for an access token.static CompletableFuture<String> acquireMSAuthCode(Consumer<URI> browserAction, Function<Boolean, @NotNull String> browserMessage, Executor executor) Generates a Microsoft login link, triggers the given browser action, and listens for a successful login callback.static CompletableFuture<String> acquireMSAuthCode(Consumer<URI> browserAction, Function<Boolean, @NotNull String> browserMessage, Executor executor, @Nullable MicrosoftUtils.MicrosoftPrompt prompt) Generates a Microsoft login link with user interaction, triggers the given browser action, and listens for a successful login callback.static CompletableFuture<String> acquireMSAuthCode(Function<Boolean, @NotNull String> browserMessage, Executor executor) Navigates to the Microsoft login, and listens for a successful login callback.static CompletableFuture<String> acquireMSAuthCode(Function<Boolean, @NotNull String> browserMessage, Executor executor, @Nullable MicrosoftUtils.MicrosoftPrompt prompt) Navigates to the Microsoft login with user interaction, and listens for a successful login callback.static CompletableFuture<String> acquireXboxAccessToken(String accessToken, Executor executor) Exchanges a Microsoft access token for an Xbox Live access token.static CompletableFuture<Map<String, String>> acquireXboxXstsToken(String accessToken, Executor executor) Exchanges an Xbox Live access token for an Xbox Live XSTS (security token service) token.static StringGenerates a random OAuth2 state.static CompletableFuture<net.minecraft.client.User> Fetches the Minecraft profile for the given access token, and returns a new Minecraft session.
-
Field Details
-
REQUEST_CONFIG
public static final org.apache.http.client.config.RequestConfig REQUEST_CONFIGA reusable Apache HTTP request configNB: We use Apache's HTTP implementation as the native HTTP client does not appear to free its resources after use!
-
CLIENT_ID
The default client id used in the configuration.- See Also:
-
AUTHORIZE_URL
The default authorization url used in the configuration.- See Also:
-
TOKEN_URL
The default token url used in the configuration.- See Also:
-
XBOX_AUTH_URL
The default Xbox authentication url used in the configuration.- See Also:
-
XBOX_XSTS_URL
The default Xbox XSTS url used in the configuration.- See Also:
-
MC_AUTH_URL
The default Minecraft authentication url used in the configuration.- See Also:
-
MC_PROFILE_URL
The default Minecraft profile url used in the configuration.- See Also:
-
-
Method Details
-
acquireMSAuthCode
public static CompletableFuture<String> acquireMSAuthCode(Function<Boolean, @NotNull String> browserMessage, Executor executor) Navigates to the Microsoft login, and listens for a successful login callback.NB: You must manually interrupt the executor thread if the completable future is cancelled!
- Parameters:
browserMessage- function that takes true if success, and returns a message to be shown in the browser after logging inexecutor- executor to run the login task on- Returns:
- completable future for the Microsoft auth token
- See Also:
-
acquireMSAuthCode
public static CompletableFuture<String> acquireMSAuthCode(Function<Boolean, @NotNull String> browserMessage, Executor executor, @Nullable @Nullable MicrosoftUtils.MicrosoftPrompt prompt) Navigates to the Microsoft login with user interaction, and listens for a successful login callback.NB: You must manually interrupt the executor thread if the completable future is cancelled!
- Parameters:
browserMessage- function that takes true if success, and returns a message to be shown in the browser after logging inexecutor- executor to run the login task onprompt- optional Microsoft interaction prompt override- Returns:
- completable future for the Microsoft auth token
- See Also:
-
acquireMSAuthCode
public static CompletableFuture<String> acquireMSAuthCode(Consumer<URI> browserAction, Function<Boolean, @NotNull String> browserMessage, Executor executor) Generates a Microsoft login link, triggers the given browser action, and listens for a successful login callback.NB: You must manually interrupt the executor thread if the completable future is cancelled!
- Parameters:
browserAction- consumer that opens the generated login urlbrowserMessage- function that takes true if success, and returns a message to be shown in the browser after logging inexecutor- executor to run the login task on- Returns:
- completable future for the Microsoft auth token
-
acquireMSAuthCode
public static CompletableFuture<String> acquireMSAuthCode(Consumer<URI> browserAction, Function<Boolean, @NotNull String> browserMessage, Executor executor, @Nullable @Nullable MicrosoftUtils.MicrosoftPrompt prompt) Generates a Microsoft login link with user interaction, triggers the given browser action, and listens for a successful login callback.NB: You must manually interrupt the executor thread if the completable future is cancelled!
- Parameters:
browserAction- consumer that opens the generated login urlbrowserMessage- function that takes true if success, and returns a message to be shown in the browser after logging inexecutor- executor to run the login task onprompt- optional Microsoft interaction prompt override- Returns:
- completable future for the Microsoft auth token
-
acquireMSAccessToken
Exchanges a Microsoft auth code for an access token.NB: You must manually interrupt the executor thread if the completable future is cancelled!
- Parameters:
authCode- Microsoft auth codeexecutor- executor to run the login task on- Returns:
- completable future for the Microsoft access token
-
acquireXboxAccessToken
public static CompletableFuture<String> acquireXboxAccessToken(String accessToken, Executor executor) Exchanges a Microsoft access token for an Xbox Live access token.NB: You must manually interrupt the executor thread if the completable future is cancelled!
- Parameters:
accessToken- Microsoft access tokenexecutor- executor to run the login task on- Returns:
- completable future for the Xbox Live access token
-
acquireXboxXstsToken
public static CompletableFuture<Map<String,String>> acquireXboxXstsToken(String accessToken, Executor executor) Exchanges an Xbox Live access token for an Xbox Live XSTS (security token service) token.NB: You must manually interrupt the executor thread if the completable future is cancelled!
- Parameters:
accessToken- Xbox Live access tokenexecutor- executor to run the login task on- Returns:
- completable future for a mapping of Xbox Live XSTS token ("Token") and user hash ("uhs")
-
acquireMCAccessToken
public static CompletableFuture<String> acquireMCAccessToken(String xstsToken, String userHash, Executor executor) Exchanges an Xbox Live XSTS token for a Minecraft access token.NB: You must manually interrupt the executor thread if the completable future is cancelled!
- Parameters:
xstsToken- Xbox Live XSTS tokenuserHash- Xbox Live user hashexecutor- executor to run the login task on- Returns:
- completable future for the Minecraft access token
-
login
Fetches the Minecraft profile for the given access token, and returns a new Minecraft session.NB: You must manually interrupt the executor thread if the completable future is cancelled!
- Parameters:
mcToken- Minecraft access tokenexecutor- executor to run the login task on- Returns:
- completable future for the new Minecraft session
- See Also:
-
generateState
Generates a random OAuth2 state.- Returns:
- OAuth2 state
-