diff --git a/internal/email/confirm.go b/internal/email/confirm.go new file mode 100644 index 000000000..06b5bc1c6 --- /dev/null +++ b/internal/email/confirm.go @@ -0,0 +1,52 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package email + +import ( + "net/smtp" +) + +const ( + confirmTemplate = "email_confirm.tmpl" + confirmSubject = "Subject: GoToSocial Email Confirmation" +) + +func (s *sender) SendConfirmEmail(toAddress string, data ConfirmData) error { + confirmBody, err := s.ExecuteTemplate(confirmTemplate, data) + if err != nil { + return err + } + + msg := AssembleMessage(confirmSubject, confirmBody) + + return smtp.SendMail(s.hostAddress, s.auth, s.from, []string{toAddress}, msg) +} + +// ConfirmData represents data passed into the confirm email address template. +type ConfirmData struct { + // Username to be addressed. + Username string + // URL of the instance to present to the receiver. + InstanceURL string + // Name of the instance to present to the receiver. + InstanceName string + // Link to present to the receiver to click on and do the confirmation. + // Should be a full link with protocol eg., https://example.org/confirm_email?token=some-long-token + ConfirmLink string +} diff --git a/internal/email/email.go b/internal/email/email.go index 3d6a9dd2d..86d13c621 100644 --- a/internal/email/email.go +++ b/internal/email/email.go @@ -16,5 +16,62 @@ along with this program. If not, see . */ -// Package email provides a service for interacting with an SMTP server package email + +import ( + "fmt" + "html/template" + "net/smtp" + "os" + "path/filepath" + + "github.com/superseriousbusiness/gotosocial/internal/config" +) + +// Sender contains functions for sending emails to instance users/new signups. +type Sender interface { + // SendConfirmEmail sends a 'please confirm your email' style email to the given toAddress, with the given data. + SendConfirmEmail(toAddress string, data ConfirmData) error + + // SendResetEmail sends a 'reset your password' style email to the given toAddress, with the given data. + SendResetEmail(toAddress string, data ResetData) error + + // ExecuteTemplate returns templated HTML using the given templateName and data. Mostly you won't need to call this, + // and can just call one of the 'Send' functions instead (which calls this under the hood anyway). + ExecuteTemplate(templateName string, data interface{}) (string, error) +} + +func NewSender(cfg *config.Config) (Sender, error) { + t, err := loadTemplates(cfg) + if err != nil { + return nil, err + } + + auth := smtp.PlainAuth("", cfg.SMTPConfig.Username, cfg.SMTPConfig.Password, cfg.SMTPConfig.Host) + + return &sender{ + hostAddress: fmt.Sprintf("%s:%d", cfg.SMTPConfig.Host, cfg.SMTPConfig.Port), + from: cfg.SMTPConfig.From, + auth: auth, + template: t, + }, nil +} + +type sender struct { + hostAddress string + from string + auth smtp.Auth + template *template.Template +} + +// loadTemplates loads html templates for use in emails +func loadTemplates(cfg *config.Config) (*template.Template, error) { + cwd, err := os.Getwd() + if err != nil { + return nil, fmt.Errorf("error getting current working directory: %s", err) + } + + // look for all templates that start with 'email_' + tmPath := filepath.Join(cwd, fmt.Sprintf("%semail_*", cfg.TemplateConfig.BaseDir)) + return template.ParseGlob(tmPath) +} diff --git a/internal/email/email_test.go b/internal/email/email_test.go new file mode 100644 index 000000000..54d7e7144 --- /dev/null +++ b/internal/email/email_test.go @@ -0,0 +1,36 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package email_test + +import ( + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/email" + "github.com/superseriousbusiness/gotosocial/testrig" +) + +type EmailTestSuite struct { + suite.Suite + + sender email.Sender +} + +func (suite *EmailTestSuite) SetupTest() { + testrig.InitTestLog() + suite.sender = testrig.NewEmailSender("../../web/template/") +} diff --git a/internal/email/reset.go b/internal/email/reset.go new file mode 100644 index 000000000..473223867 --- /dev/null +++ b/internal/email/reset.go @@ -0,0 +1,52 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package email + +import ( + "net/smtp" +) + +const ( + resetTemplate = "email_reset.tmpl" + resetSubject = "Subject: GoToSocial Password Reset" +) + +func (s *sender) SendResetEmail(toAddress string, data ResetData) error { + resetBody, err := s.ExecuteTemplate(resetTemplate, data) + if err != nil { + return err + } + + msg := AssembleMessage(resetSubject, resetBody) + + return smtp.SendMail(s.hostAddress, s.auth, s.from, []string{toAddress}, msg) +} + +// ResetData represents data passed into the reset email address template. +type ResetData struct { + // Username to be addressed. + Username string + // URL of the instance to present to the receiver. + InstanceURL string + // Name of the instance to present to the receiver. + InstanceName string + // Link to present to the receiver to click on and begin the reset process. + // Should be a full link with protocol eg., https://example.org/reset_password?token=some-reset-password-token + ResetLink string +} diff --git a/internal/email/util.go b/internal/email/util.go new file mode 100644 index 000000000..460a27f9e --- /dev/null +++ b/internal/email/util.go @@ -0,0 +1,45 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package email + +import "bytes" + +const ( + mime = `MIME-version: 1.0; +Content-Type: text/plain; charset="UTF-8";` +) + +func (s *sender) ExecuteTemplate(templateName string, data interface{}) (string, error) { + buf := &bytes.Buffer{} + if err := s.template.ExecuteTemplate(buf, templateName, data); err != nil { + return "", err + } + return buf.String(), nil +} + +// AssembleMessage concacenates the mailSubject, the mime header, and the mailBody in +// an appropriate format for sending via net/smtp. +func AssembleMessage(mailSubject string, mailBody string) []byte { + msg := []byte( + mailSubject + "\r\n" + + mime + "\r\n" + + mailBody + "\r\n") + + return msg +} diff --git a/internal/email/util_test.go b/internal/email/util_test.go new file mode 100644 index 000000000..5d6077ecf --- /dev/null +++ b/internal/email/util_test.go @@ -0,0 +1,64 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package email_test + +import ( + "testing" + + "github.com/stretchr/testify/suite" + "github.com/superseriousbusiness/gotosocial/internal/email" +) + +type UtilTestSuite struct { + EmailTestSuite +} + +func (suite *UtilTestSuite) TestTemplateConfirm() { + confirmData := email.ConfirmData{ + Username: "test", + InstanceURL: "https://example.org", + InstanceName: "Test Instance", + ConfirmLink: "https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa", + } + mailBody, err := suite.sender.ExecuteTemplate("email_confirm.tmpl", confirmData) + suite.NoError(err) + suite.Equal("\n\n \n \n
\n

\n Hello test!\n

\n
\n
\n

\n You are receiving this mail because you've requested an account on Test Instance.\n

\n

\n We just need to confirm that this is your email address. To confirm your email, click here or paste the following in your browser's address bar:\n

\n

\n \n https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n \n

\n
\n
\n

\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of Test Instance.\n

\n
\n \n", mailBody) + + message := email.AssembleMessage("Subject: something", mailBody) + suite.Equal("Subject: something\r\nMIME-version: 1.0;\nContent-Type: text/plain; charset=\"UTF-8\";\r\n\n\n \n \n
\n

\n Hello test!\n

\n
\n
\n

\n You are receiving this mail because you've requested an account on Test Instance.\n

\n

\n We just need to confirm that this is your email address. To confirm your email, click here or paste the following in your browser's address bar:\n

\n

\n \n https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n \n

\n
\n
\n

\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of Test Instance.\n

\n
\n \n\r\n", string(message)) +} + +func (suite *UtilTestSuite) TestTemplateReset() { + resetData := email.ResetData{ + Username: "test", + InstanceURL: "https://example.org", + InstanceName: "Test Instance", + ResetLink: "https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa", + } + mailBody, err := suite.sender.ExecuteTemplate("email_reset.tmpl", resetData) + suite.NoError(err) + suite.Equal("\n\n \n \n
\n

\n Hello test!\n

\n
\n
\n

\n You are receiving this mail because a password reset has been requested for your account on Test Instance.\n

\n

\n To reset your password, click here or paste the following in your browser's address bar:\n

\n

\n \n https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n \n

\n
\n
\n

\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of Test Instance.\n

\n
\n \n", mailBody) + + message := email.AssembleMessage("Subject: something", mailBody) + suite.Equal("Subject: something\r\nMIME-version: 1.0;\nContent-Type: text/plain; charset=\"UTF-8\";\r\n\n\n \n \n
\n

\n Hello test!\n

\n
\n
\n

\n You are receiving this mail because a password reset has been requested for your account on Test Instance.\n

\n

\n To reset your password, click here or paste the following in your browser's address bar:\n

\n

\n \n https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n \n

\n
\n
\n

\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of Test Instance.\n

\n
\n \n\r\n", string(message)) +} + +func TestUtilTestSuite(t *testing.T) { + suite.Run(t, &UtilTestSuite{}) +}