feat: config import
License: MIT Signed-off-by: Henrique Dias <hacdias@gmail.com>
This commit is contained in:
parent
2bcbf69b43
commit
726ecb0812
@ -26,7 +26,7 @@ type JSONAuth struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Auth authenticates the user via a json in content body.
|
// 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
|
var cred jsonCred
|
||||||
|
|
||||||
if r.Body == nil {
|
if r.Body == nil {
|
||||||
|
|||||||
@ -14,6 +14,6 @@ const MethodNoAuth settings.AuthMethod = "noauth"
|
|||||||
type NoAuth struct{}
|
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, 1)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ type ProxyAuth struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Auth authenticates the user via an HTTP header.
|
// 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)
|
username := r.Header.Get(a.Header)
|
||||||
user, err := sto.Get(root, username)
|
user, err := sto.Get(root, username)
|
||||||
if err == errors.ErrNotExist {
|
if err == errors.ErrNotExist {
|
||||||
|
|||||||
@ -12,16 +12,26 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var configExportCmd = &cobra.Command{
|
var configExportCmd = &cobra.Command{
|
||||||
Use: "export",
|
Use: "export <filename>",
|
||||||
Short: "Export the config.",
|
Short: "Export the configuration to a file.",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.ExactArgs(1),
|
||||||
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
||||||
settings, err := d.store.Settings.Get()
|
settings, err := d.store.Settings.Get()
|
||||||
checkErr(err)
|
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.SetIndent("", " ")
|
||||||
encoder.Encode(settings)
|
encoder.Encode(&settingsFile{
|
||||||
|
Settings: settings,
|
||||||
|
Auther: auther,
|
||||||
|
})
|
||||||
|
|
||||||
}, pythonConfig{}),
|
}, pythonConfig{}),
|
||||||
}
|
}
|
||||||
|
|||||||
81
cmd/config_import.go
Normal file
81
cmd/config_import.go
Normal file
@ -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 <filename>",
|
||||||
|
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
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user