-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcommon.go
354 lines (311 loc) · 10 KB
/
common.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/BurntSushi/toml"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
type GoogleConfig struct {
ClientID string `toml:"client_id"`
ClientSecret string `toml:"client_secret"`
}
type GeneralConfig struct {
DisableReminders bool `toml:"disable_reminders"`
EventVisibility string `toml:"block_event_visibility"`
AuthorizedPorts []int `toml:"authorized_ports"`
Verbosity int `toml:"verbosity"`
}
type Config struct {
General GeneralConfig `toml:"general"`
Google GoogleConfig `toml:"google"`
}
var oauthConfig *oauth2.Config
var configDir string
func initOAuthConfig(config *Config) {
oauthConfig = &oauth2.Config{
ClientID: config.Google.ClientID,
ClientSecret: config.Google.ClientSecret,
Endpoint: google.Endpoint,
Scopes: []string{calendar.CalendarScope},
// RedirectURL will be set dynamically in getTokenFromWeb
}
}
func readConfig(filename string) (*Config, error) {
// Try first current dir, then `$HOME/.config/gcalsync/`
data, err := os.ReadFile(filename)
if err != nil {
data, err = os.ReadFile(os.Getenv("HOME") + "/.config/gcalsync/" + filename)
if err != nil {
return nil, err
}
configDir = os.Getenv("HOME") + "/.config/gcalsync/"
}
// Check the config file format an update it to new, if it is old
err = upadteConfigFormatIfNeeded(data, configDir, filename)
if err != nil {
return nil, err
}
var config Config
if err := toml.Unmarshal(data, &config); err != nil {
return nil, err
}
return &config, nil
}
func upadteConfigFormatIfNeeded(data []byte, configDir, filename string) error {
type oldConfig struct {
DisableReminders bool `toml:"disable_reminders"`
EventVisibility string `toml:"block_event_visibility"`
AuthorizedPorts []int `toml:"authorized_ports"`
ClientID string `toml:"client_id"`
ClientSecret string `toml:"client_secret"`
Verbosity int `toml:"verbosity_level"`
}
var old oldConfig
if err := toml.Unmarshal(data, &old); err != nil {
return err
}
if old.ClientID == "" || old.ClientSecret == "" {
var cfg Config
if err := toml.Unmarshal(data, &cfg); err != nil {
return err
}
// The config is already in the new format or it is empty
return nil
}
fmt.Printf("⚠️ Old config file format detected. Updating to new format...\n")
// Convert old config to new format
newConfig := Config{
General: GeneralConfig{
DisableReminders: old.DisableReminders,
EventVisibility: old.EventVisibility,
AuthorizedPorts: old.AuthorizedPorts,
Verbosity: old.Verbosity,
},
Google: GoogleConfig{
ClientID: old.ClientID,
ClientSecret: old.ClientSecret,
},
}
data, err := toml.Marshal(newConfig)
if err != nil {
return err
}
// Move the old config file to a backup
if _, err := os.Stat(configDir + filename); err == nil {
timStamp := time.Now().Format("2006-01-02-150405")
backupFilename := fmt.Sprintf("%s%s.bak-%s", configDir, filename, timStamp)
err = os.Rename(configDir+filename, backupFilename)
if err != nil {
return err
}
fmt.Printf(" ℹ️ Old config file moved to %s\n", backupFilename)
}
err = os.WriteFile(configDir+filename, data, 0644)
if err != nil {
return err
}
fmt.Printf("✅ Config file updated to new format and saved to %s\n", configDir+filename)
return nil
}
func openDB(filename string) (*sql.DB, error) {
// Try first the same dir, where the config file was found
db, err := sql.Open("sqlite3", configDir+filename)
if err != nil {
// Try the current dir
db, err = sql.Open("sqlite3", filename)
if err != nil {
return nil, err
}
}
return db, nil
}
func getTokenFromWeb(config *oauth2.Config, cfg *Config) *oauth2.Token {
// Start local server
listener, err := findAvailablePort(cfg.General.AuthorizedPorts)
if err != nil {
log.Fatalf("Unable to start listener: %v", err)
}
defer listener.Close()
port := listener.Addr().(*net.TCPAddr).Port
config.RedirectURL = fmt.Sprintf("http://localhost:%d", port)
codeChan := make(chan string)
var server *http.Server
server = &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
codeChan <- code
fmt.Fprintf(w, "Authorization successful! You can close this window.")
go func() {
time.Sleep(time.Second)
server.Shutdown(context.Background())
}()
}),
}
go server.Serve(listener)
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Please visit this URL to authorize the application: \n%v\n", authURL)
// Open browser automatically
// err = openBrowser(authURL)
// if err != nil {
// fmt.Printf("Failed to open browser automatically: %v\n", err)
// fmt.Println("Please open the URL manually in your browser.")
// }
// Copy URL to clipboard
err = copyUrlToClipboard(authURL)
if err != nil {
fmt.Printf("Failed to copy URL to clipboard: %v\n", err)
fmt.Println("Please copy the URL manually and open it in your browser.")
}
code := <-codeChan
tok, err := config.Exchange(context.TODO(), code)
if err != nil {
log.Fatalf("Unable to retrieve token: %v", err)
}
return tok
}
func saveToken(db *sql.DB, accountName string, token *oauth2.Token) error {
tokenJSON, err := json.Marshal(token)
if err != nil {
return err
}
_, err = db.Exec("INSERT OR REPLACE INTO tokens (account_name, token) VALUES (?, ?)", accountName, tokenJSON)
return err
}
func getClient(ctx context.Context, config *oauth2.Config, db *sql.DB, accountName string, cfg *Config) *http.Client {
var tokenJSON []byte
err := db.QueryRow("SELECT token FROM tokens WHERE account_name = ?", accountName).Scan(&tokenJSON)
if err != nil {
if err == sql.ErrNoRows {
fmt.Printf(" ❗️ No token found for account %s. Obtaining a new token.\n", accountName)
token := getTokenFromWeb(config, cfg)
saveToken(db, accountName, token)
return config.Client(ctx, token)
}
log.Fatalf("Error retrieving token from database: %v", err)
}
var token oauth2.Token
err = json.Unmarshal(tokenJSON, &token)
if err != nil {
log.Fatalf("Error unmarshaling token: %v", err)
}
tokenSource := config.TokenSource(ctx, &token)
newToken, err := tokenSource.Token()
if err != nil {
if strings.Contains(err.Error(), "token expired") ||
strings.Contains(err.Error(), "Token has been expired or revoked") ||
strings.Contains(err.Error(), "invalid_grant") ||
strings.Contains(err.Error(), "oauth2: token expired and refresh token is not set") {
fmt.Printf(" ❗️ Token expired or revoked for account %s. Obtaining a new token.\n", accountName)
// Delete the existing invalid token
_, err := db.Exec("DELETE FROM tokens WHERE account_name = ?", accountName)
if err != nil {
log.Printf("Warning: Failed to delete invalid token: %v", err)
}
// Get a new token from the web
newToken = getTokenFromWeb(config, cfg)
saveToken(db, accountName, newToken)
return config.Client(ctx, newToken)
}
log.Fatalf("Error retrieving token from token source: %v", err)
}
if newToken.AccessToken != token.AccessToken {
fmt.Printf("Token refreshed for account %s.\n", accountName)
saveToken(db, accountName, newToken)
}
// Check if the token is expired and refresh it if necessary
if token.Expiry.Before(time.Now()) {
fmt.Printf(" ❗️ Token expired for account %s. Refreshing token.\n", accountName)
newToken, err := config.TokenSource(ctx, &token).Token()
if err != nil {
log.Fatalf("Error refreshing token: %v", err)
}
saveToken(db, accountName, newToken)
return config.Client(ctx, newToken)
}
return config.Client(ctx, &token)
}
// Check if the token has expired and refresh if necessary, return updated calendarService
func tokenExpired(db *sql.DB, accountName string, calendarService *calendar.Service, ctx context.Context) *calendar.Service {
var tokenJSON []byte
err := db.QueryRow("SELECT token FROM tokens WHERE account_name = ?", accountName).Scan(&tokenJSON)
if err != nil {
log.Fatalf("Error retrieving token from database: %v", err)
}
var token oauth2.Token
err = json.Unmarshal(tokenJSON, &token)
if err != nil {
log.Fatalf("Error unmarshaling token: %v", err)
}
if token.Expiry.Before(time.Now()) {
fmt.Printf(" ❗️ Token expired for account %s. Refreshing token.\n", accountName)
newToken, err := oauthConfig.TokenSource(ctx, &token).Token()
if err != nil {
log.Fatalf("Error refreshing token: %v", err)
}
saveToken(db, accountName, newToken)
// Create new calendar service with updated token
calendarService, err = calendar.NewService(ctx, option.WithHTTPClient(oauthConfig.Client(ctx, newToken)))
if err != nil {
log.Fatalf("Unable to create new calendar service: %v", err)
}
}
return calendarService
}
// Helper function to find an available port in a range
func findAvailablePort(authorizedPorts []int) (net.Listener, error) {
for _, port := range authorizedPorts {
listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
if err == nil {
return listener, nil
}
}
return nil, fmt.Errorf("no available ports in range %v", authorizedPorts)
}
// Open a URL in the default browser
func openBrowser(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}
// Copy a URL into a clipboard automatically
func copyUrlToClipboard(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "echo", url, "|", "clip"}
case "darwin":
cmd = "pbcopy"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xclip"
args = []string{"-selection", "clipboard"}
}
command := exec.Command(cmd, args...)
command.Stdin = strings.NewReader(url)
return command.Run()
}