forked from prasmussen/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
73 lines (63 loc) · 1.74 KB
/
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
package auth
import (
"code.google.com/p/goauth2/oauth"
"errors"
"fmt"
"github.com/prasmussen/gdrive/util"
"net/http"
)
// Get auth code from user
func promptUserForAuthCode(config *oauth.Config) string {
authUrl := config.AuthCodeURL("state")
fmt.Println("Go to the following link in your browser:")
fmt.Printf("%v\n\n", authUrl)
return util.Prompt("Enter verification code: ")
}
// Returns true if we have a valid cached token
func hasValidToken(cacheFile oauth.CacheFile, transport *oauth.Transport) bool {
// Check if we have a cached token
token, err := cacheFile.Token()
if err != nil {
return false
}
// Refresh token if its expired
if token.Expired() {
transport.Token = token
err = transport.Refresh()
if err != nil {
fmt.Println(err)
return false
}
}
return true
}
func GetOauth2Client(clientId, clientSecret, cachePath string, promptUser bool) (*http.Client, error) {
cacheFile := oauth.CacheFile(cachePath)
config := &oauth.Config{
ClientId: clientId,
ClientSecret: clientSecret,
Scope: "https://www.googleapis.com/auth/drive",
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
TokenCache: cacheFile,
}
transport := &oauth.Transport{
Config: config,
Transport: http.DefaultTransport,
}
// Return client if we have a valid token
if hasValidToken(cacheFile, transport) {
return transport.Client(), nil
}
if !promptUser {
return nil, errors.New("no valid token found")
}
// Get auth code from user and request a new token
code := promptUserForAuthCode(config)
_, err := transport.Exchange(code)
if err != nil {
return nil, err
}
return transport.Client(), nil
}