-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (151 loc) · 3.65 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/chowder/golt/internal/golt"
"github.com/pkg/browser"
"golang.org/x/oauth2"
"io"
"log"
"os"
"time"
)
func launchGameWithToken(token *oauth2.Token, idToken string) error {
idTokenPayload, err := golt.ParseIdToken(idToken)
if err != nil {
return err
}
// Get user details
sub := idTokenPayload["sub"].(string)
user, err := golt.GetUserDetails(sub, token.AccessToken)
if err != nil {
return err
}
log.Println(fmt.Sprintf("You are logged in as: %s#%s", user.DisplayName, user.Suffix))
loginProvider, ok := idTokenPayload["login_provider"]
if !ok || loginProvider != golt.LoginProvider {
log.Println("No known login_provider found in JWT token, using standard login...")
tup, err := golt.StandardLogin(idToken)
if err != nil {
return err
}
err = golt.WriteToConfigFile(tup.Second, "id_token")
if err != nil {
return err
}
sessionId, err := golt.GetGameSession(tup.Second)
if err != nil {
return err
}
accounts, err := golt.GetAccounts(sessionId)
if err != nil {
return err
}
log.Println("Found", len(accounts), "accounts")
account, err := golt.GetChosenAccount(user, accounts)
if err != nil {
if errors.Is(err, &golt.NoAccountChosenError{}) {
log.Println("No account was chosen")
return nil
}
return err
}
err = golt.LaunchGame(sessionId, account)
if err != nil {
return err
}
}
return nil
}
func handleSocialAuth(payload map[string]string) error {
token, err := golt.Exchange(payload["code"])
if err != nil {
return err
}
idToken := token.Extra("id_token").(string)
err = golt.WriteToConfigFile(idToken, "id_token")
if err != nil {
return err
}
// OAuth token
tokenAsJson, err := json.Marshal(token)
if err != nil {
return err
}
err = golt.WriteToConfigFile(string(tokenAsJson), "token")
if err != nil {
return err
}
return launchGameWithToken(token, idToken)
}
func getCachedTokens() (*golt.Tuple[*oauth2.Token, string], error) {
tokenJson, err := golt.ReadFromConfigFile("token")
if err != nil {
return nil, err
}
var token oauth2.Token
err = json.Unmarshal([]byte(tokenJson), &token)
if err != nil {
return nil, err
}
// TODO: try to refresh token (see: https://github.com/golang/oauth2/issues/84#issuecomment-254576796)
currentTime := time.Now()
if currentTime.Sub(token.Expiry) <= 30*time.Minute {
return nil, errors.New("cached token has expired")
}
idToken, err := golt.ReadFromConfigFile("id_token")
if err != nil {
return nil, err
}
return &golt.Tuple[*oauth2.Token, string]{
First: &token,
Second: idToken,
}, nil
}
func handleRegularLaunch() error {
tup, err := getCachedTokens()
if err != nil {
log.Println("Could not load cached tokens, initiating regular login flow")
err := golt.Login()
if err != nil {
return err
}
}
log.Println("Loaded cached tokens")
err = launchGameWithToken(tup.First, tup.Second)
if err != nil {
log.Println("Could not login with cached tokens, initiating regular login flow")
err := golt.Login()
if err != nil {
return err
}
}
return nil
}
func main() {
// Ignore stdout/stderr from browser - otherwise this messes up the bubbletea TUI
browser.Stdout = io.Discard
browser.Stderr = io.Discard
if len(os.Args) < 2 {
err := handleRegularLaunch()
if err != nil {
log.Println(err)
os.Exit(1)
}
os.Exit(0)
}
payload := golt.ParseIntentPayload(os.Args[1])
intent, ok := payload["intent"]
if !ok {
log.Fatal("No intent found in payload", payload)
}
if intent == "social_auth" {
err := handleSocialAuth(payload)
if err != nil {
golt.Die(err)
}
}
log.Println("Press Enter to exit...")
fmt.Scanln()
}