2022-12-26 10:39:19 +00:00
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-12-27 08:59:08 +00:00
|
|
|
"github.com/wneessen/go-mail"
|
2023-03-18 20:50:17 +00:00
|
|
|
"github.com/wneessen/go-mail/smtp"
|
2022-12-26 10:39:19 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewOpportunisticSMTPAuth is an opportunistic smtp.Auth implementation.
|
|
|
|
func NewOpportunisticSMTPAuth(config *schema.SMTPNotifierConfiguration) *OpportunisticSMTPAuth {
|
|
|
|
if config.Username == "" && config.Password == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &OpportunisticSMTPAuth{
|
|
|
|
username: config.Username,
|
|
|
|
password: config.Password,
|
2023-05-07 06:39:17 +00:00
|
|
|
host: config.Address.Hostname(),
|
2022-12-26 10:39:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpportunisticSMTPAuth is an opportunistic smtp.Auth implementation.
|
|
|
|
type OpportunisticSMTPAuth struct {
|
|
|
|
username, password, host string
|
|
|
|
|
2022-12-27 08:59:08 +00:00
|
|
|
satPreference []mail.SMTPAuthType
|
2022-12-26 10:39:19 +00:00
|
|
|
sa smtp.Auth
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start begins an authentication with a server.
|
|
|
|
// It returns the name of the authentication protocol
|
|
|
|
// and optionally data to include in the initial AUTH message
|
|
|
|
// sent to the server.
|
|
|
|
// If it returns a non-nil error, the SMTP client aborts
|
|
|
|
// the authentication attempt and closes the connection.
|
|
|
|
func (a *OpportunisticSMTPAuth) Start(server *smtp.ServerInfo) (proto string, toServer []byte, err error) {
|
|
|
|
for _, pref := range a.satPreference {
|
|
|
|
if utils.IsStringInSlice(string(pref), server.Auth) {
|
|
|
|
switch pref {
|
2022-12-27 08:59:08 +00:00
|
|
|
case mail.SMTPAuthPlain:
|
2022-12-26 10:39:19 +00:00
|
|
|
a.sa = smtp.PlainAuth("", a.username, a.password, a.host)
|
2022-12-27 08:59:08 +00:00
|
|
|
case mail.SMTPAuthLogin:
|
2023-03-18 20:50:17 +00:00
|
|
|
a.sa = smtp.LoginAuth(a.username, a.password, a.host)
|
2022-12-27 08:59:08 +00:00
|
|
|
case mail.SMTPAuthCramMD5:
|
2022-12-26 10:39:19 +00:00
|
|
|
a.sa = smtp.CRAMMD5Auth(a.username, a.password)
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.sa == nil {
|
|
|
|
for _, sa := range server.Auth {
|
2022-12-27 08:59:08 +00:00
|
|
|
switch mail.SMTPAuthType(sa) {
|
|
|
|
case mail.SMTPAuthPlain:
|
2022-12-26 10:39:19 +00:00
|
|
|
a.sa = smtp.PlainAuth("", a.username, a.password, a.host)
|
2022-12-27 08:59:08 +00:00
|
|
|
case mail.SMTPAuthLogin:
|
2023-03-18 20:50:17 +00:00
|
|
|
a.sa = smtp.LoginAuth(a.username, a.password, a.host)
|
2022-12-27 08:59:08 +00:00
|
|
|
case mail.SMTPAuthCramMD5:
|
2022-12-26 10:39:19 +00:00
|
|
|
a.sa = smtp.CRAMMD5Auth(a.username, a.password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.sa == nil {
|
|
|
|
return "", nil, fmt.Errorf("unsupported SMTP AUTH types: %s", strings.Join(server.Auth, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.sa.Start(server)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next continues the authentication. The server has just sent
|
|
|
|
// the fromServer data. If more is true, the server expects a
|
|
|
|
// response, which Next should return as toServer; otherwise
|
|
|
|
// Next should return toServer == nil.
|
|
|
|
// If Next returns a non-nil error, the SMTP client aborts
|
|
|
|
// the authentication attempt and closes the connection.
|
|
|
|
func (a *OpportunisticSMTPAuth) Next(fromServer []byte, more bool) (toServer []byte, err error) {
|
|
|
|
return a.sa.Next(fromServer, more)
|
|
|
|
}
|