This article is licensed under a Creative Commons Attribution-ShareAlike 3.0 license. Derivative works must be licensed using the same or a compatible license.

Microsoft authentication

Jump to navigation Jump to search
For API documentation, see Mojang API.

Minecraft games use Microsoft accounts for authentication. There are multiple steps and different tokens required, but in the end, a normal Minecraft token will be received. Launching the game itself hasn't changed. All accounts now use this new system.

Microsoft OAuth2 flow[edit | edit source]

Prior to any of these steps, you will first need to obtain an OAuth 2.0 client ID by creating a Microsoft Azure application. You will not need to obtain a client secret.

You can then use the OAuth2 authorization code flow to obtain an access token. You'll need to present the user with a login page that, once completed, will redirect to a specified URL with the token in the query parameters. In non-web applications this typically involves spinning up a temporary HTTP server to handle the redirect. If you'd rather not do that, consider using the (slightly less automatic) device code flow instead.

In any case, you'll need to include XboxLive.signin in the scope parameter of the authorization request; otherwise the next endpoint will complain, and not in a helpful way.

According to this support Article, new created Azure Apps must apply for the Permission to use the Minecraft API using this form. If your App don't have the Permission api.minecraftservices.com will return a 403.

Note: You must use the consumers AAD tenant to sign in with the XboxLive.signin scope. Using an Azure AD tenant ID or the common scope will just give errors. This also means you cannot sign in with users that are in the AAD tenant, only with consumer Microsoft accounts.

Authenticate with Xbox Live[edit | edit source]

Now that we are authenticated with Microsoft, we can authenticate with Xbox Live.

To do that, we send

POST https://user.auth.xboxlive.com/user/authenticate
Content-Type: application/json
Accept: application/json
{
    "Properties": {
        "AuthMethod": "RPS",
        "SiteName": "user.auth.xboxlive.com",
        "RpsTicket": "d=<access token>" // your access token from the previous step here, make sure that it is prefixed with `d=`
    },
    "RelyingParty": "http://auth.xboxlive.com",
    "TokenType": "JWT"
}

Again, it will complain if you don't set Content-Type: application/json and Accept: application/json. It will also complain if your SSL implementation does not support SSL renegotiations.

The response will look like this:

{
    "IssueInstant": "2020-12-07T19:52:08.4463796Z",
    "NotAfter": "2020-12-21T19:52:08.4463796Z",
    "Token": "token", // save this, this is your xbl token
    "DisplayClaims": {
        "xui": [
            {
                "uhs": "userhash" // save this
            }
        ]
    }
}

Obtain XSTS token for Minecraft[edit | edit source]

Now that we are authenticated with XBL, we need to get a XSTS token, we can use to login to Minecraft.

POST https://xsts.auth.xboxlive.com/xsts/authorize
Content-Type: application/json
Accept: application/json
{
    "Properties": {
        "SandboxId": "RETAIL",
        "UserTokens": [
            "xbl_token" // from above
        ]
    },
    "RelyingParty": "rp://api.minecraftservices.com/",
    "TokenType": "JWT"
}

Again, set content type and accept to json and ensure SSL renegotiation is supported by your client.

Note: When trying to get the XSTS token for the bedrock realms API, you need to change the following:

"RelyingParty": "https://pocket.realms.minecraft.net/"

also you can stop at this point, as the bedrock realms API uses the XSTS token directly instead of a seperate auth scheme.

Response will look like this:

{
    "IssueInstant": "2020-12-07T19:52:09.2345095Z",
    "NotAfter": "2020-12-08T11:52:09.2345095Z",
    "Token": "token", // save this, this is your xsts token
    "DisplayClaims": {
        "xui": [
            {
                "uhs": "userhash" // same as last request
            }
        ]
    }
}

The endpoint can return a 401 error with the below response:

{
    "Identity": "0",
    "XErr": 2148916238,
    "Message": "",
    "Redirect": "https://start.ui.xboxlive.com/AddChildToFamily"
}

The Redirect parameter usually will not resolve or go anywhere in a browser, likely they're targeting Xbox consoles.

Noted XErr codes and their meanings:

  • 2148916227: The account is banned from Xbox.
  • 2148916233: The account doesn't have an Xbox account. Once they sign up for one (or login through minecraft.net to create one) then they can proceed with the login. This shouldn't happen with accounts that have purchased Minecraft with a Microsoft account, as they would've already gone through that Xbox signup process.
  • 2148916235: The account is from a country where Xbox Live is not available/banned
  • 2148916236: The account needs adult verification on Xbox page. (South Korea)
  • 2148916237: The account needs adult verification on Xbox page. (South Korea)
  • 2148916238: The account is a child (under 18) and cannot proceed unless the account is added to a Family by an adult. This only seems to occur when using a custom Microsoft Azure application. When using the Minecraft launchers client id, this doesn't trigger.
  • 2148916262: TBD, happens rarely without any additional information.

Authenticate with Minecraft[edit | edit source]

Now we can finally start talking to Minecraft. The XSTS token from the last request allows us to authenticate with Minecraft using

POST https://api.minecraftservices.com/authentication/login_with_xbox
Content-Type: application/json
Accept: application/json
{
    "identityToken": "XBL3.0 x=<userhash>;<xsts_token>"
}

Response:

{
    "username": "some uuid", // this is not the uuid of the account
    "roles": [],
    "access_token": "minecraft access token", // jwt, your good old minecraft access token
    "token_type": "Bearer",
    "expires_in": 86400
}

This access token allows us to launch the game, but, we haven't actually checked if the account owns the game. Everything until here works with a normal Microsoft account!

Checking game ownership[edit | edit source]

So let's use our mc access token to check if a product licence is attached to the account.

GET https://api.minecraftservices.com/entitlements/mcstore
Authorization: Bearer <Minecraft Access Token>

The access token goes into the auth header: Authorization: Bearer <Minecraft Access Token>. (Keep in mind that Bearer is actually the prefix you must include!)

If the account owns the game, the response will look like this:

{
    "items": [
        {
            "name": "product_minecraft",
            "signature": "jwt sig"
        },
        {
            "name": "game_minecraft",
            "signature": "jwt sig"
        }
    ],
    "signature": "jwt sig",
    "keyId": "1"
}

The first jwts contain the values:

{
  "typ": "JWT",
  "alg": "RS256",
  "kid": "1"
}.{
  "signerId": "2535416586892404",
  "name": "product_minecraft"
}.[Signature]

the last jwt looks like this decoded:

{
  "typ": "JWT",
  "alg": "RS256",
  "kid": "1"
}.{
  "entitlements": [
    {
      "name": "product_minecraft"
    },
    {
      "name": "game_minecraft"
    }
  ],
  "signerId": "2535416586892404"
}.[Signature]

If the account doesn't own the game, the items array will be empty.

Note that Xbox Game Pass users don't technically own the game, and therefore will not show any ownership here, but will indeed have a Minecraft profile attached to their account.

Note that the signature should always be checked with the public key from Mojang to verify that it is a legitimate response from the official servers:

-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAtz7jy4jRH3psj5AbVS6W
NHjniqlr/f5JDly2M8OKGK81nPEq765tJuSILOWrC3KQRvHJIhf84+ekMGH7iGlO
4DPGDVb6hBGoMMBhCq2jkBjuJ7fVi3oOxy5EsA/IQqa69e55ugM+GJKUndLyHeNn
X6RzRzDT4tX/i68WJikwL8rR8Jq49aVJlIEFT6F+1rDQdU2qcpfT04CBYLM5gMxE
fWRl6u1PNQixz8vSOv8pA6hB2DU8Y08VvbK7X2ls+BiS3wqqj3nyVWqoxrwVKiXR
kIqIyIAedYDFSaIq5vbmnVtIonWQPeug4/0spLQoWnTUpXRZe2/+uAKN1RY9mmaB
pRFV/Osz3PDOoICGb5AZ0asLFf/qEvGJ+di6Ltt8/aaoBuVw+7fnTw2BhkhSq1S/
va6LxHZGXE9wsLj4CN8mZXHfwVD9QG0VNQTUgEGZ4ngf7+0u30p7mPt5sYy3H+Fm
sWXqFZn55pecmrgNLqtETPWMNpWc2fJu/qqnxE9o2tBGy/MqJiw3iLYxf7U+4le4
jM49AUKrO16bD1rdFwyVuNaTefObKjEMTX9gyVUF6o7oDEItp5NHxFm3CqnQRmch
HsMs+NxEnN4E9a8PDB23b4yjKOQ9VHDxBxuaZJU60GBCIOF9tslb7OAkheSJx5Xy
EYblHbogFGPRFU++NrSQRX0CAwEAAQ==
-----END PUBLIC KEY-----

See the JWT standard[1] for more details.

In case the public key ever changes, it can be extracted from the launcher library:

strings ~/.minecraft/launcher/liblauncher.so > launcher-strings.txt

The created file launcher-strings.txt will include 2 strings which begin with -----BEGIN PUBLIC KEY----- and end with -----END PUBLIC KEY-----. The first key seems to be the one used for the JWT tokens, use of the second key is unknown.

Getting the profile[edit | edit source]

Now that we know that the account owns the game, we can get their profile in order to fetch the UUID:

GET https://api.minecraftservices.com/minecraft/profile
Authorization: Bearer <Minecraft Access Token>

The response will look like this, if the account owns the game:

{
    "id": "986dec87b7ec47ff89ff033fdb95c4b5", // the real uuid of the account, woo
    "name": "HowDoesAuthWork", // the mc user name of the account
    "skins": [
        {
            "id": "6a6e65e5-76dd-4c3c-a625-162924514568",
            "state": "ACTIVE",
            "url": "http://textures.minecraft.net/texture/1a4af718455d4aab528e7a61f86fa25e6a369d1768dcb13f7df319a713eb810b",
            "variant": "CLASSIC",
            "alias": "STEVE"
        }
    ],
    "capes": [
        {
            "id": "5af20372-79e0-4e1f-80f8-6bd8e3135995",
            "state": "ACTIVE",
            "url": "http://textures.minecraft.net/texture/2340c0e03dd24a11b15a8b33c2a7e9e32abb2051b2481d0ba7defd635ca7a933",
            "alias": "Migrator"
        }
    ]
}

Else it will look like this:

{
    "path": "/minecraft/profile",
    "error": "NOT_FOUND",
    "errorMessage": "The server has not found anything matching the request URI"
}

Note that Xbox Game Pass users who haven't logged into the new Minecraft Launcher at least once will not return a profile, and will need to login once after activating Xbox Game Pass to setup their Minecraft username.

You should now have all necessary data (the mc access token, the username and the uuid) to launch the game. Well done!

Sample implementations[edit | edit source]

  • minecraft_auth.py: Authentication like the launcher does (i.e. code flow) + refresh token request.
  • A fully working kotlin implementation can be found [2] here using device tokens.
  • A fully working cli wrapper in Java using device tokens here
  • A rough sample implementation in Java (using javafx and its webview) here.
  • A fully working Java library supporting 4 login flows can be found here.
  • An implementation in Go here.
  • An implementation in JS can be found here and one using JS/TS here
  • An implementation in Python can be found here
  • An implementation in Rust can be found here.
  • A Kotlin library (JVM + JS) can be found here.
  • A C# library using webview and MSAL.NET can be found here.
  • A Rust library can be found here.
  • A PHP library can be found here.

Navigation[edit | edit source]

This article is licensed under a Creative Commons Attribution-ShareAlike 3.0 license.
 
This article has been imported from wiki.vg or is a derivative of such a page. Thus, the wiki's usual license does not apply.
Derivative works must be licensed using the same or a compatible license.