forked from unmojang/drasl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
127 lines (114 loc) · 3 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
package main
import (
"encoding/json"
"errors"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
"log"
"net/http"
"net/url"
)
type playerNameToUUIDResponse struct {
Name string `json:"name"`
ID string `json:"id"`
}
// /users/profiles/minecraft/:playerName
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 := http.Get(reqURL)
if err != nil {
log.Println(err)
continue
}
defer res.Body.Close()
if res.StatusCode == http.StatusOK {
return c.Stream(http.StatusOK, res.Header.Get("Content-Type"), res.Body)
}
}
return c.NoContent(http.StatusNoContent)
}
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)
}
}
// /profiles/minecraft
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 := http.Get(reqURL)
if err != nil {
log.Println(err)
continue
}
defer res.Body.Close()
if res.StatusCode == http.StatusOK {
var playerRes playerNameToUUIDResponse
err = json.NewDecoder(res.Body).Decode(&playerRes)
if err != nil {
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)
}
}
// /user/security/location
func AccountVerifySecurityLocation(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
return c.NoContent(http.StatusNoContent)
}
}