2021-03-11 16:20:31 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-03 14:38:12 +00:00
|
|
|
"fmt"
|
2021-03-11 16:20:31 +00:00
|
|
|
|
2021-03-13 14:54:26 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-03-11 16:20:31 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2021-04-03 14:38:12 +00:00
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
2021-03-11 16:20:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Authorize is for remote authorization of headless machines.
|
|
|
|
//
|
|
|
|
// It expects 1 or 3 arguments
|
|
|
|
//
|
|
|
|
// rclone authorize "fs name"
|
|
|
|
// rclone authorize "fs name" "client id" "client secret"
|
2021-03-13 14:54:26 +00:00
|
|
|
func Authorize(ctx context.Context, args []string, noAutoBrowser bool) error {
|
2021-03-11 16:20:31 +00:00
|
|
|
ctx = suppressConfirm(ctx)
|
|
|
|
switch len(args) {
|
2021-04-04 13:56:42 +00:00
|
|
|
case 1, 2, 3:
|
2021-03-11 16:20:31 +00:00
|
|
|
default:
|
2021-03-13 14:54:26 +00:00
|
|
|
return errors.Errorf("invalid number of arguments: %d", len(args))
|
2021-03-11 16:20:31 +00:00
|
|
|
}
|
2021-04-03 14:38:12 +00:00
|
|
|
Type := args[0] // FIXME could read this from input
|
|
|
|
ri, err := fs.Find(Type)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ri.Config == nil {
|
|
|
|
return errors.Errorf("can't authorize fs %q", Type)
|
2021-03-11 16:20:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 14:38:12 +00:00
|
|
|
// Config map for remote
|
|
|
|
inM := configmap.Simple{}
|
2021-03-11 16:20:31 +00:00
|
|
|
|
|
|
|
// Indicate that we are running rclone authorize
|
2021-04-03 14:38:12 +00:00
|
|
|
inM[ConfigAuthorize] = "true"
|
2021-03-11 16:20:31 +00:00
|
|
|
if noAutoBrowser {
|
2021-04-03 14:38:12 +00:00
|
|
|
inM[ConfigAuthNoBrowser] = "true"
|
2021-03-11 16:20:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 13:56:42 +00:00
|
|
|
// Add extra parameters if supplied
|
|
|
|
if len(args) == 2 {
|
|
|
|
err := inM.Decode(args[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if len(args) == 3 {
|
2021-04-03 14:38:12 +00:00
|
|
|
inM[ConfigClientID] = args[1]
|
|
|
|
inM[ConfigClientSecret] = args[2]
|
2021-03-11 16:20:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 14:38:12 +00:00
|
|
|
// Name used for temporary remote
|
|
|
|
name := "**temp-fs**"
|
|
|
|
|
|
|
|
m := fs.ConfigMap(ri, name, inM)
|
|
|
|
outM := configmap.Simple{}
|
|
|
|
m.ClearSetters()
|
|
|
|
m.AddSetter(outM)
|
2021-04-04 13:56:42 +00:00
|
|
|
m.AddGetter(outM, configmap.PriorityNormal)
|
2021-04-03 14:38:12 +00:00
|
|
|
|
|
|
|
ri.Config(ctx, name, m)
|
|
|
|
|
2021-04-04 13:56:42 +00:00
|
|
|
// Print the code for the user to paste
|
|
|
|
out := outM["token"]
|
2021-04-03 14:38:12 +00:00
|
|
|
|
2021-04-04 13:56:42 +00:00
|
|
|
// 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)
|
2021-04-03 14:38:12 +00:00
|
|
|
|
2021-03-13 14:54:26 +00:00
|
|
|
return nil
|
2021-03-11 16:20:31 +00:00
|
|
|
}
|