diff --git a/auth/json.go b/auth/json.go index f1d6b2ad..8f5f193e 100644 --- a/auth/json.go +++ b/auth/json.go @@ -26,7 +26,7 @@ type JSONAuth struct { } // Auth authenticates the user via a json in content body. -func (a *JSONAuth) Auth(r *http.Request, sto *users.Storage, root string) (*users.User, error) { +func (a JSONAuth) Auth(r *http.Request, sto *users.Storage, root string) (*users.User, error) { var cred jsonCred if r.Body == nil { diff --git a/auth/none.go b/auth/none.go index 0279c4e8..f10d065a 100644 --- a/auth/none.go +++ b/auth/none.go @@ -14,6 +14,6 @@ const MethodNoAuth settings.AuthMethod = "noauth" type NoAuth struct{} // 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) } diff --git a/auth/proxy.go b/auth/proxy.go index dc73fb5b..09a16c02 100644 --- a/auth/proxy.go +++ b/auth/proxy.go @@ -18,7 +18,7 @@ type ProxyAuth struct { } // Auth authenticates the user via an HTTP header. -func (a *ProxyAuth) Auth(r *http.Request, sto *users.Storage, root string) (*users.User, error) { +func (a ProxyAuth) Auth(r *http.Request, sto *users.Storage, root string) (*users.User, error) { username := r.Header.Get(a.Header) user, err := sto.Get(root, username) if err == errors.ErrNotExist { diff --git a/cmd/config_export.go b/cmd/config_export.go index 06645059..9526c9e6 100644 --- a/cmd/config_export.go +++ b/cmd/config_export.go @@ -12,16 +12,26 @@ func init() { } var configExportCmd = &cobra.Command{ - Use: "export", - Short: "Export the config.", - Args: cobra.NoArgs, + Use: "export ", + Short: "Export the configuration to a file.", + Args: cobra.ExactArgs(1), Run: python(func(cmd *cobra.Command, args []string, d pythonData) { settings, err := d.store.Settings.Get() checkErr(err) - encoder := json.NewEncoder(os.Stdout) + auther, err := d.store.Auth.Get(settings.AuthMethod) + checkErr(err) + + fd, err := os.Create(args[0]) + checkErr(err) + defer fd.Close() + + encoder := json.NewEncoder(fd) encoder.SetIndent("", " ") - encoder.Encode(settings) + encoder.Encode(&settingsFile{ + Settings: settings, + Auther: auther, + }) }, pythonConfig{}), } diff --git a/cmd/config_import.go b/cmd/config_import.go new file mode 100644 index 00000000..48be4c3d --- /dev/null +++ b/cmd/config_import.go @@ -0,0 +1,81 @@ +package cmd + +import ( + "encoding/json" + "errors" + "os" + "reflect" + + "github.com/filebrowser/filebrowser/v2/auth" + "github.com/filebrowser/filebrowser/v2/settings" + "github.com/spf13/cobra" +) + +func init() { + configCmd.AddCommand(configImportCmd) +} + +type settingsFile struct { + Settings *settings.Settings `json:"settings"` + Auther interface{} `json:"auther"` +} + +var configImportCmd = &cobra.Command{ + Use: "import ", + Short: `Import a configuration file. This will replace all the existing +configuration. Can be used with or without unexisting databases. +If used with a nonexisting database, a key will be generated +automatically. Otherwise the key will be kept the same as in the +database.`, + Args: cobra.ExactArgs(1), + Run: python(func(cmd *cobra.Command, args []string, d pythonData) { + var key []byte + if d.hadDB { + settings, err := d.store.Settings.Get() + checkErr(err) + key = settings.Key + } else { + key = generateRandomBytes(64) + } + + fd, err := os.Open(args[0]) + checkErr(err) + defer fd.Close() + + file := settingsFile{} + err = json.NewDecoder(fd).Decode(&file) + checkErr(err) + + file.Settings.Key = key + + err = d.store.Settings.Save(file.Settings) + checkErr(err) + + var auther auth.Auther + switch file.Settings.AuthMethod { + case auth.MethodJSONAuth: + auther = getAuther(auth.JSONAuth{}, file.Auther).(*auth.JSONAuth) + case auth.MethodNoAuth: + auther = getAuther(auth.NoAuth{}, file.Auther).(*auth.NoAuth) + case auth.MethodProxyAuth: + auther = getAuther(auth.ProxyAuth{}, file.Auther).(*auth.ProxyAuth) + default: + checkErr(errors.New("invalid auth method")) + } + + err = d.store.Auth.Save(auther) + checkErr(err) + printSettings(file.Settings, auther) + }, pythonConfig{allowNoDB: true}), +} + +func getAuther(sample auth.Auther, data interface{}) interface{} { + authType := reflect.TypeOf(sample) + auther := reflect.New(authType).Interface() + + bytes, err := json.Marshal(data) + checkErr(err) + err = json.Unmarshal(bytes, &auther) + + return auther +}