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

Commit d334e2a

Browse files
authored
change SMS max length (#1295)
1 parent 7cedef0 commit d334e2a

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

cmd/server/assets/realmadmin/_form_codes.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,13 @@
203203
The SMS message will be constructed based on the template you provide. The overall
204204
length of of the SMS message should not exceed 160 characters, or your message will need to be split
205205
in transit and may not be joined correctly. There are some special strings that you can use
206-
to substitute items.
206+
to substitute items.<br/>
207+
208+
If your choose to exceed 160 characters, your message will be broken up into
209+
individual messages of 153 characters and reconstructed at the mobile device. The user may be
210+
charged for each individual message. The overall maximum length of an SMS Template is {{.maxSMSTemplate}}
211+
characters before expansion.
212+
<br/>
207213
{{if $realm.EnableENExpress}}
208214
Your SMS template <em>MUST</em> contain <code>[enslink]</code>.
209215
<ul>

pkg/controller/realmadmin/settings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ func (c *Controller) renderSettings(
452452
m["longCodeHours"] = longCodeHours
453453
m["enxRedirectDomain"] = c.config.GetENXRedirectDomain()
454454

455+
m["maxSMSTemplate"] = database.SMSTemplateMaxLength
456+
455457
m["quotaLimit"] = quotaLimit
456458
m["quotaRemaining"] = quotaRemaining
457459

pkg/database/migrations.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,17 @@ func (db *Database) getMigrations(ctx context.Context) *gormigrate.Gormigrate {
17361736
return tx.Exec(sql).Error
17371737
},
17381738
},
1739+
{
1740+
ID: "00072-ChangeSMSTemplateType",
1741+
Migrate: func(tx *gorm.DB) error {
1742+
logger.Debugw("changing type of SMS Template text")
1743+
return tx.Exec("ALTER TABLE realms ALTER COLUMN sms_text_template TYPE text").Error
1744+
},
1745+
Rollback: func(tx *gorm.DB) error {
1746+
// No rollback for this, as there is no reason to do so.
1747+
return nil
1748+
},
1749+
},
17391750
})
17401751
}
17411752

pkg/database/realm.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"math"
2222
"net"
23+
"os"
2324
"sort"
2425
"strings"
2526
"time"
@@ -81,6 +82,9 @@ const (
8182
SMSLongExpires = "[longexpires]"
8283
SMSENExpressLink = "[enslink]"
8384

85+
SMSTemplateMaxLength = 800
86+
SMSTemplateExpansionMax = 918
87+
8488
EmailInviteLink = "[invitelink]"
8589
EmailPasswordResetLink = "[passwordresetlink]"
8690
EmailVerifyLink = "[verifylink]"
@@ -136,7 +140,7 @@ type Realm struct {
136140
LongCodeDuration DurationSeconds `gorm:"type:bigint; not null; default: 86400"` // default 24h
137141

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

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

376+
// Check template length.
377+
if l := len(r.SMSTextTemplate); l > SMSTemplateMaxLength {
378+
r.AddError("SMSTextTemplate", fmt.Sprintf("must be %v characters or less, current message is %v characters long", SMSTemplateMaxLength, l))
379+
}
380+
// Check expansion length based on settings.
381+
fakeCode := fmt.Sprintf(fmt.Sprintf("\\%0%d\\%d", r.CodeLength), 0)
382+
fakeLongCode := fmt.Sprintf(fmt.Sprintf("\\%0%d\\%d", r.LongCodeLength), 0)
383+
enxDomain := os.Getenv("ENX_REDIRECT_DOMAIN")
384+
expandedSMSText := r.BuildSMSText(fakeCode, fakeLongCode, enxDomain)
385+
if l := len(expandedSMSText); l > SMSTemplateExpansionMax {
386+
r.AddError("SMSTextTemplate", fmt.Sprintf("when expanded, the result message is too long (%v characters). The max expanded message is %v characters", l, SMSTemplateExpansionMax))
387+
}
388+
372389
if r.UseSystemEmailConfig && !r.CanUseSystemEmailConfig {
373390
r.AddError("useSystemEmailConfig", "is not allowed on this realm")
374391
}

0 commit comments

Comments
 (0)