diff --git a/keyserver/src/push/crypto.js b/keyserver/src/push/crypto.js --- a/keyserver/src/push/crypto.js +++ b/keyserver/src/push/crypto.js @@ -3,6 +3,7 @@ import apn from '@parse/node-apn'; import _cloneDeep from 'lodash/fp/cloneDeep.js'; +import type { AndroidNotification } from './types.js'; import { encryptAndUpdateOlmSession } from '../updaters/olm-session-updater.js'; async function encryptIOSNotification( @@ -61,6 +62,35 @@ return notification; } +async function encryptAndroidNotification( + cookieID: string, + notification: AndroidNotification, +): Promise { + const encryptedNotification = { ...notification }; + const { + data: { id, badgeOnly, ...notificationFieldsToEncrypt }, + } = encryptedNotification; + + let encryptedFields; + try { + encryptedFields = await encryptAndUpdateOlmSession( + cookieID, + 'notifications', + notificationFieldsToEncrypt, + ); + } catch (e) { + console.log('Notification encryption failed: ' + e); + encryptedNotification.data.encrypted = '0'; + return encryptedNotification; + } + + for (const fieldName in encryptedFields) { + encryptedNotification.data[fieldName] = encryptedFields[fieldName].body; + } + encryptedNotification.data.encrypted = '1'; + return encryptedNotification; +} + function prepareEncryptedIOSNotifications( cookieIDs: $ReadOnlyArray, notification: apn.Notification, @@ -71,4 +101,17 @@ return Promise.all(notificationPromises); } -export { prepareEncryptedIOSNotifications }; +function prepareEncryptedAndroidNotifications( + cookieIDs: $ReadOnlyArray, + notification: AndroidNotification, +): Promise> { + const notificationPromises = cookieIDs.map(cookieID => + encryptAndroidNotification(cookieID, notification), + ); + return Promise.all(notificationPromises); +} + +export { + prepareEncryptedIOSNotifications, + prepareEncryptedAndroidNotifications, +}; diff --git a/keyserver/src/push/types.js b/keyserver/src/push/types.js new file mode 100644 --- /dev/null +++ b/keyserver/src/push/types.js @@ -0,0 +1,9 @@ +// @flow + +export type AndroidNotification = { + +data: { + +id?: string, + +badgeOnly?: string, + [string]: string, + }, +};