aboutsummaryrefslogtreecommitdiffstats
path: root/auth/oauth.go
diff options
context:
space:
mode:
authorPetter Rasmussen2016-02-21 00:37:17 +0100
committerPetter Rasmussen2016-02-21 00:37:17 +0100
commit21cc148ec34cdb65cbbca0dfb2995beea7cbc11b (patch)
treec580ea9fa68bc4388061bb6cb8161ef268926b6c /auth/oauth.go
parent28c4eb923fd01d892a17844328d0090830bcd229 (diff)
downloadgdrive-21cc148ec34cdb65cbbca0dfb2995beea7cbc11b.tar.bz2
Add support for user-provided refresh token
Diffstat (limited to 'auth/oauth.go')
-rw-r--r--auth/oauth.go42
1 files changed, 31 insertions, 11 deletions
diff --git a/auth/oauth.go b/auth/oauth.go
index a9ab10e..1f2884b 100644
--- a/auth/oauth.go
+++ b/auth/oauth.go
@@ -2,23 +2,15 @@ package auth
import (
"fmt"
+ "time"
"net/http"
"golang.org/x/oauth2"
)
type authCodeFn func(string) func() string
-func NewOauthClient(clientId, clientSecret, tokenFile string, authFn authCodeFn) (*http.Client, error) {
- conf := &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",
- },
- }
+func NewFileSourceClient(clientId, clientSecret, tokenFile string, authFn authCodeFn) (*http.Client, error) {
+ conf := getConfig(clientId, clientSecret)
// Read cached token
token, exists, err := ReadToken(tokenFile)
@@ -42,3 +34,31 @@ func NewOauthClient(clientId, clientSecret, tokenFile string, authFn authCodeFn)
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 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",
+ },
+ }
+}