@@ -425,52 +425,63 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
425425 )
426426 }
427427
428- @Suppress(" TooGenericExceptionCaught" , " NestedBlockDepth " , " ComplexMethod " , " LongMethod " )
429- private fun initDecryptedData (inputData : Data ): Boolean {
428+ @Suppress(" TooGenericExceptionCaught" )
429+ private fun initDecryptedData (inputData : Data ): Boolean =
430430 try {
431431 if (inputData.hasKeyWithValueOfType(BundleKeys .KEY_NOTIFICATION_CLEARTEXT_SUBJECT , String ::class .java)) {
432- val subject = inputData.getString(BundleKeys .KEY_NOTIFICATION_CLEARTEXT_SUBJECT )
433- val id = inputData.getLong(BundleKeys .KEY_NOTIFICATION_USER_ID , - 1 )
434- user = userManager.getUserWithId(id).blockingGet()
435- pushMessage = LoganSquare .parse(subject, DecryptedPushMessage ::class .java)
436- return true
432+ initFromCleartextSubject(inputData)
433+ } else {
434+ initFromEncryptedSubject(inputData)
437435 }
436+ } catch (e: Exception ) {
437+ logger.e(TAG , " Error occurred while initializing decoded data " , e)
438+ false
439+ }
438440
439- val subject = inputData.getString(BundleKeys .KEY_NOTIFICATION_SUBJECT )
440- val signature = inputData.getString(BundleKeys .KEY_NOTIFICATION_SIGNATURE )
441-
442- val base64DecodedSubject = Base64 .decode(subject, Base64 .DEFAULT )
443- val base64DecodedSignature = Base64 .decode(signature, Base64 .DEFAULT )
444- val pushUtils = PushUtils ()
445- val privateKey = pushUtils.readKeyFromFile(false ) as PrivateKey
446- try {
447- val signatureVerification = pushUtils.verifySignature(
448- base64DecodedSignature,
449- base64DecodedSubject
450- )
451- if (signatureVerification.signatureValid) {
452- val decryptedSubject = decryptSubject(privateKey, base64DecodedSubject)
441+ private fun initFromCleartextSubject (inputData : Data ): Boolean {
442+ val subject = inputData.getString(BundleKeys .KEY_NOTIFICATION_CLEARTEXT_SUBJECT )
443+ val id = inputData.getLong(BundleKeys .KEY_NOTIFICATION_USER_ID , - 1 )
444+ user = userManager.getUserWithId(id).blockingGet()
445+ pushMessage = LoganSquare .parse(subject, DecryptedPushMessage ::class .java)
446+ return true
447+ }
453448
454- pushMessage = LoganSquare .parse(
455- String (decryptedSubject),
456- DecryptedPushMessage ::class .java
457- )
458- user = signatureVerification.user!!
459- return true
460- } else {
461- logger.e(TAG , " Signature verification failed, discarding push message" )
462- }
463- } catch (e: NoSuchAlgorithmException ) {
464- logger.e(TAG , " No proper algorithm to decrypt the message " , e)
465- } catch (e: NoSuchPaddingException ) {
466- logger.e(TAG , " No proper padding to decrypt the message " , e)
467- } catch (e: InvalidKeyException ) {
468- logger.e(TAG , " Invalid private key " , e)
449+ private fun initFromEncryptedSubject (inputData : Data ): Boolean {
450+ val subject = inputData.getString(BundleKeys .KEY_NOTIFICATION_SUBJECT )
451+ val signature = inputData.getString(BundleKeys .KEY_NOTIFICATION_SIGNATURE )
452+
453+ val base64DecodedSubject = Base64 .decode(subject, Base64 .DEFAULT )
454+ val base64DecodedSignature = Base64 .decode(signature, Base64 .DEFAULT )
455+ val pushUtils = PushUtils ()
456+ val privateKey = pushUtils.readKeyFromFile(false ) as PrivateKey
457+ return try {
458+ val signatureVerification = pushUtils.verifySignature(
459+ base64DecodedSignature,
460+ base64DecodedSubject
461+ )
462+ if (signatureVerification.signatureValid) {
463+ val decryptedSubject = decryptSubject(privateKey, base64DecodedSubject)
464+
465+ pushMessage = LoganSquare .parse(
466+ String (decryptedSubject),
467+ DecryptedPushMessage ::class .java
468+ )
469+ user = signatureVerification.user!!
470+ true
471+ } else {
472+ logger.e(TAG , " Signature verification failed, discarding push message" )
473+ false
469474 }
470- } catch (e: Exception ) {
471- logger.e(TAG , " Error occurred while initializing decoded data " , e)
475+ } catch (e: NoSuchAlgorithmException ) {
476+ logger.e(TAG , " No proper algorithm to decrypt the message " , e)
477+ false
478+ } catch (e: NoSuchPaddingException ) {
479+ logger.e(TAG , " No proper padding to decrypt the message " , e)
480+ false
481+ } catch (e: InvalidKeyException ) {
482+ logger.e(TAG , " Invalid private key " , e)
483+ false
472484 }
473- return false
474485 }
475486
476487 private fun decryptSubject (privateKey : PrivateKey , base64DecodedSubject : ByteArray ): ByteArray =
0 commit comments