oauthutil: tidy interface to Config to add Options struct

The interface was getting so that a new function was needed for every
Config variant. Adding an Options struct fixes this.
This commit is contained in:
Nick Craig-Wood 2020-05-25 15:06:08 +01:00
parent c08617c70f
commit 49ba4eeb86
14 changed files with 44 additions and 39 deletions

View file

@ -71,7 +71,7 @@ func init() {
Description: "Amazon Drive", Description: "Amazon Drive",
NewFs: NewFs, NewFs: NewFs,
Config: func(name string, m configmap.Mapper) { 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 { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
} }

View file

@ -93,7 +93,7 @@ func init() {
log.Fatalf("Failed to configure token with jwt authentication: %v", err) log.Fatalf("Failed to configure token with jwt authentication: %v", err)
} }
} else { } else {
err = oauthutil.Config("box", name, m, oauthConfig) err = oauthutil.Config("box", name, m, oauthConfig, nil)
if err != nil { if err != nil {
log.Fatalf("Failed to configure token with oauth authentication: %v", err) log.Fatalf("Failed to configure token with oauth authentication: %v", err)
} }

View file

@ -178,7 +178,7 @@ func init() {
} }
if opt.ServiceAccountFile == "" { if opt.ServiceAccountFile == "" {
err = oauthutil.Config("drive", name, m, driveConfig) err = oauthutil.Config("drive", name, m, driveConfig, nil)
if err != nil { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
} }

View file

@ -117,7 +117,10 @@ func init() {
Description: "Dropbox", Description: "Dropbox",
NewFs: NewFs, NewFs: NewFs,
Config: func(name string, m configmap.Mapper) { 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 { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
} }

View file

@ -82,7 +82,7 @@ func init() {
if saFile != "" || saCreds != "" { if saFile != "" || saCreds != "" {
return return
} }
err := oauthutil.Config("google cloud storage", name, m, storageConfig) err := oauthutil.Config("google cloud storage", name, m, storageConfig, nil)
if err != nil { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
} }

View file

@ -96,7 +96,7 @@ func init() {
} }
// Do the oauth // Do the oauth
err = oauthutil.Config("google photos", name, m, oauthConfig) err = oauthutil.Config("google photos", name, m, oauthConfig, nil)
if err != nil { if err != nil {
golog.Fatalf("Failed to configure token: %v", err) golog.Fatalf("Failed to configure token: %v", err)
} }

View file

@ -58,7 +58,7 @@ func init() {
Description: "Hubic", Description: "Hubic",
NewFs: NewFs, NewFs: NewFs,
Config: func(name string, m configmap.Mapper) { 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 { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
} }

View file

@ -79,7 +79,7 @@ func init() {
NewFs: NewFs, NewFs: NewFs,
Config: func(name string, m configmap.Mapper) { Config: func(name string, m configmap.Mapper) {
ctx := context.TODO() ctx := context.TODO()
err := oauthutil.Config("onedrive", name, m, oauthConfig) err := oauthutil.Config("onedrive", name, m, oauthConfig, nil)
if err != nil { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
return return

View file

@ -67,7 +67,7 @@ func init() {
Description: "Pcloud", Description: "Pcloud",
NewFs: NewFs, NewFs: NewFs,
Config: func(name string, m configmap.Mapper) { 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 { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
} }

View file

@ -79,7 +79,7 @@ func init() {
Description: "premiumize.me", Description: "premiumize.me",
NewFs: NewFs, NewFs: NewFs,
Config: func(name string, m configmap.Mapper) { 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 { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
} }

View file

@ -60,7 +60,10 @@ func init() {
Description: "Put.io", Description: "Put.io",
NewFs: NewFs, NewFs: NewFs,
Config: func(name string, m configmap.Mapper) { 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 { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
} }

View file

@ -152,7 +152,10 @@ func init() {
oauthConfig.Endpoint.TokenURL = endpoint + tokenPath oauthConfig.Endpoint.TokenURL = endpoint + tokenPath
return nil 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 { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
} }

View file

@ -61,7 +61,7 @@ func init() {
Description: "Yandex Disk", Description: "Yandex Disk",
NewFs: NewFs, NewFs: NewFs,
Config: func(name string, m configmap.Mapper) { 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 { if err != nil {
log.Fatalf("Failed to configure token: %v", err) log.Fatalf("Failed to configure token: %v", err)
return return

View file

@ -357,32 +357,25 @@ func (ar *AuthResult) Error() string {
status, ar.Name, ar.Code, ar.Description, ar.HelpURL) 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 // CheckAuthFn is called when a good Auth has been received
type CheckAuthFn func(*oauth2.Config, *AuthResult) error 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 // It may run an internal webserver to receive the results
// func Config(id, name string, m configmap.Mapper, oauthConfig *oauth2.Config, opt *Options) error {
// When the AuthResult is known the checkAuth function is called if set if opt == nil {
func ConfigWithCallback(id, name string, m configmap.Mapper, config *oauth2.Config, checkAuth CheckAuthFn, opts ...oauth2.AuthCodeOption) error { opt = &Options{}
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 {
oauthConfig, changed := overrideCredentials(name, m, oauthConfig) oauthConfig, changed := overrideCredentials(name, m, oauthConfig)
authorizeOnlyValue, ok := m.Get(config.ConfigAuthorize) authorizeOnlyValue, ok := m.Get(config.ConfigAuthorize)
authorizeOnly := ok && authorizeOnlyValue != "" // set if being run by "rclone authorize" authorizeOnly := ok && authorizeOnlyValue != "" // set if being run by "rclone authorize"
@ -461,7 +454,8 @@ version recommended):
} }
// Generate oauth URL // Generate oauth URL
if offline { opts := opt.OAuth2Opts
if !opt.NoOffline {
opts = append(opts, oauth2.AccessTypeOffline) opts = append(opts, oauth2.AccessTypeOffline)
} }
authURL := oauthConfig.AuthCodeURL(state, opts...) authURL := oauthConfig.AuthCodeURL(state, opts...)
@ -469,7 +463,7 @@ version recommended):
// Prepare webserver if needed // Prepare webserver if needed
var server *authServer var server *authServer
if useWebServer { if useWebServer {
server = newAuthServer(bindAddress, state, authURL) server = newAuthServer(opt, bindAddress, state, authURL)
err := server.Init() err := server.Init()
if err != nil { if err != nil {
return errors.Wrap(err, "failed to start auth webserver") return errors.Wrap(err, "failed to start auth webserver")
@ -497,8 +491,8 @@ version recommended):
return auth return auth
} }
fmt.Printf("Got code\n") fmt.Printf("Got code\n")
if checkAuth != nil { if opt.CheckAuth != nil {
err = checkAuth(oauthConfig, auth) err = opt.CheckAuth(oauthConfig, auth)
if err != nil { if err != nil {
return err return err
} }
@ -529,6 +523,7 @@ version recommended):
// Local web server for collecting auth // Local web server for collecting auth
type authServer struct { type authServer struct {
opt *Options
state string state string
listener net.Listener listener net.Listener
bindAddress string bindAddress string
@ -538,8 +533,9 @@ type authServer struct {
} }
// newAuthServer makes the webserver for collecting auth // 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{ return &authServer{
opt: opt,
state: state, state: state,
bindAddress: bindAddress, bindAddress: bindAddress,
authURL: authURL, // http://host/auth redirects to here authURL: authURL, // http://host/auth redirects to here