Compare commits

..

No commits in common. "4946b7e5959f02cfdd5b0a7b539f7b51b8b17578" and "ca9e41e99eaa5dd047e9c24a72cff1cdfb7b357f" have entirely different histories.

2 changed files with 39 additions and 43 deletions

View File

@ -56,8 +56,6 @@ func (a *App) CheckPrerequisites() {
a.Auth, err = TokenRefresh(*a, a.Auth) a.Auth, err = TokenRefresh(*a, a.Auth)
if err == nil { if err == nil {
authenticated = true authenticated = true
} else {
fmt.Printf("token reauth failed, requesting device code: %s\n", err)
} }
} }
if !authenticated { if !authenticated {

View File

@ -12,45 +12,46 @@ import (
wruntime "github.com/wailsapp/wails/v2/pkg/runtime" wruntime "github.com/wailsapp/wails/v2/pkg/runtime"
) )
type LauncherAuth struct { type LauncherAuth struct {
Id string Id string
Name string Name string
Token string Token string
} }
type McProfile struct { type McProfile struct {
Id string Id string
Name string Name string
} }
type devcodeResp struct { type devcodeResp struct {
User_code string User_code string
Device_code string Device_code string
Verification_url string Verification_url string
Expires_in string Expires_in string
Interval int Interval int
Message string Message string
} }
type authenticationResp struct { type authenticationResp struct {
Access_token string Access_token string
Token_type string Token_type string
Refresh_token string Refresh_token string
Expires_in string Expires_in string
Error string Error string
Error_description string Error_description string
} }
type xboxAuthProperties struct { type xboxAuthProperties struct {
AuthMethod string AuthMethod string
SiteName string SiteName string
RpsTicket string RpsTicket string
} }
type xboxAuthRequest struct { type xboxAuthRequest struct {
Properties xboxAuthProperties Properties xboxAuthProperties
RelyingParty string RelyingParty string
TokenType string TokenType string
} }
type xboxDisplayClaim struct { type xboxDisplayClaim struct {
@ -68,40 +69,40 @@ type xboxDisplayClaims struct {
} }
type xboxAuthResponse struct { type xboxAuthResponse struct {
IssueInstant time.Time IssueInstant time.Time
NotAfter time.Time NotAfter time.Time
Token string Token string
DisplayClaims xboxDisplayClaims DisplayClaims xboxDisplayClaims
} }
type XSTSProperties struct { type XSTSProperties struct {
SandboxId string SandboxId string
UserTokens []string UserTokens []string
} }
type XSTSRequest struct { type XSTSRequest struct {
Properties XSTSProperties Properties XSTSProperties
RelyingParty string RelyingParty string
TokenType string TokenType string
} }
type McAuthRequest struct { type McAuthRequest struct {
Xtoken string `json:"xtoken"` Xtoken string `json:"xtoken"`
Platform string `json:"platform"` Platform string `json:"platform"`
} }
type McAuthResponse struct { type McAuthResponse struct {
Username string Username string
Access_token string Access_token string
Expires_in string Expires_in string
Token_type string Token_type string
} }
func AuthCode(a App) (authenticationResp, error) { func AuthCode(a App) (authenticationResp, error) {
authentication := authenticationResp{} authentication := authenticationResp{}
resp, err := http.PostForm("https://login.microsoftonline.com/consumers/oauth2/v2.0/devicecode", url.Values{ resp, err := http.PostForm("https://login.microsoftonline.com/consumers/oauth2/v2.0/devicecode", url.Values{
"client_id": {client_id}, "client_id": {client_id},
"scope": {"XboxLive.SignIn XboxLive.offline_access"}, "scope": {"XboxLive.SignIn XboxLive.offline_access"},
}) })
if err != nil { if err != nil {
return authentication, fmt.Errorf("Unable to request device code: %e\n", err) return authentication, fmt.Errorf("Unable to request device code: %e\n", err)
@ -118,9 +119,9 @@ func AuthCode(a App) (authenticationResp, error) {
ticker := time.NewTicker(time.Second * time.Duration(codeResp.Interval)) ticker := time.NewTicker(time.Second * time.Duration(codeResp.Interval))
for range ticker.C { for range ticker.C {
resp, err := http.PostForm("https://login.microsoftonline.com/consumers/oauth2/v2.0/token", url.Values{ resp, err := http.PostForm("https://login.microsoftonline.com/consumers/oauth2/v2.0/token", url.Values{
"client_id": {client_id}, "client_id": {client_id},
"grant_type": {"urn:ietf:params:oauth:grant-type:device_code"}, "grant_type": {"urn:ietf:params:oauth:grant-type:device_code"},
"device_code": {codeResp.Device_code}, "device_code": {codeResp.Device_code},
}) })
if err != nil { if err != nil {
return authentication, fmt.Errorf("Authentication request error %e\n", err) return authentication, fmt.Errorf("Authentication request error %e\n", err)
@ -137,10 +138,10 @@ func AuthCode(a App) (authenticationResp, error) {
func TokenRefresh(app App, auth authenticationResp) (authenticationResp, error) { func TokenRefresh(app App, auth authenticationResp) (authenticationResp, error) {
resp, err := http.PostForm("https://login.microsoftonline.com/consumers/oauth2/v2.0/token", url.Values{ resp, err := http.PostForm("https://login.microsoftonline.com/consumers/oauth2/v2.0/token", url.Values{
"client_id": {client_id}, "client_id": {client_id},
"grant_type": {"refresh_token"}, "grant_type": {"refresh_token"},
"refresh_token": {auth.Refresh_token}, "refresh_token": {auth.Refresh_token},
"scope": {"XboxLive.SignIn XboxLive.offline_access"}, "scope": {"XboxLive.SignIn XboxLive.offline_access"},
}) })
if err != nil { if err != nil {
return authenticationResp{}, fmt.Errorf("unable to request refresh token: %e\n", err) return authenticationResp{}, fmt.Errorf("unable to request refresh token: %e\n", err)
@ -149,15 +150,12 @@ func TokenRefresh(app App, auth authenticationResp) (authenticationResp, error)
data, _ := io.ReadAll(resp.Body) data, _ := io.ReadAll(resp.Body)
authResp := authenticationResp{} authResp := authenticationResp{}
json.Unmarshal(data, &authResp) json.Unmarshal(data, &authResp)
if authResp.Error != "" {
return authResp, fmt.Errorf("unable to request new token: %s", authResp.Error_description)
}
return authResp, nil return authResp, nil
} }
func MicrosoftAuth(auth authenticationResp) (LauncherAuth, error) { func MicrosoftAuth(auth authenticationResp) (LauncherAuth, error) {
//Xbox Live Auth //Xbox Live Auth
req, _ := json.Marshal(xboxAuthRequest{Properties: xboxAuthProperties{AuthMethod: "RPS", SiteName: "user.auth.xboxlive.com", RpsTicket: "d=" + auth.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_token}, RelyingParty: "http://auth.xboxlive.com", TokenType: "JWT"})
client := http.Client{} client := http.Client{}
httpreq, _ := http.NewRequest("POST", "https://user.auth.xboxlive.com/user/authenticate", bytes.NewBuffer(req)) httpreq, _ := http.NewRequest("POST", "https://user.auth.xboxlive.com/user/authenticate", bytes.NewBuffer(req))
httpreq.Header.Add("x-xbl-contract-version", "1") httpreq.Header.Add("x-xbl-contract-version", "1")
@ -171,7 +169,7 @@ func MicrosoftAuth(auth authenticationResp) (LauncherAuth, error) {
if httpResp.StatusCode != 200 { if httpResp.StatusCode != 200 {
return LauncherAuth{}, fmt.Errorf("unable to obtain xbox live token: %s\n", httpResp.Status) return LauncherAuth{}, fmt.Errorf("unable to obtain xbox live token: %s\n", httpResp.Status)
} }
d, _ := io.ReadAll(httpResp.Body) d,_ := io.ReadAll(httpResp.Body)
xblAuth := xboxAuthResponse{} xblAuth := xboxAuthResponse{}
json.Unmarshal(d, &xblAuth) json.Unmarshal(d, &xblAuth)
xstsData, _ := json.Marshal(XSTSRequest{Properties: XSTSProperties{SandboxId: "RETAIL", UserTokens: []string{xblAuth.Token}}, RelyingParty: "rp://api.minecraftservices.com/", TokenType: "JWT"}) xstsData, _ := json.Marshal(XSTSRequest{Properties: XSTSProperties{SandboxId: "RETAIL", UserTokens: []string{xblAuth.Token}}, RelyingParty: "rp://api.minecraftservices.com/", TokenType: "JWT"})
@ -188,7 +186,7 @@ func MicrosoftAuth(auth authenticationResp) (LauncherAuth, error) {
d, _ = io.ReadAll(httpResp.Body) d, _ = io.ReadAll(httpResp.Body)
mcApi := xboxAuthResponse{} mcApi := xboxAuthResponse{}
json.Unmarshal(d, &mcApi) json.Unmarshal(d, &mcApi)
mcAuthData, _ := json.Marshal(McAuthRequest{Xtoken: "XBL 3.0 x=" + mcApi.DisplayClaims.Xui[0].Uhs + ";" + mcApi.Token, Platform: "PC_LAUNCHER"}) mcAuthData, _ := json.Marshal(McAuthRequest{Xtoken: "XBL 3.0 x=" + mcApi.DisplayClaims.Xui[0].Uhs+ ";" + mcApi.Token, Platform: "PC_LAUNCHER"})
httpReqMC, _ := http.NewRequest("POST", "https://api.minecraftservices.com/launcher/login", bytes.NewBuffer(mcAuthData)) httpReqMC, _ := http.NewRequest("POST", "https://api.minecraftservices.com/launcher/login", 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")