Skip to content

Commit eb700b4

Browse files
Merge pull request #1164 from bishtawi/bishtawi/smtp-auth
Support SMTP Auth Plain for event publishing
2 parents 89c884a + 112efaa commit eb700b4

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

server/smtp_server.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,20 @@ func (b *smtpBackend) Counts() (total int64, success int64, failure int64) {
7070

7171
// smtpSession is returned after EHLO.
7272
type smtpSession struct {
73-
backend *smtpBackend
74-
conn *smtp.Conn
75-
topic string
76-
token string
77-
mu sync.Mutex
73+
backend *smtpBackend
74+
conn *smtp.Conn
75+
topic string
76+
token string
77+
mu sync.Mutex
78+
basic_auth string
7879
}
7980

80-
func (s *smtpSession) AuthPlain(username, _ string) error {
81+
func (s *smtpSession) AuthPlain(username, password string) error {
8182
logem(s.conn).Field("smtp_username", username).Debug("AUTH PLAIN (with username %s)", username)
83+
basic_auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
84+
s.mu.Lock()
85+
s.basic_auth = basic_auth
86+
s.mu.Unlock()
8287
return nil
8388
}
8489

@@ -198,6 +203,8 @@ func (s *smtpSession) publishMessage(m *message) error {
198203
}
199204
if s.token != "" {
200205
req.Header.Add("Authorization", "Bearer "+s.token)
206+
} else if s.basic_auth != "" {
207+
req.Header.Add("Authorization", "Basic "+s.basic_auth)
201208
}
202209
rr := httptest.NewRecorder()
203210
s.backend.handler(rr, req)
@@ -214,6 +221,9 @@ func (s *smtpSession) Reset() {
214221
}
215222

216223
func (s *smtpSession) Logout() error {
224+
s.mu.Lock()
225+
s.basic_auth = ""
226+
s.mu.Unlock()
217227
return nil
218228
}
219229

server/smtp_server_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,28 @@ what's up
13861386
writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
13871387
}
13881388

1389+
func TestSmtpBackend_PlaintextWithPlainAuth(t *testing.T) {
1390+
email := `EHLO example.com
1391+
AUTH PLAIN dGVzdAB0ZXN0ADEyMzQ=
1392+
MAIL FROM: phil@example.com
1393+
RCPT TO: ntfy-mytopic@ntfy.sh
1394+
DATA
1395+
Subject: Very short mail
1396+
1397+
what's up
1398+
.
1399+
`
1400+
s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
1401+
require.Equal(t, "/mytopic", r.URL.Path)
1402+
require.Equal(t, "Very short mail", r.Header.Get("Title"))
1403+
require.Equal(t, "Basic dGVzdDoxMjM0", r.Header.Get("Authorization"))
1404+
require.Equal(t, "what's up", readAll(t, r.Body))
1405+
})
1406+
defer s.Close()
1407+
defer c.Close()
1408+
writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
1409+
}
1410+
13891411
type smtpHandlerFunc func(http.ResponseWriter, *http.Request)
13901412

13911413
func newTestSMTPServer(t *testing.T, handler smtpHandlerFunc) (s *smtp.Server, c net.Conn, conf *Config, scanner *bufio.Scanner) {

0 commit comments

Comments
 (0)