aboutsummaryrefslogtreecommitdiffstats
path: root/auth/oauth.go
diff options
context:
space:
mode:
authorPetter Rasmussen2016-02-21 21:03:26 +0100
committerPetter Rasmussen2016-02-21 21:03:26 +0100
commit1973512dd8edca24df4124fb3dfac4a432a0d481 (patch)
treec61daefa5cf24eb2211ac816862697f9e0676d86 /auth/oauth.go
parent701c7f1991ae765a51b0b7404d1edbb2dc523055 (diff)
downloadgdrive-1973512dd8edca24df4124fb3dfac4a432a0d481.tar.bz2
go fmt
Diffstat (limited to 'auth/oauth.go')
-rw-r--r--auth/oauth.go106
1 files changed, 53 insertions, 53 deletions
diff --git a/auth/oauth.go b/auth/oauth.go
index 965c7cc..150642c 100644
--- a/auth/oauth.go
+++ b/auth/oauth.go
@@ -1,78 +1,78 @@
package auth
import (
- "fmt"
- "time"
- "net/http"
- "golang.org/x/oauth2"
+ "fmt"
+ "golang.org/x/oauth2"
+ "net/http"
+ "time"
)
type authCodeFn func(string) func() string
func NewFileSourceClient(clientId, clientSecret, tokenFile string, authFn authCodeFn) (*http.Client, error) {
- conf := getConfig(clientId, clientSecret)
+ 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)
- }
+ // 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)
- }
- }
+ // 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
+ return oauth2.NewClient(
+ oauth2.NoContext,
+ FileSource(tokenFile, token, conf),
+ ), nil
}
func NewRefreshTokenClient(clientId, clientSecret, refreshToken string) *http.Client {
- conf := getConfig(clientId, clientSecret)
+ conf := getConfig(clientId, clientSecret)
- token := &oauth2.Token{
- TokenType: "Bearer",
- RefreshToken: refreshToken,
- Expiry: time.Now(),
- }
+ token := &oauth2.Token{
+ TokenType: "Bearer",
+ RefreshToken: refreshToken,
+ Expiry: time.Now(),
+ }
- return oauth2.NewClient(
- oauth2.NoContext,
- conf.TokenSource(oauth2.NoContext, token),
- )
+ return oauth2.NewClient(
+ oauth2.NoContext,
+ conf.TokenSource(oauth2.NoContext, token),
+ )
}
func NewAccessTokenClient(clientId, clientSecret, accessToken string) *http.Client {
- conf := getConfig(clientId, clientSecret)
+ conf := getConfig(clientId, clientSecret)
- token := &oauth2.Token{
- TokenType: "Bearer",
- AccessToken: accessToken,
- }
+ token := &oauth2.Token{
+ TokenType: "Bearer",
+ AccessToken: accessToken,
+ }
- return oauth2.NewClient(
- oauth2.NoContext,
- conf.TokenSource(oauth2.NoContext, token),
- )
+ 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",
- },
- }
+ 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",
+ },
+ }
}