Skip to content

Commit

Permalink
Drop Session.AuthPlain
Browse files Browse the repository at this point in the history
Server backends now need to implement the AuthSession interface
if they want to enable SASL PLAIN authentication.
  • Loading branch information
emersion committed Mar 28, 2024
1 parent 8063c1a commit 968926f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 35 deletions.
3 changes: 0 additions & 3 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ type Session interface {
// Free all resources associated with session.
Logout() error

// Authenticate the user using SASL PLAIN.
AuthPlain(username, password string) error

// Set return path for currently processed message.
Mail(from string, opts *MailOptions) error
// Add recipient for currently processed message.
Expand Down
15 changes: 13 additions & 2 deletions backendutil/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backendutil
import (
"io"

"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
)

Expand Down Expand Up @@ -33,8 +34,18 @@ func (s *transformSession) Reset() {
s.Session.Reset()
}

func (s *transformSession) AuthPlain(username, password string) error {
return s.Session.AuthPlain(username, password)
func (s *transformSession) AuthMechanisms() []string {
if authSession, ok := s.Session.(smtp.AuthSession); ok {
return authSession.AuthMechanisms()
}
return nil
}

func (s *transformSession) Auth(mech string) (sasl.Server, error) {
if authSession, ok := s.Session.(smtp.AuthSession); ok {
return authSession.Auth(mech)
}
return nil, smtp.ErrAuthUnsupported
}

func (s *transformSession) Mail(from string, opts *smtp.MailOptions) error {
Expand Down
22 changes: 16 additions & 6 deletions backendutil/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"testing"

"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
"github.com/emersion/go-smtp/backendutil"
)
Expand Down Expand Up @@ -48,12 +49,21 @@ func (s *session) Logout() error {
return nil
}

func (s *session) AuthPlain(username, password string) error {
if username != "username" || password != "password" {
return errors.New("Invalid username or password")
}
s.anonymous = false
return nil
func (s *session) AuthMechanisms() []string {
return []string{sasl.Plain}
}

func (s *session) Auth(mech string) (sasl.Server, error) {
return sasl.NewPlainServer(func(identity, username, password string) error {
if identity != "" && identity != username {
return errors.New("Invalid identity")
}
if username != "username" || password != "password" {
return errors.New("Invalid username or password")
}
s.anonymous = false
return nil
}), nil
}

func (s *session) Mail(from string, opts *smtp.MailOptions) error {
Expand Down
20 changes: 2 additions & 18 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,30 +856,14 @@ func (c *Conn) authMechanisms() []string {
if authSession, ok := c.Session().(AuthSession); ok {
return authSession.AuthMechanisms()
}
return []string{sasl.Plain}
return nil
}

func (c *Conn) auth(mech string) (sasl.Server, error) {
if authSession, ok := c.Session().(AuthSession); ok {
return authSession.Auth(mech)
}

if mech != sasl.Plain {
return nil, ErrAuthUnknownMechanism
}

return sasl.NewPlainServer(func(identity, username, password string) error {
if identity != "" && identity != username {
return errors.New("identities not supported")
}

sess := c.Session()
if sess == nil {
panic("No session when AUTH is called")
}

return sess.AuthPlain(username, password)
}), nil
return nil, ErrAuthUnknownMechanism
}

func (c *Conn) handleStartTLS() {
Expand Down
24 changes: 18 additions & 6 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"testing"

"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
)

Expand Down Expand Up @@ -66,12 +67,23 @@ type session struct {
msg *message
}

func (s *session) AuthPlain(username, password string) error {
if username != "username" || password != "password" {
return errors.New("Invalid username or password")
}
s.anonymous = false
return nil
var _ smtp.AuthSession = (*session)(nil)

func (s *session) AuthMechanisms() []string {
return []string{sasl.Plain}
}

func (s *session) Auth(mech string) (sasl.Server, error) {
return sasl.NewPlainServer(func(identity, username, password string) error {
if identity != "" && identity != username {
return errors.New("Invalid identity")
}
if username != "username" || password != "password" {
return errors.New("Invalid username or password")
}
s.anonymous = false
return nil
}), nil
}

func (s *session) Reset() {
Expand Down

0 comments on commit 968926f

Please sign in to comment.