查询用户属性

This commit is contained in:
xmdhs 2023-09-10 20:46:11 +08:00
parent 074b84bf18
commit c6ef8a8706
No known key found for this signature in database
GPG Key ID: E809D6D43DEFCC95
6 changed files with 68 additions and 41 deletions

View File

@ -6,6 +6,7 @@ import (
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/samber/lo"
"github.com/xmdhs/authlib-skin/model/yggdrasil"
sutils "github.com/xmdhs/authlib-skin/service/utils"
yggdrasilS "github.com/xmdhs/authlib-skin/service/yggdrasil"
@ -154,3 +155,20 @@ func (y *Yggdrasil) GetProfile() httprouter.Handle {
w.Write(b)
}
}
func (y *Yggdrasil) BatchProfile() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
ctx := r.Context()
a, has := getAnyModel[[]string](ctx, w, r.Body, y.validate, y.logger)
if !has {
return
}
ul, err := y.yggdrasilService.BatchProfile(ctx, a)
if err != nil {
y.logger.WarnContext(ctx, err.Error())
handleYgError(ctx, w, yggdrasil.Error{ErrorMessage: err.Error()}, 500)
return
}
w.Write(lo.Must1(json.Marshal(ul)))
}
}

View File

@ -22,23 +22,16 @@ type Error struct {
ErrorMessage string `json:"errorMessage,omitempty"`
}
type TokenUserID struct {
ID string `json:"id"`
}
type Token struct {
AccessToken string `json:"accessToken"`
AvailableProfiles []TokenProfile `json:"availableProfiles,omitempty"`
ClientToken string `json:"clientToken"`
SelectedProfile TokenProfile `json:"selectedProfile"`
User TokenUser `json:"user,omitempty"`
}
type TokenProfile struct {
// 就是 uuid
ID string `json:"id"`
Name string `json:"name"`
}
type TokenUser struct {
ID string `json:"id"`
Properties []any `json:"properties"`
AccessToken string `json:"accessToken"`
AvailableProfiles []UserInfo `json:"availableProfiles,omitempty"`
ClientToken string `json:"clientToken"`
SelectedProfile UserInfo `json:"selectedProfile"`
User TokenUserID `json:"user,omitempty"`
}
type ValidateToken struct {
@ -49,8 +42,8 @@ type ValidateToken struct {
type RefreshToken struct {
ValidateToken
RequestUser bool `json:"requestUser"`
SelectedProfile TokenProfile `json:"selectedProfile"`
RequestUser bool `json:"requestUser"`
SelectedProfile UserInfo `json:"selectedProfile"`
}
type UserInfo struct {

View File

@ -32,7 +32,8 @@ func newYggdrasil(r *httprouter.Router, handelY yggdrasil.Yggdrasil) error {
r.PUT("/api/yggdrasil/api/user/profile/:uuid/:textureType", handelY.PutTexture())
r.DELETE("/api/yggdrasil/api/user/profile/:uuid/:textureType", warpHJSON(handelY.DelTexture()))
r.GET("/api/yggdrasil/sessionserver/session/minecraft/profile/:uuid", handelY.GetProfile())
r.GET("/api/yggdrasil/sessionserver/session/minecraft/profile/:uuid", warpHJSON(handelY.GetProfile()))
r.POST("/api/profiles/minecraft", warpHJSON(handelY.BatchProfile()))
r.GET("/api/yggdrasil", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Write([]byte(`{

View File

@ -2,8 +2,6 @@ package service
import (
"context"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"strings"
@ -26,7 +24,7 @@ var (
func (w *WebService) Reg(ctx context.Context, u model.User, ip string) error {
var userUuid string
if w.config.OfflineUUID {
userUuid = uuidGen(u.Name)
userUuid = utils.UUIDGen(u.Name)
} else {
userUuid = strings.ReplaceAll(uuid.New().String(), "-", "")
}
@ -82,13 +80,3 @@ func (w *WebService) Reg(ctx context.Context, u model.User, ip string) error {
}
return nil
}
func uuidGen(t string) string {
data := []byte("OfflinePlayer:" + t)
h := md5.New()
h.Write(data)
uuid := h.Sum(nil)
uuid[6] = (uuid[6] & 0x0f) | uint8((3&0xf)<<4)
uuid[8] = (uuid[8] & 0x3f) | 0x80
return hex.EncodeToString(uuid)
}

View File

@ -85,18 +85,17 @@ func (y *Yggdrasil) Authenticate(cxt context.Context, auth yggdrasil.Authenticat
return yggdrasil.Token{}, fmt.Errorf("Authenticate: %w", err)
}
p := yggdrasil.TokenProfile{
p := yggdrasil.UserInfo{
ID: u.Edges.Profile.UUID,
Name: u.Edges.Profile.Name,
}
return yggdrasil.Token{
AccessToken: jwts,
AvailableProfiles: []yggdrasil.TokenProfile{p},
AvailableProfiles: []yggdrasil.UserInfo{p},
ClientToken: clientToken,
SelectedProfile: p,
User: yggdrasil.TokenUser{
ID: u.Edges.Profile.UUID,
Properties: []any{},
User: yggdrasil.TokenUserID{
ID: utils.UUIDGen(strconv.Itoa(u.ID)),
},
}, nil
}
@ -159,13 +158,12 @@ func (y *Yggdrasil) Refresh(ctx context.Context, token yggdrasil.RefreshToken) (
return yggdrasil.Token{
AccessToken: jwts,
ClientToken: t.CID,
SelectedProfile: yggdrasil.TokenProfile{
SelectedProfile: yggdrasil.UserInfo{
ID: up.UUID,
Name: up.Name,
},
User: yggdrasil.TokenUser{
ID: t.Subject,
Properties: []any{},
User: yggdrasil.TokenUserID{
ID: utils.UUIDGen(strconv.Itoa(t.UID)),
},
}, nil
}
@ -238,3 +236,16 @@ func (y *Yggdrasil) GetProfile(ctx context.Context, uuid string, unsigned bool,
return uinfo, nil
}
func (y *Yggdrasil) BatchProfile(ctx context.Context, names []string) ([]yggdrasil.UserInfo, error) {
pl, err := y.client.UserProfile.Query().Where(userprofile.NameIn(names...)).All(ctx)
if err != nil {
return nil, fmt.Errorf("GetProfile: %w", err)
}
return lo.Map[*ent.UserProfile, yggdrasil.UserInfo](pl, func(item *ent.UserProfile, index int) yggdrasil.UserInfo {
return yggdrasil.UserInfo{
ID: item.UUID,
Name: item.Name,
}
}), nil
}

16
utils/uuid.go Normal file
View File

@ -0,0 +1,16 @@
package utils
import (
"crypto/md5"
"encoding/hex"
)
func UUIDGen(t string) string {
data := []byte("OfflinePlayer:" + t)
h := md5.New()
h.Write(data)
uuid := h.Sum(nil)
uuid[6] = (uuid[6] & 0x0f) | uint8((3&0xf)<<4)
uuid[8] = (uuid[8] & 0x3f) | 0x80
return hex.EncodeToString(uuid)
}