-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathauthlib_injector.go
198 lines (180 loc) · 5.89 KB
/
authlib_injector.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
package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"github.com/labstack/echo/v4"
"github.com/samber/mo"
"io"
"net/http"
"net/url"
)
type authlibInjectorLinks struct {
Homepage string `json:"homepage"`
Register string `json:"register"`
}
type authlibInjectorMeta struct {
ImplementationName string `json:"implementationName"`
ImplementationVersion string `json:"implementationVersion"`
Links authlibInjectorLinks `json:"links"`
ServerName string `json:"serverName"`
FeatureEnableProfileKey bool `json:"feature.enable_profile_key"`
}
type authlibInjectorResponse struct {
Meta authlibInjectorMeta `json:"meta"`
SignaturePublickey string `json:"signaturePublickey"`
SignaturePublickeys []string `json:"signaturePublickeys"`
SkinDomains []string `json:"skinDomains"`
}
func authlibInjectorSerializeKey(key *rsa.PublicKey) (string, error) {
pubDER, err := x509.MarshalPKIXPublicKey(key)
if err != nil {
return "", err
}
pubPEM := pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: pubDER,
})
return string(pubPEM[:]), nil
}
func AuthlibInjectorRoot(app *App) func(c echo.Context) error {
skinDomains := make([]string, 0, 1+len(app.Config.FallbackAPIServers))
skinDomains = append(skinDomains, app.Config.Domain)
for _, fallbackAPIServer := range app.Config.FallbackAPIServers {
for _, skinDomain := range fallbackAPIServer.SkinDomains {
if !Contains(skinDomains, skinDomain) {
skinDomains = append(skinDomains, skinDomain)
}
}
}
signaturePublicKey, err := authlibInjectorSerializeKey(&app.PrivateKey.PublicKey)
Check(err)
signaturePublicKeys := make([]string, 0, len(app.ProfilePropertyKeys))
for _, key := range app.ProfilePropertyKeys {
serialized, err := authlibInjectorSerializeKey(&key)
Check(err)
signaturePublicKeys = append(signaturePublicKeys, serialized)
}
responseBlob := Unwrap(json.Marshal(authlibInjectorResponse{
Meta: authlibInjectorMeta{
ImplementationName: "Drasl",
ImplementationVersion: Constants.Version,
Links: authlibInjectorLinks{
Homepage: app.FrontEndURL,
Register: Unwrap(url.JoinPath(app.FrontEndURL, "web/registration")),
},
ServerName: app.Config.InstanceName,
FeatureEnableProfileKey: true,
},
SignaturePublickey: signaturePublicKey,
SignaturePublickeys: signaturePublicKeys,
SkinDomains: skinDomains,
}))
return func(c echo.Context) error {
return c.JSONBlob(http.StatusOK, responseBlob)
}
}
func (app *App) AuthlibInjectorUploadTexture(textureType string) func(c echo.Context) error {
return withBearerAuthentication(app, func(c echo.Context, caller *User, _ *Player) error {
playerID := c.Param("id")
playerUUID, err := IDToUUID(playerID)
if err != nil {
return &YggdrasilError{Code: http.StatusBadRequest, ErrorMessage: mo.Some("Invalid UUID format")}
}
textureFile, err := c.FormFile("file")
if err != nil {
return &YggdrasilError{Code: http.StatusBadRequest, ErrorMessage: mo.Some("Missing texture file")}
}
textureHandle, err := textureFile.Open()
if err != nil {
return err
}
defer textureHandle.Close()
var textureReader io.Reader = textureHandle
var targetPlayer Player
result := app.DB.Preload("User").First(&targetPlayer, "uuid = ?", playerUUID)
if result.Error != nil {
return &YggdrasilError{Code: http.StatusBadRequest, ErrorMessage: mo.Some("Player not found")}
}
switch textureType {
case TextureTypeSkin:
var model string
switch m := c.FormValue("model"); m {
case "slim":
model = SkinModelSlim
case "":
model = SkinModelClassic
default:
message := fmt.Sprintf("Unknown model: %s", m)
return &YggdrasilError{Code: http.StatusBadRequest, ErrorMessage: mo.Some(message)}
}
_, err = app.UpdatePlayer(
caller,
targetPlayer,
nil, // playerName
nil, // fallbackPlayer
&model, // skinModel
&textureReader, // skinReader
nil, // skinURL
false, // deleteSkin
nil, // capeReader
nil, // capeURL
false, // deleteCape
)
if err != nil {
return err
}
case TextureTypeCape:
_, err = app.UpdatePlayer(
caller,
targetPlayer,
nil, // playerName
nil, // fallbackPlayer
nil, // skinModel
nil, // skinReader
nil, // skinURL
false, // deleteSkin
&textureReader, // capeReader
nil, // capeURL
false, // deleteCape
)
if err != nil {
return err
}
}
return c.NoContent(http.StatusNoContent)
})
}
func (app *App) AuthlibInjectorDeleteTexture(textureType string) func(c echo.Context) error {
return withBearerAuthentication(app, func(c echo.Context, caller *User, _ *Player) error {
playerID := c.Param("id")
playerUUID, err := IDToUUID(playerID)
if err != nil {
return &YggdrasilError{Code: http.StatusBadRequest, ErrorMessage: mo.Some("Invalid player UUID")}
}
var targetPlayer Player
result := app.DB.Preload("User").First(&targetPlayer, "uuid = ?", playerUUID)
if result.Error != nil {
return &YggdrasilError{Code: http.StatusNotFound, ErrorMessage: mo.Some("Player not found")}
}
_, err = app.UpdatePlayer(
caller,
targetPlayer,
nil, // playerName
nil, // fallbackPlayer
nil, // skinModel
nil, // skinReader
nil, // skinURL
textureType == TextureTypeSkin, // deleteSkin
nil, // capeReader
nil, // capeURL
textureType == TextureTypeCape, // deleteCape
)
if err != nil {
return err
}
return c.NoContent(http.StatusNoContent)
})
}