2021-03-11 17:20:31 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-03 16:38:12 +02:00
|
|
|
"fmt"
|
2021-03-11 17:20:31 +01:00
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
2021-04-03 16:38:12 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
2021-03-11 17:20:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Authorize is for remote authorization of headless machines.
|
|
|
|
//
|
2021-04-29 10:28:18 +02:00
|
|
|
// It expects 1, 2 or 3 arguments
|
2021-03-11 17:20:31 +01:00
|
|
|
//
|
2022-08-05 17:35:41 +02:00
|
|
|
// rclone authorize "fs name"
|
|
|
|
// rclone authorize "fs name" "base64 encoded JSON blob"
|
|
|
|
// rclone authorize "fs name" "client id" "client secret"
|
2021-03-13 15:54:26 +01:00
|
|
|
func Authorize(ctx context.Context, args []string, noAutoBrowser bool) error {
|
2021-03-11 17:20:31 +01:00
|
|
|
ctx = suppressConfirm(ctx)
|
2021-04-29 10:28:18 +02:00
|
|
|
ctx = fs.ConfigOAuthOnly(ctx)
|
2021-03-11 17:20:31 +01:00
|
|
|
switch len(args) {
|
2021-04-04 15:56:42 +02:00
|
|
|
case 1, 2, 3:
|
2021-03-11 17:20:31 +01:00
|
|
|
default:
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("invalid number of arguments: %d", len(args))
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
2021-04-03 16:38:12 +02: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 11:12:57 +01:00
|
|
|
return fmt.Errorf("can't authorize fs %q", Type)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
|
2021-04-03 16:38:12 +02:00
|
|
|
// Config map for remote
|
|
|
|
inM := configmap.Simple{}
|
2021-03-11 17:20:31 +01:00
|
|
|
|
|
|
|
// Indicate that we are running rclone authorize
|
2021-04-03 16:38:12 +02:00
|
|
|
inM[ConfigAuthorize] = "true"
|
2021-03-11 17:20:31 +01:00
|
|
|
if noAutoBrowser {
|
2021-04-03 16:38:12 +02:00
|
|
|
inM[ConfigAuthNoBrowser] = "true"
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
|
2021-04-04 15:56:42 +02: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 16:38:12 +02:00
|
|
|
inM[ConfigClientID] = args[1]
|
|
|
|
inM[ConfigClientSecret] = args[2]
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
|
2021-04-03 16:38:12 +02: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 15:56:42 +02:00
|
|
|
m.AddGetter(outM, configmap.PriorityNormal)
|
2021-04-03 16:38:12 +02:00
|
|
|
|
2021-04-29 10:28:18 +02:00
|
|
|
err = PostConfig(ctx, name, m, ri)
|
2021-04-06 22:27:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-03 16:38:12 +02:00
|
|
|
|
2021-04-04 15:56:42 +02:00
|
|
|
// Print the code for the user to paste
|
|
|
|
out := outM["token"]
|
2021-04-03 16:38:12 +02:00
|
|
|
|
2021-04-04 15:56:42 +02: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 16:38:12 +02:00
|
|
|
|
2021-03-13 15:54:26 +01:00
|
|
|
return nil
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|