Skip to content

Commit

Permalink
fix: mail otp email template formatting and expiry (#249)
Browse files Browse the repository at this point in the history
Signed-off-by: Kush Sharma <thekushsharma@gmail.com>
  • Loading branch information
kushsharma committed Jun 1, 2023
1 parent 09bb80c commit 8ea72c5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ app:
validity: 10m
mail_otp:
subject: "Shield - Login Link"
body: "Please copy/paste the otp below to login.\n\n{{.Otp}}\n\nThis code will expire in 5 minutes."
# body is a go template with `Otp` as a variable
body: "Please copy/paste the OneTimePassword in login form.<h2>{{.Otp}}</h2>This code will expire in 10 minutes."
validity: 10m
# platform level administration
admin:
Expand Down
6 changes: 3 additions & 3 deletions core/authenticate/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ type Flow struct {
ExpiresAt time.Time
}

func (f Flow) IsValid() bool {
return f.ExpiresAt.Before(time.Now().UTC())
func (f Flow) IsValid(currentTime time.Time) bool {
return f.ExpiresAt.After(currentTime)
}

type Config struct {
Expand Down Expand Up @@ -83,6 +83,6 @@ type OIDCConfig struct {

type MailOTPConfig struct {
Subject string `yaml:"subject" mapstructure:"subject" default:"Shield Login OTP"`
Body string `yaml:"body" mapstructure:"body" default:"Shield Login Link"`
Body string `yaml:"body" mapstructure:"body" default:"Please copy/paste the OneTimePassword in login form.<h2>{{.Otp}}</h2>This code will expire in 10 minutes."`
Validity time.Duration `yaml:"validity" mapstructure:"validity" default:"10m"`
}
6 changes: 4 additions & 2 deletions core/authenticate/registration_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ func NewRegistrationService(logger log.Logger, config Config, flowRepo FlowRepos
userService: userService,
config: config,
mailDialer: mailDialer,
Now: time.Now().UTC,
Now: func() time.Time {
return time.Now().UTC()
},
}
return r
}
Expand Down Expand Up @@ -218,7 +220,7 @@ func (r RegistrationService) applyMail(ctx context.Context, request Registration
if err != nil {
return nil, fmt.Errorf("invalid state for mail otp: %w", err)
}
if !flow.IsValid() {
if !flow.IsValid(r.Now()) {
return nil, ErrFlowInvalid
}
if flow.Nonce != request.Code {
Expand Down
4 changes: 3 additions & 1 deletion core/authenticate/strategy/mail_otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func NewMailLink(d mailer.Dialer, subject, body string) *MailOTP {
dialer: d,
subject: subject,
body: body,
Now: time.Now().UTC,
Now: func() time.Time {
return time.Now().UTC()
},
}
}

Expand Down
4 changes: 3 additions & 1 deletion internal/store/postgres/invitation_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func NewInvitationRepository(logger log.Logger, dbc *db.Client) *InvitationRepos
return &InvitationRepository{
dbc: dbc,
log: logger,
Now: time.Now().UTC,
Now: func() time.Time {
return time.Now().UTC()
},
}
}

Expand Down

0 comments on commit 8ea72c5

Please sign in to comment.