Use the same validation in the endpoint and the task to validate the phone number is ok to send to.

Format the phone number before sending it to the sms provider.
This commit is contained in:
Rebecca Law
2016-03-16 13:33:12 +00:00
parent 977ac4f566
commit 4268f8453b
5 changed files with 112 additions and 36 deletions

View File

@@ -25,7 +25,8 @@ from sqlalchemy.exc import SQLAlchemyError
from app.aws import s3
from datetime import datetime
from utils.template import Template
from utils.recipients import RecipientCSV, validate_phone_number, format_phone_number
from utils.recipients import RecipientCSV, format_phone_number, validate_phone_number
from app.validation import (allowed_send_to_email, allowed_send_to_number)
@notify_celery.task(name="delete-verify-codes")
@@ -204,7 +205,7 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
)
client.send_sms(
to=notification['to'],
to=format_phone_number(validate_phone_number(notification['to'])),
content=template.replaced,
reference=str(notification_id)
)
@@ -223,20 +224,6 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
current_app.logger.debug(e)
def allowed_send_to_number(service, to):
if service.restricted and format_phone_number(validate_phone_number(to)) not in [
format_phone_number(validate_phone_number(user.mobile_number)) for user in service.users
]:
return False
return True
def allowed_send_to_email(service, to):
if service.restricted and to not in [user.email_address for user in service.users]:
return False
return True
@notify_celery.task(name="send-email")
def send_email(service_id, notification_id, subject, from_address, encrypted_notification, created_at):
notification = encryption.decrypt(encrypted_notification)
@@ -300,7 +287,9 @@ def send_sms_code(encrypted_verification):
verification_message = encryption.decrypt(encrypted_verification)
try:
firetext_client.send_sms(
verification_message['to'], verification_message['secret_code'], 'send-sms-code'
format_phone_number(validate_phone_number(verification_message['to'])),
verification_message['secret_code'],
'send-sms-code'
)
except FiretextClientException as e:
current_app.logger.exception(e)

View File

@@ -26,6 +26,7 @@ from app.schemas import (
notification_status_schema
)
from app.celery.tasks import send_sms, send_email
from app.validation import allowed_send_to_number, allowed_send_to_email
notifications = Blueprint('notifications', __name__)
@@ -320,7 +321,7 @@ def send_notification(notification_type):
notification_id = create_uuid()
if notification_type == 'sms':
if service.restricted and notification['to'] not in [user.mobile_number for user in service.users]:
if not allowed_send_to_number(service, notification['to']):
return jsonify(
result="error", message={'to': ['Invalid phone number for restricted service']}), 400
send_sms.apply_async((
@@ -330,7 +331,7 @@ def send_notification(notification_type):
datetime.utcnow().strftime(DATETIME_FORMAT)
), queue='sms')
else:
if service.restricted and notification['to'] not in [user.email_address for user in service.users]:
if not allowed_send_to_email(service, notification['to']):
return jsonify(
result="error", message={'to': ['Email address not permitted for restricted service']}), 400
send_email.apply_async((

15
app/validation.py Normal file
View File

@@ -0,0 +1,15 @@
from utils.recipients import format_phone_number, validate_phone_number
def allowed_send_to_number(service, to):
if service.restricted and format_phone_number(validate_phone_number(to)) not in [
format_phone_number(validate_phone_number(user.mobile_number)) for user in service.users
]:
return False
return True
def allowed_send_to_email(service, to):
if service.restricted and to not in [user.email_address for user in service.users]:
return False
return True