From 29dfea1175686432efe9f32fb44f1e178c3b50f3 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Tue, 22 Oct 2024 20:26:17 -0400 Subject: [PATCH 1/5] Refactor VonageDriver to validate phone numbers before sending SMS --- .../notification/drivers/vonage/VonageDriver.py | 7 +++++++ .../app/notifications/TestNotification.py | 11 +++++++++++ 2 files changed, 18 insertions(+) create mode 100644 tests/integrations/app/notifications/TestNotification.py diff --git a/src/masonite/notification/drivers/vonage/VonageDriver.py b/src/masonite/notification/drivers/vonage/VonageDriver.py index f2ac770fa..060dba9b8 100644 --- a/src/masonite/notification/drivers/vonage/VonageDriver.py +++ b/src/masonite/notification/drivers/vonage/VonageDriver.py @@ -41,6 +41,8 @@ def send(self, notifiable, notification): client = self.get_sms_client() recipients = sms._to for recipient in recipients: + if not self.is_valid_phone_number(recipient): + raise NotificationException(f"Invalid phone number: {recipient}") payload = sms.to(recipient).build().get_options() response = client.send_message(payload) self._handle_errors(response) @@ -67,3 +69,8 @@ def _handle_errors(self, response): status, message["error-text"] ) ) + + def is_valid_phone_number(self, phone_number): + # Add your phone number validation logic here + # For example, check if it is a 10-digit number + return phone_number.isdigit() and len(phone_number) == 10 \ No newline at end of file diff --git a/tests/integrations/app/notifications/TestNotification.py b/tests/integrations/app/notifications/TestNotification.py new file mode 100644 index 000000000..373e713b9 --- /dev/null +++ b/tests/integrations/app/notifications/TestNotification.py @@ -0,0 +1,11 @@ +from src.masonite.notification import Notification +from src.masonite.mail import Mailable +from src.masonite.notification import Sms + +class TestNotification(Notification, Mailable): + + def to_vonage(self, notifiable): + return Sms().text("Test message" ) + + def via(self, notifiable): + return ["vonage"] \ No newline at end of file From 61cbe7ab74765c4623f3b931f6486a5efb367c24 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Tue, 22 Oct 2024 20:30:43 -0400 Subject: [PATCH 2/5] Refactor VonageDriver to validate phone numbers before sending SMS --- src/masonite/notification/drivers/vonage/VonageDriver.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/masonite/notification/drivers/vonage/VonageDriver.py b/src/masonite/notification/drivers/vonage/VonageDriver.py index 060dba9b8..6b788fda9 100644 --- a/src/masonite/notification/drivers/vonage/VonageDriver.py +++ b/src/masonite/notification/drivers/vonage/VonageDriver.py @@ -40,6 +40,8 @@ def send(self, notifiable, notification): sms = self.build(notifiable, notification) client = self.get_sms_client() recipients = sms._to + if not isinstance(recipients, list): + recipients = [recipients] for recipient in recipients: if not self.is_valid_phone_number(recipient): raise NotificationException(f"Invalid phone number: {recipient}") From 3998f7a9209ca95303d21d800fe68c6cf5389add Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Tue, 22 Oct 2024 20:32:21 -0400 Subject: [PATCH 3/5] Refactor VonageDriver to validate phone numbers before sending SMS --- src/masonite/notification/drivers/vonage/VonageDriver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masonite/notification/drivers/vonage/VonageDriver.py b/src/masonite/notification/drivers/vonage/VonageDriver.py index 6b788fda9..3aad4c37d 100644 --- a/src/masonite/notification/drivers/vonage/VonageDriver.py +++ b/src/masonite/notification/drivers/vonage/VonageDriver.py @@ -75,4 +75,4 @@ def _handle_errors(self, response): def is_valid_phone_number(self, phone_number): # Add your phone number validation logic here # For example, check if it is a 10-digit number - return phone_number.isdigit() and len(phone_number) == 10 \ No newline at end of file + return phone_number.isdigit() and len(phone_number) == 10 From 48e739c9d52938f4d13612283fb0554ae5b8b580 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Tue, 22 Oct 2024 20:39:02 -0400 Subject: [PATCH 4/5] Refactor VonageDriver to use phonenumbers library for phone number validation --- src/masonite/notification/drivers/vonage/VonageDriver.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/masonite/notification/drivers/vonage/VonageDriver.py b/src/masonite/notification/drivers/vonage/VonageDriver.py index 3aad4c37d..42bf617ac 100644 --- a/src/masonite/notification/drivers/vonage/VonageDriver.py +++ b/src/masonite/notification/drivers/vonage/VonageDriver.py @@ -73,6 +73,9 @@ def _handle_errors(self, response): ) def is_valid_phone_number(self, phone_number): - # Add your phone number validation logic here - # For example, check if it is a 10-digit number - return phone_number.isdigit() and len(phone_number) == 10 + import phonenumbers + try: + parsed_number = phonenumbers.parse(phone_number, None) + return phonenumbers.is_valid_number(parsed_number) + except phonenumbers.NumberParseException: + return False From 8f30981b1389cd3df4443fcff83dc350e65e7d96 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Tue, 22 Oct 2024 20:40:29 -0400 Subject: [PATCH 5/5] Refactor setup.py to add phonenumbers library for phone number validation --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index ae8eb6a94..d3ce43d71 100644 --- a/setup.py +++ b/setup.py @@ -59,6 +59,7 @@ "werkzeug>=2,<3; python_version < '3.8'", "werkzeug>=3,<4; python_version >= '3.8'", "watchdog>=2,<=4", + "phonenumbers>=8.12,<9", ], # See https://pypi.python.org/pypi?%3Aaction=list_classifiers classifiers=[