Skip to content

Commit

Permalink
feat: add request URL to email and SMS templates
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Feb 12, 2024
1 parent dbb3a7e commit bf5f8c3
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 29 deletions.
7 changes: 4 additions & 3 deletions courier/template/email/login_code_valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ type (
model *LoginCodeValidModel
}
LoginCodeValidModel struct {
To string `json:"to"`
LoginCode string `json:"login_code"`
Identity map[string]interface{} `json:"identity"`
To string `json:"to"`
LoginCode string `json:"login_code"`
Identity map[string]interface{} `json:"identity"`
RequestURL string `json:"request_url"`
}
)

Expand Down
3 changes: 2 additions & 1 deletion courier/template/email/recovery_code_invalid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type (
model *RecoveryCodeInvalidModel
}
RecoveryCodeInvalidModel struct {
To string `json:"to"`
To string `json:"to"`
RequestURL string `json:"request_url"`
}
)

Expand Down
1 change: 1 addition & 0 deletions courier/template/email/recovery_code_valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
To string `json:"to"`
RecoveryCode string `json:"recovery_code"`
Identity map[string]interface{} `json:"identity"`
RequestURL string `json:"request_url"`
}
)

Expand Down
3 changes: 2 additions & 1 deletion courier/template/email/recovery_invalid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type (
m *RecoveryInvalidModel
}
RecoveryInvalidModel struct {
To string `json:"to"`
To string `json:"to"`
RequestURL string `json:"request_url"`
}
)

Expand Down
1 change: 1 addition & 0 deletions courier/template/email/recovery_valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
To string `json:"to"`
RecoveryURL string `json:"recovery_url"`
Identity map[string]interface{} `json:"identity"`
RequestURL string `json:"request_url"`
}
)

Expand Down
1 change: 1 addition & 0 deletions courier/template/email/registration_code_valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
To string `json:"to"`
Traits map[string]interface{} `json:"traits"`
RegistrationCode string `json:"registration_code"`
RequestURL string `json:"request_url"`
}
)

Expand Down
3 changes: 2 additions & 1 deletion courier/template/email/verification_code_invalid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type (
m *VerificationCodeInvalidModel
}
VerificationCodeInvalidModel struct {
To string `json:"to"`
To string `json:"to"`
RequestURL string `json:"request_url"`
}
)

Expand Down
1 change: 1 addition & 0 deletions courier/template/email/verification_code_valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
VerificationURL string `json:"verification_url"`
VerificationCode string `json:"verification_code"`
Identity map[string]interface{} `json:"identity"`
RequestURL string `json:"request_url"`
}
)

Expand Down
3 changes: 2 additions & 1 deletion courier/template/email/verification_invalid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type (
m *VerificationInvalidModel
}
VerificationInvalidModel struct {
To string `json:"to"`
To string `json:"to"`
RequestURL string `json:"request_url"`
}
)

Expand Down
1 change: 1 addition & 0 deletions courier/template/email/verification_valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
To string `json:"to"`
VerificationURL string `json:"verification_url"`
Identity map[string]interface{} `json:"identity"`
RequestURL string `json:"request_url"`
}
)

Expand Down
7 changes: 4 additions & 3 deletions courier/template/sms/login_code_valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type (
model *LoginCodeValidModel
}
LoginCodeValidModel struct {
To string `json:"to"`
LoginCode string `json:"login_code"`
Identity map[string]interface{} `json:"identity"`
To string `json:"to"`
LoginCode string `json:"login_code"`
Identity map[string]interface{} `json:"identity"`
RequestURL string `json:"request_url"`
}
)

Expand Down
1 change: 1 addition & 0 deletions courier/template/sms/verification_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
To string `json:"to"`
VerificationCode string `json:"verification_code"`
Identity map[string]interface{} `json:"identity"`
RequestURL string `json:"request_url"`
}
)

Expand Down
32 changes: 22 additions & 10 deletions selfservice/strategy/code/code_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (s *Sender) SendCode(ctx context.Context, f flow.Flow, id *identity.Identit
To: address.To,
RegistrationCode: rawCode,
Traits: model,
RequestURL: f.GetRequestURL(),
}

s.deps.Audit().
Expand Down Expand Up @@ -143,15 +144,17 @@ func (s *Sender) SendCode(ctx context.Context, f flow.Flow, id *identity.Identit
switch address.Via {
case identity.ChannelTypeEmail:
t = email.NewLoginCodeValid(s.deps, &email.LoginCodeValidModel{
To: address.To,
LoginCode: rawCode,
Identity: model,
To: address.To,
LoginCode: rawCode,
Identity: model,
RequestURL: f.GetRequestURL(),
})
case identity.ChannelTypeSMS:
t = sms.NewLoginCodeValid(s.deps, &sms.LoginCodeValidModel{
To: address.To,
LoginCode: rawCode,
Identity: model,
To: address.To,
LoginCode: rawCode,
Identity: model,
RequestURL: f.GetRequestURL(),
})
}

Expand Down Expand Up @@ -189,7 +192,10 @@ func (s *Sender) SendRecoveryCode(ctx context.Context, f *recovery.Flow, via ide
Info("Account recovery was requested for an unknown address.")
if !notifyUnknownRecipients {
// do nothing
} else if err := s.send(ctx, string(via), email.NewRecoveryCodeInvalid(s.deps, &email.RecoveryCodeInvalidModel{To: to})); err != nil {
} else if err := s.send(ctx, string(via), email.NewRecoveryCodeInvalid(s.deps, &email.RecoveryCodeInvalidModel{
To: to,
RequestURL: f.RequestURL,
})); err != nil {
return err
}
return errors.WithStack(ErrUnknownAddress)
Expand Down Expand Up @@ -220,10 +226,10 @@ func (s *Sender) SendRecoveryCode(ctx context.Context, f *recovery.Flow, via ide
return err
}

return s.SendRecoveryCodeTo(ctx, i, rawCode, code)
return s.SendRecoveryCodeTo(ctx, i, rawCode, code, f)
}

func (s *Sender) SendRecoveryCodeTo(ctx context.Context, i *identity.Identity, codeString string, code *RecoveryCode) error {
func (s *Sender) SendRecoveryCodeTo(ctx context.Context, i *identity.Identity, codeString string, code *RecoveryCode, f flow.Flow) error {
s.deps.Audit().
WithField("via", code.RecoveryAddress.Via).
WithField("identity_id", code.RecoveryAddress.IdentityID).
Expand All @@ -241,6 +247,7 @@ func (s *Sender) SendRecoveryCodeTo(ctx context.Context, i *identity.Identity, c
To: code.RecoveryAddress.Value,
RecoveryCode: codeString,
Identity: model,
RequestURL: f.GetRequestURL(),
}

return s.send(ctx, string(code.RecoveryAddress.Via), email.NewRecoveryCodeValid(s.deps, &emailModel))
Expand Down Expand Up @@ -268,7 +275,10 @@ func (s *Sender) SendVerificationCode(ctx context.Context, f *verification.Flow,
Info("Address verification was requested for an unknown address.")
if !notifyUnknownRecipients {
// do nothing
} else if err := s.send(ctx, string(via), email.NewVerificationCodeInvalid(s.deps, &email.VerificationCodeInvalidModel{To: to})); err != nil {
} else if err := s.send(ctx, string(via), email.NewVerificationCodeInvalid(s.deps, &email.VerificationCodeInvalidModel{
To: to,
RequestURL: f.GetRequestURL(),
})); err != nil {
return err
}
return errors.WithStack(ErrUnknownAddress)
Expand Down Expand Up @@ -333,12 +343,14 @@ func (s *Sender) SendVerificationCodeTo(ctx context.Context, f *verification.Flo
VerificationURL: s.constructVerificationLink(ctx, f.ID, codeString),
Identity: model,
VerificationCode: codeString,
RequestURL: f.GetRequestURL(),
})
case identity.ChannelTypeSMS:
t = sms.NewVerificationCodeValid(s.deps, &sms.VerificationCodeValidModel{
To: code.VerifiableAddress.Value,
VerificationCode: codeString,
Identity: model,
RequestURL: f.GetRequestURL(),
})
default:
return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Expected email or sms but got %s", code.VerifiableAddress.Via))
Expand Down
32 changes: 23 additions & 9 deletions selfservice/strategy/link/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ func (s *Sender) SendRecoveryLink(ctx context.Context, f *recovery.Flow, via ide
Info("Account recovery was requested for an unknown address.")
if !notifyUnknownRecipients {
// do nothing
} else if err := s.send(ctx, string(via), email.NewRecoveryInvalid(s.r, &email.RecoveryInvalidModel{To: to})); err != nil {
} else if err := s.send(ctx, string(via), email.NewRecoveryInvalid(s.r, &email.RecoveryInvalidModel{
To: to,
RequestURL: f.GetRequestURL(),
})); err != nil {
return err
}
return errors.WithStack(ErrUnknownAddress)
Expand Down Expand Up @@ -128,7 +131,10 @@ func (s *Sender) SendVerificationLink(ctx context.Context, f *verification.Flow,
Info("Address verification was requested for an unknown address.")
if !notifyUnknownRecipients {
// do nothing
} else if err := s.send(ctx, string(via), email.NewVerificationInvalid(s.r, &email.VerificationInvalidModel{To: to})); err != nil {
} else if err := s.send(ctx, string(via), email.NewVerificationInvalid(s.r, &email.VerificationInvalidModel{
To: to,
RequestURL: f.GetRequestURL(),
})); err != nil {
return err
}
return errors.WithStack(ErrUnknownAddress)
Expand Down Expand Up @@ -174,7 +180,10 @@ func (s *Sender) SendRecoveryTokenTo(ctx context.Context, f *recovery.Flow, i *i
url.Values{
"token": {token.Token},
"flow": {f.ID.String()},
}).String(), Identity: model}))
}).String(),
Identity: model,
RequestURL: f.GetRequestURL(),
}))
}

func (s *Sender) SendVerificationTokenTo(ctx context.Context, f *verification.Flow, i *identity.Identity, address *identity.VerifiableAddress, token *VerificationToken) error {
Expand All @@ -192,12 +201,17 @@ func (s *Sender) SendVerificationTokenTo(ctx context.Context, f *verification.Fl
}

if err := s.send(ctx, string(address.Via), email.NewVerificationValid(s.r,
&email.VerificationValidModel{To: address.Value, VerificationURL: urlx.CopyWithQuery(
urlx.AppendPaths(s.r.Config().SelfServiceLinkMethodBaseURL(ctx), verification.RouteSubmitFlow),
url.Values{
"flow": {f.ID.String()},
"token": {token.Token},
}).String(), Identity: model})); err != nil {
&email.VerificationValidModel{
To: address.Value,
VerificationURL: urlx.CopyWithQuery(
urlx.AppendPaths(s.r.Config().SelfServiceLinkMethodBaseURL(ctx), verification.RouteSubmitFlow),
url.Values{
"flow": {f.ID.String()},
"token": {token.Token},
}).String(),
Identity: model,
RequestURL: f.GetRequestURL(),
})); err != nil {
return err
}
address.Status = identity.VerifiableAddressStatusSent
Expand Down

0 comments on commit bf5f8c3

Please sign in to comment.