This commit is contained in:
Graham Steffaniak 2023-09-01 17:35:54 -05:00
parent d0cca8f285
commit 3fb940ba5b
11 changed files with 46 additions and 65 deletions

View File

@ -1,8 +0,0 @@
{
"port": 5555,
"baseURL": "",
"address": "",
"log": "stdout",
"database": "./database.db",
"root": "/srv"
}

View File

@ -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"`

View File

@ -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"`

View File

@ -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 == "" {

View File

@ -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"))

View File

@ -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\")")
}
@ -245,13 +242,7 @@ func quickSetup(flags *pflag.FlagSet, d pythonData) {
}
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)

View File

@ -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: ''

View File

@ -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

View File

@ -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.
}
@ -39,9 +44,10 @@ func setDefaults() {
IndexingInterval: 5,
Port: 8080,
NumImageProcessors: 1,
BaseURL: "/files",
BaseURL: "/",
},
Auth: Auth{
Method: "noauth",
Recaptcha: Recaptcha{
Host: "",
},

View File

@ -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

View File

@ -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)