user authentication
This commit is contained in:
@@ -35,6 +35,23 @@ type Config struct {
|
||||
ShowFilesInTree bool
|
||||
ShowImagesInFolder bool
|
||||
ShowFilesInFolder bool
|
||||
|
||||
// Database settings
|
||||
DBType string
|
||||
DBPath string
|
||||
|
||||
// Auth settings
|
||||
RequireAdminActivation bool
|
||||
RequireEmailConfirmation bool
|
||||
MFAEnabledByDefault bool
|
||||
|
||||
// Email (SMTP) settings
|
||||
SMTPHost string
|
||||
SMTPPort int
|
||||
SMTPUsername string
|
||||
SMTPPassword string
|
||||
SMTPSender string
|
||||
SMTPUseTLS bool
|
||||
}
|
||||
|
||||
var defaultConfig = map[string]map[string]string{
|
||||
@@ -62,6 +79,23 @@ var defaultConfig = map[string]map[string]string{
|
||||
"SHOW_IMAGES_IN_FOLDER": "true",
|
||||
"SHOW_FILES_IN_FOLDER": "true",
|
||||
},
|
||||
"DATABASE": {
|
||||
"TYPE": "sqlite",
|
||||
"PATH": "data/gobsidian.db",
|
||||
},
|
||||
"AUTH": {
|
||||
"REQUIRE_ADMIN_ACTIVATION": "true",
|
||||
"REQUIRE_EMAIL_CONFIRMATION": "true",
|
||||
"MFA_ENABLED_BY_DEFAULT": "false",
|
||||
},
|
||||
"EMAIL": {
|
||||
"SMTP_HOST": "",
|
||||
"SMTP_PORT": "587",
|
||||
"SMTP_USERNAME": "",
|
||||
"SMTP_PASSWORD": "",
|
||||
"SMTP_SENDER": "",
|
||||
"SMTP_USE_TLS": "true",
|
||||
},
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
@@ -130,6 +164,36 @@ func Load() (*Config, error) {
|
||||
config.ImageStoragePath = filepath.Join(wd, config.ImageStoragePath)
|
||||
}
|
||||
|
||||
// Load DATABASE section
|
||||
dbSection := cfg.Section("DATABASE")
|
||||
config.DBType = strings.ToLower(strings.TrimSpace(dbSection.Key("TYPE").String()))
|
||||
config.DBPath = dbSection.Key("PATH").String()
|
||||
if config.DBType == "sqlite" {
|
||||
if !filepath.IsAbs(config.DBPath) {
|
||||
wd, _ := os.Getwd()
|
||||
config.DBPath = filepath.Join(wd, config.DBPath)
|
||||
}
|
||||
// ensure parent dir exists
|
||||
if err := os.MkdirAll(filepath.Dir(config.DBPath), 0o755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create db directory: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Load AUTH section
|
||||
authSection := cfg.Section("AUTH")
|
||||
config.RequireAdminActivation, _ = authSection.Key("REQUIRE_ADMIN_ACTIVATION").Bool()
|
||||
config.RequireEmailConfirmation, _ = authSection.Key("REQUIRE_EMAIL_CONFIRMATION").Bool()
|
||||
config.MFAEnabledByDefault, _ = authSection.Key("MFA_ENABLED_BY_DEFAULT").Bool()
|
||||
|
||||
// Load EMAIL (SMTP) section
|
||||
emailSection := cfg.Section("EMAIL")
|
||||
config.SMTPHost = emailSection.Key("SMTP_HOST").String()
|
||||
config.SMTPPort, _ = emailSection.Key("SMTP_PORT").Int()
|
||||
config.SMTPUsername = emailSection.Key("SMTP_USERNAME").String()
|
||||
config.SMTPPassword = emailSection.Key("SMTP_PASSWORD").String()
|
||||
config.SMTPSender = emailSection.Key("SMTP_SENDER").String()
|
||||
config.SMTPUseTLS, _ = emailSection.Key("SMTP_USE_TLS").Bool()
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
@@ -265,6 +329,39 @@ func (c *Config) SaveSetting(section, key, value string) error {
|
||||
case "SHOW_FILES_IN_FOLDER":
|
||||
c.ShowFilesInFolder = value == "true"
|
||||
}
|
||||
case "DATABASE":
|
||||
switch key {
|
||||
case "TYPE":
|
||||
c.DBType = strings.ToLower(strings.TrimSpace(value))
|
||||
case "PATH":
|
||||
c.DBPath = value
|
||||
}
|
||||
case "AUTH":
|
||||
switch key {
|
||||
case "REQUIRE_ADMIN_ACTIVATION":
|
||||
c.RequireAdminActivation = value == "true"
|
||||
case "REQUIRE_EMAIL_CONFIRMATION":
|
||||
c.RequireEmailConfirmation = value == "true"
|
||||
case "MFA_ENABLED_BY_DEFAULT":
|
||||
c.MFAEnabledByDefault = value == "true"
|
||||
}
|
||||
case "EMAIL":
|
||||
switch key {
|
||||
case "SMTP_HOST":
|
||||
c.SMTPHost = value
|
||||
case "SMTP_PORT":
|
||||
if v, err := strconv.Atoi(value); err == nil {
|
||||
c.SMTPPort = v
|
||||
}
|
||||
case "SMTP_USERNAME":
|
||||
c.SMTPUsername = value
|
||||
case "SMTP_PASSWORD":
|
||||
c.SMTPPassword = value
|
||||
case "SMTP_SENDER":
|
||||
c.SMTPSender = value
|
||||
case "SMTP_USE_TLS":
|
||||
c.SMTPUseTLS = value == "true"
|
||||
}
|
||||
}
|
||||
|
||||
return cfg.SaveTo(configPath)
|
||||
|
||||
Reference in New Issue
Block a user