From 22e5c25668c32bd05d6d05ae4bf456efb4600ca9 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 27 Aug 2019 03:33:34 -0500 Subject: [PATCH] add custom email auth template --- README.md | 1 + backend/app/cmd/server.go | 20 +++++++++++++++++++- backend/app/cmd/server_test.go | 17 +++++++++++++++++ backend/app/cmd/testdata/email.tmpl | 1 + 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 backend/app/cmd/testdata/email.tmpl diff --git a/README.md b/README.md index 1689b23f..dff62172 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ _this is the recommended way to run remark42_ | auth.email.user | AUTH_EMAIL_USER | | smtp user name | | auth.email.passwd | AUTH_EMAIL_PASSWD | | smtp password | | auth.email.timeout | AUTH_EMAIL_TIMEOUT | `10s` | smtp timeout | +| auth.email.template | AUTH_EMAIL_TEMPLATE | none (predefined) | custom email message template | | notify.type | NOTIFY_TYPE | none | type of notification (none or telegram) | | notify.queue | NOTIFY_QUEUE | `100` | size of notification queue | | notify.telegram.token | NOTIFY_TELEGRAM_TOKEN | | telegram token | diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index 7ad4e2b4..cc7b47d7 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + "io/ioutil" "net/http" "net/url" "os" @@ -92,6 +93,7 @@ type ServerCommand struct { SMTPUserName string `long:"user" env:"USER" description:"smtp user name"` SMTPPassword string `long:"passwd" env:"PASSWD" description:"smtp password"` TimeOut time.Duration `long:"timeout" env:"TIMEOUT" default:"10s" description:"smtp timeout"` + MsgTemplate string `long:"template" env:"TEMPLATE" description:"message template file"` } `group:"email" namespace:"email" env-namespace:"EMAIL"` } `group:"auth" namespace:"auth" env-namespace:"AUTH"` @@ -600,7 +602,7 @@ func (s *ServerCommand) addAuthProviders(authenticator *auth.Service) { TimeOut: s.Auth.Email.TimeOut, } sndr := sender.NewEmailClient(params, log.Default()) - authenticator.AddVerifProvider("email", msgTemplate, sndr) + authenticator.AddVerifProvider("email", s.loadEmailTemplate(), sndr) } if s.Auth.Anonymous { @@ -626,6 +628,22 @@ func (s *ServerCommand) addAuthProviders(authenticator *auth.Service) { } } +// loadEmailTemplate trying to get template from opts MsgTemplate and default to embedded +// if not defined or failed to load +func (s *ServerCommand) loadEmailTemplate() string { + tmpl := msgTemplate + if s.Auth.Email.MsgTemplate != "" { + log.Printf("[DEBUG] load email template from %s", s.Auth.Email.MsgTemplate) + b, err := ioutil.ReadFile(s.Auth.Email.MsgTemplate) + if err == nil { + tmpl = string(b) + } else { + log.Printf("[WARN] failed to load email template from %s, %v", s.Auth.Email.MsgTemplate, err) + } + } + return tmpl +} + func (s *ServerCommand) makeNotify(dataStore *service.DataStore) (*notify.Service, error) { log.Printf("[INFO] make notify, type=%s", s.Notify.Type) switch s.Notify.Type { diff --git a/backend/app/cmd/server_test.go b/backend/app/cmd/server_test.go index b42e42af..fe9bc41a 100644 --- a/backend/app/cmd/server_test.go +++ b/backend/app/cmd/server_test.go @@ -431,6 +431,21 @@ func TestServerAuthHooks(t *testing.T) { app.Wait() } +func TestServer_loadEmailTemplate(t *testing.T) { + cmd := ServerCommand{} + cmd.Auth.Email.MsgTemplate = "testdata/email.tmpl" + r := cmd.loadEmailTemplate() + assert.Equal(t, "The token is {{.Token}}", r) + + cmd.Auth.Email.MsgTemplate = "" + r = cmd.loadEmailTemplate() + assert.Contains(t, r, "Remark42") + + cmd.Auth.Email.MsgTemplate = "bad-file" + r = cmd.loadEmailTemplate() + assert.Contains(t, r, "Remark42") +} + func prepServerApp(t *testing.T, duration time.Duration, fn func(o ServerCommand) ServerCommand) (*serverApp, context.Context) { cmd := ServerCommand{} cmd.SetCommon(CommonOpts{RemarkURL: "https://demo.remark42.com", SharedSecret: "secret"}) @@ -446,6 +461,8 @@ func prepServerApp(t *testing.T, duration time.Duration, fn func(o ServerCommand cmd.Auth.Google.CSEC, cmd.Auth.Google.CID = "csec", "cid" cmd.Auth.Facebook.CSEC, cmd.Auth.Facebook.CID = "csec", "cid" cmd.Auth.Yandex.CSEC, cmd.Auth.Yandex.CID = "csec", "cid" + cmd.Auth.Email.Enable = true + cmd.Auth.Email.MsgTemplate = "testdata/email.tmpl" cmd.BackupLocation = "/tmp" cmd.Notify.Type = "telegram" cmd.Notify.Telegram.API = "http://127.0.0.1:12340/" diff --git a/backend/app/cmd/testdata/email.tmpl b/backend/app/cmd/testdata/email.tmpl new file mode 100644 index 00000000..417b2def --- /dev/null +++ b/backend/app/cmd/testdata/email.tmpl @@ -0,0 +1 @@ +The token is {{.Token}} \ No newline at end of file