From 27838f5b1f8bde119bdc86f411e62ff9ca9511af Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Tue, 1 Oct 2024 15:28:10 +0200 Subject: [PATCH] Improve TLS state handling and add SCRAM-SHA-256 auth support Replaced direct TLSConnectionState call with error handling for TLS state retrieval. Introduced SCRAM-SHA-256 support in the SMTP authentication process. --- client.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index f7ed9f1..b692af3 100644 --- a/client.go +++ b/client.go @@ -748,7 +748,11 @@ func (c *Client) tls() error { return err } } - _, c.isEncrypted = c.smtpClient.TLSConnectionState() + tlsConnState, err := c.smtpClient.GetTLSConnectionState() + if err != nil { + return fmt.Errorf("failed to get TLS connection state: %w", err) + } + c.isEncrypted = tlsConnState.HandshakeComplete } return nil } @@ -790,6 +794,11 @@ func (c *Client) auth() error { return ErrXOauth2AuthNotSupported } c.smtpAuth = smtp.ScramSHA1Auth(c.user, c.pass) + case SMTPAuthSCRAMSHA256: + if !strings.Contains(smtpAuthType, string(SMTPAuthSCRAMSHA256)) { + return ErrXOauth2AuthNotSupported + } + c.smtpAuth = smtp.ScramSHA256Auth(c.user, c.pass) case SMTPAuthSCRAMSHA1PLUS: if !strings.Contains(smtpAuthType, string(SMTPAuthSCRAMSHA1PLUS)) { return ErrXOauth2AuthNotSupported @@ -799,11 +808,6 @@ func (c *Client) auth() error { return err } c.smtpAuth = smtp.ScramSHA1PlusAuth(c.user, c.pass, tlsConnState) - case SMTPAuthSCRAMSHA256: - if !strings.Contains(smtpAuthType, string(SMTPAuthSCRAMSHA256)) { - return ErrXOauth2AuthNotSupported - } - c.smtpAuth = smtp.ScramSHA256Auth(c.user, c.pass) case SMTPAuthSCRAMSHA256PLUS: if !strings.Contains(smtpAuthType, string(SMTPAuthSCRAMSHA256PLUS)) { return ErrXOauth2AuthNotSupported @@ -813,7 +817,6 @@ func (c *Client) auth() error { return err } c.smtpAuth = smtp.ScramSHA256PlusAuth(c.user, c.pass, tlsConnState) - default: return fmt.Errorf("unsupported SMTP AUTH type %q", c.smtpAuthType) }