brave-intl / bat-go

Pass "go", collect 200 BAT
Mozilla Public License 2.0
41 stars 31 forks source link

feat: handle play store developer notifications #2560

Open pavelbrm opened 1 month ago

pavelbrm commented 1 month ago

Summary

This PR introduces handling for Google Play Store Developer Notifications aka Android Webhooks. This functionality has been assumed to be implemented and working, but it was neither implemented properly, nor worked.

The new code is extensively covered with tests, unlike what was there before, which could be said to have not had test coverage at all (the original version prior to https://github.com/brave-intl/bat-go/pull/2318 and https://github.com/brave-intl/bat-go/pull/2360).

Also, this PR supersedes https://github.com/brave-intl/bat-go/pull/2341.

Main Changes

Developer notifications are received in the existing endpoint and are handled in a way similar to how App Store Server Notifications are handled:

Other Changes

A lot of the old garbage code has been purged, and it feels good.

Type of Change

Tested Environments

Before Requesting Review

Manual Test Plan

github-actions[bot] commented 2 weeks ago

The security team is monitoring all repositories for certain keywords. This PR includes the word(s) "authentication" and so security team members have been added as reviewers to take a look.
No need to request a full security review at this stage, the security team will take a look shortly and either clear the label or request more information/changes.
Notifications have already been sent, but if this is blocking your merge feel free to reach out directly to the security team on Slack so that we can expedite this check.

pavelbrm commented 2 weeks ago

The security team is monitoring all repositories for certain keywords. This PR includes the word(s) "authentication" and so security team members have been added as reviewers to take a look. No need to request a full security review at this stage, the security team will take a look shortly and either clear the label or request more information/changes. Notifications have already been sent, but if this is blocking your merge feel free to reach out directly to the security team on Slack so that we can expedite this check.

This PR is already under a security review, robot.

github-actions[bot] commented 2 weeks ago

[puLL-Merge] - brave-intl/bat-go@2560

Description

This PR makes several changes related to handling Play Store notifications for subscriptions in the skus service:

  1. Adds new types and methods for parsing and processing Play Store notifications
  2. Removes the HandleAndroidWebhook function and related types
  3. Adds a new handleWebhookPlayStore function to process Play Store notifications
  4. Refactors the gcpPushNotificationValidator to gpsNtfAuthenticator for authenticating Play Store notifications
  5. Updates error handling and logging when processing notifications
  6. Adds tests for the new Play Store notification processing logic

The motivation for these changes seems to be to improve the handling of Play Store subscription notifications in the skus service.

Changes ### Changes - `services/go.mod`: - Removes the `golang.org/x/crypto` dependency from `require` and moves it to `require` with an `// indirect` comment - `services/skus/appstore.go`: - Adds `shouldCancelOrderIOS` function and related `appStoreTransaction` type to determine if an iOS subscription should be canceled based on the notification payload - `services/skus/appstore_test.go`: - Adds tests for the new `shouldCancelOrderIOS` function - `services/skus/controllers.go`: - Replaces `HandleAndroidWebhook` with a new `handleWebhookPlayStore` function to process Play Store notifications - Refactors `gcpValidator` to `gpsAuth` of type `gpsMessageAuthenticator` - Updates error handling when processing notifications - `services/skus/controllers_noint_test.go`: - Adds a test for `handleReceiptErr` - `services/skus/controllers_pvt_test.go`: - Removes tests for the removed `gcpPushNotificationValidator` type - `services/skus/controllers_test.go`: - Removes the `TestAndroidWebhook` test - Removes `mockVendorReceiptValidator` and `mockGcpRequestValidator` types - `services/skus/input.go`: - Removes `AndroidNotification`, `AndroidNotificationMessage` and related types - `services/skus/model/model.go`: - Adds a new `ErrInvalidVendor` error - `services/skus/playstore.go`: - Adds new types and methods for parsing and processing Play Store notifications - `services/skus/playstore_test.go`: - Adds tests for the new Play Store notification processing logic - `services/skus/receipt.go`: - Removes `dumpTransport` type - Adds `fetchSubPlayStore` method to `vendorReceiptValidator` interface - `services/skus/receipt_test.go`: - Updates tests to use the new error variables from `playstore.go` - `services/skus/service.go`: - Replaces `gcpRequestValidator` with `gpsMessageAuthenticator` in `Service` struct - Adds `processPlayStoreNotification` and `processPlayStoreNotificationTx` methods to handle Play Store notifications - Removes `verifyDeveloperNotification` method - Updates `validateReceipt` to return the new `ErrInvalidVendor` error for unsupported vendors - Removes `shouldCancelOrderIOS` and related `appStoreTransaction` type (moved to `appstore.go`) - `services/skus/service_nonint_test.go`: - Adds tests for the new `processPlayStoreNotificationTx` method - Removes tests for the removed `shouldCancelOrderIOS` function

Security Hotspots

No major security hotspots were identified in this change. The updates primarily focus on refactoring and improving the handling of Play Store notifications for subscriptions.