forked from TrueCloudLab/rclone
rclone authorize: Send and receive extra config options to fix oauth
Before this change any backends which required extra config in the oauth phase (like the `region` for zoho) didn't work with `rclone authorize`. This change serializes the extra config and passes it to `rclone authorize` and returns new config items to be set from rclone authorize. `rclone authorize` will still accept its previous configuration parameters for use with old rclones. Fixes #5178
This commit is contained in:
parent
9d5c5bf7ab
commit
f52ae75a51
2 changed files with 54 additions and 13 deletions
|
@ -18,7 +18,7 @@ import (
|
||||||
func Authorize(ctx context.Context, args []string, noAutoBrowser bool) error {
|
func Authorize(ctx context.Context, args []string, noAutoBrowser bool) error {
|
||||||
ctx = suppressConfirm(ctx)
|
ctx = suppressConfirm(ctx)
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
case 1, 3:
|
case 1, 2, 3:
|
||||||
default:
|
default:
|
||||||
return errors.Errorf("invalid number of arguments: %d", len(args))
|
return errors.Errorf("invalid number of arguments: %d", len(args))
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,13 @@ func Authorize(ctx context.Context, args []string, noAutoBrowser bool) error {
|
||||||
inM[ConfigAuthNoBrowser] = "true"
|
inM[ConfigAuthNoBrowser] = "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) == 3 {
|
// Add extra parameters if supplied
|
||||||
|
if len(args) == 2 {
|
||||||
|
err := inM.Decode(args[1])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if len(args) == 3 {
|
||||||
inM[ConfigClientID] = args[1]
|
inM[ConfigClientID] = args[1]
|
||||||
inM[ConfigClientSecret] = args[2]
|
inM[ConfigClientSecret] = args[2]
|
||||||
}
|
}
|
||||||
|
@ -52,13 +58,21 @@ func Authorize(ctx context.Context, args []string, noAutoBrowser bool) error {
|
||||||
outM := configmap.Simple{}
|
outM := configmap.Simple{}
|
||||||
m.ClearSetters()
|
m.ClearSetters()
|
||||||
m.AddSetter(outM)
|
m.AddSetter(outM)
|
||||||
|
m.AddGetter(outM, configmap.PriorityNormal)
|
||||||
|
|
||||||
ri.Config(ctx, name, m)
|
ri.Config(ctx, name, m)
|
||||||
|
|
||||||
// Print code if we are doing a manual auth
|
// Print the code for the user to paste
|
||||||
fmt.Printf("Paste the following into your remote machine --->\n%s\n<---End paste\n", outM["token"])
|
out := outM["token"]
|
||||||
|
|
||||||
fs.Debugf(nil, "Set parameters %q", outM)
|
// If received a config blob, then return one
|
||||||
|
if len(args) == 2 {
|
||||||
|
out, err = outM.Encode()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("Paste the following into your remote machine --->\n%s\n<---End paste\n", out)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -447,19 +447,46 @@ Execute the following on the machine with the web browser (same rclone
|
||||||
version recommended):
|
version recommended):
|
||||||
|
|
||||||
`)
|
`)
|
||||||
if changed {
|
// Find the configuration
|
||||||
fmt.Printf("\trclone authorize %q -- %q %q\n", id, oauthConfig.ClientID, oauthConfig.ClientSecret)
|
ri, err := fs.Find(id)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "oauthutil authorize")
|
||||||
|
}
|
||||||
|
// Find the overridden options
|
||||||
|
inM := ri.Options.NonDefault(m)
|
||||||
|
delete(inM, config.ConfigToken) // delete token as we are refreshing it
|
||||||
|
for k, v := range inM {
|
||||||
|
fs.Debugf(nil, "sending %s = %q", k, v)
|
||||||
|
}
|
||||||
|
// Encode them into a string
|
||||||
|
mCopyString, err := inM.Encode()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "oauthutil authorize encode")
|
||||||
|
}
|
||||||
|
// Write what the user has to do
|
||||||
|
if len(mCopyString) > 0 {
|
||||||
|
fmt.Printf("\trclone authorize %q %q\n", id, mCopyString)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("\trclone authorize %q\n", id)
|
fmt.Printf("\trclone authorize %q\n", id)
|
||||||
}
|
}
|
||||||
fmt.Println("\nThen paste the result below:")
|
fmt.Println("\nThen paste the result below:")
|
||||||
code := config.ReadNonEmptyLine("result> ")
|
// Read the updates to the config
|
||||||
token := &oauth2.Token{}
|
var outM configmap.Simple
|
||||||
err := json.Unmarshal([]byte(code), token)
|
for {
|
||||||
if err != nil {
|
outM = configmap.Simple{}
|
||||||
return err
|
code := config.ReadNonEmptyLine("result> ")
|
||||||
|
err = outM.Decode(code)
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
fmt.Printf("Couldn't decode response - try again (make sure you are using a matching version of rclone on both sides: %v\n", err)
|
||||||
}
|
}
|
||||||
return PutToken(name, m, token, true)
|
// Save the config updates
|
||||||
|
for k, v := range outM {
|
||||||
|
m.Set(k, v)
|
||||||
|
fs.Debugf(nil, "received %s = %q", k, v)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue