From 09301c61f05e7144e6e4d6783ff7e39727629e4e Mon Sep 17 00:00:00 2001 From: Alex Tomlins Date: Mon, 16 Sep 2024 11:42:26 +0100 Subject: [PATCH] Add checking for auth to Server example. This was originally added to the README.md in 9e949e0, but was lost when the examples were removed from there. Re-add it so that it's clearer that this library doesn't enforce authentication by default. --- example_server_test.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/example_server_test.go b/example_server_test.go index f1f8c90..71f4a4f 100644 --- a/example_server_test.go +++ b/example_server_test.go @@ -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. @@ -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 {