-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
147 lines (132 loc) · 3.39 KB
/
client.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
package cli
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/spf13/viper"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/gmail/v1"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"sync"
)
const (
redirectURL = "http://localhost:8080/callback"
)
var (
initServer sync.Once
mtx sync.Mutex
codeChan chan string
)
func getClient(email string) (*http.Client, error) {
config := &oauth2.Config{
ClientID: viper.Get("id").(string),
ClientSecret: viper.Get("secret").(string),
RedirectURL: redirectURL,
Scopes: []string{gmail.GmailReadonlyScope},
Endpoint: google.Endpoint,
}
tokenFile := filepath.Join(GetTokenDir(), fmt.Sprintf("%s.json", email))
token, err := tokenFromFile(tokenFile)
if err == nil {
return config.Client(context.Background(), token), nil
}
mtx.Lock()
defer mtx.Unlock()
println("Choose account %s to authorize", email)
token, err = getTokenFromWeb(config)
if err != nil {
return nil, err
}
saveToken(tokenFile, token)
return config.Client(context.Background(), token), nil
}
func getTokenFromWeb(config *oauth2.Config) (*oauth2.Token, error) {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser: \n%v\n", authURL)
openBrowser(authURL)
startServerAndWaitForCode()
code := <-codeChan
if code == "" {
return nil, fmt.Errorf("didn't get authorization code")
}
token, err := config.Exchange(context.TODO(), code)
if err != nil {
return nil, fmt.Errorf("unable to retrieve token from web: %v", err)
}
return token, nil
}
func startServerAndWaitForCode() {
initServer.Do(func() {
codeChan = make(chan string)
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
codeChan <- code
_, err := fmt.Fprintf(w, "Authorization successful! You can close this window now.")
if err != nil {
log.Printf("Error writing response: %v", err)
return
}
})
go func() {
if err := http.ListenAndServe("localhost:8080", nil); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("HTTP server ListenAndServe: %v", err)
}
}()
})
}
func openBrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
//case "windows":
// err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Printf("Error opening browser: %v", err)
}
}
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
log.Fatalf("Unable to close file: %v", err)
}
}(f)
token := &oauth2.Token{}
err = json.NewDecoder(f).Decode(token)
return token, err
}
func saveToken(path string, token *oauth2.Token) {
fmt.Printf("Saving credential file to: %s\n", path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("Unable to cache oauth token: %v", err)
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
log.Fatalf("Unable to close file: %v", err)
}
}(f)
err = json.NewEncoder(f).Encode(token)
if err != nil {
log.Fatalf("Unable to encode token: %v", err)
return
}
}