Skip to content

Commit

Permalink
Update example server auth for AuthSession interface in 0.21.0 update
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonfriedland authored and emersion committed Apr 4, 2024
1 parent dd32c05 commit 53172ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
24 changes: 16 additions & 8 deletions example_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package smtp_test
import (
"errors"
"io"
"io/ioutil"
"log"
"time"

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

Expand All @@ -21,12 +21,20 @@ func (bkd *Backend) NewSession(c *smtp.Conn) (smtp.Session, error) {
// A Session is returned after successful login.
type Session struct{}

// AuthPlain implements authentication using SASL PLAIN.
func (s *Session) AuthPlain(username, password string) error {
if username != "username" || password != "password" {
return errors.New("Invalid username or password")
}
return nil
// AuthMechanisms returns a slice of available auth mechanisms; only PLAIN is
// supported in this example.
func (s *Session) AuthMechanisms() []string {
return []string{sasl.Plain}
}

// Auth is the handler for supported authenticators.
func (s *Session) Auth(mech string) (sasl.Server, error) {
return sasl.NewPlainServer(func(identity, username, password string) error {
if username != "username" || password != "password" {
return errors.New("Invalid username or password")
}
return nil
}), nil
}

func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
Expand All @@ -40,7 +48,7 @@ func (s *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
}

func (s *Session) Data(r io.Reader) error {
if b, err := ioutil.ReadAll(r); err != nil {
if b, err := io.ReadAll(r); err != nil {
return err
} else {
log.Println("Data:", string(b))
Expand Down
4 changes: 1 addition & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
"time"
)

var (
ErrServerClosed = errors.New("smtp: server already closed")
)
var ErrServerClosed = errors.New("smtp: server already closed")

// Logger interface is used by Server to report unexpected internal errors.
type Logger interface {
Expand Down

0 comments on commit 53172ad

Please sign in to comment.