Make --auth.method parameter optional when changing auth parameters Fixes: #715
This commit is contained in:
commit
abed362dc5
|
@ -44,15 +44,37 @@ func addConfigFlags(flags *pflag.FlagSet) {
|
||||||
flags.Bool("branding.disableExternal", false, "disable external links such as GitHub links")
|
flags.Bool("branding.disableExternal", false, "disable external links such as GitHub links")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAuthentication(flags *pflag.FlagSet) (settings.AuthMethod, auth.Auther) {
|
func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) {
|
||||||
method := settings.AuthMethod(mustGetString(flags, "auth.method"))
|
method := settings.AuthMethod(mustGetString(flags, "auth.method"))
|
||||||
|
|
||||||
|
var defaultAuther map[string]interface{}
|
||||||
|
if len(defaults) > 0 {
|
||||||
|
if hasAuth := defaults[0]; hasAuth != true {
|
||||||
|
for _, arg := range defaults {
|
||||||
|
switch def := arg.(type) {
|
||||||
|
case *settings.Settings:
|
||||||
|
method = settings.AuthMethod(def.AuthMethod)
|
||||||
|
case auth.Auther:
|
||||||
|
ms, err := json.Marshal(def)
|
||||||
|
checkErr(err)
|
||||||
|
json.Unmarshal(ms, &defaultAuther)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var auther auth.Auther
|
var auther auth.Auther
|
||||||
if method == auth.MethodProxyAuth {
|
if method == auth.MethodProxyAuth {
|
||||||
header := mustGetString(flags, "auth.header")
|
header := mustGetString(flags, "auth.header")
|
||||||
|
|
||||||
if header == "" {
|
if header == "" {
|
||||||
panic(nerrors.New("you must set the flag 'auth.header' for method 'proxy'"))
|
header = defaultAuther["header"].(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if header == "" {
|
||||||
|
checkErr(nerrors.New("you must set the flag 'auth.header' for method 'proxy'"))
|
||||||
|
}
|
||||||
|
|
||||||
auther = &auth.ProxyAuth{Header: header}
|
auther = &auth.ProxyAuth{Header: header}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,17 +84,28 @@ func getAuthentication(flags *pflag.FlagSet) (settings.AuthMethod, auth.Auther)
|
||||||
|
|
||||||
if method == auth.MethodJSONAuth {
|
if method == auth.MethodJSONAuth {
|
||||||
jsonAuth := &auth.JSONAuth{}
|
jsonAuth := &auth.JSONAuth{}
|
||||||
|
|
||||||
host := mustGetString(flags, "recaptcha.host")
|
host := mustGetString(flags, "recaptcha.host")
|
||||||
key := mustGetString(flags, "recaptcha.key")
|
key := mustGetString(flags, "recaptcha.key")
|
||||||
secret := mustGetString(flags, "recaptcha.secret")
|
secret := mustGetString(flags, "recaptcha.secret")
|
||||||
|
|
||||||
if key != "" && secret != "" {
|
if key == "" {
|
||||||
jsonAuth.ReCaptcha = &auth.ReCaptcha{
|
kmap := defaultAuther["recaptcha"].(map[string]interface{})
|
||||||
Host: host,
|
key = kmap["key"].(string)
|
||||||
Key: key,
|
}
|
||||||
Secret: secret,
|
|
||||||
}
|
if secret == "" {
|
||||||
|
smap := defaultAuther["recaptcha"].(map[string]interface{})
|
||||||
|
secret = smap["secret"].(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
if key == "" || secret == "" {
|
||||||
|
checkErr(nerrors.New("you must set the flag 'recaptcha.key' and 'recaptcha.secret' for method 'json'"))
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonAuth.ReCaptcha = &auth.ReCaptcha{
|
||||||
|
Host: host,
|
||||||
|
Key: key,
|
||||||
|
Secret: secret,
|
||||||
}
|
}
|
||||||
|
|
||||||
auther = jsonAuth
|
auther = jsonAuth
|
||||||
|
|
|
@ -3,7 +3,6 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/filebrowser/filebrowser/v2/auth"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
@ -63,16 +62,15 @@ you want to change. Other options will remain unchanged.`,
|
||||||
|
|
||||||
getUserDefaults(flags, &set.Defaults, false)
|
getUserDefaults(flags, &set.Defaults, false)
|
||||||
|
|
||||||
var auther auth.Auther
|
// read the defaults
|
||||||
if hasAuth {
|
auther, err := d.store.Auth.Get(set.AuthMethod)
|
||||||
set.AuthMethod, auther = getAuthentication(flags)
|
checkErr(err)
|
||||||
err = d.store.Auth.Save(auther)
|
|
||||||
checkErr(err)
|
|
||||||
} else {
|
|
||||||
auther, err = d.store.Auth.Get(set.AuthMethod)
|
|
||||||
checkErr(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// check if there are new flags for existing auth method
|
||||||
|
set.AuthMethod, auther = getAuthentication(flags, hasAuth, set, auther)
|
||||||
|
|
||||||
|
err = d.store.Auth.Save(auther)
|
||||||
|
checkErr(err)
|
||||||
err = d.store.Settings.Save(set)
|
err = d.store.Settings.Save(set)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
err = d.store.Settings.SaveServer(ser)
|
err = d.store.Settings.SaveServer(ser)
|
||||||
|
|
Loading…
Reference in New Issue