Class MicrosoftUtils

java.lang.Object
me.axieum.mcmod.authme.api.util.MicrosoftUtils

public final class MicrosoftUtils extends Object
Utility methods for authenticating via Microsoft.

For more information refer to: https://wiki.vg/Microsoft_Authentication_Scheme

  • Field Details

    • REQUEST_CONFIG

      public static final org.apache.http.client.config.RequestConfig REQUEST_CONFIG
      A reusable Apache HTTP request config

      NB: We use Apache's HTTP implementation as the native HTTP client does not appear to free its resources after use!

    • CLIENT_ID

      public static final String CLIENT_ID
      The default client id used in the configuration.
      See Also:
    • AUTHORIZE_URL

      public static final String AUTHORIZE_URL
      The default authorization url used in the configuration.
      See Also:
    • TOKEN_URL

      public static final String TOKEN_URL
      The default token url used in the configuration.
      See Also:
    • XBOX_AUTH_URL

      public static final String XBOX_AUTH_URL
      The default Xbox authentication url used in the configuration.
      See Also:
    • XBOX_XSTS_URL

      public static final String XBOX_XSTS_URL
      The default Xbox XSTS url used in the configuration.
      See Also:
    • MC_AUTH_URL

      public static final String MC_AUTH_URL
      The default Minecraft authentication url used in the configuration.
      See Also:
    • MC_PROFILE_URL

      public static final String 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 in
      executor - 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 in
      executor - executor to run the login task on
      prompt - 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 url
      browserMessage - function that takes true if success, and returns a message to be shown in the browser after logging in
      executor - 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 url
      browserMessage - function that takes true if success, and returns a message to be shown in the browser after logging in
      executor - executor to run the login task on
      prompt - optional Microsoft interaction prompt override
      Returns:
      completable future for the Microsoft auth token
    • acquireMSAccessToken

      public static CompletableFuture<String> acquireMSAccessToken(String authCode, Executor executor)
      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 code
      executor - 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 token
      executor - 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 token
      executor - 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 token
      userHash - Xbox Live user hash
      executor - executor to run the login task on
      Returns:
      completable future for the Minecraft access token
    • login

      public static CompletableFuture<net.minecraft.client.User> login(String mcToken, Executor executor)
      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 token
      executor - executor to run the login task on
      Returns:
      completable future for the new Minecraft session
      See Also:
    • generateState

      public static String generateState()
      Generates a random OAuth2 state.
      Returns:
      OAuth2 state