2021-03-12 00:20:31 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-03 22:38:12 +08:00
|
|
|
"fmt"
|
2021-03-12 00:20:31 +08:00
|
|
|
|
2021-03-13 22:54:26 +08:00
|
|
|
"github.com/pkg/errors"
|
2021-03-12 00:20:31 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2021-04-03 22:38:12 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
2021-03-12 00:20:31 +08: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 22:54:26 +08:00
|
|
|
func Authorize(ctx context.Context, args []string, noAutoBrowser bool) error {
|
2021-03-12 00:20:31 +08:00
|
|
|
ctx = suppressConfirm(ctx)
|
|
|
|
switch len(args) {
|
2021-04-04 21:56:42 +08:00
|
|
|
case 1, 2, 3:
|
2021-03-12 00:20:31 +08:00
|
|
|
default:
|
2021-03-13 22:54:26 +08:00
|
|
|
return errors.Errorf("invalid number of arguments: %d", len(args))
|
2021-03-12 00:20:31 +08:00
|
|
|
}
|
2021-04-03 22:38:12 +08: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-12 00:20:31 +08:00
|
|
|
}
|
|
|
|
|
2021-04-03 22:38:12 +08:00
|
|
|
// Config map for remote
|
|
|
|
inM := configmap.Simple{}
|
2021-03-12 00:20:31 +08:00
|
|
|
|
|
|
|
// Indicate that we are running rclone authorize
|
2021-04-03 22:38:12 +08:00
|
|
|
inM[ConfigAuthorize] = "true"
|
2021-03-12 00:20:31 +08:00
|
|
|
if noAutoBrowser {
|
2021-04-03 22:38:12 +08:00
|
|
|
inM[ConfigAuthNoBrowser] = "true"
|
2021-03-12 00:20:31 +08:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:56:42 +08: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 22:38:12 +08:00
|
|
|
inM[ConfigClientID] = args[1]
|
|
|
|
inM[ConfigClientSecret] = args[2]
|
2021-03-12 00:20:31 +08:00
|
|
|
}
|
|
|
|
|
2021-04-03 22:38:12 +08: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 21:56:42 +08:00
|
|
|
m.AddGetter(outM, configmap.PriorityNormal)
|
2021-04-03 22:38:12 +08:00
|
|
|
|
|
|
|
ri.Config(ctx, name, m)
|
|
|
|
|
2021-04-04 21:56:42 +08:00
|
|
|
// Print the code for the user to paste
|
|
|
|
out := outM["token"]
|
2021-04-03 22:38:12 +08:00
|
|
|
|
2021-04-04 21:56:42 +08: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 22:38:12 +08:00
|
|
|
|
2021-03-13 22:54:26 +08:00
|
|
|
return nil
|
2021-03-12 00:20:31 +08:00
|
|
|
}
|