Skip to content

Commit

Permalink
Modify /api/account/mcAccount to accept mcauth codes
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Oct 13, 2024
1 parent d74efb2 commit d7396d4
Showing 1 changed file with 81 additions and 35 deletions.
116 changes: 81 additions & 35 deletions pkg/web/api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,41 +257,87 @@ func McAccountHandler(w http.ResponseWriter, r *http.Request) {
}

if r.Method == "POST" {
// Get Minecraft username
username := r.URL.Query().Get("username")
if username == "" {
core.JsonError(w, "Invalid username 1.")
return
}

// Get uuid
req, _ := http.NewRequest("GET", "https://api.mojang.com/users/profiles/minecraft/"+username, bytes.NewReader([]byte{}))
req.Header.Set("User-Agent", "Meteor Server")

client := http.Client{}
res, err := client.Do(req)
if err != nil {
core.JsonError(w, "Invalid username 2.")
return
}

body, _ := ioutil.ReadAll(res.Body)
var user mcUser
_ = json.Unmarshal(body, &user)

_ = res.Body.Close()

id, err := uuid.Parse(user.Id)
if err != nil {
core.JsonError(w, "Invalid username 3. "+err.Error())
return
}

// Add Minecraft account
err = account.AddMcAccount(id)
if err != nil {
core.JsonError(w, err.Error())
return
code := r.URL.Query().Get("code")

if code != "" {
res, err := http.Get("mcauth:8080/retrieve/" + code)
if err != nil {
core.JsonError(w, "Failed to retrieve code's UUID")
return
}

//goland:noinspection GoUnhandledErrorResult
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
var data struct{ Error string }

err := json.NewDecoder(res.Body).Decode(&data)
if err != nil {
core.JsonError(w, "Failed to parse code's UUID Error")
return
}

core.JsonError(w, data.Error)
return
}

var data struct{ Uuid string }

err = json.NewDecoder(res.Body).Decode(&data)
if err != nil {
core.JsonError(w, "Failed to parse code's UUID")
return
}

id, err := uuid.Parse(data.Uuid)
if err != nil {
core.JsonError(w, "Invalid UUID")
return
}

err = account.AddMcAccount(id)
if err != nil {
core.JsonError(w, err.Error())
return
}
} else {
// Get Minecraft username
username := r.URL.Query().Get("username")
if username == "" {
core.JsonError(w, "Invalid username 1.")
return
}

// Get uuid
req, _ := http.NewRequest("GET", "https://api.mojang.com/users/profiles/minecraft/"+username, bytes.NewReader([]byte{}))
req.Header.Set("User-Agent", "Meteor Server")

client := http.Client{}
res, err := client.Do(req)
if err != nil {
core.JsonError(w, "Invalid username 2.")
return
}

body, _ := ioutil.ReadAll(res.Body)
var user mcUser
_ = json.Unmarshal(body, &user)

_ = res.Body.Close()

id, err := uuid.Parse(user.Id)
if err != nil {
core.JsonError(w, "Invalid username 3. "+err.Error())
return
}

// Add Minecraft account
err = account.AddMcAccount(id)
if err != nil {
core.JsonError(w, err.Error())
return
}
}

UpdateCapes()
Expand Down

0 comments on commit d7396d4

Please sign in to comment.