Add signature to the custom profile property when ?unsigned=false

This commit is contained in:
ErickSkrauch 2024-07-09 18:37:47 +02:00
parent 27c7b79b32
commit cfe8fea3f7
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
5 changed files with 81 additions and 32 deletions

View File

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Adjusted Mojang usernames filter to be stickier according to their docs - Adjusted Mojang usernames filter to be stickier according to their docs
- `/profile/{username}` endpoint now returns the correct signature for the custom property as well.
### Changed ### Changed
- Bumped Go version to 1.21. - Bumped Go version to 1.21.

View File

@ -243,11 +243,12 @@ Response example:
"properties": [ "properties": [
{ {
"name": "textures", "name": "textures",
"signature": "signature value", "signature": "textures signature value",
"value": "base64 encoded value" "value": "base64 encoded value"
}, },
{ {
"name": "chrly", "name": "chrly",
"signature": "custom property signature value",
"value": "how do you tame a horse in Minecraft?" "value": "how do you tame a horse in Minecraft?"
} }
] ]

View File

@ -106,19 +106,24 @@ func newSkinsystemHandler(
capesRepository CapesRepository, capesRepository CapesRepository,
mojangTexturesProvider MojangTexturesProvider, mojangTexturesProvider MojangTexturesProvider,
texturesSigner TexturesSigner, texturesSigner TexturesSigner,
) *mux.Router { ) (*mux.Router, error) {
config.SetDefault("textures.extra_param_name", "chrly") config.SetDefault("textures.extra_param_name", "chrly")
config.SetDefault("textures.extra_param_value", "how do you tame a horse in Minecraft?") config.SetDefault("textures.extra_param_value", "how do you tame a horse in Minecraft?")
return (&Skinsystem{ app, err := NewSkinsystem(
Emitter: emitter, emitter,
SkinsRepo: skinsRepository, skinsRepository,
CapesRepo: capesRepository, capesRepository,
MojangTexturesProvider: mojangTexturesProvider, mojangTexturesProvider,
TexturesSigner: texturesSigner, texturesSigner,
TexturesExtraParamName: config.GetString("textures.extra_param_name"), config.GetString("textures.extra_param_name"),
TexturesExtraParamValue: config.GetString("textures.extra_param_value"), config.GetString("textures.extra_param_value"),
}).Handler() )
if err != nil {
return nil, err
}
return app.Handler(), nil
} }
func newApiHandler(skinsRepository SkinsRepository) *mux.Router { func newApiHandler(skinsRepository SkinsRepository) *mux.Router {

View File

@ -6,6 +6,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"encoding/pem" "encoding/pem"
"fmt"
"io" "io"
"net/http" "net/http"
"strings" "strings"
@ -43,12 +44,39 @@ type TexturesSigner interface {
type Skinsystem struct { type Skinsystem struct {
Emitter Emitter
SkinsRepo SkinsRepository SkinsRepo SkinsRepository
CapesRepo CapesRepository CapesRepo CapesRepository
MojangTexturesProvider MojangTexturesProvider MojangTexturesProvider MojangTexturesProvider
TexturesSigner TexturesSigner TexturesSigner TexturesSigner
TexturesExtraParamName string TexturesExtraParamName string
TexturesExtraParamValue string TexturesExtraParamValue string
texturesExtraParamSignature string
}
func NewSkinsystem(
emitter Emitter,
skinsRepo SkinsRepository,
capesRepo CapesRepository,
mojangTexturesProvider MojangTexturesProvider,
texturesSigner TexturesSigner,
texturesExtraParamName string,
texturesExtraParamValue string,
) (*Skinsystem, error) {
texturesExtraParamSignature, err := texturesSigner.SignTextures(texturesExtraParamValue)
if err != nil {
return nil, fmt.Errorf("unable to generate signature for textures extra param: %w", err)
}
return &Skinsystem{
Emitter: emitter,
SkinsRepo: skinsRepo,
CapesRepo: capesRepo,
MojangTexturesProvider: mojangTexturesProvider,
TexturesSigner: texturesSigner,
TexturesExtraParamName: texturesExtraParamName,
TexturesExtraParamValue: texturesExtraParamValue,
texturesExtraParamSignature: texturesExtraParamSignature,
}, nil
} }
type profile struct { type profile struct {
@ -215,14 +243,20 @@ func (ctx *Skinsystem) profileHandler(response http.ResponseWriter, request *htt
Name: "textures", Name: "textures",
Value: texturesPropEncodedValue, Value: texturesPropEncodedValue,
} }
customProp := &mojang.Property{
Name: ctx.TexturesExtraParamName,
Value: ctx.TexturesExtraParamValue,
}
if request.URL.Query().Get("unsigned") == "false" { if request.URL.Query().Get("unsigned") == "false" {
signature, err := ctx.TexturesSigner.SignTextures(texturesProp.Value) customProp.Signature = ctx.texturesExtraParamSignature
texturesSignature, err := ctx.TexturesSigner.SignTextures(texturesProp.Value)
if err != nil { if err != nil {
panic(err) panic(err)
} }
texturesProp.Signature = signature texturesProp.Signature = texturesSignature
} }
profileResponse := &mojang.SignedTexturesResponse{ profileResponse := &mojang.SignedTexturesResponse{
@ -230,10 +264,7 @@ func (ctx *Skinsystem) profileHandler(response http.ResponseWriter, request *htt
Name: profile.Username, Name: profile.Username,
Props: []*mojang.Property{ Props: []*mojang.Property{
texturesProp, texturesProp,
{ customProp,
Name: ctx.TexturesExtraParamName,
Value: ctx.TexturesExtraParamValue,
},
}, },
} }

View File

@ -142,15 +142,17 @@ func (suite *skinsystemTestSuite) SetupTest() {
suite.TexturesSigner = &texturesSignerMock{} suite.TexturesSigner = &texturesSignerMock{}
suite.Emitter = &emitterMock{} suite.Emitter = &emitterMock{}
suite.App = &Skinsystem{ suite.TexturesSigner.On("SignTextures", "texturesParamValue").Times(1).Return("texturesParamSignature", nil)
SkinsRepo: suite.SkinsRepository,
CapesRepo: suite.CapesRepository, suite.App, _ = NewSkinsystem(
MojangTexturesProvider: suite.MojangTexturesProvider, suite.Emitter,
TexturesSigner: suite.TexturesSigner, suite.SkinsRepository,
Emitter: suite.Emitter, suite.CapesRepository,
TexturesExtraParamName: "texturesParamName", suite.MojangTexturesProvider,
TexturesExtraParamValue: "texturesParamValue", suite.TexturesSigner,
} "texturesParamName",
"texturesParamValue",
)
} }
func (suite *skinsystemTestSuite) TearDownTest() { func (suite *skinsystemTestSuite) TearDownTest() {
@ -801,6 +803,7 @@ var profileTestsCases = []*profileTestCase{
}, },
{ {
"name": "texturesParamName", "name": "texturesParamName",
"signature": "texturesParamSignature",
"value": "texturesParamValue" "value": "texturesParamValue"
} }
] ]
@ -830,6 +833,7 @@ var profileTestsCases = []*profileTestCase{
}, },
{ {
"name": "texturesParamName", "name": "texturesParamName",
"signature": "texturesParamSignature",
"value": "texturesParamValue" "value": "texturesParamValue"
} }
] ]
@ -859,6 +863,7 @@ var profileTestsCases = []*profileTestCase{
}, },
{ {
"name": "texturesParamName", "name": "texturesParamName",
"signature": "texturesParamSignature",
"value": "texturesParamValue" "value": "texturesParamValue"
} }
] ]
@ -892,6 +897,7 @@ var profileTestsCases = []*profileTestCase{
}, },
{ {
"name": "texturesParamName", "name": "texturesParamName",
"signature": "texturesParamSignature",
"value": "texturesParamValue" "value": "texturesParamValue"
} }
] ]
@ -925,6 +931,7 @@ var profileTestsCases = []*profileTestCase{
}, },
{ {
"name": "texturesParamName", "name": "texturesParamName",
"signature": "texturesParamSignature",
"value": "texturesParamValue" "value": "texturesParamValue"
} }
] ]
@ -954,6 +961,7 @@ var profileTestsCases = []*profileTestCase{
}, },
{ {
"name": "texturesParamName", "name": "texturesParamName",
"signature": "texturesParamSignature",
"value": "texturesParamValue" "value": "texturesParamValue"
} }
] ]
@ -983,6 +991,7 @@ var profileTestsCases = []*profileTestCase{
}, },
{ {
"name": "texturesParamName", "name": "texturesParamName",
"signature": "texturesParamSignature",
"value": "texturesParamValue" "value": "texturesParamValue"
} }
] ]
@ -1012,6 +1021,7 @@ var profileTestsCases = []*profileTestCase{
}, },
{ {
"name": "texturesParamName", "name": "texturesParamName",
"signature": "texturesParamSignature",
"value": "texturesParamValue" "value": "texturesParamValue"
} }
] ]
@ -1054,6 +1064,7 @@ var profileTestsCases = []*profileTestCase{
}, },
{ {
"name": "texturesParamName", "name": "texturesParamName",
"signature": "texturesParamSignature",
"value": "texturesParamValue" "value": "texturesParamValue"
} }
] ]