Merge branch 'main' into stvnrlly-paperless-api

This commit is contained in:
stvnrlly
2023-02-06 12:28:10 -05:00
87 changed files with 2484 additions and 2087 deletions

View File

@@ -1,60 +0,0 @@
from flask import Blueprint
from app.errors import register_errors
sms_callback_blueprint = Blueprint("sms_callback", __name__, url_prefix="/notifications/sms")
register_errors(sms_callback_blueprint)
# TODO SNS SMS delivery receipts delivered here
# @sms_callback_blueprint.route('/mmg', methods=['POST'])
# def process_mmg_response():
# client_name = 'MMG'
# data = json.loads(request.data)
# errors = validate_callback_data(data=data,
# fields=['status', 'CID'],
# client_name=client_name)
# if errors:
# raise InvalidRequest(errors, status_code=400)
# status = str(data.get('status'))
# detailed_status_code = str(data.get('substatus'))
# provider_reference = data.get('CID')
# process_sms_client_response.apply_async(
# [status, provider_reference, client_name, detailed_status_code],
# queue=QueueNames.SMS_CALLBACKS,
# )
# return jsonify(result='success'), 200
# @sms_callback_blueprint.route('/firetext', methods=['POST'])
# def process_firetext_response():
# client_name = 'Firetext'
# errors = validate_callback_data(data=request.form,
# fields=['status', 'reference'],
# client_name=client_name)
# if errors:
# raise InvalidRequest(errors, status_code=400)
# status = request.form.get('status')
# detailed_status_code = request.form.get('code')
# provider_reference = request.form.get('reference')
# process_sms_client_response.apply_async(
# [status, provider_reference, client_name, detailed_status_code],
# queue=QueueNames.SMS_CALLBACKS,
# )
# return jsonify(result='success'), 200
def validate_callback_data(data, fields, client_name):
errors = []
for f in fields:
if not str(data.get(f, '')):
error = "{} callback failed: {} missing".format(client_name, f)
errors.append(error)
return errors if len(errors) > 0 else None

View File

@@ -104,13 +104,13 @@ def persist_notification(
document_download_count=None,
updated_at=None
):
current_app.logger.info('Presisting notification')
current_app.logger.info('Persisting notification')
notification_created_at = created_at or datetime.utcnow()
if not notification_id:
notification_id = uuid.uuid4()
current_app.logger.info('Presisting notification with id {}'.format(notification_id))
current_app.logger.info('Persisting notification with id {}'.format(notification_id))
notification = Notification(
id=notification_id,
@@ -135,7 +135,7 @@ def persist_notification(
updated_at=updated_at
)
current_app.logger.info('Presisting notification with to address: {}'.format(notification.to))
current_app.logger.info('Persisting notification with to address: {}'.format(notification.to))
if notification_type == SMS_TYPE:
formatted_recipient = validate_and_format_phone_number(recipient, international=True)
@@ -145,9 +145,9 @@ def persist_notification(
notification.phone_prefix = recipient_info.country_prefix
notification.rate_multiplier = recipient_info.billable_units
elif notification_type == EMAIL_TYPE:
current_app.logger.info('Presisting notification with type: {}'.format(EMAIL_TYPE))
current_app.logger.info('Persisting notification with type: {}'.format(EMAIL_TYPE))
notification.normalised_to = format_email_address(notification.to)
current_app.logger.info('Presisting notification to formatted email: {}'.format(notification.normalised_to))
current_app.logger.info('Persisting notification to formatted email: {}'.format(notification.normalised_to))
elif notification_type == LETTER_TYPE:
notification.postage = postage
notification.international = postage in INTERNATIONAL_POSTAGE_TYPES

View File

@@ -1,8 +1,4 @@
from datetime import datetime
from urllib.parse import unquote
import iso8601
from flask import Blueprint, abort, current_app, json, jsonify, request
from flask import Blueprint, current_app, json, jsonify, request
from gds_metrics.metrics import Counter
from notifications_utils.recipients import try_validate_and_format_phone_number
@@ -93,113 +89,10 @@ def receive_sns_sms():
), 200
@receive_notifications_blueprint.route('/notifications/sms/receive/mmg', methods=['POST'])
def receive_mmg_sms():
"""
{
'MSISDN': '447123456789'
'Number': '40604',
'Message': 'some+uri+encoded+message%3A',
'ID': 'SOME-MMG-SPECIFIC-ID',
'DateRecieved': '2017-05-21+11%3A56%3A11'
}
"""
post_data = request.get_json()
auth = request.authorization
if not auth:
current_app.logger.warning("Inbound sms (MMG) no auth header")
abort(401)
elif auth.username not in current_app.config['MMG_INBOUND_SMS_USERNAME'] \
or auth.password not in current_app.config['MMG_INBOUND_SMS_AUTH']:
current_app.logger.warning("Inbound sms (MMG) incorrect username ({}) or password".format(auth.username))
abort(403)
inbound_number = strip_leading_forty_four(post_data['Number'])
service = fetch_potential_service(inbound_number, 'mmg')
if not service:
# since this is an issue with our service <-> number mapping, or no inbound_sms service permission
# we should still tell MMG that we received it successfully
return 'RECEIVED', 200
INBOUND_SMS_COUNTER.labels("mmg").inc()
inbound = create_inbound_sms_object(service,
content=format_mmg_message(post_data["Message"]),
from_number=post_data['MSISDN'],
provider_ref=post_data["ID"],
date_received=post_data.get('DateRecieved'),
provider_name="mmg")
tasks.send_inbound_sms_to_service.apply_async([str(inbound.id), str(service.id)], queue=QueueNames.NOTIFY)
current_app.logger.debug(
'{} received inbound SMS with reference {} from MMG'.format(service.id, inbound.provider_reference))
return jsonify({
"status": "ok"
}), 200
@receive_notifications_blueprint.route('/notifications/sms/receive/firetext', methods=['POST'])
def receive_firetext_sms():
post_data = request.form
auth = request.authorization
if not auth:
current_app.logger.warning("Inbound sms (Firetext) no auth header")
abort(401)
elif auth.username != 'notify' or auth.password not in current_app.config['FIRETEXT_INBOUND_SMS_AUTH']:
current_app.logger.warning("Inbound sms (Firetext) incorrect username ({}) or password".format(auth.username))
abort(403)
inbound_number = strip_leading_forty_four(post_data['destination'])
service = fetch_potential_service(inbound_number, 'firetext')
if not service:
return jsonify({
"status": "ok"
}), 200
inbound = create_inbound_sms_object(service=service,
content=post_data["message"],
from_number=post_data['source'],
provider_ref=None,
date_received=post_data['time'],
provider_name="firetext")
INBOUND_SMS_COUNTER.labels("firetext").inc()
tasks.send_inbound_sms_to_service.apply_async([str(inbound.id), str(service.id)], queue=QueueNames.NOTIFY)
current_app.logger.debug(
'{} received inbound SMS with reference {} from Firetext'.format(service.id, inbound.provider_reference))
return jsonify({
"status": "ok"
}), 200
def format_mmg_message(message):
return unescape_string(unquote(message.replace('+', ' ')))
def unescape_string(string):
return string.encode('raw_unicode_escape').decode('unicode_escape')
def format_mmg_datetime(date):
"""
We expect datetimes in format 2017-05-21+11%3A56%3A11 - ie, spaces replaced with pluses, and URI encoded
and in UTC
"""
try:
orig_date = format_mmg_message(date)
parsed_datetime = iso8601.parse_date(orig_date).replace(tzinfo=None)
return parsed_datetime
except iso8601.ParseError:
return datetime.utcnow()
def create_inbound_sms_object(service, content, from_number, provider_ref, date_received, provider_name):
user_number = try_validate_and_format_phone_number(
from_number,
@@ -208,9 +101,6 @@ def create_inbound_sms_object(service, content, from_number, provider_ref, date_
)
provider_date = date_received
if provider_date:
provider_date = format_mmg_datetime(provider_date)
inbound = InboundSms(
service=service,
notify_number=service.get_inbound_number(),
@@ -244,9 +134,3 @@ def fetch_potential_service(inbound_number, provider_name):
def has_inbound_sms_permissions(permissions):
str_permissions = [p.permission for p in permissions]
return set([INBOUND_SMS_TYPE, SMS_TYPE]).issubset(set(str_permissions))
def strip_leading_forty_four(number):
if number.startswith('44'):
return number.replace('44', '0', 1)
return number

View File

@@ -151,10 +151,7 @@ def check_if_service_can_send_to_number(service, number):
else:
permissions = service.permissions
if (
# if number is international and not a crown dependency
international_phone_info.international and not international_phone_info.crown_dependency
) and INTERNATIONAL_SMS_TYPE not in permissions:
if international_phone_info.international and INTERNATIONAL_SMS_TYPE not in permissions:
raise BadRequestError(message="Cannot send to international mobile numbers")
else:
return international_phone_info