refactor: insecure no auth/tls

This commit is contained in:
urko 2025-05-29 09:24:26 +02:00
parent 9844ebc65d
commit b57eb95497
3 changed files with 37 additions and 18 deletions

View File

@ -9,7 +9,7 @@ import (
func main() { func main() {
// Here fill with real data // Here fill with real data
emailService := email.NewInsecure(email.MailServiceConfig{ emailService := email.NewInsecure(email.SecureConfig{
Auth: smtp.PlainAuth("", "your@email.com", "your-password", "smtp.youremail.com"), Auth: smtp.PlainAuth("", "your@email.com", "your-password", "smtp.youremail.com"),
Host: "smtp.youremail.com", Host: "smtp.youremail.com",
Port: "587", Port: "587",

View File

@ -18,6 +18,19 @@ const (
delimeter = "**=myohmy689407924327" delimeter = "**=myohmy689407924327"
) )
type InsecureConfig struct {
Host string
Port string
From string // Sender email address
}
type SecureConfig struct {
Auth smtp.Auth
Host string
Port string
From string // Sender email address
}
type EmailMessage struct { type EmailMessage struct {
To string To string
Subject string Subject string
@ -46,7 +59,16 @@ type EmailService struct {
dial SmtpDialFn dial SmtpDialFn
} }
func NewInsecure(config MailServiceConfig) *EmailService { func NewInsecureNoAuth(config InsecureConfig) *EmailService {
return &EmailService{
host: config.Host,
port: config.Port,
from: config.From,
dial: dial,
}
}
func NewInsecure(config SecureConfig) *EmailService {
return &EmailService{ return &EmailService{
auth: config.Auth, auth: config.Auth,
host: config.Host, host: config.Host,
@ -121,7 +143,7 @@ func customVerify(host string) func(cs tls.ConnectionState) error {
} }
} }
func NewSecure(config MailServiceConfig) *EmailService { func NewSecure(config SecureConfig) *EmailService {
return &EmailService{ return &EmailService{
auth: config.Auth, auth: config.Auth,
host: config.Host, host: config.Host,
@ -136,7 +158,7 @@ func NewSecure(config MailServiceConfig) *EmailService {
} }
} }
func NewSecure465(config MailServiceConfig) *EmailService { func NewSecure465(config SecureConfig) *EmailService {
tlsCfg := tls.Config{ tlsCfg := tls.Config{
// Ideally, InsecureSkipVerify: false, // Ideally, InsecureSkipVerify: false,
// or do a proper certificate validation // or do a proper certificate validation
@ -156,13 +178,6 @@ func NewSecure465(config MailServiceConfig) *EmailService {
} }
} }
type MailServiceConfig struct {
Auth smtp.Auth
Host string
Port string
From string // Sender email address
}
func dial(hostPort string) (SMTPClientIface, error) { func dial(hostPort string) (SMTPClientIface, error) {
client, err := smtp.Dial(hostPort) client, err := smtp.Dial(hostPort)
if err != nil { if err != nil {
@ -212,13 +227,17 @@ func (e *EmailService) send(to string, msg []byte) error {
return fmt.Errorf("DIAL: %s", err) return fmt.Errorf("DIAL: %s", err)
} }
if err = c.StartTLS(e.tlsconfig); err != nil { if e.tlsconfig != nil {
return fmt.Errorf("c.StartTLS: %s", err) if err = c.StartTLS(e.tlsconfig); err != nil {
return fmt.Errorf("c.StartTLS: %s", err)
}
} }
// Auth // Auth
if err = c.Auth(e.auth); err != nil { if e.auth != nil {
return fmt.Errorf("c.Auth: %s", err) if err = c.Auth(e.auth); err != nil {
return fmt.Errorf("c.Auth: %s", err)
}
} }
// To && From // To && From

View File

@ -60,7 +60,7 @@ func TestMockSendEmail(t *testing.T) {
func TestNewInsecure(t *testing.T) { func TestNewInsecure(t *testing.T) {
cfg := newConfig(".env.test") cfg := newConfig(".env.test")
mailSrv := NewInsecure(MailServiceConfig{ mailSrv := NewInsecure(SecureConfig{
Auth: smtp.PlainAuth("", cfg.MailUser, cfg.MailPassword, cfg.MailHost), Auth: smtp.PlainAuth("", cfg.MailUser, cfg.MailPassword, cfg.MailHost),
Host: cfg.MailHost, Host: cfg.MailHost,
Port: cfg.MailPort, Port: cfg.MailPort,
@ -131,7 +131,7 @@ func TestNewInsecure(t *testing.T) {
t.Run("TestSendEmail_FailedAuthentication", func(t *testing.T) { t.Run("TestSendEmail_FailedAuthentication", func(t *testing.T) {
// set up authentication to fail // set up authentication to fail
mailSrv := NewInsecure(MailServiceConfig{ mailSrv := NewInsecure(SecureConfig{
Auth: smtp.PlainAuth("", "wronguser", "wrongpassword", cfg.MailHost), Auth: smtp.PlainAuth("", "wronguser", "wrongpassword", cfg.MailHost),
Host: cfg.MailHost, Host: cfg.MailHost,
Port: cfg.MailPort, Port: cfg.MailPort,
@ -150,7 +150,7 @@ func TestNewInsecure(t *testing.T) {
func TestSecure(t *testing.T) { func TestSecure(t *testing.T) {
cfg := newConfig(".env.test") cfg := newConfig(".env.test")
emailService := NewSecure(MailServiceConfig{ emailService := NewSecure(SecureConfig{
Auth: smtp.PlainAuth("", cfg.MailUser, cfg.MailPassword, cfg.MailHost), Auth: smtp.PlainAuth("", cfg.MailUser, cfg.MailPassword, cfg.MailHost),
Host: cfg.MailHost, Host: cfg.MailHost,
Port: cfg.MailPort, Port: cfg.MailPort,