Custom auth. optained access token

This commit is contained in:
Samuel Walker 2024-10-25 08:05:21 -06:00
parent 47de783732
commit b6f54f0687

131
main.go
View File

@ -1,47 +1,114 @@
package main package main
import ( import (
"context" "encoding/json"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt" "fmt"
"io"
"github.com/sandertv/gophertunnel/minecraft/auth" "net/http"
"net/url"
"time"
) )
const client_id string = "9305aeb8-5ecb-4e7a-b28f-c33aefcfbd8d"
type devCodeResp struct {
User_code string
Device_code string
Verification_uri string
Expires_in string
Interval int
Message string
}
type authenticationResp struct {
Access_token string
Token_type string
Refresh_token string
Expires_in string
Error string
Error_description string
}
func main() { func main() {
fmt.Println("Requesting Oauth")
token, err := auth.RequestLiveToken() 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 { if err != nil {
fmt.Println(err) fmt.Printf("Device Auth Step: %s\n", err)
return return
} else { } else {
ts := auth.RefreshTokenSource(token) defer resp.Body.Close()
fmt.Println("Generating Key") if resp.StatusCode != 200 {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) fmt.Printf("Device Auth Step: %v\n", resp.Status)
if err != nil {
fmt.Println(err)
return return
} }
ctx := context.Background() data, _ := io.ReadAll(resp.Body)
fmt.Println("Requesting XBL Token") codeResp := devCodeResp{}
token, err = ts.Token() json.Unmarshal(data, &codeResp)
if err != nil { fmt.Println(codeResp.Message)
fmt.Println(err) ticker := time.NewTicker(time.Second * time.Duration(codeResp.Interval))
return defer ticker.Stop()
} authentication := authenticationResp{}
xbl, err := auth.RequestXBLToken(ctx, token, "rp://api.minecraftservices.com/") for range ticker.C {
if err != nil { resp, err := http.PostForm("https://login.microsoftonline.com/consumers/oauth2/v2.0/token", url.Values{
fmt.Println(err) "client_id": {client_id},
return "grant_type": {"urn:ietf:params:oauth:grant-type:device_code"},
} "device_code": {codeResp.Device_code},
fmt.Println("Requesting Minecraft Chain") })
ctx = context.Background() if err != nil {
_, err = auth.RequestMinecraftChain(ctx, xbl, key) fmt.Printf("Authentication Request Error: %s\n", err)
if err != nil { }
fmt.Println(err) defer resp.Body.Close()
return //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!")
authentication = authResp
break
}
} }
fmt.Printf("Auth Response: %+v\n", authentication)
} }
/*
fmt.Println("Requesting Oauth")
token, err := auth.RequestLiveToken()
if err != nil {
fmt.Println(err)
return
} else {
ts := auth.RefreshTokenSource(token)
fmt.Println("Generating Key")
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
fmt.Println(err)
return
}
ctx := context.Background()
fmt.Println("Requesting XBL Token")
token, err = ts.Token()
if err != nil {
fmt.Println(err)
return
}
xbl, err := auth.RequestXBLToken(ctx, token, "rp://api.minecraftservices.com/")
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Requesting Minecraft Chain")
ctx = context.Background()
_, err = auth.RequestMinecraftChain(ctx, xbl, key)
if err != nil {
fmt.Println(err)
return
}
}*/
} }