From e0a3ce95f2ef2e2319848a5b5de7739ec842f0c9 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 8 Jan 2019 18:43:04 +0000 Subject: [PATCH] feat: add no auth to quick setup (#621) --- auth/none.go | 2 +- cmd/root.go | 53 ++++++++++++++++++++++++++++++++-------------------- frontend | 2 +- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/auth/none.go b/auth/none.go index f10d065a..a206157f 100644 --- a/auth/none.go +++ b/auth/none.go @@ -15,5 +15,5 @@ type NoAuth struct{} // Auth uses authenticates user 1. func (a NoAuth) Auth(r *http.Request, sto *users.Storage, root string) (*users.User, error) { - return sto.Get(root, 1) + return sto.Get(root, uint(1)) } diff --git a/cmd/root.go b/cmd/root.go index e33f11ca..de260a5d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -37,6 +37,7 @@ func init() { persistent.StringVarP(&cfgFile, "config", "c", "", "config file path") persistent.StringP("database", "d", "./filebrowser.db", "database path") + flags.Bool("noauth", false, "use the noauth auther when using quick setup") flags.String("username", "admin", "username for the first user when using quick config") flags.String("password", "", "hashed password for the first user when using quick config (default \"admin\")") @@ -53,25 +54,27 @@ func addServerFlags(flags *pflag.FlagSet) { flags.StringP("baseurl", "b", "", "base url") } +func isFlagSet(flags *pflag.FlagSet, key string) bool { + set:= false + flags.Visit(func(flag *pflag.Flag) { + if flag.Name == key { + set = true + } + }) + return set +} + // NOTE: we could simply bind the flags to viper and use IsSet. // Although there is a bug on Viper that always returns true on IsSet // if a flag is binded. Our alternative way is to manually check // the flag and then the value from env/config/gotten by viper. // https://github.com/spf13/viper/pull/331 func getStringViperFlag(flags *pflag.FlagSet, key string) (string, bool) { - value := "" - set := false + value, _ := flags.GetString(key) // If set on Flags, use it. - flags.Visit(func(flag *pflag.Flag) { - if flag.Name == key { - set = true - value, _ = flags.GetString(key) - } - }) - - if set { - return value, set + if isFlagSet(flags, key) { + return value, true } // If set through viper (env, config), return it. @@ -80,7 +83,6 @@ func getStringViperFlag(flags *pflag.FlagSet, key string) (string, bool) { } // Otherwise use default value on flags. - value, _ = flags.GetString(key) return value, false } @@ -217,14 +219,12 @@ func setupLog(logMethod string) { MaxBackups: 10, }) } - } func quickSetup(flags *pflag.FlagSet, d pythonData) { set := &settings.Settings{ Key: generateRandomBytes(64), // 256 bit Signup: false, - AuthMethod: auth.MethodJSONAuth, Defaults: settings.UserDefaults{ Scope: ".", Locale: "en", @@ -241,6 +241,25 @@ func quickSetup(flags *pflag.FlagSet, d pythonData) { }, } + noauth, err := flags.GetBool("noauth") + checkErr(err) + + if !isFlagSet(flags, "noauth") && v.IsSet("noauth") { + noauth = v.GetBool("noauth") + } + + if noauth { + set.AuthMethod = auth.MethodNoAuth + err = d.store.Auth.Save(&auth.NoAuth{}) + } else { + set.AuthMethod = auth.MethodJSONAuth + err = d.store.Auth.Save(&auth.JSONAuth{}) + } + + checkErr(err) + err = d.store.Settings.Save(set) + checkErr(err) + ser := &settings.Server{ BaseURL: mustGetStringViperFlag(flags, "baseurl"), Port: mustGetStringViperFlag(flags, "port"), @@ -251,15 +270,9 @@ func quickSetup(flags *pflag.FlagSet, d pythonData) { Root: mustGetStringViperFlag(flags, "root"), } - err := d.store.Settings.Save(set) - checkErr(err) - err = d.store.Settings.SaveServer(ser) checkErr(err) - err = d.store.Auth.Save(&auth.JSONAuth{}) - checkErr(err) - username := mustGetStringViperFlag(flags, "username") password := mustGetStringViperFlag(flags, "password") diff --git a/frontend b/frontend index 0e7d4ef1..2ed87feb 160000 --- a/frontend +++ b/frontend @@ -1 +1 @@ -Subproject commit 0e7d4ef110ee550375d4bf15dfa9ded70214076a +Subproject commit 2ed87febcb71c71588f53d5b6ed560cd5c99f526