diff --git a/backend/amazonclouddrive/amazonclouddrive.go b/backend/amazonclouddrive/amazonclouddrive.go index 01b215a4c..2341c8b9a 100644 --- a/backend/amazonclouddrive/amazonclouddrive.go +++ b/backend/amazonclouddrive/amazonclouddrive.go @@ -71,7 +71,7 @@ func init() { Description: "Amazon Drive", NewFs: NewFs, Config: func(name string, m configmap.Mapper) { - err := oauthutil.Config("amazon cloud drive", name, m, acdConfig) + err := oauthutil.Config("amazon cloud drive", name, m, acdConfig, nil) if err != nil { log.Fatalf("Failed to configure token: %v", err) } diff --git a/backend/box/box.go b/backend/box/box.go index a4383fea8..23ea0f17b 100644 --- a/backend/box/box.go +++ b/backend/box/box.go @@ -93,7 +93,7 @@ func init() { log.Fatalf("Failed to configure token with jwt authentication: %v", err) } } else { - err = oauthutil.Config("box", name, m, oauthConfig) + err = oauthutil.Config("box", name, m, oauthConfig, nil) if err != nil { log.Fatalf("Failed to configure token with oauth authentication: %v", err) } diff --git a/backend/drive/drive.go b/backend/drive/drive.go index 0a9066060..f199858f0 100755 --- a/backend/drive/drive.go +++ b/backend/drive/drive.go @@ -178,7 +178,7 @@ func init() { } if opt.ServiceAccountFile == "" { - err = oauthutil.Config("drive", name, m, driveConfig) + err = oauthutil.Config("drive", name, m, driveConfig, nil) if err != nil { log.Fatalf("Failed to configure token: %v", err) } diff --git a/backend/dropbox/dropbox.go b/backend/dropbox/dropbox.go index 07ce35dda..7af321da3 100755 --- a/backend/dropbox/dropbox.go +++ b/backend/dropbox/dropbox.go @@ -117,7 +117,10 @@ func init() { Description: "Dropbox", NewFs: NewFs, Config: func(name string, m configmap.Mapper) { - err := oauthutil.ConfigNoOffline("dropbox", name, m, dropboxConfig) + opt := oauthutil.Options{ + NoOffline: true, + } + err := oauthutil.Config("dropbox", name, m, dropboxConfig, &opt) if err != nil { log.Fatalf("Failed to configure token: %v", err) } diff --git a/backend/googlecloudstorage/googlecloudstorage.go b/backend/googlecloudstorage/googlecloudstorage.go index acafc955a..5ca7dbfde 100644 --- a/backend/googlecloudstorage/googlecloudstorage.go +++ b/backend/googlecloudstorage/googlecloudstorage.go @@ -82,7 +82,7 @@ func init() { if saFile != "" || saCreds != "" { return } - err := oauthutil.Config("google cloud storage", name, m, storageConfig) + err := oauthutil.Config("google cloud storage", name, m, storageConfig, nil) if err != nil { log.Fatalf("Failed to configure token: %v", err) } diff --git a/backend/googlephotos/googlephotos.go b/backend/googlephotos/googlephotos.go index 6f5abd105..2cbb1627d 100644 --- a/backend/googlephotos/googlephotos.go +++ b/backend/googlephotos/googlephotos.go @@ -96,7 +96,7 @@ func init() { } // Do the oauth - err = oauthutil.Config("google photos", name, m, oauthConfig) + err = oauthutil.Config("google photos", name, m, oauthConfig, nil) if err != nil { golog.Fatalf("Failed to configure token: %v", err) } diff --git a/backend/hubic/hubic.go b/backend/hubic/hubic.go index c233727d6..a558858de 100644 --- a/backend/hubic/hubic.go +++ b/backend/hubic/hubic.go @@ -58,7 +58,7 @@ func init() { Description: "Hubic", NewFs: NewFs, Config: func(name string, m configmap.Mapper) { - err := oauthutil.Config("hubic", name, m, oauthConfig) + err := oauthutil.Config("hubic", name, m, oauthConfig, nil) if err != nil { log.Fatalf("Failed to configure token: %v", err) } diff --git a/backend/onedrive/onedrive.go b/backend/onedrive/onedrive.go index c18760bb2..8e764543e 100755 --- a/backend/onedrive/onedrive.go +++ b/backend/onedrive/onedrive.go @@ -79,7 +79,7 @@ func init() { NewFs: NewFs, Config: func(name string, m configmap.Mapper) { ctx := context.TODO() - err := oauthutil.Config("onedrive", name, m, oauthConfig) + err := oauthutil.Config("onedrive", name, m, oauthConfig, nil) if err != nil { log.Fatalf("Failed to configure token: %v", err) return diff --git a/backend/pcloud/pcloud.go b/backend/pcloud/pcloud.go index 67b191f7d..61fe271c2 100644 --- a/backend/pcloud/pcloud.go +++ b/backend/pcloud/pcloud.go @@ -67,7 +67,7 @@ func init() { Description: "Pcloud", NewFs: NewFs, Config: func(name string, m configmap.Mapper) { - err := oauthutil.Config("pcloud", name, m, oauthConfig) + err := oauthutil.Config("pcloud", name, m, oauthConfig, nil) if err != nil { log.Fatalf("Failed to configure token: %v", err) } diff --git a/backend/premiumizeme/premiumizeme.go b/backend/premiumizeme/premiumizeme.go index e89fab1e7..f2ec858d5 100644 --- a/backend/premiumizeme/premiumizeme.go +++ b/backend/premiumizeme/premiumizeme.go @@ -79,7 +79,7 @@ func init() { Description: "premiumize.me", NewFs: NewFs, Config: func(name string, m configmap.Mapper) { - err := oauthutil.Config("premiumizeme", name, m, oauthConfig) + err := oauthutil.Config("premiumizeme", name, m, oauthConfig, nil) if err != nil { log.Fatalf("Failed to configure token: %v", err) } diff --git a/backend/putio/putio.go b/backend/putio/putio.go index fcb59cecd..3ee74f869 100644 --- a/backend/putio/putio.go +++ b/backend/putio/putio.go @@ -60,7 +60,10 @@ func init() { Description: "Put.io", NewFs: NewFs, Config: func(name string, m configmap.Mapper) { - err := oauthutil.ConfigNoOffline("putio", name, m, putioConfig) + opt := oauthutil.Options{ + NoOffline: true, + } + err := oauthutil.Config("putio", name, m, putioConfig, &opt) if err != nil { log.Fatalf("Failed to configure token: %v", err) } diff --git a/backend/sharefile/sharefile.go b/backend/sharefile/sharefile.go index 798bb04c5..d72112334 100644 --- a/backend/sharefile/sharefile.go +++ b/backend/sharefile/sharefile.go @@ -152,7 +152,10 @@ func init() { oauthConfig.Endpoint.TokenURL = endpoint + tokenPath return nil } - err := oauthutil.ConfigWithCallback("sharefile", name, m, oauthConfig, checkAuth) + opt := oauthutil.Options{ + CheckAuth: checkAuth, + } + err := oauthutil.Config("sharefile", name, m, oauthConfig, &opt) if err != nil { log.Fatalf("Failed to configure token: %v", err) } diff --git a/backend/yandex/yandex.go b/backend/yandex/yandex.go index 3b9541538..b5b638588 100644 --- a/backend/yandex/yandex.go +++ b/backend/yandex/yandex.go @@ -61,7 +61,7 @@ func init() { Description: "Yandex Disk", NewFs: NewFs, Config: func(name string, m configmap.Mapper) { - err := oauthutil.Config("yandex", name, m, oauthConfig) + err := oauthutil.Config("yandex", name, m, oauthConfig, nil) if err != nil { log.Fatalf("Failed to configure token: %v", err) return diff --git a/lib/oauthutil/oauthutil.go b/lib/oauthutil/oauthutil.go index 28dcea3b3..ff9a9c0f6 100644 --- a/lib/oauthutil/oauthutil.go +++ b/lib/oauthutil/oauthutil.go @@ -357,32 +357,25 @@ func (ar *AuthResult) Error() string { status, ar.Name, ar.Code, ar.Description, ar.HelpURL) } -// Config does the initial creation of the token -// -// It may run an internal webserver to receive the results -func Config(id, name string, m configmap.Mapper, config *oauth2.Config, opts ...oauth2.AuthCodeOption) error { - return doConfig(id, name, m, config, true, nil, opts) -} - // CheckAuthFn is called when a good Auth has been received type CheckAuthFn func(*oauth2.Config, *AuthResult) error -// ConfigWithCallback does the initial creation of the token +// Options for the oauth config +type Options struct { + NoOffline bool // If set then "access_type=offline" parameter is not passed + CheckAuth CheckAuthFn // When the AuthResult is known the checkAuth function is called if set + OAuth2Opts []oauth2.AuthCodeOption // extra oauth2 options +} + +// Config does the initial creation of the token +// +// If opt is nil it will use the default Options // // It may run an internal webserver to receive the results -// -// When the AuthResult is known the checkAuth function is called if set -func ConfigWithCallback(id, name string, m configmap.Mapper, config *oauth2.Config, checkAuth CheckAuthFn, opts ...oauth2.AuthCodeOption) error { - return doConfig(id, name, m, config, true, checkAuth, opts) -} - -// ConfigNoOffline does the same as Config but does not pass the -// "access_type=offline" parameter. -func ConfigNoOffline(id, name string, m configmap.Mapper, config *oauth2.Config, opts ...oauth2.AuthCodeOption) error { - return doConfig(id, name, m, config, false, nil, opts) -} - -func doConfig(id, name string, m configmap.Mapper, oauthConfig *oauth2.Config, offline bool, checkAuth CheckAuthFn, opts []oauth2.AuthCodeOption) error { +func Config(id, name string, m configmap.Mapper, oauthConfig *oauth2.Config, opt *Options) error { + if opt == nil { + opt = &Options{} + } oauthConfig, changed := overrideCredentials(name, m, oauthConfig) authorizeOnlyValue, ok := m.Get(config.ConfigAuthorize) authorizeOnly := ok && authorizeOnlyValue != "" // set if being run by "rclone authorize" @@ -461,7 +454,8 @@ version recommended): } // Generate oauth URL - if offline { + opts := opt.OAuth2Opts + if !opt.NoOffline { opts = append(opts, oauth2.AccessTypeOffline) } authURL := oauthConfig.AuthCodeURL(state, opts...) @@ -469,7 +463,7 @@ version recommended): // Prepare webserver if needed var server *authServer if useWebServer { - server = newAuthServer(bindAddress, state, authURL) + server = newAuthServer(opt, bindAddress, state, authURL) err := server.Init() if err != nil { return errors.Wrap(err, "failed to start auth webserver") @@ -497,8 +491,8 @@ version recommended): return auth } fmt.Printf("Got code\n") - if checkAuth != nil { - err = checkAuth(oauthConfig, auth) + if opt.CheckAuth != nil { + err = opt.CheckAuth(oauthConfig, auth) if err != nil { return err } @@ -529,6 +523,7 @@ version recommended): // Local web server for collecting auth type authServer struct { + opt *Options state string listener net.Listener bindAddress string @@ -538,8 +533,9 @@ type authServer struct { } // newAuthServer makes the webserver for collecting auth -func newAuthServer(bindAddress, state, authURL string) *authServer { +func newAuthServer(opt *Options, bindAddress, state, authURL string) *authServer { return &authServer{ + opt: opt, state: state, bindAddress: bindAddress, authURL: authURL, // http://host/auth redirects to here