Add custom SMTP authentication type support

Introduce the SMTPAuthCustom type to represent user-defined SMTP authentication mechanisms. Updated relevant functions in client.go to handle the new type appropriately and made sure the client distinguishes between built-in and custom authentication methods.
This commit is contained in:
Winni Neessen 2024-10-04 22:33:42 +02:00
parent d900f5403e
commit 972a3c51c7
Signed by: wneessen
GPG key ID: 385AC9889632126E
2 changed files with 16 additions and 6 deletions

View file

@ -23,6 +23,12 @@ const (
// https://datatracker.ietf.org/doc/html/draft-ietf-sasl-crammd5-to-historic-00.html // https://datatracker.ietf.org/doc/html/draft-ietf-sasl-crammd5-to-historic-00.html
SMTPAuthCramMD5 SMTPAuthType = "CRAM-MD5" SMTPAuthCramMD5 SMTPAuthType = "CRAM-MD5"
// SMTPAuthCustom is a custom SMTP AUTH mechanism provided by the user. If a user provides
// a custom smtp.Auth function to the Client, the Client will its smtpAuthType to this type.
//
// Do not use this SMTPAuthType without setting a custom smtp.Auth function on the Client.
SMTPAuthCustom SMTPAuthType = "CUSTOM"
// SMTPAuthLogin is the "LOGIN" SASL authentication mechanism. This authentication mechanism // SMTPAuthLogin is the "LOGIN" SASL authentication mechanism. This authentication mechanism
// does not have an official RFC that could be followed. There is a spec by Microsoft and an // does not have an official RFC that could be followed. There is a spec by Microsoft and an
// IETF draft. The IETF draft is more lax than the MS spec, therefore we follow the I-D, which // IETF draft. The IETF draft is more lax than the MS spec, therefore we follow the I-D, which

View file

@ -396,11 +396,12 @@ func WithSMTPAuth(authtype SMTPAuthType) Option {
} }
} }
// WithSMTPAuthCustom sets a custom SMTP authentication mechanism for the client instance. The provided // WithSMTPAuthCustom sets a custom SMTP authentication mechanism for the Client. The provided
// authentication mechanism has to satisfy the smtp.Auth interface. // authentication mechanism has to satisfy the smtp.Auth interface.
func WithSMTPAuthCustom(smtpAuth smtp.Auth) Option { func WithSMTPAuthCustom(smtpAuth smtp.Auth) Option {
return func(c *Client) error { return func(c *Client) error {
c.smtpAuth = smtpAuth c.smtpAuth = smtpAuth
c.smtpAuthType = SMTPAuthCustom
return nil return nil
} }
} }
@ -623,25 +624,28 @@ func (c *Client) SetTLSConfig(tlsconfig *tls.Config) error {
return nil return nil
} }
// SetUsername overrides the current username string with the given value // SetUsername sets or overrides the username, the Client will use for the SMTP authentication.
func (c *Client) SetUsername(username string) { func (c *Client) SetUsername(username string) {
c.user = username c.user = username
} }
// SetPassword overrides the current password string with the given value // SetPassword sets or overrides the password, the Client will use for the SMTP authentication.
func (c *Client) SetPassword(password string) { func (c *Client) SetPassword(password string) {
c.pass = password c.pass = password
} }
// SetSMTPAuth overrides the current SMTP AUTH type setting with the given value // SetSMTPAuth sets or overrides the SMTPAuthType that is currently set on the Client for the SMTP
// authentication.
func (c *Client) SetSMTPAuth(authtype SMTPAuthType) { func (c *Client) SetSMTPAuth(authtype SMTPAuthType) {
c.smtpAuthType = authtype c.smtpAuthType = authtype
c.smtpAuth = nil c.smtpAuth = nil
} }
// SetSMTPAuthCustom overrides the current SMTP AUTH setting with the given custom smtp.Auth // SetSMTPAuthCustom sets or overrides the custom SMTP authentication mechanism currently set for
// the Client. The provided authentication mechanism has to satisfy the smtp.Auth interface.
func (c *Client) SetSMTPAuthCustom(smtpAuth smtp.Auth) { func (c *Client) SetSMTPAuthCustom(smtpAuth smtp.Auth) {
c.smtpAuth = smtpAuth c.smtpAuth = smtpAuth
c.smtpAuthType = SMTPAuthCustom
} }
// setDefaultHelo retrieves the current hostname and sets it as HELO/EHLO hostname // setDefaultHelo retrieves the current hostname and sets it as HELO/EHLO hostname
@ -827,7 +831,7 @@ func (c *Client) auth() error {
if err := c.checkConn(); err != nil { if err := c.checkConn(); err != nil {
return fmt.Errorf("failed to authenticate: %w", err) return fmt.Errorf("failed to authenticate: %w", err)
} }
if c.smtpAuth == nil && c.smtpAuthType != "" { if c.smtpAuth == nil && c.smtpAuthType != SMTPAuthCustom {
hasSMTPAuth, smtpAuthType := c.smtpClient.Extension("AUTH") hasSMTPAuth, smtpAuthType := c.smtpClient.Extension("AUTH")
if !hasSMTPAuth { if !hasSMTPAuth {
return fmt.Errorf("server does not support SMTP AUTH") return fmt.Errorf("server does not support SMTP AUTH")