feat: support additional /publickeys endpoint
This commit is contained in:
		@@ -1,5 +1,5 @@
 | 
				
			|||||||
/*
 | 
					/*
 | 
				
			||||||
 * Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
 | 
					 * Copyright (C) 2022-2023. Gardel <sunxinao@hotmail.com> and contributors
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * This program is free software: you can redistribute it and/or modify
 | 
					 * This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
 * it under the terms of the GNU Affero General Public License as published by
 | 
					 * it under the terms of the GNU Affero General Public License as published by
 | 
				
			||||||
@@ -18,8 +18,11 @@
 | 
				
			|||||||
package router
 | 
					package router
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"encoding/base64"
 | 
				
			||||||
 | 
						"encoding/pem"
 | 
				
			||||||
	"github.com/gin-gonic/gin"
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"yggdrasil-go/util"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type MetaInfo struct {
 | 
					type MetaInfo struct {
 | 
				
			||||||
@@ -42,17 +45,31 @@ type ServerMeta struct {
 | 
				
			|||||||
	SignaturePublickey string   `json:"signaturePublickey"`
 | 
						SignaturePublickey string   `json:"signaturePublickey"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type KeyPair struct {
 | 
				
			||||||
 | 
						PrivateKey string `json:"privateKey,omitempty"`
 | 
				
			||||||
 | 
						PublicKey  string `json:"publicKey,omitempty"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type PublicKeys struct {
 | 
				
			||||||
 | 
						ProfilePropertyKeys   []KeyPair `json:"profilePropertyKeys,omitempty"`
 | 
				
			||||||
 | 
						PlayerCertificateKeys []KeyPair `json:"playerCertificateKeys,omitempty"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type HomeRouter interface {
 | 
					type HomeRouter interface {
 | 
				
			||||||
	Home(c *gin.Context)
 | 
						Home(c *gin.Context)
 | 
				
			||||||
 | 
						PublicKeys(c *gin.Context)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type homeRouterImpl struct {
 | 
					type homeRouterImpl struct {
 | 
				
			||||||
	serverMeta ServerMeta
 | 
						serverMeta ServerMeta
 | 
				
			||||||
 | 
						myPubKey   KeyPair
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewHomeRouter(meta *ServerMeta) HomeRouter {
 | 
					func NewHomeRouter(meta *ServerMeta) HomeRouter {
 | 
				
			||||||
 | 
						signaturePubKey, _ := pem.Decode([]byte(meta.SignaturePublickey))
 | 
				
			||||||
	homeRouter := homeRouterImpl{
 | 
						homeRouter := homeRouterImpl{
 | 
				
			||||||
		serverMeta: *meta,
 | 
							serverMeta: *meta,
 | 
				
			||||||
 | 
							myPubKey:   KeyPair{PublicKey: base64.StdEncoding.EncodeToString(signaturePubKey.Bytes)},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return &homeRouter
 | 
						return &homeRouter
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -61,3 +78,15 @@ func NewHomeRouter(meta *ServerMeta) HomeRouter {
 | 
				
			|||||||
func (h *homeRouterImpl) Home(c *gin.Context) {
 | 
					func (h *homeRouterImpl) Home(c *gin.Context) {
 | 
				
			||||||
	c.JSON(http.StatusOK, h.serverMeta)
 | 
						c.JSON(http.StatusOK, h.serverMeta)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (h *homeRouterImpl) PublicKeys(c *gin.Context) {
 | 
				
			||||||
 | 
						publicKeys := PublicKeys{}
 | 
				
			||||||
 | 
						err := util.GetObject("https://api.minecraftservices.com/publickeys", &publicKeys)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							util.HandleError(c, err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						publicKeys.ProfilePropertyKeys = append(publicKeys.ProfilePropertyKeys, h.myPubKey)
 | 
				
			||||||
 | 
						publicKeys.PlayerCertificateKeys = append(publicKeys.PlayerCertificateKeys, h.myPubKey)
 | 
				
			||||||
 | 
						c.JSON(http.StatusOK, publicKeys)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,5 @@
 | 
				
			|||||||
/*
 | 
					/*
 | 
				
			||||||
 * Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
 | 
					 * Copyright (C) 2022-2023. Gardel <sunxinao@hotmail.com> and contributors
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * This program is free software: you can redistribute it and/or modify
 | 
					 * This program is free software: you can redistribute it and/or modify
 | 
				
			||||||
 * it under the terms of the GNU Affero General Public License as published by
 | 
					 * it under the terms of the GNU Affero General Public License as published by
 | 
				
			||||||
@@ -72,5 +72,9 @@ func InitRouters(router *gin.Engine, db *gorm.DB, meta *ServerMeta, skinRootUrl
 | 
				
			|||||||
		api.DELETE("/user/profile/:uuid/:textureType", textureRouter.DeleteTexture)
 | 
							api.DELETE("/user/profile/:uuid/:textureType", textureRouter.DeleteTexture)
 | 
				
			||||||
		api.GET("/users/profiles/minecraft/:username", userRouter.UsernameToUUID)
 | 
							api.GET("/users/profiles/minecraft/:username", userRouter.UsernameToUUID)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	router.POST("/minecraftservices/player/certificates", userRouter.ProfileKey)
 | 
						minecraftservices := router.Group("/minecraftservices")
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							minecraftservices.POST("/player/certificates", userRouter.ProfileKey)
 | 
				
			||||||
 | 
							minecraftservices.GET("/publickeys", homeRouter.PublicKeys)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user