add custom email auth template

This commit is contained in:
Umputun
2019-08-27 03:33:34 -05:00
parent e9414f7267
commit 22e5c25668
4 changed files with 38 additions and 1 deletions
+1
View File
@@ -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 |
+19 -1
View File
@@ -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 {
+17
View File
@@ -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</h1>")
cmd.Auth.Email.MsgTemplate = "bad-file"
r = cmd.loadEmailTemplate()
assert.Contains(t, r, "Remark42</h1>")
}
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/"
+1
View File
@@ -0,0 +1 @@
The token is {{.Token}}