forked from unmojang/drasl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
135 lines (121 loc) · 3.56 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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
"log"
"net/http"
"net/url"
)
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, "users/profiles/minecraft", playerName)
if err != nil {
log.Println(err)
continue
}
res, err := app.CachedGet(reqURL, 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 {
// Be silent, 404s will be common here
continue
}
return c.Blob(http.StatusOK, "application/json", res.BodyBytes)
}
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 {
// playerNames := &[]string{}
var playerNames []string
if err := json.NewDecoder(c.Request().Body).Decode(&playerNames); err != nil {
return err
}
n := len(playerNames)
response := make([]playerNameToUUIDResponse, 0, n)
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) {
for _, fallbackAPIServer := range app.Config.FallbackAPIServers {
reqURL, err := url.JoinPath(fallbackAPIServer.AccountURL, "users/profiles/minecraft", playerName)
if err != nil {
log.Println(err)
continue
}
res, err := app.CachedGet(reqURL, 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 playerRes playerNameToUUIDResponse
err = json.Unmarshal(res.BodyBytes, &playerRes)
if err != nil {
log.Printf("Received invalid response from fallback API server at %s\n", reqURL)
continue
}
response = append(response, playerRes)
break
}
} 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)
}
}
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)
}
}