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

pull/623/head
Henrique Dias 2019-01-08 18:43:04 +00:00 committed by GitHub
parent d0fd97d943
commit e0a3ce95f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 22 deletions

View File

@ -15,5 +15,5 @@ type NoAuth struct{}
// Auth uses authenticates user 1. // Auth uses authenticates user 1.
func (a NoAuth) Auth(r *http.Request, sto *users.Storage, root string) (*users.User, error) { 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.StringVarP(&cfgFile, "config", "c", "", "config file path")
persistent.StringP("database", "d", "./filebrowser.db", "database 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("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\")") 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") 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. // 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 // 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 // if a flag is binded. Our alternative way is to manually check
// the flag and then the value from env/config/gotten by viper. // the flag and then the value from env/config/gotten by viper.
// https://github.com/spf13/viper/pull/331 // https://github.com/spf13/viper/pull/331
func getStringViperFlag(flags *pflag.FlagSet, key string) (string, bool) { func getStringViperFlag(flags *pflag.FlagSet, key string) (string, bool) {
value := "" value, _ := flags.GetString(key)
set := false
// If set on Flags, use it. // If set on Flags, use it.
flags.Visit(func(flag *pflag.Flag) { if isFlagSet(flags, key) {
if flag.Name == key { return value, true
set = true
value, _ = flags.GetString(key)
}
})
if set {
return value, set
} }
// If set through viper (env, config), return it. // 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. // Otherwise use default value on flags.
value, _ = flags.GetString(key)
return value, false return value, false
} }
@ -217,14 +219,12 @@ func setupLog(logMethod string) {
MaxBackups: 10, MaxBackups: 10,
}) })
} }
} }
func quickSetup(flags *pflag.FlagSet, d pythonData) { func quickSetup(flags *pflag.FlagSet, d pythonData) {
set := &settings.Settings{ set := &settings.Settings{
Key: generateRandomBytes(64), // 256 bit Key: generateRandomBytes(64), // 256 bit
Signup: false, Signup: false,
AuthMethod: auth.MethodJSONAuth,
Defaults: settings.UserDefaults{ Defaults: settings.UserDefaults{
Scope: ".", Scope: ".",
Locale: "en", 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{ ser := &settings.Server{
BaseURL: mustGetStringViperFlag(flags, "baseurl"), BaseURL: mustGetStringViperFlag(flags, "baseurl"),
Port: mustGetStringViperFlag(flags, "port"), Port: mustGetStringViperFlag(flags, "port"),
@ -251,15 +270,9 @@ func quickSetup(flags *pflag.FlagSet, d pythonData) {
Root: mustGetStringViperFlag(flags, "root"), Root: mustGetStringViperFlag(flags, "root"),
} }
err := d.store.Settings.Save(set)
checkErr(err)
err = d.store.Settings.SaveServer(ser) err = d.store.Settings.SaveServer(ser)
checkErr(err) checkErr(err)
err = d.store.Auth.Save(&auth.JSONAuth{})
checkErr(err)
username := mustGetStringViperFlag(flags, "username") username := mustGetStringViperFlag(flags, "username")
password := mustGetStringViperFlag(flags, "password") password := mustGetStringViperFlag(flags, "password")

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