-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_auth.go
165 lines (142 loc) · 4.2 KB
/
google_auth.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
package auth
import (
"context"
"errors"
"fmt"
"log"
"os"
"github.com/manifoldco/promptui"
"github.com/mati2251/notion-to-google-tasks/keys"
"github.com/spf13/viper"
"golang.org/x/oauth2"
"google.golang.org/api/option"
"google.golang.org/api/tasks/v1"
)
func GoogleConfig() {
setDefaults()
getClientIdAndSecret()
var err error
TasksService, err = setGoogleToken()
if err != nil {
log.Fatalf("Unable get Google Token: %v\n", err)
}
}
func RemoveGoogleConfig() {
viper.Set(keys.GOOGLE_CLIENT_ID_KEY, "")
viper.Set(keys.GOOGLE_CLIENT_SECRET_KEY, "")
viper.Set(keys.GOOGLE_REFRESH_TOKEN_KEY, "")
viper.Set(keys.GOOGLE_ACCESS_TOKEN_KEY, "")
viper.Set(keys.GOOGLE_TOKEN_TYPE_KEY, "")
viper.Set(keys.GOOGLE_EXPIRY_KEY, "")
setDefaults()
viper.WriteConfig()
}
func GetTasksService() (*tasks.Service, error) {
conf := getGoogleOauth2Conf()
tok, err := getTokenFromConfig()
if err != nil {
return nil, err
}
return getServiceFromToken(conf, tok)
}
func setDefaults() {
viper.SetDefault(keys.GOOGLE_AUTH_URI_KEY, "https://accounts.google.com/o/oauth2/auth")
viper.SetDefault(keys.GOOGLE_TOKEN_URI_KEY, "https://oauth2.googleapis.com/token")
viper.SetDefault(keys.GOOGLE_AUTH_PROVIDER_X509_CERT_URL_KEY, "https://www.googleapis.com/oauth2/v1/certs")
viper.SetDefault(keys.GOOGLE_REDIRECT_URIS_KEY, "http://localhost")
}
func getClientIdAndSecret() {
prompt := promptui.Prompt{
Label: "Google Client ID",
}
clientId, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
os.Exit(1)
}
prompt = promptui.Prompt{
Label: "Google Client Secret",
}
clientSecret, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
os.Exit(1)
}
viper.Set(keys.GOOGLE_CLIENT_ID_KEY, clientId)
viper.Set(keys.GOOGLE_CLIENT_SECRET_KEY, clientSecret)
}
func setGoogleToken() (*tasks.Service, error) {
conf := getGoogleOauth2Conf()
tok, err := getTokenFromWeb(conf)
if err != nil {
log.Fatalf("Unable to get token from web: %v", err)
return nil, err
}
saveNewToken(tok)
return getServiceFromToken(conf, tok)
}
func saveNewToken(tok *oauth2.Token) {
viper.Set(keys.GOOGLE_REFRESH_TOKEN_KEY, tok.RefreshToken)
viper.Set(keys.GOOGLE_ACCESS_TOKEN_KEY, tok.AccessToken)
viper.Set(keys.GOOGLE_TOKEN_TYPE_KEY, tok.TokenType)
viper.Set(keys.GOOGLE_EXPIRY_KEY, tok.Expiry)
viper.SafeWriteConfig()
viper.WriteConfig()
}
func getGoogleOauth2Conf() *oauth2.Config {
viper.ReadInConfig()
return &oauth2.Config{
ClientID: viper.GetString(keys.GOOGLE_CLIENT_ID_KEY),
ClientSecret: viper.GetString(keys.GOOGLE_CLIENT_SECRET_KEY),
RedirectURL: viper.GetString(keys.GOOGLE_REDIRECT_URIS_KEY),
Scopes: []string{
tasks.TasksScope,
},
Endpoint: oauth2.Endpoint{
AuthURL: viper.GetString(keys.GOOGLE_AUTH_URI_KEY),
TokenURL: viper.GetString(keys.GOOGLE_TOKEN_URI_KEY),
},
}
}
func getServiceFromToken(conf *oauth2.Config, tok *oauth2.Token) (*tasks.Service, error) {
ctx := context.Background()
client := conf.Client(ctx, tok)
srv, err := tasks.NewService(ctx, option.WithHTTPClient(client))
saveNewToken(tok)
if err != nil {
log.Fatalf("Unable to retrieve tasks Client %v", err)
return nil, err
}
return srv, err
}
func getTokenFromConfig() (*oauth2.Token, error) {
if viper.GetString(keys.GOOGLE_REFRESH_TOKEN_KEY) == "" {
return nil, errors.New("no refresh token found")
}
tok := &oauth2.Token{
RefreshToken: viper.GetString(keys.GOOGLE_REFRESH_TOKEN_KEY),
AccessToken: viper.GetString(keys.GOOGLE_ACCESS_TOKEN_KEY),
TokenType: viper.GetString(keys.GOOGLE_TOKEN_TYPE_KEY),
Expiry: viper.GetTime(keys.GOOGLE_EXPIRY_KEY),
}
return tok, 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 then type the "+
"authorization code: \n%v\n", authURL)
prompt := promptui.Prompt{
Label: "Authorization code",
}
authCode, err := prompt.Run()
if err != nil {
log.Fatalf("Unable to read authorization code: %v", err)
return nil, err
}
tok, err := config.Exchange(context.TODO(), authCode)
if err != nil {
log.Fatalf("Unable to retrieve token from web: %v", err)
return nil, err
}
return tok, nil
}