rcd: auto-login for web-gui

rcd: auto use authentication if none is provided for web-gui
This commit is contained in:
Chaitanya 2019-08-20 23:55:04 +05:30 committed by Nick Craig-Wood
parent 5d6593de4f
commit efd826ad4b
2 changed files with 26 additions and 1 deletions

View file

@ -17,6 +17,7 @@ import (
"github.com/rclone/rclone/fs/rc/rcflags" "github.com/rclone/rclone/fs/rc/rcflags"
"github.com/rclone/rclone/fs/rc/rcserver" "github.com/rclone/rclone/fs/rc/rcserver"
"github.com/rclone/rclone/lib/errors" "github.com/rclone/rclone/lib/errors"
"github.com/rclone/rclone/lib/random"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -54,6 +55,20 @@ See the [rc documentation](/rc/) for more info on the rc flags.
if err := checkRelease(rcflags.Opt.WebGUIUpdate); err != nil { if err := checkRelease(rcflags.Opt.WebGUIUpdate); err != nil {
log.Fatalf("Error while fetching the latest release of rclone-webui-react %v", err) log.Fatalf("Error while fetching the latest release of rclone-webui-react %v", err)
} }
if rcflags.Opt.NoAuth {
rcflags.Opt.NoAuth = false
fs.Infof(nil, "Cannot run web-gui without authentication, using default auth")
}
if rcflags.Opt.HTTPOptions.BasicUser == "" {
rcflags.Opt.HTTPOptions.BasicUser = "gui"
fs.Infof("Using default username: %s \n", rcflags.Opt.HTTPOptions.BasicUser)
}
if rcflags.Opt.HTTPOptions.BasicPass == "" {
randomPass := random.String(16)
rcflags.Opt.HTTPOptions.BasicPass = randomPass
fs.Infof("No password specified. Using random password: %s \n", randomPass)
}
rcflags.Opt.Serve = true
} }
s, err := rcserver.Start(&rcflags.Opt) s, err := rcserver.Start(&rcflags.Opt)

View file

@ -2,6 +2,7 @@
package rcserver package rcserver
import ( import (
"encoding/base64"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
@ -89,8 +90,17 @@ func (s *Server) Serve() error {
} }
// Add username, password into the URL if they are set // Add username, password into the URL if they are set
user, pass := s.opt.HTTPOptions.BasicUser, s.opt.HTTPOptions.BasicPass user, pass := s.opt.HTTPOptions.BasicUser, s.opt.HTTPOptions.BasicPass
if user != "" || pass != "" { if user != "" && pass != "" {
openURL.User = url.UserPassword(user, pass) openURL.User = url.UserPassword(user, pass)
// Base64 encode username and password to be sent through url
loginToken := user + ":" + pass
parameters := url.Values{}
encodedToken := base64.URLEncoding.EncodeToString([]byte(loginToken))
fs.Debugf(nil, "login_token %q", encodedToken)
parameters.Add("login_token", encodedToken)
openURL.RawQuery = parameters.Encode()
openURL.RawPath = "/#/login"
} }
// Don't open browser if serving in testing environment. // Don't open browser if serving in testing environment.
if flag.Lookup("test.v") == nil { if flag.Lookup("test.v") == nil {