diff --git a/res/default/password.eml b/res/default/account_password.eml similarity index 57% rename from res/default/password.eml rename to res/default/account_password.eml index 863e39e65..98c5fc9af 100644 --- a/res/default/password.eml +++ b/res/default/account_password.eml @@ -7,5 +7,7 @@ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline +We have received request to reset password for you Matrix account. Please click following link to confirm your email: {{.Link}} +Alternatively pass following token to your Matrix client: {{.Token}} diff --git a/res/default/verification.eml b/res/default/account_threepid.eml similarity index 57% rename from res/default/verification.eml rename to res/default/account_threepid.eml index 863e39e65..a540a22c3 100644 --- a/res/default/verification.eml +++ b/res/default/account_threepid.eml @@ -7,5 +7,7 @@ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline +We have received request to add this email to your Matrix account. Please click following link to confirm your email: {{.Link}} +Alternatively pass following token to your Matrix client: {{.Token}} diff --git a/res/default/register.eml b/res/default/register.eml new file mode 100644 index 000000000..773264810 --- /dev/null +++ b/res/default/register.eml @@ -0,0 +1,13 @@ +Date: {{.Date}} +From: noreply@example.com +To: {{.To}} +Message-ID: <{{.MessageId}}> +Subject: Confirm your email address for Matrix +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Disposition: inline + +We have received request to register account in Matrix server using this email. Please click following link to finish registration: +{{.Link}} +Alternatively pass following token to your Matrix client: +{{.Token}} \ No newline at end of file diff --git a/userapi/api/api.go b/userapi/api/api.go index 72177a80a..4fbe63d2e 100644 --- a/userapi/api/api.go +++ b/userapi/api/api.go @@ -23,13 +23,15 @@ import ( ) const ( - // Verification is used in - // - https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-account-3pid-email-requesttoken - // - https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-register-email-requesttoken - Verification ThreepidSessionType = iota - // Password is used in + // AccountPassword is used in // - https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-account-password-email-requesttoken - Password + AccountPassword ThreepidSessionType = iota + // AccountThreepid is used in + // - https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-account-3pid-email-requesttoken + AccountThreepid + // Register is used in + // - https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-register-email-requesttoken + Register ) // UserInternalAPI is the internal API for information about users and devices. @@ -464,3 +466,19 @@ type IsSessionValidatedResponse struct { } type ThreepidSessionType int + +func (t ThreepidSessionType) Name() string { + return [...]string{ + "account_password", + "account_threepid", + "register", + }[t] +} + +func ThreepidSessionTypes() []ThreepidSessionType { + return []ThreepidSessionType{ + AccountPassword, + AccountThreepid, + Register, + } +} diff --git a/userapi/mail/mail.go b/userapi/mail/mail.go index 33cc84f69..c3f8b5abe 100644 --- a/userapi/mail/mail.go +++ b/userapi/mail/mail.go @@ -26,7 +26,7 @@ type Mailer interface { } type SmtpMailer struct { conf config.EmailConf - templates map[api.ThreepidSessionType]*template.Template + templates []*template.Template } type Mail struct { @@ -78,25 +78,23 @@ func (m *SmtpMailer) send(mail *Mail, t *template.Template) error { } func NewMailer(c *config.UserAPI) (Mailer, error) { - templateRaw, err := ioutil.ReadFile(fmt.Sprintf("%s/verification.eml", c.Email.TemplatesPath)) - if err != nil { - return nil, err + sessionTypes := api.ThreepidSessionTypes() + templates := make([]*template.Template, len(sessionTypes)) + for _, t := range sessionTypes { + name := t.Name() + templateRaw, err := ioutil.ReadFile(fmt.Sprintf("%s/%s.eml", c.Email.TemplatesPath, name)) + if err != nil { + return nil, err + } + template, err := template.New(name).Parse(string(templateRaw)) + if err != nil { + return nil, err + } + templates[t] = template } - verificationT, err := template.New("verification").Parse(string(templateRaw)) - if err != nil { - return nil, err - } - templateRaw, err = ioutil.ReadFile(fmt.Sprintf("%s/password.eml", c.Email.TemplatesPath)) - if err != nil { - return nil, err - } - passwordT, err := template.New("password").Parse(string(templateRaw)) return &SmtpMailer{ - conf: c.Email, - templates: map[api.ThreepidSessionType]*template.Template{ - api.Password: passwordT, - api.Verification: verificationT, - }, - }, err + conf: c.Email, + templates: templates, + }, nil } diff --git a/userapi/userapi_test.go b/userapi/userapi_test.go index c7a68db30..ade81ec2a 100644 --- a/userapi/userapi_test.go +++ b/userapi/userapi_test.go @@ -36,15 +36,16 @@ var ( } ctx = context.Background() mailer = &testMailer{ - c: map[api.ThreepidSessionType]chan *mail.Mail{ - api.Password: make(chan *mail.Mail, 3), - api.Verification: make(chan *mail.Mail, 3), + c: []chan *mail.Mail{ + api.AccountPassword: make(chan *mail.Mail, 3), + api.AccountThreepid: make(chan *mail.Mail, 3), + api.Register: make(chan *mail.Mail, 3), }, } ) type testMailer struct { - c map[api.ThreepidSessionType]chan *mail.Mail + c []chan *mail.Mail } func (tm *testMailer) Send(s *mail.Mail, t api.ThreepidSessionType) error { @@ -171,7 +172,7 @@ func TestCreateSession_Twice(t *testing.T) { is.NoErr(err) is.Equal(len(resp.Sid), 43) select { - case <-mailer.c[api.Verification]: + case <-mailer.c[api.AccountPassword]: t.Fatal("email was received, but sent attempt was not increased") default: break @@ -188,7 +189,7 @@ func TestCreateSession_Twice_IncreaseSendAttempt(t *testing.T) { err := internalApi.CreateSession(ctx, &testReqBumped, &resp) is.NoErr(err) is.Equal(len(resp.Sid), 43) - sub := <-mailer.c[api.Verification] + sub := <-mailer.c[api.AccountPassword] is.Equal(len(sub.Token), 64) is.Equal(sub.To, testReq.ThreePid) } @@ -234,7 +235,7 @@ func mustCreateSession(is *is.I, i *internal.UserInternalAPI) (resp *api.CreateS err := i.CreateSession(ctx, testReq, resp) is.NoErr(err) is.Equal(len(resp.Sid), 43) - sub := <-mailer.c[api.Verification] + sub := <-mailer.c[api.AccountPassword] is.Equal(len(sub.Token), 64) is.Equal(sub.To, testReq.ThreePid) token = sub.Token