查询用户属性
This commit is contained in:
parent
074b84bf18
commit
c6ef8a8706
@ -6,6 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
"github.com/samber/lo"
|
||||||
"github.com/xmdhs/authlib-skin/model/yggdrasil"
|
"github.com/xmdhs/authlib-skin/model/yggdrasil"
|
||||||
sutils "github.com/xmdhs/authlib-skin/service/utils"
|
sutils "github.com/xmdhs/authlib-skin/service/utils"
|
||||||
yggdrasilS "github.com/xmdhs/authlib-skin/service/yggdrasil"
|
yggdrasilS "github.com/xmdhs/authlib-skin/service/yggdrasil"
|
||||||
@ -154,3 +155,20 @@ func (y *Yggdrasil) GetProfile() httprouter.Handle {
|
|||||||
w.Write(b)
|
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)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -22,23 +22,16 @@ type Error struct {
|
|||||||
ErrorMessage string `json:"errorMessage,omitempty"`
|
ErrorMessage string `json:"errorMessage,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TokenUserID struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
type Token struct {
|
type Token struct {
|
||||||
AccessToken string `json:"accessToken"`
|
AccessToken string `json:"accessToken"`
|
||||||
AvailableProfiles []TokenProfile `json:"availableProfiles,omitempty"`
|
AvailableProfiles []UserInfo `json:"availableProfiles,omitempty"`
|
||||||
ClientToken string `json:"clientToken"`
|
ClientToken string `json:"clientToken"`
|
||||||
SelectedProfile TokenProfile `json:"selectedProfile"`
|
SelectedProfile UserInfo `json:"selectedProfile"`
|
||||||
User TokenUser `json:"user,omitempty"`
|
User TokenUserID `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"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ValidateToken struct {
|
type ValidateToken struct {
|
||||||
@ -50,7 +43,7 @@ type ValidateToken struct {
|
|||||||
type RefreshToken struct {
|
type RefreshToken struct {
|
||||||
ValidateToken
|
ValidateToken
|
||||||
RequestUser bool `json:"requestUser"`
|
RequestUser bool `json:"requestUser"`
|
||||||
SelectedProfile TokenProfile `json:"selectedProfile"`
|
SelectedProfile UserInfo `json:"selectedProfile"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserInfo struct {
|
type UserInfo struct {
|
||||||
|
@ -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.PUT("/api/yggdrasil/api/user/profile/:uuid/:textureType", handelY.PutTexture())
|
||||||
r.DELETE("/api/yggdrasil/api/user/profile/:uuid/:textureType", warpHJSON(handelY.DelTexture()))
|
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) {
|
r.GET("/api/yggdrasil", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||||
w.Write([]byte(`{
|
w.Write([]byte(`{
|
||||||
|
@ -2,8 +2,6 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/md5"
|
|
||||||
"encoding/hex"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
@ -26,7 +24,7 @@ var (
|
|||||||
func (w *WebService) Reg(ctx context.Context, u model.User, ip string) error {
|
func (w *WebService) Reg(ctx context.Context, u model.User, ip string) error {
|
||||||
var userUuid string
|
var userUuid string
|
||||||
if w.config.OfflineUUID {
|
if w.config.OfflineUUID {
|
||||||
userUuid = uuidGen(u.Name)
|
userUuid = utils.UUIDGen(u.Name)
|
||||||
} else {
|
} else {
|
||||||
userUuid = strings.ReplaceAll(uuid.New().String(), "-", "")
|
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
|
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)
|
|
||||||
}
|
|
||||||
|
@ -85,18 +85,17 @@ func (y *Yggdrasil) Authenticate(cxt context.Context, auth yggdrasil.Authenticat
|
|||||||
return yggdrasil.Token{}, fmt.Errorf("Authenticate: %w", err)
|
return yggdrasil.Token{}, fmt.Errorf("Authenticate: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
p := yggdrasil.TokenProfile{
|
p := yggdrasil.UserInfo{
|
||||||
ID: u.Edges.Profile.UUID,
|
ID: u.Edges.Profile.UUID,
|
||||||
Name: u.Edges.Profile.Name,
|
Name: u.Edges.Profile.Name,
|
||||||
}
|
}
|
||||||
return yggdrasil.Token{
|
return yggdrasil.Token{
|
||||||
AccessToken: jwts,
|
AccessToken: jwts,
|
||||||
AvailableProfiles: []yggdrasil.TokenProfile{p},
|
AvailableProfiles: []yggdrasil.UserInfo{p},
|
||||||
ClientToken: clientToken,
|
ClientToken: clientToken,
|
||||||
SelectedProfile: p,
|
SelectedProfile: p,
|
||||||
User: yggdrasil.TokenUser{
|
User: yggdrasil.TokenUserID{
|
||||||
ID: u.Edges.Profile.UUID,
|
ID: utils.UUIDGen(strconv.Itoa(u.ID)),
|
||||||
Properties: []any{},
|
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@ -159,13 +158,12 @@ func (y *Yggdrasil) Refresh(ctx context.Context, token yggdrasil.RefreshToken) (
|
|||||||
return yggdrasil.Token{
|
return yggdrasil.Token{
|
||||||
AccessToken: jwts,
|
AccessToken: jwts,
|
||||||
ClientToken: t.CID,
|
ClientToken: t.CID,
|
||||||
SelectedProfile: yggdrasil.TokenProfile{
|
SelectedProfile: yggdrasil.UserInfo{
|
||||||
ID: up.UUID,
|
ID: up.UUID,
|
||||||
Name: up.Name,
|
Name: up.Name,
|
||||||
},
|
},
|
||||||
User: yggdrasil.TokenUser{
|
User: yggdrasil.TokenUserID{
|
||||||
ID: t.Subject,
|
ID: utils.UUIDGen(strconv.Itoa(t.UID)),
|
||||||
Properties: []any{},
|
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@ -238,3 +236,16 @@ func (y *Yggdrasil) GetProfile(ctx context.Context, uuid string, unsigned bool,
|
|||||||
|
|
||||||
return uinfo, nil
|
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
16
utils/uuid.go
Normal 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)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user