feat: add yggdrasil metadata endpoint and support authlib-injector

This commit is contained in:
2026-01-29 17:08:52 +03:00
parent a1e022e966
commit f1d763e056
4 changed files with 88 additions and 2 deletions

View File

@@ -12,7 +12,9 @@ import (
)
type AuthHandler struct {
Service *core.AuthService
Service *core.AuthService
ProfileService *core.ProfileService
Domain string
}
// YggdrasilError - стандартный формат ошибки для authserver
@@ -126,3 +128,27 @@ func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}
func (h *AuthHandler) GetMetadata(w http.ResponseWriter, r *http.Request) {
pubKey, err := h.ProfileService.GetPublicKey()
if err != nil {
slog.Error("Failed to get public key", "error", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
metadata := models.YggdrasilMetadata{
Meta: models.MetaLinks{
Homepage: "https://" + h.Domain,
Register: "https://" + h.Domain + "/register",
},
SkinDomains: models.MetaSkinDomains{
Deny: []string{},
},
Signature: pubKey,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(metadata)
}

View File

@@ -158,3 +158,19 @@ func (s *ProfileService) UpdateUserSkin(ctx context.Context, userID int, file mu
return s.UserRepo.UpdateSkinHash(ctx, userID, hash)
}
// GetPublicKey возвращает публичный ключ в формате PEM
func (s *ProfileService) GetPublicKey() (string, error) {
pubKey := &s.privateKey.PublicKey
pubASN1, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
return "", err
}
pubBytes := pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: pubASN1,
})
return string(pubBytes), nil
}

View File

@@ -86,3 +86,22 @@ type LoginResponse struct {
Token string `json:"token"`
User *User `json:"user"`
}
// MetaLinks - ссылки в метаданных
type MetaLinks struct {
Homepage string `json:"homepage,omitempty"`
Register string `json:"register,omitempty"`
}
// MetaSkinDomains - домены скинов
type MetaSkinDomains struct {
Deny []string `json:"deny,omitempty"`
}
// YggdrasilMetadata - структура для ответа на запрос метаданных (/)
// См. https://github.com/yushijinhun/authlib-injector/wiki/Yggdrasil-API-Implementation-Spec
type YggdrasilMetadata struct {
Meta MetaLinks `json:"meta,omitempty"`
SkinDomains MetaSkinDomains `json:"skinDomains"`
Signature string `json:"signature"` // Public key in PEM format
}