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

91
main.go
View File

@ -7,6 +7,8 @@ import (
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
)
@ -93,48 +95,60 @@ type McAuthRequest2 struct {
func main() {
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"},
})
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)
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"},
})
if err != nil {
fmt.Printf("Device Auth Step: %s\n", err)
return
}
data, _ := io.ReadAll(resp.Body)
codeResp := devCodeResp{}
json.Unmarshal(data, &codeResp)
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},
"grant_type": {"urn:ietf:params:oauth:grant-type:device_code"},
"device_code": {codeResp.Device_code},
})
if err != nil {
fmt.Printf("Authentication Request Error: %s\n", err)
}
defer resp.Body.Close()
//if resp.StatusCode != 200 {
// fmt.Printf("Authentication Request Error: %s\n", resp.Status)
//}
data, _ := io.ReadAll(resp.Body)
authResp := authenticationResp{}
json.Unmarshal(data, &authResp)
if authResp.Error == "" {
fmt.Printf("Authenticated!\n")
auth.Access = authResp
break
if resp.StatusCode != 200 {
fmt.Printf("Device Auth Step: %v\n", resp.Status)
return
}
data, _ := io.ReadAll(resp.Body)
codeResp := devCodeResp{}
json.Unmarshal(data, &codeResp)
fmt.Println(codeResp.Message)
ticker := time.NewTicker(time.Second * time.Duration(codeResp.Interval))
defer ticker.Stop()
for range ticker.C {
resp, err := http.PostForm("https://login.microsoftonline.com/consumers/oauth2/v2.0/token", url.Values{
"client_id": {client_id},
"grant_type": {"urn:ietf:params:oauth:grant-type:device_code"},
"device_code": {codeResp.Device_code},
})
if err != nil {
fmt.Printf("Authentication Request Error: %s\n", err)
}
defer resp.Body.Close()
//if resp.StatusCode != 200 {
// fmt.Printf("Authentication Request Error: %s\n", resp.Status)
//}
data, _ := io.ReadAll(resp.Body)
authResp := authenticationResp{}
json.Unmarshal(data, &authResp)
if authResp.Error == "" {
fmt.Printf("Authenticated!\n")
auth.Access = authResp
break
}
}
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{}
@ -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.