1
mirror of https://github.com/rclone/rclone synced 2025-03-10 13:54:31 +01:00

lib/oauthutil: add configurable bind address for callback server

Add new config option auth_bind_addr to allow users to configure the bind address for the OAuth callback server.
This commit is contained in:
You Puhe 2024-12-19 11:25:31 +08:00
parent 0ce2e12d9f
commit ee57baae3c
No known key found for this signature in database
GPG Key ID: 1FC4F5C31B337487
2 changed files with 19 additions and 4 deletions

View File

@ -49,6 +49,9 @@ const (
// ConfigClientCredentials - use OAUTH2 client credentials
ConfigClientCredentials = "client_credentials"
// ConfigAuthServerBindAddress is the local bind address for OAuth callback server
ConfigAuthServerBindAddress = "auth_bind_addr"
// ConfigEncoding is the config key to change the encoding for a backend
ConfigEncoding = "encoding"

View File

@ -41,11 +41,8 @@ const (
// bindPort is the port that we bind the local webserver to
bindPort = "53682"
// bindAddress is binding for local webserver when active
bindAddress = "127.0.0.1:" + bindPort
// RedirectURL is redirect to local webserver when active
RedirectURL = "http://" + bindAddress + "/"
RedirectURL = "http://127.0.0.1:" + bindPort + "/"
// RedirectPublicURL is redirect to local webserver when active with public name
RedirectPublicURL = "http://localhost.rclone.org:" + bindPort + "/"
@ -157,6 +154,11 @@ var SharedOptions = []fs.Option{{
Default: false,
Help: "Use client credentials OAuth flow.\n\nThis will use the OAUTH2 client Credentials Flow as described in RFC 6749.",
Advanced: true,
}, {
Name: config.ConfigAuthServerBindAddress,
Default: "127.0.0.1",
Help: "Local bind address for OAuth callback server.\n\nLeave blank to use the default of 127.0.0.1",
Advanced: true,
}}
// oldToken contains an end-user's tokens.
@ -772,6 +774,15 @@ func noWebserverNeeded(oauthConfig *Config) bool {
return oauthConfig.RedirectURL == TitleBarRedirectURL
}
// get the bind address for the OAuth callback server
func getBindAddress(m configmap.Mapper) string {
bindAddr, ok := m.Get("auth_bind_addr")
if !ok || bindAddr == "" {
bindAddr = "127.0.0.1"
}
return bindAddr + ":" + bindPort
}
// get the URL we need to send the user to
func getAuthURL(name string, m configmap.Mapper, oauthConfig *Config, opt *Options) (authURL string, state string, err error) {
oauthConfig, _ = OverrideCredentials(name, m, oauthConfig)
@ -846,6 +857,7 @@ func configSetup(ctx context.Context, id, name string, m configmap.Mapper, oauth
}
// Prepare webserver
bindAddress := getBindAddress(m)
server := newAuthServer(opt, bindAddress, state, authURL)
err = server.Init()
if err != nil {