Skip to content

Commit

Permalink
Add support for rejecting sms reqs to countries
Browse files Browse the repository at this point in the history
Reject requests to send SMS to countries based on rules in the
config file

element-hq/element-web#3542
  • Loading branch information
dbkr committed Apr 21, 2017
1 parent bb9e3a3 commit ca303fe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
8 changes: 7 additions & 1 deletion sydent/http/servlets/msisdnservlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
from twisted.web.resource import Resource
import phonenumbers

from sydent.validators import IncorrectClientSecretException, SessionExpiredException
from sydent.validators import (
IncorrectClientSecretException, SessionExpiredException, DestinationRejectedException
)

from sydent.http.servlets import get_args, jsonwrap, send_cors

Expand Down Expand Up @@ -70,6 +72,10 @@ def render_POST(self, request):
sid = self.sydent.validators.msisdn.requestToken(
phone_number_object, clientSecret, sendAttempt, None
)
except DestinationRejectedException:
logger.error("Destination rejected for number: %s", msisdn);
request.setResponseCode(400)
resp = {'errcode': 'M_DESTINATION_REJECTED', 'error': 'Phone numbers in this country are not currently supported'}
except Exception as e:
logger.error("Exception sending SMS: %r", e);
request.setResponseCode(500)
Expand Down
6 changes: 5 additions & 1 deletion sydent/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ class InvalidSessionIdException(Exception):


class SessionNotValidatedException(Exception):
pass
pass


class DestinationRejectedException(Exception):
pass
19 changes: 18 additions & 1 deletion sydent/validators/msisdnvalidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from sydent.validators import ValidationSession, common
from sydent.sms.openmarket import OpenMarketSMS

from sydent.validators import DestinationRejectedException

from sydent.util import time_msec

logger = logging.getLogger(__name__)
Expand All @@ -33,8 +35,9 @@ def __init__(self, sydent):
self.sydent = sydent
self.omSms = OpenMarketSMS(sydent)

# cache originators from config file
# cache originators & sms rules from config file
self.originators = {}
self.smsRules = {}
for opt in self.sydent.cfg.options('sms'):
if opt.startswith('originators.'):
country = opt.split('.')[1]
Expand All @@ -52,8 +55,22 @@ def __init__(self, sydent):
"type": parts[0],
"text": parts[1],
})
elif opt.startswith('smsrule.'):
country = opt.split('.')[1]
action = self.sydent.cfg.get('sms', opt)

if action not in ['allow', 'reject']:
raise Exception("Invalid SMS rule action: %s, expecting 'allow' or 'deny'" % action)

self.smsRules[country] = action


def requestToken(self, phoneNumber, clientSecret, sendAttempt, nextLink):
if str(phoneNumber.country_code) in self.smsRules:
action = self.smsRules[str(phoneNumber.country_code)]
if action == 'reject':
raise DestinationRejectedException()

valSessionStore = ThreePidValSessionStore(self.sydent)

msisdn = phonenumbers.format_number(
Expand Down

0 comments on commit ca303fe

Please sign in to comment.