From bb4a15aa6cbe838a679e9f5cd3735c67cf4c50af Mon Sep 17 00:00:00 2001 From: James Priebe Date: Mon, 26 Aug 2024 17:21:20 -0400 Subject: [PATCH 1/4] explicitly handle 403s --- CHANGES.rst | 4 ++++ pyfcm/baseapi.py | 5 +++++ pyfcm/errors.py | 9 +++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 9e50d8e..5ff7548 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -158,3 +158,7 @@ Unreleased - Replace deprecated urllib3.Retry options v2 .. _Ɓukasz Rogowski: https://github.com/Rogalek + +- Explicitly handle 403 SENDER_ID_MISMATCH response + +.. _James Priebe: https://github.com/J-Priebe/ diff --git a/pyfcm/baseapi.py b/pyfcm/baseapi.py index 147050b..fe275f9 100644 --- a/pyfcm/baseapi.py +++ b/pyfcm/baseapi.py @@ -16,6 +16,7 @@ AuthenticationError, InvalidDataError, FCMError, + FCMSenderIdMismatchError, FCMServerError, FCMNotRegisteredError, ) @@ -210,6 +211,10 @@ def parse_response(self, response): ) elif response.status_code == 400: raise InvalidDataError(response.text) + elif response.status_code == 403: + raise FCMSenderIdMismatchError( + "The authenticated sender ID is different from the sender ID for the registration token." + ) elif response.status_code == 404: raise FCMNotRegisteredError("Token not registered") else: diff --git a/pyfcm/errors.py b/pyfcm/errors.py index a186fa3..70ae23c 100644 --- a/pyfcm/errors.py +++ b/pyfcm/errors.py @@ -23,6 +23,15 @@ class FCMNotRegisteredError(FCMError): pass +class FCMSenderIdMismatchError(FCMError): + """ + Sender is not allowed for the given device tokens + https://firebase.google.com/docs/reference/fcm/rest/v1/ErrorCode + """ + + pass + + class FCMServerError(FCMError): """ Internal server error or timeout error on Firebase cloud messaging server From b3e2abfc6bbd64a7e17059f8e7fbc40b074449c6 Mon Sep 17 00:00:00 2001 From: James Priebe Date: Mon, 26 Aug 2024 17:25:08 -0400 Subject: [PATCH 2/4] update notify docstring for parity with actual parsed response --- pyfcm/fcm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyfcm/fcm.py b/pyfcm/fcm.py index ca3f945..47d60ca 100644 --- a/pyfcm/fcm.py +++ b/pyfcm/fcm.py @@ -41,7 +41,7 @@ def notify( timeout (int, optional): Set time limit for the request Returns: - dict: Response from FCM server (`multicast_id`, `success`, `failure`, `canonical_ids`, `results`) + dict: name (str) - uThe identifier of the message sent, in the format of projects/*/messages/{message_id} Raises: AuthenticationError: If api_key is not set or provided or there is an error authenticating the sender. From e5acb1a27bee319e6e38931523d111238e24dbba Mon Sep 17 00:00:00 2001 From: James Priebe Date: Mon, 26 Aug 2024 17:25:35 -0400 Subject: [PATCH 3/4] typo --- pyfcm/baseapi.py | 2 +- pyfcm/fcm.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyfcm/baseapi.py b/pyfcm/baseapi.py index fe275f9..bf99913 100644 --- a/pyfcm/baseapi.py +++ b/pyfcm/baseapi.py @@ -186,7 +186,7 @@ def parse_response(self, response): Parses the json response sent back by the server and tries to get out the important return variables Returns: - dict: name (str) - uThe identifier of the message sent, in the format of projects/*/messages/{message_id} + dict: name (str) - The identifier of the message sent, in the format of projects/*/messages/{message_id} Raises: FCMServerError: FCM is temporary not available diff --git a/pyfcm/fcm.py b/pyfcm/fcm.py index 47d60ca..7e8f401 100644 --- a/pyfcm/fcm.py +++ b/pyfcm/fcm.py @@ -41,7 +41,7 @@ def notify( timeout (int, optional): Set time limit for the request Returns: - dict: name (str) - uThe identifier of the message sent, in the format of projects/*/messages/{message_id} + dict: name (str) - The identifier of the message sent, in the format of projects/*/messages/{message_id} Raises: AuthenticationError: If api_key is not set or provided or there is an error authenticating the sender. From c6e98219b49b7845b24d2468c4c6897e5e504ac4 Mon Sep 17 00:00:00 2001 From: James Priebe Date: Tue, 27 Aug 2024 11:15:43 -0400 Subject: [PATCH 4/4] update docstring on raised errors from parse_response --- pyfcm/baseapi.py | 2 ++ pyfcm/fcm.py | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pyfcm/baseapi.py b/pyfcm/baseapi.py index bf99913..7178dd8 100644 --- a/pyfcm/baseapi.py +++ b/pyfcm/baseapi.py @@ -192,6 +192,8 @@ def parse_response(self, response): FCMServerError: FCM is temporary not available AuthenticationError: error authenticating the sender account InvalidDataError: data passed to FCM was incorrecly structured + FCMSenderIdMismatchError: the authenticated sender is different from the sender registered to the token + FCMNotRegisteredError: device token is missing, not registered, or invalid """ if response.status_code == 200: diff --git a/pyfcm/fcm.py b/pyfcm/fcm.py index 7e8f401..13647c9 100644 --- a/pyfcm/fcm.py +++ b/pyfcm/fcm.py @@ -44,10 +44,11 @@ def notify( dict: name (str) - The identifier of the message sent, in the format of projects/*/messages/{message_id} Raises: - AuthenticationError: If api_key is not set or provided or there is an error authenticating the sender. - FCMServerError: Internal server error or timeout error on Firebase cloud messaging server - InvalidDataError: Invalid data provided - InternalPackageError: Mostly from changes in the response of FCM, contact the project owner to resolve the issue + FCMServerError: FCM is temporary not available + AuthenticationError: error authenticating the sender account + InvalidDataError: data passed to FCM was incorrecly structured + FCMSenderIdMismatchError: the authenticated sender is different from the sender registered to the token + FCMNotRegisteredError: device token is missing, not registered, or invalid """ payload = self.parse_payload( fcm_token=fcm_token,