-
Notifications
You must be signed in to change notification settings - Fork 18
/
authlib_injector.go
85 lines (75 loc) · 2.55 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
package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"github.com/labstack/echo/v4"
"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.Key.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)
}
}