From 6daf13a29124a9ab2013dab1661611c47bd3a663 Mon Sep 17 00:00:00 2001 From: ThomasSession Date: Wed, 2 Oct 2024 15:38:46 +1000 Subject: [PATCH] Grabbing server hash from notification data --- .../notifications/HuaweiPushService.kt | 2 +- .../securesms/notifications/PushReceiver.kt | 62 ++++++++++++++----- .../notifications/FirebasePushService.kt | 2 +- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushService.kt b/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushService.kt index dc7bf893d7..b8f1ba293f 100644 --- a/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushService.kt +++ b/app/src/huawei/kotlin/org/thoughtcrime/securesms/notifications/HuaweiPushService.kt @@ -21,7 +21,7 @@ class HuaweiPushService: HmsMessageService() { override fun onMessageReceived(message: RemoteMessage?) { Log.d(TAG, "onMessageReceived") message?.dataOfMap?.takeIf { it.isNotEmpty() }?.let(pushReceiver::onPush) ?: - pushReceiver.onPush(message?.data?.let(Base64::decode)) + pushReceiver.onPushDataReceived(message?.data?.let(Base64::decode)) } override fun onNewToken(token: String?) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/PushReceiver.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/PushReceiver.kt index 8eaca4000b..fc300020cc 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/PushReceiver.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/PushReceiver.kt @@ -1,6 +1,9 @@ package org.thoughtcrime.securesms.notifications +import android.Manifest import android.content.Context +import android.content.pm.PackageManager +import androidx.core.app.ActivityCompat import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.core.content.ContextCompat.getString @@ -30,27 +33,47 @@ private const val TAG = "PushHandler" class PushReceiver @Inject constructor(@ApplicationContext val context: Context) { private val json = Json { ignoreUnknownKeys = true } - fun onPush(dataMap: Map?) { - onPush(dataMap?.asByteArray()) + fun onPushDataReceived(dataMap: Map?) { + addMessageReceiveJob(dataMap?.asPushData()) } - fun onPush(data: ByteArray?) { - if (data == null) { - onPush() + fun onPushDataReceived(data: ByteArray?) { +//todo DELETION currently Huawei sends data to this. We need to check what it actually sends, as it might actually be a map like the above firebase one - then we need to hook the huawei push service appropriately to work with this updated class + } + + private fun addMessageReceiveJob(pushData: PushData?){ + // send a generic notification if we have no data + if (pushData?.data == null) { + sendGenericNotification() return } try { - val envelopeAsData = MessageWrapper.unwrap(data).toByteArray() - val job = BatchMessageReceiveJob(listOf(MessageReceiveParameters(envelopeAsData)), null) + val envelopeAsData = MessageWrapper.unwrap(pushData.data).toByteArray() + val job = BatchMessageReceiveJob(listOf( + MessageReceiveParameters( + data = envelopeAsData, + serverHash = pushData.metadata?.msg_hash + ) + ), null) JobQueue.shared.add(job) } catch (e: Exception) { Log.d(TAG, "Failed to unwrap data for message due to error.", e) } } - private fun onPush() { + private fun sendGenericNotification() { Log.d(TAG, "Failed to decode data for message.") + + // no need to do anything if notification permissions are not granted + if (ActivityCompat.checkSelfPermission( + context, + Manifest.permission.POST_NOTIFICATIONS + ) != PackageManager.PERMISSION_GRANTED + ) { + return + } + val builder = NotificationCompat.Builder(context, NotificationChannels.OTHER) .setSmallIcon(R.drawable.ic_notification) .setColor(context.getColor(R.color.textsecure_primary)) @@ -61,10 +84,11 @@ class PushReceiver @Inject constructor(@ApplicationContext val context: Context) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setAutoCancel(true) + NotificationManagerCompat.from(context).notify(11111, builder.build()) } - private fun Map.asByteArray() = + private fun Map.asPushData(): PushData = when { // this is a v2 push notification containsKey("spns") -> { @@ -72,14 +96,14 @@ class PushReceiver @Inject constructor(@ApplicationContext val context: Context) decrypt(Base64.decode(this["enc_payload"])) } catch (e: Exception) { Log.e(TAG, "Invalid push notification", e) - null + PushData(null, null) } } // old v1 push notification; we still need this for receiving legacy closed group notifications - else -> this["ENCRYPTED_DATA"]?.let(Base64::decode) + else -> PushData(this["ENCRYPTED_DATA"]?.let(Base64::decode), null) } - private fun decrypt(encPayload: ByteArray): ByteArray? { + private fun decrypt(encPayload: ByteArray): PushData { Log.d(TAG, "decrypt() called") val encKey = getOrCreateNotificationKey() @@ -95,9 +119,12 @@ class PushReceiver @Inject constructor(@ApplicationContext val context: Context) val metadataJson = (expectedList[0] as? BencodeString)?.value ?: error("no metadata") val metadata: PushNotificationMetadata = json.decodeFromString(String(metadataJson)) - return (expectedList.getOrNull(1) as? BencodeString)?.value.also { - // null content is valid only if we got a "data_too_long" flag - it?.let { check(metadata.data_len == it.size) { "wrong message data size" } } + return PushData( + data = (expectedList.getOrNull(1) as? BencodeString)?.value, + metadata = metadata + ).also { pushData -> + // null data content is valid only if we got a "data_too_long" flag + pushData.data?.let { check(metadata.data_len == it.size) { "wrong message data size" } } ?: check(metadata.data_too_long) { "missing message data, but no too-long flag" } } } @@ -115,4 +142,9 @@ class PushReceiver @Inject constructor(@ApplicationContext val context: Context) ) ) } + + data class PushData( + val data: ByteArray?, + val metadata: PushNotificationMetadata? + ) } diff --git a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushService.kt b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushService.kt index 2a3b054a58..ba9cb4c12f 100644 --- a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushService.kt +++ b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushService.kt @@ -23,7 +23,7 @@ class FirebasePushService : FirebaseMessagingService() { override fun onMessageReceived(message: RemoteMessage) { Log.d(TAG, "Received a push notification.") - pushReceiver.onPush(message.data) + pushReceiver.onPushDataReceived(message.data) } override fun onDeletedMessages() {