Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checking for auth to Server example. #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion example_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func (bkd *Backend) NewSession(c *smtp.Conn) (smtp.Session, error) {
}

// A Session is returned after successful login.
type Session struct{}
type Session struct {
auth bool
}

// AuthMechanisms returns a slice of available auth mechanisms; only PLAIN is
// supported in this example.
Expand All @@ -33,21 +35,31 @@ func (s *Session) Auth(mech string) (sasl.Server, error) {
if username != "username" || password != "password" {
return errors.New("Invalid username or password")
}
s.auth = true
return nil
}), nil
}

func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
if !s.auth {
return smtp.ErrAuthRequired
}
log.Println("Mail from:", from)
return nil
}

func (s *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
if !s.auth {
return smtp.ErrAuthRequired
}
log.Println("Rcpt to:", to)
return nil
}

func (s *Session) Data(r io.Reader) error {
if !s.auth {
return smtp.ErrAuthRequired
}
if b, err := io.ReadAll(r); err != nil {
return err
} else {
Expand Down