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

Add support for rejecting sms reqs to countries #43

Merged
merged 2 commits into from
Apr 25, 2017
Merged
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
8 changes: 7 additions & 1 deletion sydent/http/servlets/msisdnservlet.py
Original file line number Diff line number Diff line change
@@ -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

@@ -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)
6 changes: 5 additions & 1 deletion sydent/validators/__init__.py
Original file line number Diff line number Diff line change
@@ -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
@@ -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__)
@@ -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]
@@ -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 'reject'" % 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(