From 1fc22a1558e5e090d4614cf16967eb4b633d55a4 Mon Sep 17 00:00:00 2001 From: Slipeer Date: Wed, 31 May 2017 09:42:03 +0300 Subject: [PATCH] Update from mainstream (#3) * Add support for rejecting sms reqs to countries Reject requests to send SMS to countries based on rules in the config file https://github.com/vector-im/riot-web/issues/3542 * Oops - fix docs to not lie --- sydent/http/servlets/msisdnservlet.py | 8 +++++++- sydent/validators/__init__.py | 6 +++++- sydent/validators/msisdnvalidator.py | 19 ++++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/sydent/http/servlets/msisdnservlet.py b/sydent/http/servlets/msisdnservlet.py index 750aaed4..64087ffb 100644 --- a/sydent/http/servlets/msisdnservlet.py +++ b/sydent/http/servlets/msisdnservlet.py @@ -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) diff --git a/sydent/validators/__init__.py b/sydent/validators/__init__.py index 13e14599..821f1b84 100644 --- a/sydent/validators/__init__.py +++ b/sydent/validators/__init__.py @@ -44,4 +44,8 @@ class InvalidSessionIdException(Exception): class SessionNotValidatedException(Exception): - pass \ No newline at end of file + pass + + +class DestinationRejectedException(Exception): + pass diff --git a/sydent/validators/msisdnvalidator.py b/sydent/validators/msisdnvalidator.py index dcd7d296..45edb913 100644 --- a/sydent/validators/msisdnvalidator.py +++ b/sydent/validators/msisdnvalidator.py @@ -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(