Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support match_app_id #17

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/lib/config/sms_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,15 @@ type ProviderSelectorSwitchType string

const (
ProviderSelectorSwitchTypeMatchPhoneNumberAlpha2 ProviderSelectorSwitchType = "match_phone_number_alpha2"
ProviderSelectorSwitchTypeMatchAppID ProviderSelectorSwitchType = "match_app_id"
ProviderSelectorSwitchTypeMatchAppIDAndPhoneNumberAlpha2 ProviderSelectorSwitchType = "match_app_id_and_phone_number_alpha2"
ProviderSelectorSwitchTypeDefault ProviderSelectorSwitchType = "default"
)

var _ = RootSchema.Add("ProviderSelectorSwitchType", `
{
"type": "string",
"enum": ["match_phone_number_alpha2", "match_app_id_and_phone_number_alpha2", "default"]
"enum": ["match_phone_number_alpha2", "match_app_id", "match_app_id_and_phone_number_alpha2", "default"]
}
`)

Expand All @@ -174,6 +175,10 @@ var _ = RootSchema.Add("ProviderSelectorSwitchRule", `
"if": { "properties": { "type": { "const": "match_phone_number_alpha2" } }},
"then": { "required": ["phone_number_alpha2"] }
},
{
"if": { "properties": { "type": { "const": "match_app_id" } }},
"then": { "required": ["app_id"] }
},
{
"if": { "properties": { "type": { "const": "match_app_id_and_phone_number_alpha2" } }},
"then": { "required": ["phone_number_alpha2", "app_id"] }
Expand Down
7 changes: 7 additions & 0 deletions pkg/lib/config/testdata/authgear-sms-gateway.tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ config:
use_provider: sendcloud
phone_number_alpha2: CN
app_id: "123"
- type: match_app_id
use_provider: twilio
app_id: "123"
- type: match_phone_number_alpha2
use_provider: accessyou
phone_number_alpha2: HK
Expand Down Expand Up @@ -382,6 +385,8 @@ error: |-
/provider_selector/switch/0: required
map[actual:[type use_provider] expected:[phone_number_alpha2] missing:[phone_number_alpha2]]
/provider_selector/switch/1: required
map[actual:[type use_provider] expected:[app_id] missing:[app_id]]
/provider_selector/switch/2: required
map[actual:[type use_provider] expected:[app_id phone_number_alpha2] missing:[app_id phone_number_alpha2]]
config:
providers:
Expand All @@ -395,6 +400,8 @@ config:
switch:
- type: match_phone_number_alpha2
use_provider: twilio
- type: match_app_id
use_provider: twilio
- type: match_app_id_and_phone_number_alpha2
use_provider: twilio
- type: default
Expand Down
32 changes: 27 additions & 5 deletions pkg/lib/sms/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type MatcherPhoneNumberAlpha2 struct {
Code string
}

var _ Matcher = &MatcherPhoneNumberAlpha2{}

func (m *MatcherPhoneNumberAlpha2) Match(ctx *MatchContext) bool {
num, err := phonenumbers.Parse(ctx.PhoneNumber, "")
if err != nil {
Expand All @@ -30,13 +32,31 @@ func (m *MatcherPhoneNumberAlpha2) Match(ctx *MatchContext) bool {
return m.Code == regionCode
}

var _ Matcher = &MatcherPhoneNumberAlpha2{}
type MatcherAppID struct {
AppID string
}

var _ Matcher = &MatcherAppID{}

func (m *MatcherAppID) Match(ctx *MatchContext) bool {
if m.AppID == "" {
return false
}

if m.AppID == ctx.AppID {
return true
}

return false
}

type MatcherAppIDAndPhoneNumberAlpha2 struct {
AppID string
Code string
}

var _ Matcher = &MatcherAppIDAndPhoneNumberAlpha2{}

func (m *MatcherAppIDAndPhoneNumberAlpha2) Match(ctx *MatchContext) bool {
num, err := phonenumbers.Parse(ctx.PhoneNumber, "")
if err != nil {
Expand All @@ -50,22 +70,24 @@ func (m *MatcherAppIDAndPhoneNumberAlpha2) Match(ctx *MatchContext) bool {
return m.Code == regionCode && m.AppID == ctx.AppID
}

var _ Matcher = &MatcherAppIDAndPhoneNumberAlpha2{}

type MatcherDefault struct{}

var _ Matcher = &MatcherDefault{}

func (m *MatcherDefault) Match(ctx *MatchContext) bool {
return true
}

var _ Matcher = &MatcherDefault{}

func ParseMatcher(rule *config.ProviderSelectorSwitchRule) Matcher {
switch rule.Type {
case config.ProviderSelectorSwitchTypeMatchPhoneNumberAlpha2:
return &MatcherPhoneNumberAlpha2{
Code: rule.PhoneNumberAlpha2,
}
case config.ProviderSelectorSwitchTypeMatchAppID:
return &MatcherAppID{
AppID: rule.AppID,
}
case config.ProviderSelectorSwitchTypeMatchAppIDAndPhoneNumberAlpha2:
return &MatcherAppIDAndPhoneNumberAlpha2{
AppID: rule.AppID,
Expand Down
9 changes: 9 additions & 0 deletions pkg/lib/sms/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ provider_selector:
use_provider: p2
phone_number_alpha2: CN
app_id: "123"
- type: match_app_id
use_provider: p3
app_id: "use_p3"
- type: match_phone_number_alpha2
use_provider: p4
phone_number_alpha2: CN
Expand Down Expand Up @@ -79,6 +82,12 @@ func TestSelector(t *testing.T) {
&MatchContext{AppID: "123", PhoneNumber: "+8698765432"},
"p2",
)
test(
"App ID match. Pick p3",
config1,
&MatchContext{AppID: "use_p3", PhoneNumber: "+8698765432"},
"p3",
)
test(
"App ID and Country Code (HK) not match. Pick default",
config1,
Expand Down
Loading