Skip to content

Commit ebe85fb

Browse files
feat: sign custom Ely property (#2)
* feat: sign custom Ely property Signed-off-by: Octol1ttle <[email protected]> * fix: avoid duplicating custom property value across code Signed-off-by: Octol1ttle <[email protected]> * Remove customPropertySignature cache (and fix race condition) --------- Signed-off-by: Octol1ttle <[email protected]> Co-authored-by: ErickSkrauch <[email protected]>
1 parent ffd55e6 commit ebe85fb

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

internal/http/mojang_api.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
var timeNow = time.Now
1616

1717
var emptyTextures = []byte("{}")
18+
var customPropertyValue = []byte("but why are you asking?")
1819

1920
type AccountsRepository interface {
2021
FindUsernameByUuid(ctx context.Context, uuid string) (string, error)
@@ -83,7 +84,7 @@ func (s *MojangApi) getProfileByUuidHandler(c *gin.Context) {
8384
textures = emptyTextures
8485
}
8586

86-
serializedProfile, err := s.createProfileResponse(uuid, username, textures, c.Query("unsigned") == "false")
87+
serializedProfile, err := s.createProfileResponse(c.Request.Context(), uuid, username, textures, c.Query("unsigned") == "false")
8788
if err != nil {
8889
c.Error(fmt.Errorf("unable to create a profile response: %w", err))
8990
return
@@ -111,6 +112,7 @@ func (s *MojangApi) getUuidByUsernameHandler(c *gin.Context) {
111112
}
112113

113114
func (s *MojangApi) createProfileResponse(
115+
ctx context.Context,
114116
uuid string,
115117
username string,
116118
texturesJson []byte,
@@ -139,22 +141,45 @@ func (s *MojangApi) createProfileResponse(
139141
)
140142

141143
if sign {
142-
signature, err := s.SignerService.Sign(context.Background(), encodedTexturesBuf)
144+
textureSignature, err := s.signAndEncodeBase64(ctx, encodedTexturesBuf)
143145
if err != nil {
144146
return nil, fmt.Errorf("unable to sign textures: %w", err)
145147
}
146148

147-
encodedSignatureBuf := make([]byte, base64.StdEncoding.EncodedLen(len(signature)))
148-
base64.StdEncoding.Encode(encodedSignatureBuf, signature)
149+
result = fmt.Appendf(result, `,"signature":%q`, textureSignature)
150+
}
151+
152+
result = fmt.Appendf(result, `},{"name":"ely","value":%q`, customPropertyValue)
153+
154+
if sign {
155+
// Despite the fact that the signed value itself is always the same,
156+
// the signature service may rotate keys and at some point the signature will change.
157+
// It is better to avoid the cache for now and add it on the signature service side later.
158+
customPropertySignature, err := s.signAndEncodeBase64(ctx, customPropertyValue)
159+
if err != nil {
160+
return nil, fmt.Errorf("unable to sign custom property: %w", err)
161+
}
149162

150-
result = fmt.Appendf(result, `,"signature":%q`, encodedSignatureBuf)
163+
result = fmt.Appendf(result, `,"signature":%q`, customPropertySignature)
151164
}
152165

153-
result = fmt.Appendf(result, `},{"name":"ely","value":"but why are you asking?"}]}`)
166+
result = fmt.Appendf(result, `}]}`)
154167

155168
return result, nil
156169
}
157170

171+
func (s *MojangApi) signAndEncodeBase64(ctx context.Context, data []byte) ([]byte, error) {
172+
signature, err := s.SignerService.Sign(ctx, data)
173+
if err != nil {
174+
return nil, err
175+
}
176+
177+
encodedSignatureBuf := make([]byte, base64.StdEncoding.EncodedLen(len(signature)))
178+
base64.StdEncoding.Encode(encodedSignatureBuf, signature)
179+
180+
return encodedSignatureBuf, nil
181+
}
182+
158183
var invalidUuid = errors.New("invalid uuid")
159184

160185
func formatUuid(input string) (string, error) {

0 commit comments

Comments
 (0)