Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🛂 Fix authorization while mapping APNS tokens to FCM #1002

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/push/silent_ios_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from builtins import *
import json
import logging
logging.basicConfig(level=logging.DEBUG)
import argparse

import emission.net.ext_service.push.notify_usage as pnu
Expand Down
23 changes: 14 additions & 9 deletions emission/net/ext_service/push/notify_interface_impl/firebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,17 @@ def map_existing_fcm_tokens(self, token_map):
unmapped_token_list.append(token)
return (mapped_token_map, unmapped_token_list)

def retrieve_fcm_tokens(self, token_list, dev):
def retrieve_fcm_tokens(self, push_service, token_list, dev):
if len(token_list) == 0:
logging.debug("len(token_list) == 0, skipping fcm token mapping to save API call")
return []
importedResultList = []
importHeaders = {"Authorization": "key=%s" % self.server_auth_token,
existing_headers = push_service.requests_session.headers
logging.debug(f"Reading existing headers from current session {existing_headers}")
# Copying over the authorization from existing headers since, as of Dec
# 2024, we cannot use the server API key and must use an OAuth2 token instead
importHeaders = {"Authorization": existing_headers['Authorization'],
"access_token_auth": "true",
"Content-Type": "application/json"}
for curr_first in range(0, len(token_list), 100):
curr_batch = token_list[curr_first:curr_first + 100]
Expand All @@ -115,7 +120,7 @@ def retrieve_fcm_tokens(self, token_list, dev):
print("After appending result of size %s, total size = %s" %
(len(importedResult), len(importedResultList)))
else:
print(f"Received invalid result for batch starting at = {curr_first}")
print(f"Received invalid response {importResponse} for batch starting at = {curr_first}")
return importedResultList

def process_fcm_token_result(self, importedResultList):
Expand All @@ -133,9 +138,9 @@ def process_fcm_token_result(self, importedResultList):
(result, i));
return ret_list

def convert_to_fcm_if_necessary(self, token_map, dev):
def convert_to_fcm_if_necessary(self, push_service, token_map, dev):
(mapped_token_map, unmapped_token_list) = self.map_existing_fcm_tokens(token_map)
importedResultList = self.retrieve_fcm_tokens(unmapped_token_list, dev)
importedResultList = self.retrieve_fcm_tokens(push_service, unmapped_token_list, dev)
newly_mapped_token_list = self.process_fcm_token_result(importedResultList)
print("after mapping iOS tokens, imported %s -> processed %s" %
(len(importedResultList), len(newly_mapped_token_list)))
Expand All @@ -152,15 +157,15 @@ def send_visible_notification(self, token_map, title, message, json_data, dev=Fa
logging.info("len(token_map) == 0, early return to save api calls")
return

# convert tokens if necessary
fcm_token_map = self.convert_to_fcm_if_necessary(token_map, dev)

push_service = FCMNotification(
service_account_file=self.service_account_file,
project_id=self.project_id)
# Send android and iOS messages separately because they have slightly
# different formats
# https://github.com/e-mission/e-mission-server/issues/564#issuecomment-360720598
# convert tokens if necessary
fcm_token_map = self.convert_to_fcm_if_necessary(push_service, token_map, dev)

android_response = self.notify_multiple_devices(push_service,
fcm_token_map["android"],
notification_body = message,
Expand Down Expand Up @@ -192,7 +197,7 @@ def send_silent_notification(self, token_map, json_data, dev=False):
project_id=self.project_id)

# convert tokens if necessary
fcm_token_map = self.convert_to_fcm_if_necessary(token_map, dev)
fcm_token_map = self.convert_to_fcm_if_necessary(push_service, token_map, dev)

response = {}
response["ios"] = self.notify_multiple_devices(push_service,
Expand Down
Loading