push_notifs: Support APNs token auth, as well as cert auth.

This will make it possible to send notifications to multiple
distinct app IDs over the same connection.
This commit is contained in:
Greg Price
2023-11-07 16:37:08 -08:00
committed by Tim Abbott
parent 1af1a6d7e9
commit ff32d51700
6 changed files with 36 additions and 15 deletions

View File

@@ -145,13 +145,17 @@ class APNsContext:
loop: asyncio.AbstractEventLoop
def apns_enabled() -> bool:
return settings.APNS_TOKEN_KEY_FILE is not None or settings.APNS_CERT_FILE is not None
@lru_cache(maxsize=None)
def get_apns_context() -> Optional[APNsContext]:
# We lazily do this import as part of optimizing Zulip's base
# import time.
import aioapns
if settings.APNS_CERT_FILE is None: # nocoverage
if not apns_enabled(): # nocoverage
return None
# NB if called concurrently, this will make excess connections.
@@ -171,6 +175,9 @@ def get_apns_context() -> Optional[APNsContext]:
async def make_apns() -> aioapns.APNs:
return aioapns.APNs(
client_cert=settings.APNS_CERT_FILE,
key=settings.APNS_TOKEN_KEY_FILE,
key_id=settings.APNS_TOKEN_KEY_ID,
team_id=settings.APNS_TEAM_ID,
topic=settings.APNS_TOPIC,
max_connection_attempts=APNS_MAX_RETRIES,
use_sandbox=settings.APNS_SANDBOX,
@@ -181,10 +188,6 @@ def get_apns_context() -> Optional[APNsContext]:
return APNsContext(apns=apns, loop=loop)
def apns_enabled() -> bool:
return settings.APNS_CERT_FILE is not None
def modernize_apns_payload(data: Mapping[str, Any]) -> Mapping[str, Any]:
"""Take a payload in an unknown Zulip version's format, and return in current format."""
# TODO this isn't super robust as is -- if a buggy remote server