-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriveauth.go
More file actions
93 lines (83 loc) · 2.58 KB
/
Copy pathdriveauth.go
File metadata and controls
93 lines (83 loc) · 2.58 KB
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
package main
import (
"encoding/json"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
"github.com/apex/log"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/net/context"
"golang.org/x/oauth2"
)
// From: https://developers.google.com/drive/v3/web/quickstart/go
// getClient uses a Context and Config to retrieve a Token
// then generate a Client. It returns the generated Client.
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
cacheFile, err := tokenCacheFile()
if err != nil {
log.Fatalf("Unable to get path to cached credential file. %v", err)
}
tok, err := tokenFromFile(cacheFile)
if err != nil {
tok = getTokenFromWeb(config)
saveToken(cacheFile, tok)
}
return config.Client(ctx, tok)
}
// getTokenFromWeb uses Config to request a Token.
// It returns the retrieved Token.
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
if err := openbrowser(authURL); err != nil {
log.Warn("Go to the following link in your browser:")
log.Warn(authURL)
log.Warn("Then type the authorization code: ")
} else {
log.Warn("Follow the instructions in your browser then type the authorization code: ")
}
code, err := terminal.ReadPassword(0)
if err != nil {
log.WithError(err).Fatal("Unable to read authorization code")
}
tok, err := config.Exchange(oauth2.NoContext, string(code))
if err != nil {
log.WithError(err).Fatal("Unable to retrieve token from web")
}
return tok
}
// tokenCacheFile generates credential file path/filename.
// It returns the generated credential path/filename.
func tokenCacheFile() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
tokenCacheDir := filepath.Join(usr.HomeDir, ".credentials")
os.MkdirAll(tokenCacheDir, 0700)
return filepath.Join(tokenCacheDir, url.QueryEscape("docs2email.json")), err
}
// tokenFromFile retrieves a Token from a given file path.
// It returns the retrieved Token and any read error encountered.
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
t := &oauth2.Token{}
err = json.NewDecoder(f).Decode(t)
defer f.Close()
return t, err
}
// saveToken uses a file path to create a file and store the
// token in it.
func saveToken(file string, token *oauth2.Token) {
log.WithField("filename", file).Info("Saving credentials")
f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.WithError(err).Fatal("Unable to cache oauth token")
}
defer f.Close()
json.NewEncoder(f).Encode(token)
}