From 520ddbccebb938729dfbde68e0a51e193fc419b2 Mon Sep 17 00:00:00 2001 From: Xiaoxing Ye Date: Sun, 27 Oct 2019 03:19:22 +0800 Subject: [PATCH] config: do not open browser on headless if google fs On google fs (drive, google photos, and google cloud storage), if headless is selected, do not open browser. This also supplies a new option "auth-no-open-browser" for authorize if the user does not want it. This should fix #3323. --- cmd/authorize/authorize.go | 14 ++++++++++++-- docs/content/commands/rclone_authorize.md | 3 ++- fs/config/config.go | 10 +++++++++- lib/oauthutil/oauthutil.go | 12 +++++++++--- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/cmd/authorize/authorize.go b/cmd/authorize/authorize.go index 076821467..178ca306c 100644 --- a/cmd/authorize/authorize.go +++ b/cmd/authorize/authorize.go @@ -3,11 +3,18 @@ package authorize import ( "github.com/rclone/rclone/cmd" "github.com/rclone/rclone/fs/config" + "github.com/rclone/rclone/fs/config/flags" "github.com/spf13/cobra" ) +var ( + noAutoBrowser bool +) + func init() { cmd.Root.AddCommand(commandDefinition) + cmdFlags := commandDefinition.Flags() + flags.BoolVarP(cmdFlags, &noAutoBrowser, "auth-no-open-browser", "", false, "Do not automatically open auth link in default browser") } var commandDefinition = &cobra.Command{ @@ -16,9 +23,12 @@ var commandDefinition = &cobra.Command{ Long: ` Remote authorization. Used to authorize a remote or headless rclone from a machine with a browser - use as instructed by -rclone config.`, +rclone config. + +Use the --auth-no-open-browser to prevent rclone to open auth +link in default browser automatically.`, Run: func(command *cobra.Command, args []string) { cmd.CheckArgs(1, 3, command, args) - config.Authorize(args) + config.Authorize(args, noAutoBrowser) }, } diff --git a/docs/content/commands/rclone_authorize.md b/docs/content/commands/rclone_authorize.md index f57e3f342..b3176f4b7 100644 --- a/docs/content/commands/rclone_authorize.md +++ b/docs/content/commands/rclone_authorize.md @@ -22,7 +22,8 @@ rclone authorize [flags] ### Options ``` - -h, --help help for authorize + --auth-no-open-browser Do not automatically open auth link in default browser + -h, --help help for authorize ``` See the [global flags page](/flags/) for global options not listed here. diff --git a/fs/config/config.go b/fs/config/config.go index 2832f2b12..2c2cd08f6 100644 --- a/fs/config/config.go +++ b/fs/config/config.go @@ -62,6 +62,9 @@ const ( // ConfigAuthorize indicates that we just want "rclone authorize" ConfigAuthorize = "config_authorize" + + // ConfigAuthNoBrowser indicates that we do not want to open browser + ConfigAuthNoBrowser = "config_auth_no_browser" ) // Global @@ -1299,7 +1302,7 @@ func SetPassword() { // // rclone authorize "fs name" // rclone authorize "fs name" "client id" "client secret" -func Authorize(args []string) { +func Authorize(args []string, noAutoBrowser bool) { defer suppressConfirm()() switch len(args) { case 1, 3: @@ -1319,10 +1322,15 @@ func Authorize(args []string) { // Indicate that we are running rclone authorize getConfigData().SetValue(name, ConfigAuthorize, "true") + if noAutoBrowser { + getConfigData().SetValue(name, ConfigAuthNoBrowser, "true") + } + if len(args) == 3 { getConfigData().SetValue(name, ConfigClientID, args[1]) getConfigData().SetValue(name, ConfigClientSecret, args[2]) } + m := fs.ConfigMap(f, name) f.Config(name, m) } diff --git a/lib/oauthutil/oauthutil.go b/lib/oauthutil/oauthutil.go index fdb480540..a4f5acde8 100644 --- a/lib/oauthutil/oauthutil.go +++ b/lib/oauthutil/oauthutil.go @@ -386,6 +386,8 @@ func doConfig(id, name string, m configmap.Mapper, oauthConfig *oauth2.Config, o oauthConfig, changed := overrideCredentials(name, m, oauthConfig) authorizeOnlyValue, ok := m.Get(config.ConfigAuthorize) authorizeOnly := ok && authorizeOnlyValue != "" // set if being run by "rclone authorize" + authorizeNoAutoBrowserValue, ok := m.Get(config.ConfigAuthNoBrowser) + authorizeNoAutoBrowser := ok && authorizeNoAutoBrowserValue != "" // See if already have a token tokenString, ok := m.Get("token") @@ -470,9 +472,13 @@ func doConfig(id, name string, m configmap.Mapper, oauthConfig *oauth2.Config, o authURL = "http://" + bindAddress + "/auth?state=" + state } - // Open the URL for the user to visit - _ = open.Start(authURL) - fmt.Printf("If your browser doesn't open automatically go to the following link: %s\n", authURL) + if !authorizeNoAutoBrowser && oauthConfig.RedirectURL != TitleBarRedirectURL { + // Open the URL for the user to visit + _ = open.Start(authURL) + fmt.Printf("If your browser doesn't open automatically go to the following link: %s\n", authURL) + } else { + fmt.Printf("Please go to the following link: %s\n", authURL) + } fmt.Printf("Log in and authorize rclone for access\n") // Read the code via the webserver or manually