Browse Source

Merge pull request #43 from matrix-org/dbkr/reject_sms_destinations

Add support for rejecting sms reqs to countries
David Baker 7 years ago
parent
commit
3d3ac5ab80

+ 7 - 1
sydent/http/servlets/msisdnservlet.py

@@ -19,7 +19,9 @@ import logging
 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 @@ class MsisdnRequestCodeServlet(Resource):
             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)

+ 5 - 1
sydent/validators/__init__.py

@@ -44,4 +44,8 @@ class InvalidSessionIdException(Exception):
 
 
 class SessionNotValidatedException(Exception):
-    pass
+    pass
+
+
+class DestinationRejectedException(Exception):
+    pass

+ 18 - 1
sydent/validators/msisdnvalidator.py

@@ -23,6 +23,8 @@ from sydent.db.valsession import ThreePidValSessionStore
 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 @@ class MsisdnValidator:
         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 @@ class MsisdnValidator:
                         "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(