Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/push/crypto.js
// @flow | // @flow | ||||
import apn from '@parse/node-apn'; | import apn from '@parse/node-apn'; | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import type { AndroidNotification } from './types.js'; | |||||
import { encryptAndUpdateOlmSession } from '../updaters/olm-session-updater.js'; | import { encryptAndUpdateOlmSession } from '../updaters/olm-session-updater.js'; | ||||
async function encryptIOSNotification( | async function encryptIOSNotification( | ||||
cookieID: string, | cookieID: string, | ||||
notification: apn.Notification, | notification: apn.Notification, | ||||
): Promise<apn.Notification> { | ): Promise<apn.Notification> { | ||||
invariant( | invariant( | ||||
!notification.collapseId, | !notification.collapseId, | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | try { | ||||
return encryptedNotification; | return encryptedNotification; | ||||
} | } | ||||
encryptedNotification.payload.encryptedPayload = | encryptedNotification.payload.encryptedPayload = | ||||
encryptedSerializedPayload.body; | encryptedSerializedPayload.body; | ||||
return encryptedNotification; | return encryptedNotification; | ||||
} | } | ||||
async function encryptAndroidNotification( | |||||
cookieID: string, | |||||
notification: AndroidNotification, | |||||
): Promise<AndroidNotification> { | |||||
const { id, badgeOnly, ...unencryptedPayload } = notification.data; | |||||
const encryptedNotification = { data: { id, badgeOnly } }; | |||||
let encryptedSerializedPayload; | |||||
try { | |||||
const unencryptedSerializedPayload = JSON.stringify(unencryptedPayload); | |||||
const { serializedPayload } = await encryptAndUpdateOlmSession( | |||||
cookieID, | |||||
'notifications', | |||||
{ | |||||
serializedPayload: unencryptedSerializedPayload, | |||||
}, | |||||
); | |||||
encryptedSerializedPayload = serializedPayload; | |||||
} catch (e) { | |||||
console.log('Notification encryption failed: ' + e); | |||||
encryptedNotification.data = { | |||||
...unencryptedPayload, | |||||
...encryptedNotification.data, | |||||
encryptionFailed: '1', | |||||
}; | |||||
return encryptedNotification; | |||||
} | |||||
encryptedNotification.data = { | |||||
...encryptedNotification.data, | |||||
encryptedPayload: encryptedSerializedPayload.body, | |||||
}; | |||||
return encryptedNotification; | |||||
} | |||||
function prepareEncryptedIOSNotifications( | function prepareEncryptedIOSNotifications( | ||||
cookieIDs: $ReadOnlyArray<string>, | cookieIDs: $ReadOnlyArray<string>, | ||||
notification: apn.Notification, | notification: apn.Notification, | ||||
): Promise<Array<apn.Notification>> { | ): Promise<Array<apn.Notification>> { | ||||
const notificationPromises = cookieIDs.map(cookieID => | const notificationPromises = cookieIDs.map(cookieID => | ||||
encryptIOSNotification(cookieID, notification), | encryptIOSNotification(cookieID, notification), | ||||
); | ); | ||||
return Promise.all(notificationPromises); | return Promise.all(notificationPromises); | ||||
} | } | ||||
export { prepareEncryptedIOSNotifications }; | function prepareEncryptedAndroidNotifications( | ||||
cookieIDs: $ReadOnlyArray<string>, | |||||
notification: AndroidNotification, | |||||
): Promise<Array<AndroidNotification>> { | |||||
const notificationPromises = cookieIDs.map(cookieID => | |||||
encryptAndroidNotification(cookieID, notification), | |||||
); | |||||
return Promise.all(notificationPromises); | |||||
} | |||||
export { | |||||
prepareEncryptedIOSNotifications, | |||||
prepareEncryptedAndroidNotifications, | |||||
}; |