don't crash on invalid json response for push subscription (#2613)

This commit is contained in:
Konrad Pozniak 2022-07-11 18:17:58 +02:00 committed by GitHub
parent 3a4a7d8701
commit 526f170d51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -157,7 +157,14 @@ private fun buildSubscriptionData(context: Context, account: AccountEntity): Map
} }
// Called by UnifiedPush callback // Called by UnifiedPush callback
suspend fun registerUnifiedPushEndpoint(context: Context, api: MastodonApi, accountManager: AccountManager, account: AccountEntity, endpoint: String) { suspend fun registerUnifiedPushEndpoint(
context: Context,
api: MastodonApi,
accountManager: AccountManager,
account: AccountEntity,
endpoint: String
) = withContext(Dispatchers.IO) {
// Generate a prime256v1 key pair for WebPush // Generate a prime256v1 key pair for WebPush
// Decryption is unimplemented for now, since Mastodon uses an old WebPush // Decryption is unimplemented for now, since Mastodon uses an old WebPush
// standard which does not send needed information for decryption in the payload // standard which does not send needed information for decryption in the payload
@ -166,27 +173,22 @@ suspend fun registerUnifiedPushEndpoint(context: Context, api: MastodonApi, acco
val keyPair = CryptoUtil.generateECKeyPair(CryptoUtil.CURVE_PRIME256_V1) val keyPair = CryptoUtil.generateECKeyPair(CryptoUtil.CURVE_PRIME256_V1)
val auth = CryptoUtil.secureRandomBytesEncoded(16) val auth = CryptoUtil.secureRandomBytesEncoded(16)
withContext(Dispatchers.IO) { api.subscribePushNotifications(
api.subscribePushNotifications( "Bearer ${account.accessToken}", account.domain,
"Bearer ${account.accessToken}", account.domain, endpoint, keyPair.pubkey, auth,
endpoint, keyPair.pubkey, auth, buildSubscriptionData(context, account)
buildSubscriptionData(context, account) ).onFailure { throwable ->
).onFailure { Log.w(TAG, "Error setting push endpoint for account ${account.id}", throwable)
Log.d(TAG, "Error setting push endpoint for account ${account.id}") disableUnifiedPushNotificationsForAccount(context, account)
Log.d(TAG, Log.getStackTraceString(it)) }.onSuccess {
Log.d(TAG, (it as HttpException).response().toString()) Log.d(TAG, "UnifiedPush registration succeeded for account ${account.id}")
disableUnifiedPushNotificationsForAccount(context, account) account.pushPubKey = keyPair.pubkey
}.onSuccess { account.pushPrivKey = keyPair.privKey
Log.d(TAG, "UnifiedPush registration succeeded for account ${account.id}") account.pushAuth = auth
account.pushServerKey = it.serverKey
account.pushPubKey = keyPair.pubkey account.unifiedPushUrl = endpoint
account.pushPrivKey = keyPair.privKey accountManager.saveAccount(account)
account.pushAuth = auth
account.pushServerKey = it.serverKey
account.unifiedPushUrl = endpoint
accountManager.saveAccount(account)
}
} }
} }