-
Notifications
You must be signed in to change notification settings - Fork 1
/
webapi.go
293 lines (258 loc) · 6.21 KB
/
webapi.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package liter
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"path"
"strings"
)
const (
DefaultApiServer = "https://api.mojang.com/"
DefaultAuthServer = "https://authserver.mojang.com/"
DefaultSessionServer = "https://sessionserver.mojang.com/session/minecraft/"
)
var (
ErrPlayerNameInvalid = errors.New("Player name is invalid")
ErrPlayerNotExists = errors.New("Player not exists on the server")
)
type AuthClient struct {
Client *http.Client
ApiServer string
AuthServer string
SessionServer string
}
var DefaultAuthClient = &AuthClient{
Client: &http.Client{},
ApiServer: DefaultApiServer,
AuthServer: DefaultAuthServer,
SessionServer: DefaultSessionServer,
}
type HttpStatusError struct {
Code int
}
var _ error = (*HttpStatusError)(nil)
func (e *HttpStatusError) Error() string {
return fmt.Sprintf("%d %s", e.Code, http.StatusText(e.Code))
}
func (cli *AuthClient) GetPlayerInfo(name string) (player PlayerInfo, err error) {
const EndPoint = "/users/profiles/minecraft"
if len(name) == 0 {
err = ErrPlayerNameInvalid
return
}
url := joinUrl(cli.ApiServer, EndPoint, path.Base(name))
var res *http.Response
if res, err = cli.Client.Get(url); err != nil {
return
}
if res.StatusCode == http.StatusNoContent {
err = ErrPlayerNotExists
return
}
if res.StatusCode != http.StatusOK {
err = &HttpStatusError{res.StatusCode}
return
}
var body []byte
if body, err = readBody(res); err != nil {
return
}
if err = json.Unmarshal(body, &player); err != nil {
return
}
return
}
func (cli *AuthClient) GetPlayerProfile(id UUID) (profile *PlayerProfile, err error) {
const EndPoint = "/profile"
url := joinUrl(cli.SessionServer, EndPoint, id.String())
var res *http.Response
if res, err = cli.Client.Get(url); err != nil {
return
}
if res.StatusCode == http.StatusNoContent {
err = ErrPlayerNotExists
return
}
if res.StatusCode != http.StatusOK {
err = &HttpStatusError{res.StatusCode}
return
}
var body []byte
if body, err = readBody(res); err != nil {
return
}
profile = new(PlayerProfile)
if err = json.Unmarshal(body, profile); err != nil {
return
}
return
}
func (cli *AuthClient) auth_request(point string, req any, res any) (err error) {
url := joinUrl(cli.AuthServer, point)
var buf []byte
if buf, err = json.Marshal(req); err != nil {
return
}
var res0 *http.Response
if res0, err = cli.Client.Post(url, "application/json", bytes.NewReader(buf)); err != nil {
return
}
var body []byte
if body, err = readBody(res0); err != nil {
return
}
failed := res0.StatusCode/100 != 2 /* if not 2xx */
if failed {
yggErr := new(YggError)
if err = json.Unmarshal(body, yggErr); err != nil {
return
}
return yggErr
}
if res0.StatusCode != http.StatusNoContent {
if len(body) > 0 {
if err = json.Unmarshal(body, res); err != nil {
return
}
}
}
return
}
type YggError struct {
Code int `json:"-"`
ErrorShort string `json:"error"`
ErrorMessage string `json:"errorMessage"`
Cause string `json:"cause,omitempty"`
}
var _ error = (*YggError)(nil)
func (e *YggError) Error() (s string) {
s = fmt.Sprintf("HTTP %d %s: %s", e.Code, e.ErrorShort, e.ErrorMessage)
if len(e.Cause) > 0 {
s += " caused by " + e.Cause
}
return
}
type LoginData struct {
Username string `json:"username"`
Password string `json:"password"`
}
type AccessData struct {
ClientToken string `json:"clientToken"`
AccessToken string `json:"accessToken"`
}
type Prop struct {
Key string `json:"key"`
Value string `json:"value"`
}
type Props []Prop
type UserData struct {
Username string `json:"username"`
Id string `json:"id"`
Properties Props `json:"properties"`
}
type AuthData struct {
LoginData
ClientToken string `json:"clientToken"`
}
type AuthResponse struct {
User UserData `json:"user"`
ClientToken string `json:"clientToken"`
AccessToken string `json:"accessToken"`
AvailableProfiles []PlayerInfo `json:"availableProfiles"`
SelectedProfile PlayerInfo `json:"selectedProfile"`
}
func (cli *AuthClient) Auth(data AuthData) (res *AuthResponse, err error) {
const EndPoint = "/authenticate"
type agentT struct {
Name string `json:"name"`
Version int `json:"version"`
}
type payload struct {
Agent agentT `json:"agent"`
AuthData
RequestUser bool `json:"requestUser"`
}
res = new(AuthResponse)
if err = cli.auth_request(EndPoint, payload{
Agent: agentT{
Name: "Minecraft",
Version: 1,
},
AuthData: data,
RequestUser: true,
}, res); err != nil {
return
}
return
}
type RefreshResponse struct {
User UserData `json:"user"`
ClientToken string `json:"clientToken"`
AccessToken string `json:"accessToken"`
SelectedProfile PlayerInfo `json:"selectedProfile"`
}
func (cli *AuthClient) Refresh(data AccessData) (res *RefreshResponse, err error) {
const EndPoint = "/refresh"
type payload struct {
AccessData
RequestUser bool `json:"requestUser"`
}
res = new(RefreshResponse)
if err = cli.auth_request(EndPoint, payload{
AccessData: data,
RequestUser: true,
}, res); err != nil {
return
}
return
}
func (cli *AuthClient) Validate(data AccessData) (err error) {
const EndPoint = "/validate"
if err = cli.auth_request(EndPoint, data, nil); err != nil {
return
}
return
}
func (cli *AuthClient) Signout(data LoginData) (err error) {
const EndPoint = "/signout"
if err = cli.auth_request(EndPoint, data, nil); err != nil {
return
}
return
}
func (cli *AuthClient) Invalidate(data AccessData) (err error) {
const EndPoint = "/invalidate"
if err = cli.auth_request(EndPoint, data, nil); err != nil {
return
}
return
}
func joinUrl(base string, paths ...string) (url string) {
var scheme string
if i := strings.Index(base, "://"); i != -1 {
scheme, base = base[:i], base[i+3:]
}
url = path.Join(base, path.Join(paths...))
if len(scheme) > 0 {
url = scheme + "://" + url
}
return
}
func readBody(res *http.Response) (body []byte, err error) {
defer res.Body.Close()
if res.StatusCode == http.StatusNoContent {
return
}
if l := res.ContentLength; l >= 0 {
body = make([]byte, l)
if _, err = io.ReadFull(res.Body, body); err != nil {
return
}
} else if body, err = io.ReadAll(res.Body); err != nil {
return
}
return
}