implemented saving auth token

This commit is contained in:
Samuel Walker 2024-10-26 22:49:24 -06:00
parent e11f1f1a4c
commit 664741d3b2
2 changed files with 53 additions and 40 deletions

21
main.go
View File

@ -7,6 +7,8 @@ import (
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
)
@ -93,6 +95,9 @@ type McAuthRequest2 struct {
func main() {
auth := Authentication{}
dir, _ := os.UserConfigDir()
if _, err := os.Stat(filepath.Join(dir, "minecraft_test", "authentication.json")); err != nil {
resp, err := http.PostForm("https://login.microsoftonline.com/consumers/oauth2/v2.0/devicecode", url.Values{
"client_id": {client_id},
"scope": {"XboxLive.SignIn XboxLive.offline_access"},
@ -100,7 +105,7 @@ func main() {
if err != nil {
fmt.Printf("Device Auth Step: %s\n", err)
return
} else {
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Printf("Device Auth Step: %v\n", resp.Status)
@ -112,7 +117,6 @@ func main() {
fmt.Println(codeResp.Message)
ticker := time.NewTicker(time.Second * time.Duration(codeResp.Interval))
defer ticker.Stop()
auth := Authentication{}
for range ticker.C {
resp, err := http.PostForm("https://login.microsoftonline.com/consumers/oauth2/v2.0/token", url.Values{
"client_id": {client_id},
@ -136,6 +140,16 @@ func main() {
}
}
os.MkdirAll(filepath.Join(dir, "minecraft_test"), 0755)
f, _ := os.OpenFile(filepath.Join(dir, "minecraft_test", "authentication.json"), os.O_CREATE|os.O_RDWR, 0755)
defer f.Close()
data, _ = json.Marshal(auth.Access)
f.Write(data)
} else {
f, _ := os.OpenFile(filepath.Join(dir, "minecraft_test", "authentication.json"), os.O_CREATE|os.O_RDWR, 0755)
data, _ := io.ReadAll(f)
json.Unmarshal(data, &auth.Access)
}
req, _ := json.Marshal(xboxAuthRequest{Properties: xboxAuthProperties{AuthMethod: "RPS", SiteName: "user.auth.xboxlive.com", RpsTicket: "d=" + auth.Access.Access_token}, RelyingParty: "http://auth.xboxlive.com", TokenType: "JWT"})
client := http.Client{}
httpReq, _ := http.NewRequest("POST", "https://user.auth.xboxlive.com/user/authenticate", bytes.NewBuffer(req))
@ -187,7 +201,7 @@ func main() {
httpReqMC, _ := http.NewRequest("POST", "https://api.minecraftservices.com/authentication/login_with_xbox", bytes.NewBuffer(mcAuthData))
httpReqMC.Header.Add("Content-Type", "application/json")
httpReqMC.Header.Add("Accept", "application/json")
resp, err = client.Do(httpReqMC)
resp, err := client.Do(httpReqMC)
if err != nil {
fmt.Printf("MC Auth Error: %s\n", err)
}
@ -197,7 +211,6 @@ func main() {
}
d, _ = io.ReadAll(resp.Body)
fmt.Printf("MC Auth Response: %s\n", d)
}
/*
fmt.Println("Requesting Oauth")

BIN
minecraft

Binary file not shown.