mirror of
https://github.com/zitadel/zitadel.git
synced 2025-10-20 22:41:56 +00:00
feat: notifications (#109)
* implement notification providers * email provider * notification handler * notify users * implement code sent on user eventstore * send email implementation * send init code * handle codes * fix project member handler * add some logs for debug * send emails * text changes * send sms * notification process * send password code * format phone number * test format phone * remove fmts * remove unused code * rename files * add mocks * merge master * Update internal/notification/providers/email/message.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/notification/repository/eventsourcing/handler/notification.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/notification/repository/eventsourcing/handler/notification.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/notification/providers/email/provider.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * requested changes of mr * move locker to eventstore pkg * Update internal/notification/providers/chat/message.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * move locker to eventstore pkg * linebreak * Update internal/notification/providers/email/provider.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/notification/repository/eventsourcing/handler/notification.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/notification/repository/eventsourcing/handler/notification.go Co-authored-by: Silvan <silvan.reusser@gmail.com> Co-authored-by: Silvan <silvan.reusser@gmail.com> Co-authored-by: Livio Amstutz <livio.a@gmail.com>
This commit is contained in:
18
internal/notification/providers/email/config.go
Normal file
18
internal/notification/providers/email/config.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package email
|
||||
|
||||
type EmailConfig struct {
|
||||
SMTP SMTP
|
||||
Tls bool
|
||||
From string
|
||||
FromName string
|
||||
}
|
||||
|
||||
type SMTP struct {
|
||||
Host string
|
||||
User string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (smtp *SMTP) HasAuth() bool {
|
||||
return smtp.User != "" && smtp.Password != ""
|
||||
}
|
47
internal/notification/providers/email/message.go
Normal file
47
internal/notification/providers/email/message.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
isHTMLRgx = regexp.MustCompile(`.*<html.*>.*`)
|
||||
lineBreak = "\r\n"
|
||||
)
|
||||
|
||||
type EmailMessage struct {
|
||||
Recipients []string
|
||||
BCC []string
|
||||
CC []string
|
||||
SenderEmail string
|
||||
Subject string
|
||||
Content string
|
||||
}
|
||||
|
||||
func (msg *EmailMessage) GetContent() string {
|
||||
headers := make(map[string]string)
|
||||
headers["From"] = msg.SenderEmail
|
||||
headers["To"] = strings.Join(msg.Recipients, ", ")
|
||||
headers["Cc"] = strings.Join(msg.CC, ", ")
|
||||
|
||||
message := ""
|
||||
for k, v := range headers {
|
||||
message += fmt.Sprintf("%s: %s"+lineBreak, k, v)
|
||||
}
|
||||
|
||||
//default mime-type is html
|
||||
mime := "MIME-version: 1.0;" + lineBreak + "Content-Type: text/html; charset=\"UTF-8\";" + lineBreak + lineBreak
|
||||
if !isHTML(msg.Content) {
|
||||
mime = "MIME-version: 1.0;" + lineBreak + "Content-Type: text/plain; charset=\"UTF-8\";" + lineBreak + lineBreak
|
||||
}
|
||||
subject := "Subject: " + msg.Subject + lineBreak
|
||||
message += subject + mime + lineBreak + msg.Content
|
||||
|
||||
return message
|
||||
}
|
||||
|
||||
func isHTML(input string) bool {
|
||||
return isHTMLRgx.MatchString(input)
|
||||
}
|
123
internal/notification/providers/email/provider.go
Normal file
123
internal/notification/providers/email/provider.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"github.com/caos/logging"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/notification/providers"
|
||||
"net"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
type Email struct {
|
||||
smtpClient *smtp.Client
|
||||
}
|
||||
|
||||
func InitEmailProvider(config EmailConfig) (*Email, error) {
|
||||
client, err := config.SMTP.connectToSMTP(config.Tls)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Email{
|
||||
smtpClient: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (email *Email) CanHandleMessage(message providers.Message) bool {
|
||||
msg, ok := message.(*EmailMessage)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return msg.Content != "" && msg.Subject != "" && len(msg.Recipients) > 0
|
||||
}
|
||||
|
||||
func (email *Email) HandleMessage(message providers.Message) error {
|
||||
defer email.smtpClient.Close()
|
||||
emailMsg, ok := message.(*EmailMessage)
|
||||
if !ok {
|
||||
return caos_errs.ThrowInternal(nil, "EMAIL-s8JLs", "message is not EmailMessage")
|
||||
}
|
||||
// To && From
|
||||
if err := email.smtpClient.Mail(emailMsg.SenderEmail); err != nil {
|
||||
return caos_errs.ThrowInternalf(err, "EMAIL-s3is3", "could not set sender: %v", emailMsg.SenderEmail)
|
||||
}
|
||||
|
||||
for _, recp := range append(append(emailMsg.Recipients, emailMsg.CC...), emailMsg.BCC...) {
|
||||
if err := email.smtpClient.Rcpt(recp); err != nil {
|
||||
return caos_errs.ThrowInternalf(err, "EMAIL-s4is4", "could not set recipient: %v", recp)
|
||||
}
|
||||
}
|
||||
|
||||
// Data
|
||||
w, err := email.smtpClient.Data()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = w.Write([]byte(emailMsg.GetContent()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = w.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer logging.LogWithFields("EMAI-a1c87ec8").Debug("email sent")
|
||||
return email.smtpClient.Quit()
|
||||
}
|
||||
|
||||
func (smtpConfig SMTP) connectToSMTP(tlsRequired bool) (client *smtp.Client, err error) {
|
||||
host, _, err := net.SplitHostPort(smtpConfig.Host)
|
||||
if err != nil {
|
||||
return nil, caos_errs.ThrowInternal(err, "EMAIL-spR56", "could not split host and port for connect to smtp")
|
||||
}
|
||||
|
||||
if !tlsRequired {
|
||||
client, err = smtpConfig.getSMPTClient()
|
||||
} else {
|
||||
client, err = smtpConfig.getSMPTClientWithTls(host)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = smtpConfig.smtpAuth(client, host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (smtpConfig SMTP) getSMPTClient() (*smtp.Client, error) {
|
||||
client, err := smtp.Dial(smtpConfig.Host)
|
||||
if err != nil {
|
||||
return nil, caos_errs.ThrowInternal(err, "EMAIL-skwos", "Could not make smtp dial")
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (smtpConfig SMTP) getSMPTClientWithTls(host string) (*smtp.Client, error) {
|
||||
conn, err := tls.Dial("tcp", smtpConfig.Host, &tls.Config{})
|
||||
if err != nil {
|
||||
return nil, caos_errs.ThrowInternal(err, "EMAIL-sl39s", "Could not make tls dial")
|
||||
}
|
||||
|
||||
client, err := smtp.NewClient(conn, host)
|
||||
if err != nil {
|
||||
return nil, caos_errs.ThrowInternal(err, "EMAIL-skwi4", "Could not create smtp client")
|
||||
}
|
||||
return client, err
|
||||
}
|
||||
|
||||
func (smtpConfig SMTP) smtpAuth(client *smtp.Client, host string) error {
|
||||
if !smtpConfig.HasAuth() {
|
||||
return nil
|
||||
}
|
||||
// Auth
|
||||
auth := smtp.PlainAuth("", smtpConfig.User, smtpConfig.Password, host)
|
||||
err := client.Auth(auth)
|
||||
logging.Log("EMAIL-s9kfs").WithField("smtp user", smtpConfig.User).OnError(err).Debug("Could not add smtp auth")
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user