forked from prasmussen/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.go
96 lines (80 loc) · 2.38 KB
/
oauth.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
package auth
import (
"fmt"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"net/http"
"time"
)
type authCodeFn func(string) func() string
func NewFileSourceClient(clientId, clientSecret, tokenFile string, authFn authCodeFn) (*http.Client, error) {
conf := getConfig(clientId, clientSecret)
// Read cached token
token, exists, err := ReadToken(tokenFile)
if err != nil {
return nil, fmt.Errorf("Failed to read token: %s", err)
}
// Require auth code if token file does not exist
// or refresh token is missing
if !exists || token.RefreshToken == "" {
authUrl := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
authCode := authFn(authUrl)()
token, err = conf.Exchange(oauth2.NoContext, authCode)
if err != nil {
return nil, fmt.Errorf("Failed to exchange auth code for token: %s", err)
}
}
return oauth2.NewClient(
oauth2.NoContext,
FileSource(tokenFile, token, conf),
), nil
}
func NewRefreshTokenClient(clientId, clientSecret, refreshToken string) *http.Client {
conf := getConfig(clientId, clientSecret)
token := &oauth2.Token{
TokenType: "Bearer",
RefreshToken: refreshToken,
Expiry: time.Now(),
}
return oauth2.NewClient(
oauth2.NoContext,
conf.TokenSource(oauth2.NoContext, token),
)
}
func NewAccessTokenClient(clientId, clientSecret, accessToken string) *http.Client {
conf := getConfig(clientId, clientSecret)
token := &oauth2.Token{
TokenType: "Bearer",
AccessToken: accessToken,
}
return oauth2.NewClient(
oauth2.NoContext,
conf.TokenSource(oauth2.NoContext, token),
)
}
func NewServiceAccountClient(serviceAccountFile string) (*http.Client, error) {
content, exists, err := ReadFile(serviceAccountFile)
if(!exists) {
return nil, fmt.Errorf("Service account filename %q not found", serviceAccountFile)
}
if(err != nil) {
return nil, err
}
conf, err := google.JWTConfigFromJSON(content, "https://www.googleapis.com/auth/drive")
if(err != nil) {
return nil, err
}
return conf.Client(oauth2.NoContext), nil
}
func getConfig(clientId, clientSecret string) *oauth2.Config {
return &oauth2.Config{
ClientID: clientId,
ClientSecret: clientSecret,
Scopes: []string{"https://www.googleapis.com/auth/drive"},
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
Endpoint: oauth2.Endpoint{
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
},
}
}