mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-19 06:11:55 +00:00
New SPNS subscription and notifications
Finishes the WIP for subscribing to push notifications and handling the new-style pushes we get.
This commit is contained in:
@@ -8,6 +8,7 @@ import com.goterl.lazysodium.interfaces.Sign
|
||||
import com.goterl.lazysodium.utils.Key
|
||||
import com.goterl.lazysodium.utils.KeyPair
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.decodeFromStream
|
||||
@@ -17,6 +18,7 @@ import okhttp3.MediaType
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationMetadata
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.SubscriptionRequest
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.SubscriptionResponse
|
||||
import org.session.libsession.messaging.utilities.SodiumUtilities
|
||||
@@ -26,7 +28,6 @@ import org.session.libsession.snode.Version
|
||||
import org.session.libsession.utilities.TextSecurePreferences
|
||||
import org.session.libsession.utilities.TextSecurePreferences.Companion.getLocalNumber
|
||||
import org.session.libsession.utilities.bencode.Bencode
|
||||
import org.session.libsession.utilities.bencode.BencodeDict
|
||||
import org.session.libsession.utilities.bencode.BencodeList
|
||||
import org.session.libsession.utilities.bencode.BencodeString
|
||||
import org.session.libsignal.utilities.Base64
|
||||
@@ -60,35 +61,32 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS
|
||||
)
|
||||
}
|
||||
|
||||
fun decrypt(encPayload: ByteArray) {
|
||||
fun decrypt(encPayload: ByteArray): ByteArray? {
|
||||
val encKey = getOrCreateNotificationKey()
|
||||
val nonce = encPayload.take(AEAD.XCHACHA20POLY1305_IETF_NPUBBYTES).toByteArray()
|
||||
val payload = encPayload.drop(AEAD.XCHACHA20POLY1305_IETF_NPUBBYTES).toByteArray()
|
||||
val decrypted = SodiumUtilities.decrypt(payload, encKey.asBytes, nonce)
|
||||
?: return Log.e("Loki", "Failed to decrypt push notification")
|
||||
val padded = SodiumUtilities.decrypt(payload, encKey.asBytes, nonce)
|
||||
?: error("Failed to decrypt push notification")
|
||||
val decrypted = padded.dropLastWhile { it.toInt() == 0 }.toByteArray()
|
||||
val bencoded = Bencode.Decoder(decrypted)
|
||||
val expectedList = (bencoded.decode() as? BencodeList)
|
||||
?: return Log.e("Loki", "Failed to decode bencoded list from payload")
|
||||
val expectedList = (bencoded.decode() as? BencodeList)?.values
|
||||
?: error("Failed to decode bencoded list from payload")
|
||||
|
||||
val (metadata, content) = expectedList.values
|
||||
val metadataDict = (metadata as? BencodeDict)?.values
|
||||
?: return Log.e("Loki", "Failed to decode metadata dict")
|
||||
val metadataJson = (expectedList[0] as? BencodeString)?.value
|
||||
?: error("no metadata")
|
||||
val metadata:PushNotificationMetadata = Json.decodeFromString(String(metadataJson))
|
||||
|
||||
val push = """
|
||||
Push metadata received was:
|
||||
@: ${metadataDict["@"]}
|
||||
#: ${metadataDict["#"]}
|
||||
n: ${metadataDict["n"]}
|
||||
l: ${metadataDict["l"]}
|
||||
B: ${metadataDict["B"]}
|
||||
""".trimIndent()
|
||||
val content: ByteArray? = if (expectedList.size >= 2) (expectedList[1] as? BencodeString)?.value else null
|
||||
// null content is valid only if we got a "data_too_long" flag
|
||||
if (content == null)
|
||||
check(metadata.data_too_long) { "missing message data, but no too-long flag" }
|
||||
else
|
||||
check(metadata.data_len == content.size) { "wrong message data size" }
|
||||
|
||||
Log.d("Loki", "push")
|
||||
Log.d("Loki",
|
||||
"Received push for ${metadata.account}/${metadata.namespace}, msg ${metadata.msg_hash}, ${metadata.data_len}B")
|
||||
|
||||
val contentBytes = (content as? BencodeString)?.value
|
||||
?: return Log.e("Loki", "Failed to decode content string")
|
||||
|
||||
// TODO: something with contentBytes
|
||||
return content
|
||||
}
|
||||
|
||||
override fun register(force: Boolean) {
|
||||
@@ -158,10 +156,10 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS
|
||||
TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
|
||||
} else {
|
||||
val (_, message) = response.errorInfo()
|
||||
Log.d("Loki", "Couldn't register for FCM due to error: $message.")
|
||||
Log.e("Loki", "Couldn't register for FCM due to error: $message.")
|
||||
}
|
||||
}.fail { exception ->
|
||||
Log.d("Loki", "Couldn't register for FCM due to error: ${exception}.")
|
||||
Log.e("Loki", "Couldn't register for FCM due to error: ${exception}.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -28,15 +28,19 @@ class PushNotificationService : FirebaseMessagingService() {
|
||||
|
||||
override fun onMessageReceived(message: RemoteMessage) {
|
||||
Log.d("Loki", "Received a push notification.")
|
||||
if (message.data.containsKey("spns")) {
|
||||
// assume this is the new push notification content
|
||||
// deal with the enc payload (probably decrypting through the PushManager?
|
||||
Log.d("Loki", "TODO: deal with the enc_payload\n${message.data["enc_payload"]}")
|
||||
pushManager.decrypt(Base64.decode(message.data["enc_payload"]))
|
||||
return
|
||||
val data: ByteArray? = if (message.data.containsKey("spns")) {
|
||||
// this is a v2 push notification
|
||||
try {
|
||||
pushManager.decrypt(Base64.decode(message.data["enc_payload"]))
|
||||
} catch(e: Exception) {
|
||||
Log.e("Loki", "Invalid push notification: ${e.message}")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// old v1 push notification; we still need this for receiving legacy closed group notifications
|
||||
val base64EncodedData = message.data?.get("ENCRYPTED_DATA")
|
||||
base64EncodedData?.let { Base64.decode(it) }
|
||||
}
|
||||
val base64EncodedData = message.data?.get("ENCRYPTED_DATA")
|
||||
val data = base64EncodedData?.let { Base64.decode(it) }
|
||||
if (data != null) {
|
||||
try {
|
||||
val envelopeAsData = MessageWrapper.unwrap(data).toByteArray()
|
||||
|
Reference in New Issue
Block a user