diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ff7207..4a48af9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - 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 - Bumped Go version to 1.21. diff --git a/README.md b/README.md index 8db7f9e..b7b7319 100644 --- a/README.md +++ b/README.md @@ -243,11 +243,12 @@ Response example: "properties": [ { "name": "textures", - "signature": "signature value", + "signature": "textures signature value", "value": "base64 encoded value" }, { "name": "chrly", + "signature": "custom property signature value", "value": "how do you tame a horse in Minecraft?" } ] diff --git a/di/handlers.go b/di/handlers.go index a0ed9dc..221ea7b 100644 --- a/di/handlers.go +++ b/di/handlers.go @@ -106,19 +106,24 @@ func newSkinsystemHandler( capesRepository CapesRepository, mojangTexturesProvider MojangTexturesProvider, texturesSigner TexturesSigner, -) *mux.Router { +) (*mux.Router, error) { config.SetDefault("textures.extra_param_name", "chrly") config.SetDefault("textures.extra_param_value", "how do you tame a horse in Minecraft?") - return (&Skinsystem{ - Emitter: emitter, - SkinsRepo: skinsRepository, - CapesRepo: capesRepository, - MojangTexturesProvider: mojangTexturesProvider, - TexturesSigner: texturesSigner, - TexturesExtraParamName: config.GetString("textures.extra_param_name"), - TexturesExtraParamValue: config.GetString("textures.extra_param_value"), - }).Handler() + app, err := NewSkinsystem( + emitter, + skinsRepository, + capesRepository, + mojangTexturesProvider, + texturesSigner, + config.GetString("textures.extra_param_name"), + config.GetString("textures.extra_param_value"), + ) + if err != nil { + return nil, err + } + + return app.Handler(), nil } func newApiHandler(skinsRepository SkinsRepository) *mux.Router { diff --git a/http/skinsystem.go b/http/skinsystem.go index f15a7e5..555af34 100644 --- a/http/skinsystem.go +++ b/http/skinsystem.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "encoding/json" "encoding/pem" + "fmt" "io" "net/http" "strings" @@ -43,12 +44,39 @@ type TexturesSigner interface { type Skinsystem struct { Emitter - SkinsRepo SkinsRepository - CapesRepo CapesRepository - MojangTexturesProvider MojangTexturesProvider - TexturesSigner TexturesSigner - TexturesExtraParamName string - TexturesExtraParamValue string + SkinsRepo SkinsRepository + CapesRepo CapesRepository + MojangTexturesProvider MojangTexturesProvider + TexturesSigner TexturesSigner + TexturesExtraParamName 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 { @@ -215,14 +243,20 @@ func (ctx *Skinsystem) profileHandler(response http.ResponseWriter, request *htt Name: "textures", Value: texturesPropEncodedValue, } + customProp := &mojang.Property{ + Name: ctx.TexturesExtraParamName, + Value: ctx.TexturesExtraParamValue, + } 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 { panic(err) } - texturesProp.Signature = signature + texturesProp.Signature = texturesSignature } profileResponse := &mojang.SignedTexturesResponse{ @@ -230,10 +264,7 @@ func (ctx *Skinsystem) profileHandler(response http.ResponseWriter, request *htt Name: profile.Username, Props: []*mojang.Property{ texturesProp, - { - Name: ctx.TexturesExtraParamName, - Value: ctx.TexturesExtraParamValue, - }, + customProp, }, } diff --git a/http/skinsystem_test.go b/http/skinsystem_test.go index a3e0de1..9e51f87 100644 --- a/http/skinsystem_test.go +++ b/http/skinsystem_test.go @@ -142,15 +142,17 @@ func (suite *skinsystemTestSuite) SetupTest() { suite.TexturesSigner = &texturesSignerMock{} suite.Emitter = &emitterMock{} - suite.App = &Skinsystem{ - SkinsRepo: suite.SkinsRepository, - CapesRepo: suite.CapesRepository, - MojangTexturesProvider: suite.MojangTexturesProvider, - TexturesSigner: suite.TexturesSigner, - Emitter: suite.Emitter, - TexturesExtraParamName: "texturesParamName", - TexturesExtraParamValue: "texturesParamValue", - } + suite.TexturesSigner.On("SignTextures", "texturesParamValue").Times(1).Return("texturesParamSignature", nil) + + suite.App, _ = NewSkinsystem( + suite.Emitter, + suite.SkinsRepository, + suite.CapesRepository, + suite.MojangTexturesProvider, + suite.TexturesSigner, + "texturesParamName", + "texturesParamValue", + ) } func (suite *skinsystemTestSuite) TearDownTest() { @@ -801,6 +803,7 @@ var profileTestsCases = []*profileTestCase{ }, { "name": "texturesParamName", + "signature": "texturesParamSignature", "value": "texturesParamValue" } ] @@ -830,6 +833,7 @@ var profileTestsCases = []*profileTestCase{ }, { "name": "texturesParamName", + "signature": "texturesParamSignature", "value": "texturesParamValue" } ] @@ -859,6 +863,7 @@ var profileTestsCases = []*profileTestCase{ }, { "name": "texturesParamName", + "signature": "texturesParamSignature", "value": "texturesParamValue" } ] @@ -892,6 +897,7 @@ var profileTestsCases = []*profileTestCase{ }, { "name": "texturesParamName", + "signature": "texturesParamSignature", "value": "texturesParamValue" } ] @@ -925,6 +931,7 @@ var profileTestsCases = []*profileTestCase{ }, { "name": "texturesParamName", + "signature": "texturesParamSignature", "value": "texturesParamValue" } ] @@ -954,6 +961,7 @@ var profileTestsCases = []*profileTestCase{ }, { "name": "texturesParamName", + "signature": "texturesParamSignature", "value": "texturesParamValue" } ] @@ -983,6 +991,7 @@ var profileTestsCases = []*profileTestCase{ }, { "name": "texturesParamName", + "signature": "texturesParamSignature", "value": "texturesParamValue" } ] @@ -1012,6 +1021,7 @@ var profileTestsCases = []*profileTestCase{ }, { "name": "texturesParamName", + "signature": "texturesParamSignature", "value": "texturesParamValue" } ] @@ -1054,6 +1064,7 @@ var profileTestsCases = []*profileTestCase{ }, { "name": "texturesParamName", + "signature": "texturesParamSignature", "value": "texturesParamValue" } ]