feature (plg_backend_local): home calculation and additional options for authentication

This commit is contained in:
MickaelK 2024-01-06 16:55:55 +11:00
parent 08c9e5116c
commit 763201eaa6

View file

@ -5,6 +5,7 @@ import (
"golang.org/x/crypto/bcrypt"
"io"
"os"
"os/user"
)
func init() {
@ -14,13 +15,16 @@ func init() {
type Local struct{}
func (this Local) Init(params map[string]string, app *App) (IBackend, error) {
if err := bcrypt.CompareHashAndPassword(
backend := Local{}
if params["password"] == Config.Get("general.secret_key").String() {
return backend, nil
} else if err := bcrypt.CompareHashAndPassword(
[]byte(Config.Get("auth.admin").String()),
[]byte(params["password"]),
); err != nil {
return nil, ErrAuthenticationFailed
); err == nil {
return backend, nil
}
return Local{}, nil
return nil, ErrAuthenticationFailed
}
func (this Local) LoginForm() Form {
@ -46,11 +50,13 @@ func (this Local) LoginForm() Form {
}
func (this Local) Home() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "/", nil
if home, err := os.UserHomeDir(); err == nil {
return home, nil
}
return home, nil
if currentUser, err := user.Current(); err == nil && currentUser.HomeDir != "" {
return currentUser.HomeDir, nil
}
return "/", nil
}
func (this Local) Ls(path string) ([]os.FileInfo, error) {