Support SMTP Auth Plain

This commit is contained in:
Bishtawi
2024-08-07 17:08:47 -07:00
parent 72f36f8296
commit 112efaae90
2 changed files with 38 additions and 6 deletions

View File

@@ -70,15 +70,20 @@ func (b *smtpBackend) Counts() (total int64, success int64, failure int64) {
// smtpSession is returned after EHLO.
type smtpSession struct {
backend *smtpBackend
conn *smtp.Conn
topic string
token string
mu sync.Mutex
backend *smtpBackend
conn *smtp.Conn
topic string
token string
mu sync.Mutex
basic_auth string
}
func (s *smtpSession) AuthPlain(username, _ string) error {
func (s *smtpSession) AuthPlain(username, password string) error {
logem(s.conn).Field("smtp_username", username).Debug("AUTH PLAIN (with username %s)", username)
basic_auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
s.mu.Lock()
s.basic_auth = basic_auth
s.mu.Unlock()
return nil
}
@@ -198,6 +203,8 @@ func (s *smtpSession) publishMessage(m *message) error {
}
if s.token != "" {
req.Header.Add("Authorization", "Bearer "+s.token)
} else if s.basic_auth != "" {
req.Header.Add("Authorization", "Basic "+s.basic_auth)
}
rr := httptest.NewRecorder()
s.backend.handler(rr, req)
@@ -214,6 +221,9 @@ func (s *smtpSession) Reset() {
}
func (s *smtpSession) Logout() error {
s.mu.Lock()
s.basic_auth = ""
s.mu.Unlock()
return nil
}