/
xbox.go
193 lines (174 loc) · 6.56 KB
/
xbox.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package auth
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/google/uuid"
"golang.org/x/oauth2"
"net/http"
"time"
)
// XBLToken holds info on the authorization token used for authenticating with XBOX Live.
type XBLToken struct {
AuthorizationToken struct {
DisplayClaims struct {
UserInfo []struct {
GamerTag string `json:"gtg"`
XUID string `json:"xid"`
UserHash string `json:"uhs"`
} `json:"xui"`
}
Token string
}
}
// SetAuthHeader returns a string that may be used for the 'Authorization' header used for Minecraft
// related endpoints that need an XBOX Live authenticated caller.
func (t XBLToken) SetAuthHeader(r *http.Request) {
r.Header.Set("Authorization", fmt.Sprintf("XBL3.0 x=%v;%v", t.AuthorizationToken.DisplayClaims.UserInfo[0].UserHash, t.AuthorizationToken.Token))
}
// RequestXBLToken requests an XBOX Live auth token using the passed Live token pair.
func RequestXBLToken(ctx context.Context, liveToken *oauth2.Token, relyingParty string) (*XBLToken, error) {
if !liveToken.Valid() {
return nil, fmt.Errorf("live token is no longer valid")
}
c := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
Renegotiation: tls.RenegotiateOnceAsClient,
InsecureSkipVerify: true,
},
},
}
defer c.CloseIdleConnections()
// We first generate an ECDSA private key which will be used to provide a 'ProofKey' to each of the
// requests, and to sign these requests.
key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
deviceToken, err := obtainDeviceToken(ctx, c, key)
if err != nil {
return nil, err
}
return obtainXBLToken(ctx, c, key, liveToken, deviceToken, relyingParty)
}
func obtainXBLToken(ctx context.Context, c *http.Client, key *ecdsa.PrivateKey, liveToken *oauth2.Token, device *deviceToken, relyingParty string) (*XBLToken, error) {
data, _ := json.Marshal(map[string]any{
"AccessToken": "t=" + liveToken.AccessToken,
"AppId": "0000000048183522",
"deviceToken": device.Token,
"Sandbox": "RETAIL",
"UseModernGamertag": true,
"SiteName": "user.auth.xboxlive.com",
"RelyingParty": relyingParty,
"ProofKey": map[string]any{
"crv": "P-256",
"alg": "ES256",
"use": "sig",
"kty": "EC",
"x": base64.RawURLEncoding.EncodeToString(key.PublicKey.X.Bytes()),
"y": base64.RawURLEncoding.EncodeToString(key.PublicKey.Y.Bytes()),
},
})
req, _ := http.NewRequestWithContext(ctx, "POST", "https://sisu.xboxlive.com/authorize", bytes.NewReader(data))
req.Header.Set("x-xbl-contract-version", "1")
sign(req, data, key)
resp, err := c.Do(req)
if err != nil {
return nil, fmt.Errorf("POST %v: %v", "https://sisu.xboxlive.com/authorize", err)
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("POST %v: %v", "https://sisu.xboxlive.com/authorize", resp.Status)
}
info := new(XBLToken)
return info, json.NewDecoder(resp.Body).Decode(info)
}
// deviceToken is the token obtained by requesting a device token by posting to xblDeviceAuthURL. Its Token
// field may be used in a request to obtain the XSTS token.
type deviceToken struct {
Token string
}
// obtainDeviceToken sends a POST request to the device auth endpoint using the ECDSA private key passed to
// sign the request.
func obtainDeviceToken(ctx context.Context, c *http.Client, key *ecdsa.PrivateKey) (token *deviceToken, err error) {
data, _ := json.Marshal(map[string]any{
"RelyingParty": "http://auth.xboxlive.com",
"TokenType": "JWT",
"Properties": map[string]any{
"AuthMethod": "ProofOfPossession",
"Id": "{" + uuid.New().String() + "}",
"DeviceType": "Android",
"Version": "10",
"ProofKey": map[string]any{
"crv": "P-256",
"alg": "ES256",
"use": "sig",
"kty": "EC",
"x": base64.RawURLEncoding.EncodeToString(key.PublicKey.X.Bytes()),
"y": base64.RawURLEncoding.EncodeToString(key.PublicKey.Y.Bytes()),
},
},
})
request, _ := http.NewRequestWithContext(ctx, "POST", "https://device.auth.xboxlive.com/device/authenticate", bytes.NewReader(data))
request.Header.Set("x-xbl-contract-version", "1")
sign(request, data, key)
resp, err := c.Do(request)
if err != nil {
return nil, fmt.Errorf("POST %v: %v", "https://device.auth.xboxlive.com/device/authenticate", err)
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("POST %v: %v", "https://device.auth.xboxlive.com/device/authenticate", resp.Status)
}
token = &deviceToken{}
return token, json.NewDecoder(resp.Body).Decode(token)
}
// sign signs the request passed containing the body passed. It signs the request using the ECDSA private key
// passed. If the request has a 'ProofKey' field in the Properties field, that key must be passed here.
func sign(request *http.Request, body []byte, key *ecdsa.PrivateKey) {
currentTime := windowsTimestamp()
hash := sha256.New()
// Signature policy version (0, 0, 0, 1) + 0 byte.
buf := bytes.NewBuffer([]byte{0, 0, 0, 1, 0})
// Timestamp + 0 byte.
_ = binary.Write(buf, binary.BigEndian, currentTime)
buf.Write([]byte{0})
hash.Write(buf.Bytes())
// HTTP method, generally POST + 0 byte.
hash.Write([]byte("POST"))
hash.Write([]byte{0})
// Request uri path + raw query + 0 byte.
hash.Write([]byte(request.URL.Path + request.URL.RawQuery))
hash.Write([]byte{0})
// Authorization header if present, otherwise an empty string + 0 byte.
hash.Write([]byte(request.Header.Get("Authorization")))
hash.Write([]byte{0})
// Body data (only up to a certain limit, but this limit is practically never reached) + 0 byte.
hash.Write(body)
hash.Write([]byte{0})
// Sign the checksum produced, and combine the 'r' and 's' into a single signature.
r, s, _ := ecdsa.Sign(rand.Reader, key, hash.Sum(nil))
signature := append(r.Bytes(), s.Bytes()...)
// The signature begins with 12 bytes, the first being the signature policy version (0, 0, 0, 1) again,
// and the other 8 the timestamp again.
buf = bytes.NewBuffer([]byte{0, 0, 0, 1})
_ = binary.Write(buf, binary.BigEndian, currentTime)
// Append the signature to the other 12 bytes, and encode the signature with standard base64 encoding.
sig := append(buf.Bytes(), signature...)
request.Header.Set("Signature", base64.StdEncoding.EncodeToString(sig))
}
// windowsTimestamp returns a Windows specific timestamp. It has a certain offset from Unix time which must be
// accounted for.
func windowsTimestamp() int64 {
return (time.Now().Unix() + 11644473600) * 10000000
}