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() {
// 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"),
Host: "smtp.youremail.com",
Port: "587",

View File

@ -18,6 +18,19 @@ const (
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 {
To string
Subject string
@ -46,7 +59,16 @@ type EmailService struct {
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{
auth: config.Auth,
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{
auth: config.Auth,
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{
// Ideally, InsecureSkipVerify: false,
// 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) {
client, err := smtp.Dial(hostPort)
if err != nil {
@ -212,13 +227,17 @@ func (e *EmailService) send(to string, msg []byte) error {
return fmt.Errorf("DIAL: %s", err)
}
if err = c.StartTLS(e.tlsconfig); err != nil {
return fmt.Errorf("c.StartTLS: %s", err)
if e.tlsconfig != nil {
if err = c.StartTLS(e.tlsconfig); err != nil {
return fmt.Errorf("c.StartTLS: %s", err)
}
}
// Auth
if err = c.Auth(e.auth); err != nil {
return fmt.Errorf("c.Auth: %s", err)
if e.auth != nil {
if err = c.Auth(e.auth); err != nil {
return fmt.Errorf("c.Auth: %s", err)
}
}
// To && From

View File

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