From 8da3df2d8b3ee9850fb71c7bb5ac611ac349d655 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 8 Jan 2019 18:02:18 +0000 Subject: [PATCH] feat: add noauth to quick setup License: MIT Signed-off-by: Henrique Dias --- auth/none.go | 2 +- cmd/root.go | 52 +++++++++++++++++++++++++++++++++------------------- frontend | 2 +- 3 files changed, 35 insertions(+), 21 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 c9c9f7d1..8e4da165 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -38,6 +38,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\")") @@ -54,25 +55,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. @@ -81,7 +84,6 @@ func getStringViperFlag(flags *pflag.FlagSet, key string) (string, bool) { } // Otherwise use default value on flags. - value, _ = flags.GetString(key) return value, false } @@ -222,7 +224,6 @@ 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", @@ -239,6 +240,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"), @@ -249,15 +269,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