feat: add no auth to quick setup (#621)

Former-commit-id: 56d5c323e0f176247779946e04664990b13146e8 [formerly 43ee26a4e1929560135c81ef2d1bfb60f6d15815] [formerly db983bfd8d1e9cea1e749b02ac11d933af05624b [formerly e0a3ce95f2]]
Former-commit-id: 20d14a1193271ef23c090dab1b3828ba7655b687 [formerly bdeb651589e0ca20ff669bfeb85ae0358e8e1d95]
Former-commit-id: 1f00d921e2a7e60f3a290b11603acc6a67a874a2
This commit is contained in:
Henrique Dias 2019-01-08 18:43:04 +00:00 committed by GitHub
parent 9a54abfe62
commit cc428d3cd6
3 changed files with 35 additions and 22 deletions

View File

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

View File

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

@ -1 +1 @@
Subproject commit 0e7d4ef110ee550375d4bf15dfa9ded70214076a
Subproject commit 2ed87febcb71c71588f53d5b6ed560cd5c99f526