Refactor threepid session types and SmtpMailer initialization

This commit is contained in:
Piotr Kozimor 2021-08-11 07:22:19 +02:00
parent 3827837d76
commit 5065e77dd7
6 changed files with 66 additions and 32 deletions

View file

@ -7,5 +7,7 @@ MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8 Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline Content-Disposition: inline
We have received request to reset password for you Matrix account. Please click following link to confirm your email:
{{.Link}} {{.Link}}
Alternatively pass following token to your Matrix client:
{{.Token}} {{.Token}}

View file

@ -7,5 +7,7 @@ MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8 Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline Content-Disposition: inline
We have received request to add this email to your Matrix account. Please click following link to confirm your email:
{{.Link}} {{.Link}}
Alternatively pass following token to your Matrix client:
{{.Token}} {{.Token}}

13
res/default/register.eml Normal file
View file

@ -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}}

View file

@ -23,13 +23,15 @@ import (
) )
const ( const (
// Verification is used in // AccountPassword 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
// - https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-account-password-email-requesttoken // - 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. // UserInternalAPI is the internal API for information about users and devices.
@ -464,3 +466,19 @@ type IsSessionValidatedResponse struct {
} }
type ThreepidSessionType int type ThreepidSessionType int
func (t ThreepidSessionType) Name() string {
return [...]string{
"account_password",
"account_threepid",
"register",
}[t]
}
func ThreepidSessionTypes() []ThreepidSessionType {
return []ThreepidSessionType{
AccountPassword,
AccountThreepid,
Register,
}
}

View file

@ -26,7 +26,7 @@ type Mailer interface {
} }
type SmtpMailer struct { type SmtpMailer struct {
conf config.EmailConf conf config.EmailConf
templates map[api.ThreepidSessionType]*template.Template templates []*template.Template
} }
type Mail struct { type Mail struct {
@ -78,25 +78,23 @@ func (m *SmtpMailer) send(mail *Mail, t *template.Template) error {
} }
func NewMailer(c *config.UserAPI) (Mailer, error) { func NewMailer(c *config.UserAPI) (Mailer, error) {
templateRaw, err := ioutil.ReadFile(fmt.Sprintf("%s/verification.eml", c.Email.TemplatesPath)) 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 { if err != nil {
return nil, err return nil, err
} }
verificationT, err := template.New("verification").Parse(string(templateRaw)) template, err := template.New(name).Parse(string(templateRaw))
if err != nil { if err != nil {
return nil, err return nil, err
} }
templateRaw, err = ioutil.ReadFile(fmt.Sprintf("%s/password.eml", c.Email.TemplatesPath)) templates[t] = template
if err != nil {
return nil, err
} }
passwordT, err := template.New("password").Parse(string(templateRaw))
return &SmtpMailer{ return &SmtpMailer{
conf: c.Email, conf: c.Email,
templates: map[api.ThreepidSessionType]*template.Template{ templates: templates,
api.Password: passwordT, }, nil
api.Verification: verificationT,
},
}, err
} }

View file

@ -36,15 +36,16 @@ var (
} }
ctx = context.Background() ctx = context.Background()
mailer = &testMailer{ mailer = &testMailer{
c: map[api.ThreepidSessionType]chan *mail.Mail{ c: []chan *mail.Mail{
api.Password: make(chan *mail.Mail, 3), api.AccountPassword: make(chan *mail.Mail, 3),
api.Verification: make(chan *mail.Mail, 3), api.AccountThreepid: make(chan *mail.Mail, 3),
api.Register: make(chan *mail.Mail, 3),
}, },
} }
) )
type testMailer struct { 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 { 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.NoErr(err)
is.Equal(len(resp.Sid), 43) is.Equal(len(resp.Sid), 43)
select { select {
case <-mailer.c[api.Verification]: case <-mailer.c[api.AccountPassword]:
t.Fatal("email was received, but sent attempt was not increased") t.Fatal("email was received, but sent attempt was not increased")
default: default:
break break
@ -188,7 +189,7 @@ func TestCreateSession_Twice_IncreaseSendAttempt(t *testing.T) {
err := internalApi.CreateSession(ctx, &testReqBumped, &resp) err := internalApi.CreateSession(ctx, &testReqBumped, &resp)
is.NoErr(err) is.NoErr(err)
is.Equal(len(resp.Sid), 43) is.Equal(len(resp.Sid), 43)
sub := <-mailer.c[api.Verification] sub := <-mailer.c[api.AccountPassword]
is.Equal(len(sub.Token), 64) is.Equal(len(sub.Token), 64)
is.Equal(sub.To, testReq.ThreePid) 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) err := i.CreateSession(ctx, testReq, resp)
is.NoErr(err) is.NoErr(err)
is.Equal(len(resp.Sid), 43) is.Equal(len(resp.Sid), 43)
sub := <-mailer.c[api.Verification] sub := <-mailer.c[api.AccountPassword]
is.Equal(len(sub.Token), 64) is.Equal(len(sub.Token), 64)
is.Equal(sub.To, testReq.ThreePid) is.Equal(sub.To, testReq.ThreePid)
token = sub.Token token = sub.Token