From 3fb940ba5b88f9bc2f4e7d9b7aefce5173ec3f79 Mon Sep 17 00:00:00 2001 From: Graham Steffaniak Date: Fri, 1 Sep 2023 17:35:54 -0500 Subject: [PATCH] new --- backend/.filebrowser.json | 8 -------- backend/auth/hook.go | 3 --- backend/auth/json.go | 3 --- backend/cmd/config.go | 12 ++++-------- backend/cmd/config_import.go | 13 +++++++------ backend/cmd/root.go | 21 ++++++--------------- backend/filebrowser.yml | 4 ++-- backend/http/static.go | 4 ++-- backend/settings/config.go | 20 +++++++++++++------- backend/storage/bolt/auth.go | 13 +++++++------ backend/storage/bolt/importer/conf.go | 10 +++++----- 11 files changed, 46 insertions(+), 65 deletions(-) delete mode 100644 backend/.filebrowser.json diff --git a/backend/.filebrowser.json b/backend/.filebrowser.json deleted file mode 100644 index 40e89fbb..00000000 --- a/backend/.filebrowser.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "port": 5555, - "baseURL": "", - "address": "", - "log": "stdout", - "database": "./database.db", - "root": "/srv" -} \ No newline at end of file diff --git a/backend/auth/hook.go b/backend/auth/hook.go index 7e165f07..00b2a625 100644 --- a/backend/auth/hook.go +++ b/backend/auth/hook.go @@ -15,9 +15,6 @@ import ( "github.com/gtsteffaniak/filebrowser/users" ) -// MethodHookAuth is used to identify hook auth. -const MethodHookAuth = "hook" - type hookCred struct { Password string `json:"password"` Username string `json:"username"` diff --git a/backend/auth/json.go b/backend/auth/json.go index bcd8af52..ce7143de 100644 --- a/backend/auth/json.go +++ b/backend/auth/json.go @@ -11,9 +11,6 @@ import ( "github.com/gtsteffaniak/filebrowser/users" ) -// MethodJSONAuth is used to identify json auth. -const MethodJSONAuth = "json" - type jsonCred struct { Password string `json:"password"` Username string `json:"username"` diff --git a/backend/cmd/config.go b/backend/cmd/config.go index a8d9211d..21d69d2a 100644 --- a/backend/cmd/config.go +++ b/backend/cmd/config.go @@ -32,10 +32,6 @@ func addConfigFlags(flags *pflag.FlagSet) { flags.BoolP("signup", "s", false, "allow users to signup") flags.String("shell", "", "shell command to which other commands should be appended") - flags.String("auth.method", string(auth.MethodJSONAuth), "authentication type") - flags.String("auth.header", "", "HTTP header for auth.method=proxy") - flags.String("auth.command", "", "command for auth.method=hook") - flags.String("recaptcha.host", "https://www.google.com", "use another host for ReCAPTCHA. recaptcha.net might be useful in China") flags.String("recaptcha.key", "", "ReCaptcha site key") flags.String("recaptcha.secret", "", "ReCaptcha secret") @@ -52,7 +48,7 @@ func getAuthentication() (string, auth.Auther) { method := settings.GlobalConfiguration.Auth.Method var defaultAuther map[string]interface{} var auther auth.Auther - if method == auth.MethodProxyAuth { + if method == "proxy" { header := settings.GlobalConfiguration.Auth.Header if header == "" { header = defaultAuther["header"].(string) @@ -65,11 +61,11 @@ func getAuthentication() (string, auth.Auther) { auther = &auth.ProxyAuth{Header: header} } - if method == auth.MethodNoAuth { + if method == "noauth" { auther = &auth.NoAuth{} } - if method == auth.MethodJSONAuth { + if method == "password" { jsonAuth := &auth.JSONAuth{} host := settings.GlobalConfiguration.Auth.Recaptcha.Host key := settings.GlobalConfiguration.Auth.Recaptcha.Key @@ -97,7 +93,7 @@ func getAuthentication() (string, auth.Auther) { auther = jsonAuth } - if method == auth.MethodHookAuth { + if method == "hook" { command := settings.GlobalConfiguration.Auth.Command if command == "" { diff --git a/backend/cmd/config_import.go b/backend/cmd/config_import.go index 800b0512..08ff806d 100644 --- a/backend/cmd/config_import.go +++ b/backend/cmd/config_import.go @@ -5,6 +5,7 @@ import ( "errors" "path/filepath" "reflect" + "log" "github.com/spf13/cobra" @@ -46,7 +47,7 @@ The path must be for a json or yaml file.`, file := settingsFile{} err := unmarshal(args[0], &file) checkErr(err) - + log.Println(file.Settings) file.Settings.Key = key err = d.store.Settings.Save(file.Settings) checkErr(err) @@ -60,16 +61,16 @@ The path must be for a json or yaml file.`, } else { rawAuther = file.Auther } - + log.Println("config_import",file.Settings.Auth) var auther auth.Auther switch file.Settings.Auth.Method { - case auth.MethodJSONAuth: + case "password": auther = getAuther(auth.JSONAuth{}, rawAuther).(*auth.JSONAuth) - case auth.MethodNoAuth: + case "noauth": auther = getAuther(auth.NoAuth{}, rawAuther).(*auth.NoAuth) - case auth.MethodProxyAuth: + case "proxy": auther = getAuther(auth.ProxyAuth{}, rawAuther).(*auth.ProxyAuth) - case auth.MethodHookAuth: + case "hook": auther = getAuther(&auth.HookAuth{}, rawAuther).(*auth.HookAuth) default: checkErr(errors.New("invalid auth method")) diff --git a/backend/cmd/root.go b/backend/cmd/root.go index ca31422d..79e16d8e 100644 --- a/backend/cmd/root.go +++ b/backend/cmd/root.go @@ -19,7 +19,6 @@ import ( v "github.com/spf13/viper" lumberjack "gopkg.in/natefinch/lumberjack.v2" - "github.com/gtsteffaniak/filebrowser/auth" "github.com/gtsteffaniak/filebrowser/diskcache" fbhttp "github.com/gtsteffaniak/filebrowser/http" "github.com/gtsteffaniak/filebrowser/img" @@ -51,8 +50,6 @@ func init() { persistent := rootCmd.PersistentFlags() 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\")") } @@ -115,7 +112,7 @@ user created with the credentials from options "username" and "password".`, checkErr(err) var listener net.Listener - address := serverConfig.Address+":"+strconv.Itoa(serverConfig.Port) + address := serverConfig.Address + ":" + strconv.Itoa(serverConfig.Port) switch { case serverConfig.Socket != "": @@ -238,20 +235,14 @@ func quickSetup(flags *pflag.FlagSet, d pythonData) { Download: true, }, }, - Frontend: settings.Frontend{}, - Commands: nil, - Shell: nil, - Rules: nil, + Frontend: settings.Frontend{}, + Commands: nil, + Shell: nil, + Rules: nil, } var err error - if settings.GlobalConfiguration.Auth.Method == "noAuth" { - set.Auth.Method = "noAuth" - err = d.store.Auth.Save(&auth.NoAuth{}) - }else{ - set.Auth.Method = auth.MethodJSONAuth - err = d.store.Auth.Save(&auth.JSONAuth{}) - } + set.Auth.Method = settings.GlobalConfiguration.Auth.Method err = d.store.Settings.Save(set) checkErr(err) diff --git a/backend/filebrowser.yml b/backend/filebrowser.yml index ba1f1d57..4c1631f7 100644 --- a/backend/filebrowser.yml +++ b/backend/filebrowser.yml @@ -1,6 +1,6 @@ server: port: 8050 - baseURL: /files + baseURL: / address: '' log: stdout database: ./database.db @@ -12,7 +12,7 @@ general: disable-type-detection-by-header: false auth: header: '' - method: 'noauth' + method: noauth command: '' signup: false shell: '' diff --git a/backend/http/static.go b/backend/http/static.go index 8b7ac8c8..b0e1685e 100644 --- a/backend/http/static.go +++ b/backend/http/static.go @@ -35,7 +35,7 @@ func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, fSys "Version": version.Version, "StaticURL": path.Join(d.server.BaseURL, "/static"), "Signup": d.settings.Signup, - "NoAuth": d.settings.Auth.Method == auth.MethodNoAuth, + "NoAuth": d.settings.Auth.Method == "noauth", "AuthMethod": d.settings.Auth.Method, "LoginPage": auther.LoginPage(), "CSS": false, @@ -59,7 +59,7 @@ func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, fSys } } - if d.settings.Auth.Method == auth.MethodJSONAuth { + if d.settings.Auth.Method == "password" { raw, err := d.store.Auth.Get(d.settings.Auth.Method) //nolint:govet if err != nil { return http.StatusInternalServerError, err diff --git a/backend/settings/config.go b/backend/settings/config.go index 4f093c05..ac46b743 100644 --- a/backend/settings/config.go +++ b/backend/settings/config.go @@ -22,12 +22,17 @@ func Initialize() { if err != nil { log.Fatalf("Error reading YAML data: %v", err) } + log.Println(GlobalConfiguration) setDefaults() + log.Println(GlobalConfiguration) + // Unmarshal the YAML data into the Settings struct err = yaml.Unmarshal(yamlData, &GlobalConfiguration) if err != nil { log.Fatalf("Error unmarshaling YAML data: %v", err) } + log.Println(GlobalConfiguration) + // Now you have the Settings struct with values from the YAML file // You can access the values like: defaultSettings.Key, defaultSettings.Server.Port, etc. } @@ -35,16 +40,17 @@ func Initialize() { func setDefaults() { GlobalConfiguration = Settings{ Signup: true, - Server: Server{ - IndexingInterval: 5, - Port: 8080, + Server: Server{ + IndexingInterval: 5, + Port: 8080, NumImageProcessors: 1, - BaseURL: "/files", - }, + BaseURL: "/", + }, Auth: Auth{ + Method: "noauth", Recaptcha: Recaptcha{ Host: "", }, }, - } -} \ No newline at end of file + } +} diff --git a/backend/storage/bolt/auth.go b/backend/storage/bolt/auth.go index abba4e1d..79fa9133 100644 --- a/backend/storage/bolt/auth.go +++ b/backend/storage/bolt/auth.go @@ -1,8 +1,9 @@ package bolt import ( - "github.com/asdine/storm/v3" + "fmt" + "github.com/asdine/storm/v3" "github.com/gtsteffaniak/filebrowser/auth" "github.com/gtsteffaniak/filebrowser/errors" ) @@ -13,15 +14,15 @@ type authBackend struct { func (s authBackend) Get(t string) (auth.Auther, error) { var auther auth.Auther - + fmt.Println("auth.go", t) switch t { - case auth.MethodJSONAuth: + case "password": auther = &auth.JSONAuth{} - case auth.MethodProxyAuth: + case "proxy": auther = &auth.ProxyAuth{} - case auth.MethodHookAuth: + case "hook": auther = &auth.HookAuth{} - case auth.MethodNoAuth: + case "noauth": auther = &auth.NoAuth{} default: return nil, errors.ErrInvalidAuthMethod diff --git a/backend/storage/bolt/importer/conf.go b/backend/storage/bolt/importer/conf.go index b40e155c..a6d84a21 100644 --- a/backend/storage/bolt/importer/conf.go +++ b/backend/storage/bolt/importer/conf.go @@ -144,18 +144,18 @@ func importConf(db *storm.DB, path string, sto *storage.Storage) error { Address: cfg.Address, Log: cfg.Log, } - + fmt.Println("config.go", server) var auther auth.Auther switch cfg.Auth.Method { case "proxy": auther = &auth.ProxyAuth{Header: cfg.Auth.Header} - s.Auth.Method = string(auth.MethodProxyAuth) + s.Auth.Method = "proxy" case "hook": auther = &auth.HookAuth{Command: cfg.Auth.Command} - s.Auth.Method = string(auth.MethodHookAuth) + s.Auth.Method = "hoook" case "none": auther = &auth.NoAuth{} - s.Auth.Method = string(auth.MethodNoAuth) + s.Auth.Method = "noauth" default: auther = &auth.JSONAuth{ ReCaptcha: &auth.ReCaptcha{ @@ -164,7 +164,7 @@ func importConf(db *storm.DB, path string, sto *storage.Storage) error { Secret: cfg.ReCaptcha.Secret, }, } - s.Auth.Method = string(auth.MethodJSONAuth) + s.Auth.Method = "password" } err = sto.Auth.Save(auther)