Skip to content

Commit

Permalink
Add minecraft/profile/lookup/bulk/byname alias
Browse files Browse the repository at this point in the history
As of 23w42a, the "Usernames to UUIDs" endpoint, previously at POST
https://api.mojang.com/profiles/minecraft, has been moved to POST
https://api.minecraftservices.com/minecraft/profile/lookup/bulk/byname.

This patch adds an alias for the new endpoint. The old endpoint will
still work.

Related: yushijinhun/authlib-injector#232
  • Loading branch information
evan-goode committed Mar 21, 2024
1 parent f59962f commit 72d3b1c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
26 changes: 1 addition & 25 deletions account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAccount(t *testing.T) {
ts.CreateTestUser(ts.Server, TEST_USERNAME)

t.Run("Test /users/profiles/minecraft/:playerName", ts.testAccountPlayerNameToID)
t.Run("Test /profiles/minecraft", ts.testAccountPlayerNamesToIDs)
t.Run("Test /profiles/minecraft", ts.makeTestAccountPlayerNamesToIDs("/profiles/minecraft"))
}
{
ts := &TestSuite{}
Expand Down Expand Up @@ -70,30 +70,6 @@ func (ts *TestSuite) testAccountPlayerNameToID(t *testing.T) {
assert.Equal(t, user.UUID, uuid)
}

func (ts *TestSuite) testAccountPlayerNamesToIDs(t *testing.T) {
payload := []string{TEST_USERNAME, "nonexistent"}
body, err := json.Marshal(payload)
assert.Nil(t, err)

req := httptest.NewRequest(http.MethodPost, "/profiles/minecraft", bytes.NewBuffer(body))
rec := httptest.NewRecorder()
ts.Server.ServeHTTP(rec, req)

assert.Equal(t, http.StatusOK, rec.Code)
var response []playerNameToUUIDResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))

// Get the real UUID
var user User
result := ts.App.DB.First(&user, "username = ?", TEST_USERNAME)
assert.Nil(t, result.Error)
id, err := UUIDToID(user.UUID)
assert.Nil(t, err)

// There should only be one user, the nonexistent user should not be present
assert.Equal(t, []playerNameToUUIDResponse{{Name: TEST_USERNAME, ID: id}}, response)
}

func (ts *TestSuite) testAccountPlayerNameToIDFallback(t *testing.T) {
{
req := httptest.NewRequest(http.MethodGet, "/users/profiles/minecraft/"+TEST_USERNAME, nil)
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func GetServer(app *App) *echo.Echo {
e.POST("/minecraft/profile/skins", servicesUploadSkin)
e.PUT("/minecraft/profile/name/:playerName", servicesChangeName)
e.GET("/publickeys", servicesPublicKeys)
e.POST("/minecraft/profile/lookup/bulk/byname", accountPlayerNamesToIDs)

e.GET("/services/player/attributes", servicesPlayerAttributes)
e.POST("/services/player/certificates", servicesPlayerCertificates)
Expand All @@ -264,6 +265,7 @@ func GetServer(app *App) *echo.Echo {
e.POST("/services/minecraft/profile/skins", servicesUploadSkin)
e.PUT("/services/minecraft/profile/name/:playerName", servicesChangeName)
e.GET("/services/publickeys", servicesPublicKeys)
e.POST("/services/minecraft/profile/lookup/bulk/byname", accountPlayerNamesToIDs)

return e
}
Expand Down
27 changes: 27 additions & 0 deletions services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestServices(t *testing.T) {
t.Run("Test GET /privacy/blocklist", ts.testServicesPrivacyBlocklist)
t.Run("Test GET /rollout/v1/msamigration", ts.testServicesMSAMigration)
t.Run("Test POST /publickey", ts.testServicesPublicKeys)
t.Run("Test POST /minecraft/profile/lookup/bulk/byname", ts.makeTestAccountPlayerNamesToIDs("/minecraft/profile/lookup/bulk/byname"))
}
{
ts := &TestSuite{}
Expand Down Expand Up @@ -486,3 +487,29 @@ func (ts *TestSuite) testServicesPublicKeys(t *testing.T) {
validateSerializedKey(t, key.PublicKey)
}
}

func (ts *TestSuite) makeTestAccountPlayerNamesToIDs(url string) func(t *testing.T) {
return func(t *testing.T) {
payload := []string{TEST_USERNAME, "nonexistent"}
body, err := json.Marshal(payload)
assert.Nil(t, err)

req := httptest.NewRequest(http.MethodPost, url, bytes.NewBuffer(body))
rec := httptest.NewRecorder()
ts.Server.ServeHTTP(rec, req)

assert.Equal(t, http.StatusOK, rec.Code)
var response []playerNameToUUIDResponse
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))

// Get the real UUID
var user User
result := ts.App.DB.First(&user, "username = ?", TEST_USERNAME)
assert.Nil(t, result.Error)
id, err := UUIDToID(user.UUID)
assert.Nil(t, err)

// There should only be one user, the nonexistent user should not be present
assert.Equal(t, []playerNameToUUIDResponse{{Name: TEST_USERNAME, ID: id}}, response)
}
}

0 comments on commit 72d3b1c

Please sign in to comment.