2020-01-22 03:15:40 +07:00
|
|
|
package validator
|
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-03 19:24:51 +07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2022-04-03 19:24:51 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/templates"
|
2020-04-05 19:37:21 +07:00
|
|
|
)
|
2020-01-22 03:15:40 +07:00
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// ValidateNotifier validates and update notifier configuration.
|
2022-02-28 10:15:01 +07:00
|
|
|
func ValidateNotifier(config *schema.NotifierConfiguration, validator *schema.StructValidator) {
|
|
|
|
if config == nil || (config.SMTP == nil && config.FileSystem == nil) {
|
2021-08-07 10:58:08 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierNotConfigured))
|
|
|
|
|
|
|
|
return
|
2022-02-28 10:15:01 +07:00
|
|
|
} else if config.SMTP != nil && config.FileSystem != nil {
|
2021-08-07 10:58:08 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierMultipleConfigured))
|
2020-01-22 03:15:40 +07:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.FileSystem != nil {
|
|
|
|
if config.FileSystem.Filename == "" {
|
2021-08-07 10:58:08 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierFileSystemFileNameNotConfigured))
|
2020-01-22 03:15:40 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-01-22 03:15:40 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
validateSMTPNotifier(config.SMTP, validator)
|
2022-04-03 19:24:51 +07:00
|
|
|
|
|
|
|
validateNotifierTemplates(config, validator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateNotifierTemplates(config *schema.NotifierConfiguration, validator *schema.StructValidator) {
|
|
|
|
if config.TemplatePath == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
t *template.Template
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err = os.Stat(config.TemplatePath)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case os.IsNotExist(err):
|
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierTemplatePathNotExist, config.TemplatePath))
|
|
|
|
return
|
|
|
|
case err != nil:
|
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierTemplatePathUnknownError, config.TemplatePath, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-07 10:05:20 +07:00
|
|
|
if t, err = template.ParseFiles(filepath.Join(config.TemplatePath, templates.TemplateNameIdentityVerification+".html")); err == nil {
|
|
|
|
templates.EmailIdentityVerificationHTML = t
|
2022-04-03 19:24:51 +07:00
|
|
|
} else {
|
2022-04-07 10:05:20 +07:00
|
|
|
validator.PushWarning(fmt.Errorf(errFmtNotifierTemplateLoad, templates.TemplateNameIdentityVerification+".html", err))
|
2022-04-03 19:24:51 +07:00
|
|
|
}
|
|
|
|
|
2022-04-07 10:05:20 +07:00
|
|
|
if t, err = template.ParseFiles(filepath.Join(config.TemplatePath, templates.TemplateNameIdentityVerification+".txt")); err == nil {
|
|
|
|
templates.EmailIdentityVerificationPlainText = t
|
2022-04-03 19:24:51 +07:00
|
|
|
} else {
|
2022-04-07 10:05:20 +07:00
|
|
|
validator.PushWarning(fmt.Errorf(errFmtNotifierTemplateLoad, templates.TemplateNameIdentityVerification+".txt", err))
|
2022-04-03 19:24:51 +07:00
|
|
|
}
|
|
|
|
|
2022-04-07 10:05:20 +07:00
|
|
|
if t, err = template.ParseFiles(filepath.Join(config.TemplatePath, templates.TemplateNameBasic+".html")); err == nil {
|
|
|
|
templates.EmailPasswordResetHTML = t
|
2022-04-03 19:24:51 +07:00
|
|
|
} else {
|
2022-04-07 10:05:20 +07:00
|
|
|
validator.PushWarning(fmt.Errorf(errFmtNotifierTemplateLoad, templates.TemplateNameBasic+".html", err))
|
2022-04-03 19:24:51 +07:00
|
|
|
}
|
|
|
|
|
2022-04-07 10:05:20 +07:00
|
|
|
if t, err = template.ParseFiles(filepath.Join(config.TemplatePath, templates.TemplateNameBasic+".txt")); err == nil {
|
|
|
|
templates.EmailPasswordResetPlainText = t
|
2022-04-03 19:24:51 +07:00
|
|
|
} else {
|
2022-04-07 10:05:20 +07:00
|
|
|
validator.PushWarning(fmt.Errorf(errFmtNotifierTemplateLoad, templates.TemplateNameBasic+".txt", err))
|
2022-04-03 19:24:51 +07:00
|
|
|
}
|
2021-04-16 08:44:37 +07:00
|
|
|
}
|
2020-04-09 07:21:28 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
func validateSMTPNotifier(config *schema.SMTPNotifierConfiguration, validator *schema.StructValidator) {
|
|
|
|
if config.StartupCheckAddress == "" {
|
|
|
|
config.StartupCheckAddress = schema.DefaultSMTPNotifierConfiguration.StartupCheckAddress
|
2021-04-16 08:44:37 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.Host == "" {
|
2021-08-07 10:58:08 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierSMTPNotConfigured, "host"))
|
2021-04-16 08:44:37 +07:00
|
|
|
}
|
2020-11-05 06:22:10 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.Port == 0 {
|
2021-08-07 10:58:08 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierSMTPNotConfigured, "port"))
|
2021-04-16 08:44:37 +07:00
|
|
|
}
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.Timeout == 0 {
|
|
|
|
config.Timeout = schema.DefaultSMTPNotifierConfiguration.Timeout
|
2021-08-10 07:52:41 +07:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.Sender.Address == "" {
|
2021-08-07 10:58:08 +07:00
|
|
|
validator.Push(fmt.Errorf(errFmtNotifierSMTPNotConfigured, "sender"))
|
2021-04-16 08:44:37 +07:00
|
|
|
}
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.Subject == "" {
|
|
|
|
config.Subject = schema.DefaultSMTPNotifierConfiguration.Subject
|
2021-04-16 08:44:37 +07:00
|
|
|
}
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.Identifier == "" {
|
|
|
|
config.Identifier = schema.DefaultSMTPNotifierConfiguration.Identifier
|
2021-04-16 08:44:37 +07:00
|
|
|
}
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.TLS == nil {
|
|
|
|
config.TLS = schema.DefaultSMTPNotifierConfiguration.TLS
|
2021-04-16 08:44:37 +07:00
|
|
|
}
|
2021-01-04 17:28:55 +07:00
|
|
|
|
2022-02-28 10:15:01 +07:00
|
|
|
if config.TLS.ServerName == "" {
|
|
|
|
config.TLS.ServerName = config.Host
|
2020-01-22 03:15:40 +07:00
|
|
|
}
|
|
|
|
}
|