Merge pull request #177 from wneessen/feature/176_noauth-smtpauth-type

Update SMTP authentication mechanisms and related tests
This commit is contained in:
Winni Neessen 2024-02-22 15:49:16 +01:00 committed by GitHub
commit 68e6284e4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 6 deletions

11
auth.go
View file

@ -11,15 +11,20 @@ type SMTPAuthType string
// Supported SMTP AUTH types // Supported SMTP AUTH types
const ( const (
// SMTPAuthCramMD5 is the "CRAM-MD5" SASL authentication mechanism as described in RFC 4954
SMTPAuthCramMD5 SMTPAuthType = "CRAM-MD5"
// SMTPAuthLogin is the "LOGIN" SASL authentication mechanism // SMTPAuthLogin is the "LOGIN" SASL authentication mechanism
SMTPAuthLogin SMTPAuthType = "LOGIN" SMTPAuthLogin SMTPAuthType = "LOGIN"
// SMTPAuthNoAuth is equivalent to performing no authentication at all. It is a convenience
// option and should not be used. Instead, for mail servers that do no support/require
// authentication, the Client should not be used with the WithSMTPAuth option
SMTPAuthNoAuth SMTPAuthType = ""
// SMTPAuthPlain is the "PLAIN" authentication mechanism as described in RFC 4616 // SMTPAuthPlain is the "PLAIN" authentication mechanism as described in RFC 4616
SMTPAuthPlain SMTPAuthType = "PLAIN" SMTPAuthPlain SMTPAuthType = "PLAIN"
// SMTPAuthCramMD5 is the "CRAM-MD5" SASL authentication mechanism as described in RFC 4954
SMTPAuthCramMD5 SMTPAuthType = "CRAM-MD5"
// SMTPAuthXOAUTH2 is the "XOAUTH2" SASL authentication mechanism. // SMTPAuthXOAUTH2 is the "XOAUTH2" SASL authentication mechanism.
// https://developers.google.com/gmail/imap/xoauth2-protocol // https://developers.google.com/gmail/imap/xoauth2-protocol
SMTPAuthXOAUTH2 SMTPAuthType = "XOAUTH2" SMTPAuthXOAUTH2 SMTPAuthType = "XOAUTH2"

View file

@ -97,6 +97,7 @@ func TestNewClientWithOptions(t *testing.T) {
{"WithTLSPortPolicy()", WithTLSPortPolicy(TLSOpportunistic), false}, {"WithTLSPortPolicy()", WithTLSPortPolicy(TLSOpportunistic), false},
{"WithTLSConfig()", WithTLSConfig(&tls.Config{}), false}, {"WithTLSConfig()", WithTLSConfig(&tls.Config{}), false},
{"WithTLSConfig(); config is nil", WithTLSConfig(nil), true}, {"WithTLSConfig(); config is nil", WithTLSConfig(nil), true},
{"WithSMTPAuth(NoAuth)", WithSMTPAuth(SMTPAuthNoAuth), false},
{"WithSMTPAuth()", WithSMTPAuth(SMTPAuthLogin), false}, {"WithSMTPAuth()", WithSMTPAuth(SMTPAuthLogin), false},
{ {
"WithSMTPAuthCustom()", "WithSMTPAuthCustom()",
@ -576,9 +577,9 @@ func TestSetSMTPAuthCustom(t *testing.T) {
want string want string
sf bool sf bool
}{ }{
{"SMTPAuth: PLAIN", smtp.PlainAuth("", "", "", ""), "PLAIN", false},
{"SMTPAuth: CRAM-MD5", smtp.CRAMMD5Auth("", ""), "CRAM-MD5", false}, {"SMTPAuth: CRAM-MD5", smtp.CRAMMD5Auth("", ""), "CRAM-MD5", false},
{"SMTPAuth: LOGIN", smtp.LoginAuth("", "", ""), "LOGIN", false}, {"SMTPAuth: LOGIN", smtp.LoginAuth("", "", ""), "LOGIN", false},
{"SMTPAuth: PLAIN", smtp.PlainAuth("", "", "", ""), "PLAIN", false},
} }
si := smtp.ServerInfo{TLS: true} si := smtp.ServerInfo{TLS: true}
for _, tt := range tests { for _, tt := range tests {
@ -1405,7 +1406,7 @@ func TestXOAuth2OK(t *testing.T) {
} }
c, err := NewClient("fake.host", c, err := NewClient("fake.host",
WithDialContextFunc(getFakeDialFunc(fake)), WithDialContextFunc(getFakeDialFunc(fake)),
WithTLSPolicy(TLSOpportunistic), WithTLSPortPolicy(TLSOpportunistic),
WithSMTPAuth(SMTPAuthXOAUTH2), WithSMTPAuth(SMTPAuthXOAUTH2),
WithUsername("user"), WithUsername("user"),
WithPassword("token")) WithPassword("token"))
@ -1444,7 +1445,7 @@ func TestXOAuth2Unsupported(t *testing.T) {
} }
c, err := NewClient("fake.host", c, err := NewClient("fake.host",
WithDialContextFunc(getFakeDialFunc(fake)), WithDialContextFunc(getFakeDialFunc(fake)),
WithTLSPolicy(TLSOpportunistic), WithTLSPortPolicy(TLSOpportunistic),
WithSMTPAuth(SMTPAuthXOAUTH2)) WithSMTPAuth(SMTPAuthXOAUTH2))
if err != nil { if err != nil {
t.Fatalf("unable to create new client: %v", err) t.Fatalf("unable to create new client: %v", err)