-
Notifications
You must be signed in to change notification settings - Fork 14
/
account.go
176 lines (154 loc) · 4.65 KB
/
account.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
"log"
"net/http"
"net/url"
"strings"
)
type playerNameToUUIDResponse struct {
Name string `json:"name"`
ID string `json:"id"`
}
// GET /users/profiles/minecraft/:playerName
// https://wiki.vg/Mojang_API#Username_to_UUID
func AccountPlayerNameToID(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
playerName := c.Param("playerName")
var user User
result := app.DB.First(&user, "player_name = ?", playerName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
for _, fallbackAPIServer := range app.Config.FallbackAPIServers {
reqURL, err := url.JoinPath(fallbackAPIServer.AccountURL, "profiles/minecraft")
if err != nil {
log.Println(err)
continue
}
payload := []string{playerName}
body, err := json.Marshal(payload)
if err != nil {
return err
}
res, err := app.CachedPostJSON(reqURL, body, fallbackAPIServer.CacheTTLSeconds)
if err != nil {
log.Printf("Couldn't access fallback API server at %s: %s\n", reqURL, err)
continue
}
if res.StatusCode != http.StatusOK {
continue
}
var fallbackResponses []playerNameToUUIDResponse
err = json.Unmarshal(res.BodyBytes, &fallbackResponses)
if err != nil {
log.Printf("Received invalid response from fallback API server at %s\n", reqURL)
continue
}
if len(fallbackResponses) == 1 && strings.EqualFold(playerName, fallbackResponses[0].Name) {
return c.JSON(http.StatusOK, fallbackResponses[0])
}
}
errorMessage := fmt.Sprintf("Couldn't find any profile with name %s", playerName)
return MakeErrorResponse(&c, http.StatusNotFound, nil, Ptr(errorMessage))
}
return result.Error
}
id, err := UUIDToID(user.UUID)
if err != nil {
return err
}
res := playerNameToUUIDResponse{
Name: user.PlayerName,
ID: id,
}
return c.JSON(http.StatusOK, res)
}
}
// POST /profiles/minecraft
// https://wiki.vg/Mojang_API#Usernames_to_UUIDs
func AccountPlayerNamesToIDs(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
var playerNames []string
if err := json.NewDecoder(c.Request().Body).Decode(&playerNames); err != nil {
return err
}
n := len(playerNames)
if !(1 <= n && n <= 10) {
return MakeErrorResponse(&c, http.StatusBadRequest, Ptr("CONSTRAINT_VIOLATION"), Ptr("getProfileName.profileNames: size must be between 1 and 10"))
}
response := make([]playerNameToUUIDResponse, 0, n)
remainingPlayers := map[string]bool{}
for _, playerName := range playerNames {
var user User
result := app.DB.First(&user, "player_name = ?", playerName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
remainingPlayers[strings.ToLower(playerName)] = true
} else {
return result.Error
}
} else {
id, err := UUIDToID(user.UUID)
if err != nil {
return err
}
playerRes := playerNameToUUIDResponse{
Name: user.PlayerName,
ID: id,
}
response = append(response, playerRes)
}
}
for _, fallbackAPIServer := range app.Config.FallbackAPIServers {
reqURL, err := url.JoinPath(fallbackAPIServer.AccountURL, "profiles/minecraft")
if err != nil {
log.Println(err)
continue
}
payload := make([]string, 0, len(remainingPlayers))
for remainingPlayer := range remainingPlayers {
payload = append(payload, remainingPlayer)
}
body, err := json.Marshal(payload)
if err != nil {
return err
}
res, err := app.CachedPostJSON(reqURL, body, fallbackAPIServer.CacheTTLSeconds)
if err != nil {
log.Printf("Couldn't access fallback API server at %s: %s\n", reqURL, err)
continue
}
if res.StatusCode != http.StatusOK {
continue
}
var fallbackResponses []playerNameToUUIDResponse
err = json.Unmarshal(res.BodyBytes, &fallbackResponses)
if err != nil {
log.Printf("Received invalid response from fallback API server at %s\n", reqURL)
continue
}
for _, fallbackResponse := range fallbackResponses {
lowerName := strings.ToLower(fallbackResponse.Name)
if _, ok := remainingPlayers[lowerName]; ok {
response = append(response, fallbackResponse)
delete(remainingPlayers, lowerName)
}
}
if len(remainingPlayers) == 0 {
break
}
}
return c.JSON(http.StatusOK, response)
}
}
// GET /user/security/location
// https://wiki.vg/Mojang_API#Verify_Security_Location
func AccountVerifySecurityLocation(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
return c.NoContent(http.StatusNoContent)
}
}