From 6e48a6b512e059d3a8eff604f964e18b76b59fde Mon Sep 17 00:00:00 2001 From: cnone Date: Sun, 19 May 2019 17:13:34 +0300 Subject: [PATCH 1/6] Make --auth.method parameter optional Fixes: #715 --- cmd/config.go | 5 ++++- cmd/config_set.go | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/config.go b/cmd/config.go index ce8ebdb7..3812d692 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -44,8 +44,11 @@ func addConfigFlags(flags *pflag.FlagSet) { 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 ...*settings.Settings) (settings.AuthMethod, auth.Auther) { method := settings.AuthMethod(mustGetString(flags, "auth.method")) + if len(defaults) > 0 { + method = defaults[0].AuthMethod + } var auther auth.Auther if method == auth.MethodProxyAuth { diff --git a/cmd/config_set.go b/cmd/config_set.go index 5b417140..e1ffb6e5 100644 --- a/cmd/config_set.go +++ b/cmd/config_set.go @@ -71,6 +71,10 @@ you want to change. Other options will remain unchanged.`, } 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, set) + err = d.store.Auth.Save(auther) + checkErr(err) } err = d.store.Settings.Save(set) From 748e4acfb699ab9fd207e9898e28c7ca59a344fd Mon Sep 17 00:00:00 2001 From: cnone Date: Sun, 19 May 2019 17:31:25 +0300 Subject: [PATCH 2/6] Fix empty json auth parameter bug --- cmd/config.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 3812d692..59ad9bfd 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -70,12 +70,14 @@ func getAuthentication(flags *pflag.FlagSet, defaults ...*settings.Settings) (se key := mustGetString(flags, "recaptcha.key") secret := mustGetString(flags, "recaptcha.secret") - if key != "" && secret != "" { - jsonAuth.ReCaptcha = &auth.ReCaptcha{ - Host: host, - Key: key, - Secret: secret, - } + if key == "" || secret == "" { + panic(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 From aabf0843abda6752569ac7ee7b692526db60fd55 Mon Sep 17 00:00:00 2001 From: cnone Date: Sun, 19 May 2019 22:03:26 +0300 Subject: [PATCH 3/6] Make auth parameters optional --- cmd/config.go | 30 +++++++++++++++++++++++++++--- cmd/config_set.go | 2 +- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 59ad9bfd..3a61fdbe 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -44,18 +44,32 @@ func addConfigFlags(flags *pflag.FlagSet) { flags.Bool("branding.disableExternal", false, "disable external links such as GitHub links") } -func getAuthentication(flags *pflag.FlagSet, defaults ...*settings.Settings) (settings.AuthMethod, auth.Auther) { +func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) { method := settings.AuthMethod(mustGetString(flags, "auth.method")) - if len(defaults) > 0 { - method = defaults[0].AuthMethod + + var defaultAuther map[string]interface{} + for _, arg := range defaults { + switch def := arg.(type) { + case *settings.Settings: + method = settings.AuthMethod(def.AuthMethod) + case auth.Auther: + ms, _ := json.Marshal(def) + json.Unmarshal(ms, &defaultAuther) + } } var auther auth.Auther if method == auth.MethodProxyAuth { header := mustGetString(flags, "auth.header") + + if header == "" { + header = defaultAuther["header"].(string) + } + if header == "" { panic(nerrors.New("you must set the flag 'auth.header' for method 'proxy'")) } + auther = &auth.ProxyAuth{Header: header} } @@ -70,6 +84,16 @@ func getAuthentication(flags *pflag.FlagSet, defaults ...*settings.Settings) (se key := mustGetString(flags, "recaptcha.key") secret := mustGetString(flags, "recaptcha.secret") + if key == "" { + kmap := defaultAuther["recaptcha"].(map[string]interface{}) + key = kmap["key"].(string) + } + + if secret == "" { + smap := defaultAuther["recaptcha"].(map[string]interface{}) + secret = smap["secret"].(string) + } + if key == "" || secret == "" { panic(nerrors.New("you must set the flag 'recaptcha.key' and 'recaptcha.secret' for method 'json'")) } diff --git a/cmd/config_set.go b/cmd/config_set.go index e1ffb6e5..ffce2939 100644 --- a/cmd/config_set.go +++ b/cmd/config_set.go @@ -72,7 +72,7 @@ you want to change. Other options will remain unchanged.`, 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, set) + set.AuthMethod, auther = getAuthentication(flags, set, auther) err = d.store.Auth.Save(auther) checkErr(err) } From c3a4e33245aa71d286f230f424e550f3ed3789fc Mon Sep 17 00:00:00 2001 From: cnone Date: Sun, 19 May 2019 22:05:42 +0300 Subject: [PATCH 4/6] Fix linter issue --- cmd/config.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 3a61fdbe..0d0cc324 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -46,7 +46,6 @@ func addConfigFlags(flags *pflag.FlagSet) { func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) { method := settings.AuthMethod(mustGetString(flags, "auth.method")) - var defaultAuther map[string]interface{} for _, arg := range defaults { switch def := arg.(type) { @@ -79,7 +78,6 @@ func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings. if method == auth.MethodJSONAuth { jsonAuth := &auth.JSONAuth{} - host := mustGetString(flags, "recaptcha.host") key := mustGetString(flags, "recaptcha.key") secret := mustGetString(flags, "recaptcha.secret") From 030f6607f01718e3d7535aa85f1beb3279bd32ad Mon Sep 17 00:00:00 2001 From: cnone Date: Mon, 20 May 2019 04:55:36 +0300 Subject: [PATCH 5/6] Refactor the code --- cmd/config.go | 20 +++++++++++++------- cmd/config_set.go | 22 ++++++++-------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 0d0cc324..932c1229 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -46,14 +46,20 @@ func addConfigFlags(flags *pflag.FlagSet) { func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) { method := settings.AuthMethod(mustGetString(flags, "auth.method")) + var defaultAuther map[string]interface{} - for _, arg := range defaults { - switch def := arg.(type) { - case *settings.Settings: - method = settings.AuthMethod(def.AuthMethod) - case auth.Auther: - ms, _ := json.Marshal(def) - json.Unmarshal(ms, &defaultAuther) + 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) + } + } } } diff --git a/cmd/config_set.go b/cmd/config_set.go index ffce2939..9b49b234 100644 --- a/cmd/config_set.go +++ b/cmd/config_set.go @@ -3,7 +3,6 @@ package cmd import ( "strings" - "github.com/filebrowser/filebrowser/v2/auth" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -63,20 +62,15 @@ you want to change. Other options will remain unchanged.`, getUserDefaults(flags, &set.Defaults, false) - var auther auth.Auther - if hasAuth { - set.AuthMethod, auther = getAuthentication(flags) - 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, set, auther) - err = d.store.Auth.Save(auther) - checkErr(err) - } + // read the defaults + 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) checkErr(err) err = d.store.Settings.SaveServer(ser) From ce78299464a299c2c467b992c751551b80738d06 Mon Sep 17 00:00:00 2001 From: cnone Date: Mon, 20 May 2019 19:20:41 +0300 Subject: [PATCH 6/6] Fix panic with checkerr --- cmd/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 932c1229..888a3fa6 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -72,7 +72,7 @@ func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings. } if header == "" { - panic(nerrors.New("you must set the flag 'auth.header' for method 'proxy'")) + checkErr(nerrors.New("you must set the flag 'auth.header' for method 'proxy'")) } auther = &auth.ProxyAuth{Header: header} @@ -99,7 +99,7 @@ func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings. } if key == "" || secret == "" { - panic(nerrors.New("you must set the flag 'recaptcha.key' and 'recaptcha.secret' for method 'json'")) + checkErr(nerrors.New("you must set the flag 'recaptcha.key' and 'recaptcha.secret' for method 'json'")) } jsonAuth.ReCaptcha = &auth.ReCaptcha{