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.
This commit is contained in:
Xiaoxing Ye 2019-10-27 03:19:22 +08:00 committed by Nick Craig-Wood
parent 1ce1ea34aa
commit 520ddbcceb
4 changed files with 32 additions and 7 deletions

View File

@ -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)
},
}

View File

@ -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.

View File

@ -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)
}

View File

@ -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