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
- 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.

View File

@ -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?"
}
]

View File

@ -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 {

View File

@ -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,
},
}

View File

@ -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"
}
]