/
minecraft.go
48 lines (42 loc) · 1.78 KB
/
minecraft.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package auth
import (
"context"
"crypto/ecdsa"
"crypto/x509"
"encoding/base64"
"fmt"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"io"
"net/http"
"strings"
)
// minecraftAuthURL is the URL that an authentication request is made to to get an encoded JWT claim chain.
const minecraftAuthURL = `https://multiplayer.minecraft.net/authentication`
// RequestMinecraftChain requests a fully processed Minecraft JWT chain using the XSTS token passed, and the
// ECDSA private key of the client. This key will later be used to initialise encryption, and must be saved
// for when packets need to be decrypted/encrypted.
func RequestMinecraftChain(ctx context.Context, token *XBLToken, key *ecdsa.PrivateKey) (string, error) {
data, _ := x509.MarshalPKIXPublicKey(&key.PublicKey)
// The body of the requests holds a JSON object with one key in it, the 'identityPublicKey', which holds
// the public key data of the private key passed.
body := `{"identityPublicKey":"` + base64.StdEncoding.EncodeToString(data) + `"}`
request, _ := http.NewRequestWithContext(ctx, "POST", minecraftAuthURL, strings.NewReader(body))
request.Header.Set("Content-Type", "application/json")
// The Authorization header is important in particular. It is composed of the 'uhs' found in the XSTS
// token, and the Token it holds itself.
token.SetAuthHeader(request)
request.Header.Set("User-Agent", "MCPE/Android")
request.Header.Set("Client-Version", protocol.CurrentVersion)
c := &http.Client{}
resp, err := c.Do(request)
if err != nil {
return "", fmt.Errorf("POST %v: %v", minecraftAuthURL, err)
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("POST %v: %v", minecraftAuthURL, resp.Status)
}
data, err = io.ReadAll(resp.Body)
_ = resp.Body.Close()
c.CloseIdleConnections()
return string(data), err
}