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

93
main.go
View File

@ -7,6 +7,8 @@ import (
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"os"
"path/filepath"
"time" "time"
) )
@ -92,49 +94,61 @@ type McAuthRequest2 struct {
} }
func main() { func main() {
resp, err := http.PostForm("https://login.microsoftonline.com/consumers/oauth2/v2.0/devicecode", url.Values{ auth := Authentication{}
"client_id": {client_id}, dir, _ := os.UserConfigDir()
"scope": {"XboxLive.SignIn XboxLive.offline_access"}, 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{
if err != nil { "client_id": {client_id},
fmt.Printf("Device Auth Step: %s\n", err) "scope": {"XboxLive.SignIn XboxLive.offline_access"},
return })
} else { if err != nil {
defer resp.Body.Close() fmt.Printf("Device Auth Step: %s\n", err)
if resp.StatusCode != 200 {
fmt.Printf("Device Auth Step: %v\n", resp.Status)
return 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() defer resp.Body.Close()
//if resp.StatusCode != 200 { if resp.StatusCode != 200 {
// fmt.Printf("Authentication Request Error: %s\n", resp.Status) fmt.Printf("Device Auth Step: %v\n", resp.Status)
//} return
data, _ := io.ReadAll(resp.Body)
authResp := authenticationResp{}
json.Unmarshal(data, &authResp)
if authResp.Error == "" {
fmt.Printf("Authenticated!\n")
auth.Access = authResp
break
} }
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"}) 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{} 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, _ := http.NewRequest("POST", "https://api.minecraftservices.com/authentication/login_with_xbox", bytes.NewBuffer(mcAuthData))
httpReqMC.Header.Add("Content-Type", "application/json") httpReqMC.Header.Add("Content-Type", "application/json")
httpReqMC.Header.Add("Accept", "application/json") httpReqMC.Header.Add("Accept", "application/json")
resp, err = client.Do(httpReqMC) resp, err := client.Do(httpReqMC)
if err != nil { if err != nil {
fmt.Printf("MC Auth Error: %s\n", err) fmt.Printf("MC Auth Error: %s\n", err)
} }
@ -197,7 +211,6 @@ func main() {
} }
d, _ = io.ReadAll(resp.Body) d, _ = io.ReadAll(resp.Body)
fmt.Printf("MC Auth Response: %s\n", d) fmt.Printf("MC Auth Response: %s\n", d)
}
/* /*
fmt.Println("Requesting Oauth") fmt.Println("Requesting Oauth")

BIN
minecraft

Binary file not shown.