diff --git a/internal/handler/routes.go b/internal/handler/routes.go index 24427f0..18fc977 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -186,6 +186,8 @@ func registerYggdrasilRoutesWithDI(v1 *gin.RouterGroup, h *Handlers) { mcServices.POST("/player/attributes", h.Friends.UpdateAttributes) mcServices.POST("/presence", h.Friends.UpdatePresence) mcServices.GET("/privacy/blocklist", h.Friends.GetBlocklist) + // MC 1.21.7 /whitelist add 查询单用户名 UUID(authlib-injector issue #277) + mcServices.GET("/minecraft/profile/lookup/name/:name", h.Yggdrasil.LookupProfileByName) } authserver := ygg.Group("/authserver") diff --git a/internal/handler/yggdrasil_handler.go b/internal/handler/yggdrasil_handler.go index 4376fc9..b613927 100644 --- a/internal/handler/yggdrasil_handler.go +++ b/internal/handler/yggdrasil_handler.go @@ -682,6 +682,41 @@ func (h *YggdrasilHandler) GetProfileByName(c *gin.Context) { c.JSON(http.StatusOK, result) } +// LookupProfileByName 单用户名查 UUID(authlib-injector issue #277 / MC 1.21.7) +// @Summary MinecraftServices 按用户名查 UUID +// @Description 对应官方 api.minecraftservices.com/minecraft/profile/lookup/name/{name}, +// @Description 1.21.7 的 /whitelist add 等命令会请求该接口。authlib-injector 把 +// @Description api.minecraftservices.com override 到 {apiRoot}/minecraftservices。 +// @Description 返回 {id,name},未找到返回 404。 +// @Tags Yggdrasil +// @Produce json +// @Param name path string true "用户名" +// @Success 200 {object} model.NameAndId "档案标识" +// @Failure 404 {object} map[string]string "档案不存在" +// @Router /api/yggdrasil/minecraftservices/minecraft/profile/lookup/name/{name} [get] +func (h *YggdrasilHandler) LookupProfileByName(c *gin.Context) { + name := c.Param("name") + if name == "" { + c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"}) + return + } + + result, err := h.container.ProfileService.ProfileSearchByNameSingle(c.Request.Context(), name) + if err != nil { + h.logger.Warn("按用户名查档失败", zap.String("name", name), zap.Error(err)) + c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"}) + return + } + if result == nil { + h.logger.Info("用户名未找到", zap.String("name", name)) + c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"}) + return + } + + h.logger.Info("成功获取档案", zap.String("name", name), zap.String("uuid", result.ID)) + c.JSON(http.StatusOK, result) +} + // GetMetaData 获取Yggdrasil元数据 // @Summary Yggdrasil元数据 // @Description Yggdrasil协议: 获取服务器元数据