-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
81 lines (64 loc) · 1.87 KB
/
login.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/brawaru/marct/network"
"github.com/brawaru/marct/utils"
)
type AuthResponse struct {
Username string `json:"username"`
Roles []interface{} `json:"roles"`
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
}
type APIError struct {
Path string `json:"path"`
ErrorType string `json:"errorType"`
ErrorCode string `json:"error"`
ErrorMessage string `json:"errorMessage"`
DeveloperMessage string `json:"developerMessage"`
}
func (e *APIError) Error() string {
return e.ErrorMessage
}
const (
loginWithXboxURL = "https://api.minecraftservices.com/authentication/login_with_xbox"
)
func LoginWithXbox(userHash string, xstsToken string) (*AuthResponse, error) {
marshal, bodyMarshalErr := json.Marshal(struct {
IdentityToken string `json:"identityToken"`
}{
IdentityToken: "XBL3.0 x=" + userHash + ";" + xstsToken,
})
if bodyMarshalErr != nil {
return nil, bodyMarshalErr
}
req, reqCreateErr := http.NewRequest("POST", loginWithXboxURL, bytes.NewBuffer(marshal))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
if reqCreateErr != nil {
return nil, reqCreateErr
}
resp, reqErr := network.PerformRequest(req, network.WithRetries())
if reqErr != nil {
return nil, reqErr
}
defer utils.DClose(resp.Body)
if resp.StatusCode != 200 {
if resp.StatusCode == 400 {
var e APIError
if json.NewDecoder(resp.Body).Decode(&e) == nil {
return nil, &e
}
}
return nil, fmt.Errorf("bad response code: %v (%s)", resp.StatusCode, resp.Status)
}
var authResp AuthResponse
if decodeErr := json.NewDecoder(resp.Body).Decode(&authResp); decodeErr != nil {
return nil, decodeErr
}
return &authResp, nil
}