github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/fs/config/authorize.go (about) 1 package config 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/rclone/rclone/fs" 8 "github.com/rclone/rclone/fs/config/configmap" 9 ) 10 11 // Authorize is for remote authorization of headless machines. 12 // 13 // It expects 1, 2 or 3 arguments 14 // 15 // rclone authorize "fs name" 16 // rclone authorize "fs name" "base64 encoded JSON blob" 17 // rclone authorize "fs name" "client id" "client secret" 18 func Authorize(ctx context.Context, args []string, noAutoBrowser bool, templateFile string) error { 19 ctx = suppressConfirm(ctx) 20 ctx = fs.ConfigOAuthOnly(ctx) 21 switch len(args) { 22 case 1, 2, 3: 23 default: 24 return fmt.Errorf("invalid number of arguments: %d", len(args)) 25 } 26 Type := args[0] // FIXME could read this from input 27 ri, err := fs.Find(Type) 28 if err != nil { 29 return err 30 } 31 if ri.Config == nil { 32 return fmt.Errorf("can't authorize fs %q", Type) 33 } 34 35 // Config map for remote 36 inM := configmap.Simple{} 37 38 // Indicate that we are running rclone authorize 39 inM[ConfigAuthorize] = "true" 40 if noAutoBrowser { 41 inM[ConfigAuthNoBrowser] = "true" 42 } 43 44 // Indicate if we specified a custom template via a file 45 if templateFile != "" { 46 inM[ConfigTemplateFile] = templateFile 47 } 48 49 // Add extra parameters if supplied 50 if len(args) == 2 { 51 err := inM.Decode(args[1]) 52 if err != nil { 53 return err 54 } 55 } else if len(args) == 3 { 56 inM[ConfigClientID] = args[1] 57 inM[ConfigClientSecret] = args[2] 58 } 59 60 // Name used for temporary remote 61 name := "**temp-fs**" 62 63 m := fs.ConfigMap(ri, name, inM) 64 outM := configmap.Simple{} 65 m.ClearSetters() 66 m.AddSetter(outM) 67 m.AddGetter(outM, configmap.PriorityNormal) 68 69 err = PostConfig(ctx, name, m, ri) 70 if err != nil { 71 return err 72 } 73 74 // Print the code for the user to paste 75 out := outM["token"] 76 77 // If received a config blob, then return one 78 if len(args) == 2 { 79 out, err = outM.Encode() 80 if err != nil { 81 return err 82 } 83 } 84 fmt.Printf("Paste the following into your remote machine --->\n%s\n<---End paste\n", out) 85 86 return nil 87 }