Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
change SMS max length (#1295)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehelmick authored Dec 8, 2020
1 parent 7cedef0 commit d334e2a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cmd/server/assets/realmadmin/_form_codes.html
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@
The SMS message will be constructed based on the template you provide. The overall
length of of the SMS message should not exceed 160 characters, or your message will need to be split
in transit and may not be joined correctly. There are some special strings that you can use
to substitute items.
to substitute items.<br/>

If your choose to exceed 160 characters, your message will be broken up into
individual messages of 153 characters and reconstructed at the mobile device. The user may be
charged for each individual message. The overall maximum length of an SMS Template is {{.maxSMSTemplate}}
characters before expansion.
<br/>
{{if $realm.EnableENExpress}}
Your SMS template <em>MUST</em> contain <code>[enslink]</code>.
<ul>
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/realmadmin/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ func (c *Controller) renderSettings(
m["longCodeHours"] = longCodeHours
m["enxRedirectDomain"] = c.config.GetENXRedirectDomain()

m["maxSMSTemplate"] = database.SMSTemplateMaxLength

m["quotaLimit"] = quotaLimit
m["quotaRemaining"] = quotaRemaining

Expand Down
11 changes: 11 additions & 0 deletions pkg/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,17 @@ func (db *Database) getMigrations(ctx context.Context) *gormigrate.Gormigrate {
return tx.Exec(sql).Error
},
},
{
ID: "00072-ChangeSMSTemplateType",
Migrate: func(tx *gorm.DB) error {
logger.Debugw("changing type of SMS Template text")
return tx.Exec("ALTER TABLE realms ALTER COLUMN sms_text_template TYPE text").Error
},
Rollback: func(tx *gorm.DB) error {
// No rollback for this, as there is no reason to do so.
return nil
},
},
})
}

Expand Down
19 changes: 18 additions & 1 deletion pkg/database/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"math"
"net"
"os"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -81,6 +82,9 @@ const (
SMSLongExpires = "[longexpires]"
SMSENExpressLink = "[enslink]"

SMSTemplateMaxLength = 800
SMSTemplateExpansionMax = 918

EmailInviteLink = "[invitelink]"
EmailPasswordResetLink = "[passwordresetlink]"
EmailVerifyLink = "[verifylink]"
Expand Down Expand Up @@ -136,7 +140,7 @@ type Realm struct {
LongCodeDuration DurationSeconds `gorm:"type:bigint; not null; default: 86400"` // default 24h

// SMS configuration
SMSTextTemplate string `gorm:"type:varchar(400); not null; default: 'This is your Exposure Notifications Verification code: [longcode] Expires in [longexpires] hours'"`
SMSTextTemplate string `gorm:"type:text; not null; default: 'This is your Exposure Notifications Verification code: [longcode] Expires in [longexpires] hours'"`

// SMSCountry is an optional field to hint the default phone picker country
// code.
Expand Down Expand Up @@ -369,6 +373,19 @@ func (r *Realm) BeforeSave(tx *gorm.DB) error {
}
}

// Check template length.
if l := len(r.SMSTextTemplate); l > SMSTemplateMaxLength {
r.AddError("SMSTextTemplate", fmt.Sprintf("must be %v characters or less, current message is %v characters long", SMSTemplateMaxLength, l))
}
// Check expansion length based on settings.
fakeCode := fmt.Sprintf(fmt.Sprintf("\\%0%d\\%d", r.CodeLength), 0)
fakeLongCode := fmt.Sprintf(fmt.Sprintf("\\%0%d\\%d", r.LongCodeLength), 0)
enxDomain := os.Getenv("ENX_REDIRECT_DOMAIN")
expandedSMSText := r.BuildSMSText(fakeCode, fakeLongCode, enxDomain)
if l := len(expandedSMSText); l > SMSTemplateExpansionMax {
r.AddError("SMSTextTemplate", fmt.Sprintf("when expanded, the result message is too long (%v characters). The max expanded message is %v characters", l, SMSTemplateExpansionMax))
}

if r.UseSystemEmailConfig && !r.CanUseSystemEmailConfig {
r.AddError("useSystemEmailConfig", "is not allowed on this realm")
}
Expand Down

0 comments on commit d334e2a

Please sign in to comment.