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:
parent
c08617c70f
commit
49ba4eeb86
14 changed files with 44 additions and 39 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue