Skip to content

Commit

Permalink
feat: add test OTP support for mobile app reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
hf committed Jul 4, 2023
1 parent 154dd91 commit e772a40
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
43 changes: 28 additions & 15 deletions internal/api/phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,42 @@ func (a *API) sendPhoneConfirmation(ctx context.Context, tx *storage.Connection,
return "", internalServerError("invalid otp type")
}

// intentionally keeping this before the test OTP, so that the behavior
// of regular and test OTPs is similar
if sentAt != nil && !sentAt.Add(config.Sms.MaxFrequency).Before(time.Now()) {
return "", MaxFrequencyLimitError
}
oldToken := *token
otp, err := crypto.GenerateOtp(config.Sms.OtpLength)
if err != nil {
return "", internalServerError("error generating otp").WithInternalError(err)
}
*token = fmt.Sprintf("%x", sha256.Sum224([]byte(phone+otp)))

var message string
if config.Sms.Template == "" {
message = fmt.Sprintf(defaultSmsMessage, otp)
} else {
message = strings.Replace(config.Sms.Template, "{{ .Code }}", otp, -1)
var otp, messageID string

if config.External.Phone.Enabled && config.External.Phone.TestOTP != nil {
if testOTP, ok := config.External.Phone.TestOTP[phone]; ok && testOTP != "" {
otp = testOTP
messageID = "test-otp"
}
}

messageID, serr := smsProvider.SendMessage(phone, message, channel)
if serr != nil {
*token = oldToken
return messageID, serr
if otp == "" { // not using test OTPs
otp, err := crypto.GenerateOtp(config.Sms.OtpLength)
if err != nil {
return "", internalServerError("error generating otp").WithInternalError(err)
}

var message string
if config.Sms.Template == "" {
message = fmt.Sprintf(defaultSmsMessage, otp)
} else {
message = strings.Replace(config.Sms.Template, "{{ .Code }}", otp, -1)
}

messageID, err = smsProvider.SendMessage(phone, message, channel)
if err != nil {
return messageID, err
}
}

*token = fmt.Sprintf("%x", sha256.Sum224([]byte(phone+otp)))

now := time.Now()

switch otpType {
Expand Down
2 changes: 2 additions & 0 deletions internal/conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ type MailerConfiguration struct {

type PhoneProviderConfiguration struct {
Enabled bool `json:"enabled" default:"false"`

TestOTP map[string]string `json:"test_otp" split_words:"true"`
}

type SmsProviderConfiguration struct {
Expand Down

0 comments on commit e772a40

Please sign in to comment.