Open platform to collect and prioritize feedback
feature: allow support of Office 365 SMTP (#584)
Oxynux54e1c15bfdc4
diff --git a/.example.env b/.example.envindex d4878a71e..6a9a3ce4f 100644--- a/.example.env+++ b/.example.env@@ -22,4 +22,4 @@ EMAIL_MAILGUN_DOMAIN=EMAIL_SMTP_HOST=smtp.yourdomain.comEMAIL_SMTP_PORT=587EMAIL_SMTP_USERNAME=-EMAIL_SMTP_PASSWORD=+EMAIL_SMTP_PASSWORD=\ No newline at end of filediff --git a/app/middlewares/setup.go b/app/middlewares/setup.goindex 75f32e114..eac5812ba 100644--- a/app/middlewares/setup.go+++ b/app/middlewares/setup.go@@ -4,17 +4,16 @@ import ("fmt""time"+ "github.com/getfider/fider/app""github.com/getfider/fider/app/pkg/email""github.com/getfider/fider/app/pkg/email/mailgun""github.com/getfider/fider/app/pkg/email/noop""github.com/getfider/fider/app/pkg/email/smtp""github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/pkg/log"- "github.com/getfider/fider/app/pkg/worker"-- "github.com/getfider/fider/app""github.com/getfider/fider/app/pkg/web""github.com/getfider/fider/app/pkg/web/util"+ "github.com/getfider/fider/app/pkg/worker""github.com/getfider/fider/app/storage/postgres")diff --git a/app/pkg/email/smtp/auth.go b/app/pkg/email/smtp/auth.gonew file mode 100644index 000000000..d3b2e72c0--- /dev/null+++ b/app/pkg/email/smtp/auth.go@@ -0,0 +1,46 @@+package smtp++import (+ gosmtp "net/smtp"++ "github.com/getfider/fider/app/pkg/errors"+)++type agnosticAuth struct {+ identity, username, password, host string+ auth gosmtp.Auth+}++func (a *agnosticAuth) FindAuth(server *gosmtp.ServerInfo) gosmtp.Auth {+ for _, auth := range server.Auth {+ switch auth {+ case "LOGIN":+ return LoginAuth(a.username, a.password)+ case "PLAIN":+ return gosmtp.PlainAuth(a.identity, a.username, a.password, a.host)+ case "CRAM-MD5":+ return gosmtp.CRAMMD5Auth(a.username, a.password)+ default:+ continue+ }+ }+ return nil+}++// AgnosticAuth returns an Auth that match the correct authentication+// thanks to gosmtp.ServerInfo+func AgnosticAuth(identity, username, password, host string) gosmtp.Auth {+ return &agnosticAuth{identity, username, password, host, nil}+}++func (a *agnosticAuth) Start(server *gosmtp.ServerInfo) (string, []byte, error) {+ a.auth = a.FindAuth(server)+ if a.auth != nil {+ return a.auth.Start(server)+ }+ return "", nil, errors.New("server auth mechanism not supported")+}++func (a *agnosticAuth) Next(fromServer []byte, more bool) ([]byte, error) {+ return a.auth.Next(fromServer, more)+}diff --git a/app/pkg/email/smtp/auth_login.go b/app/pkg/email/smtp/auth_login.gonew file mode 100644index 000000000..238104756--- /dev/null+++ b/app/pkg/email/smtp/auth_login.go@@ -0,0 +1,32 @@+package smtp++import (+ gosmtp "net/smtp"+)++type loginAuth struct {+ username, password string+}++// LoginAuth returns an Auth that implements the LOGIN authentication+// mechanism as defined in Internet-Draft draft-murchison-sasl-login-00.+// The LOGIN mechanism is still used by some SMTP server.+func LoginAuth(username, password string) gosmtp.Auth {+ return &loginAuth{username, password}+}++func (a *loginAuth) Start(server *gosmtp.ServerInfo) (string, []byte, error) {+ return "LOGIN", []byte(a.username), nil+}++func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {+ if more {+ switch string(fromServer) {+ case "Username:":+ return []byte(a.username), nil+ case "Password:":+ return []byte(a.password), nil+ }+ }+ return nil, nil+}diff --git a/app/pkg/email/smtp/auth_test.go b/app/pkg/email/smtp/auth_test.gonew file mode 100644index 000000000..59afe089c--- /dev/null+++ b/app/pkg/email/smtp/auth_test.go@@ -0,0 +1,43 @@+package smtp_test++import (+ gosmtp "net/smtp"+ "testing"++ . "github.com/getfider/fider/app/pkg/assert"+ "github.com/getfider/fider/app/pkg/email/smtp"+)++func TestAgnosticAuth_Login(t *testing.T) {+ RegisterT(t)++ auth := smtp.AgnosticAuth("", "jon", "s3cr3t", "test.fider.io")+ proto, bytes, err := auth.Start(&gosmtp.ServerInfo{+ Auth: []string{"LOGIN"},+ })++ Expect(err).IsNil()+ Expect(proto).Equals("LOGIN")+ Expect(bytes).Equals([]byte("jon"))++ bytes, err = auth.Next([]byte("Username:"), true)+ Expect(err).IsNil()+ Expect(bytes).Equals([]byte("jon"))++ bytes, err = auth.Next([]byte("Password:"), true)+ Expect(err).IsNil()+ Expect(bytes).Equals([]byte("s3cr3t"))+}++func TestAgnosticAuth_NoMatchingAuth(t *testing.T) {+ RegisterT(t)++ auth := smtp.AgnosticAuth("", "jon", "s3cr3t", "test.fider.io")+ proto, bytes, err := auth.Start(&gosmtp.ServerInfo{+ Auth: []string{"FAKE-MD5"},+ })++ Expect(err).IsNotNil()+ Expect(proto).IsEmpty()+ Expect(bytes).IsNil()+}diff --git a/app/pkg/email/smtp/smtp.go b/app/pkg/email/smtp/smtp.goindex 6b8c2949e..b62e0f43c 100644--- a/app/pkg/email/smtp/smtp.go+++ b/app/pkg/email/smtp/smtp.go@@ -20,7 +20,7 @@ func authenticate(username string, password string, host string) gosmtp.Auth {if username == "" && password == "" {return nil}- return gosmtp.PlainAuth("", username, password, host)+ return AgnosticAuth("", username, password, host)}type builder struct {diff --git a/app/pkg/email/smtp/smtp_test.go b/app/pkg/email/smtp/smtp_test.goindex 005b570c1..1c6c63298 100644--- a/app/pkg/email/smtp/smtp_test.go+++ b/app/pkg/email/smtp/smtp_test.go@@ -5,13 +5,12 @@ import ("regexp""testing"- "github.com/getfider/fider/app/pkg/worker"-"github.com/getfider/fider/app/models". "github.com/getfider/fider/app/pkg/assert""github.com/getfider/fider/app/pkg/email""github.com/getfider/fider/app/pkg/email/smtp""github.com/getfider/fider/app/pkg/log/noop"+ "github.com/getfider/fider/app/pkg/worker")type request struct {@@ -58,7 +57,7 @@ func TestSend_Success(t *testing.T) {Expect(err).IsNil()Expect(requests).HasLen(1)Expect(requests[0].servername).Equals("localhost:1234")- Expect(requests[0].auth).Equals(gosmtp.PlainAuth("", "us3r", "p4ss", "localhost"))+ Expect(requests[0].auth).Equals(smtp.AgnosticAuth("", "us3r", "p4ss", "localhost"))Expect(requests[0].from).Equals("[email protected]")Expect(requests[0].to).Equals([]string{"[email protected]"})Expect(string(requests[0].body)).ContainsSubstring("From: \"Fider Test\" <[email protected]>\r\nReply-To: [email protected]\r\nTo: \"Jon Sow\" <[email protected]>\r\nSubject: Message to: Hello\r\nMIME-version: 1.0\r\nContent-Type: text/html; charset=\"UTF-8\"\r\nDate: ")@@ -129,7 +128,7 @@ func TestBatch_Success(t *testing.T) {Expect(requests).HasLen(2)Expect(requests[0].servername).Equals("localhost:1234")- Expect(requests[0].auth).Equals(gosmtp.PlainAuth("", "us3r", "p4ss", "localhost"))+ Expect(requests[0].auth).Equals(smtp.AgnosticAuth("", "us3r", "p4ss", "localhost"))Expect(requests[0].from).Equals("[email protected]")Expect(requests[0].to).Equals([]string{"[email protected]"})Expect(string(requests[0].body)).ContainsSubstring("From: \"Fider Test\" <[email protected]>\r\nReply-To: [email protected]\r\nTo: \"Jon Sow\" <[email protected]>\r\nSubject: Message to: Jon\r\nMIME-version: 1.0\r\nContent-Type: text/html; charset=\"UTF-8\"\r\nDate: ")@@ -137,7 +136,7 @@ func TestBatch_Success(t *testing.T) {Expect(string(requests[0].body)).ContainsSubstring("Hello World Jon!")Expect(requests[1].servername).Equals("localhost:1234")- Expect(requests[1].auth).Equals(gosmtp.PlainAuth("", "us3r", "p4ss", "localhost"))+ Expect(requests[1].auth).Equals(smtp.AgnosticAuth("", "us3r", "p4ss", "localhost"))Expect(requests[1].from).Equals("[email protected]")Expect(requests[1].to).Equals([]string{"[email protected]"})Expect(string(requests[1].body)).ContainsSubstring("From: \"Fider Test\" <[email protected]>\r\nReply-To: [email protected]\r\nTo: \"Arya Stark\" <[email protected]>\r\nSubject: Message to: Arya\r\nMIME-version: 1.0\r\nContent-Type: text/html; charset=\"UTF-8\"\r\nDate: ")