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
|
|
|
|
|
|
|
"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.
|
|
|
|
//
|
2021-04-29 08:28:18 +00:00
|
|
|
// It expects 1, 2 or 3 arguments
|
2021-03-11 16:20:31 +00:00
|
|
|
//
|
2022-08-05 15:35:41 +00:00
|
|
|
// rclone authorize "fs name"
|
|
|
|
// rclone authorize "fs name" "base64 encoded JSON blob"
|
|
|
|
// 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)
|
2021-04-29 08:28:18 +00:00
|
|
|
ctx = fs.ConfigOAuthOnly(ctx)
|
2021-03-11 16:20:31 +00:00
|
|
|
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-11-04 10:12:57 +00:00
|
|
|
return fmt.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 {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.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
|
|
|
|
2021-04-29 08:28:18 +00:00
|
|
|
err = PostConfig(ctx, name, m, ri)
|
2021-04-06 20:27:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-03 14:38:12 +00:00
|
|
|
|
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
|
|
|
}
|