Skip to content

[API Pull] Auth Flow #2424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jun 26, 2024
Merged

[API Pull] Auth Flow #2424

merged 23 commits into from
Jun 26, 2024

Conversation

puntope
Copy link
Contributor

@puntope puntope commented Jun 7, 2024

Changes proposed in this Pull Request:

  • Auth flow for granting access to WPCOM after enabling the product sync feature
  • Tweak the banner
  • Revoke access when disabling the feature

Detailed test instructions:

  1. Go to Settings and verify you see "We will soon transition to a new and improved method for synchronizing product data with Google -- Get early access" banner
  2. Click on Get early access (see the button loading)
  3. You will be redirected to auth flow
  4. Complete the auth flow
  5. You will be redirected back to the store
  6. You see "Google has been granted access to fetch your product data." in green on the Google MC card
  7. Click on "Disable product data fetch"
  8. Auth is disabled and you see the banner again

Additional details:

  • Deeper unit Tests coming on further PRs
  • Consistency and more tracking events coming in further PRs
  • Notice that the errors are still not handled from Google. You can see that for example removing the nonce param in get_data_from_google function

@puntope puntope changed the title Auth Flow [API Pull] Auth Flow Jun 7, 2024
@github-actions github-actions bot added changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement. labels Jun 7, 2024
Copy link
Contributor

@jorgemd24 jorgemd24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @puntope for working on this. I tested it along with https://github.com/Automattic/woocommerce-connect-server/pull/2485 , and it worked well except that after authorizing the WPCOM App, it seems Google is redirecting to the wrong URL.

image

However, the OAuth flow was successful, as I could see the GLA App connected to my site. I then went to the settings to disconnect the API pull, and it successfully revoked the token.

image

I left some minor comments about the code too.


$response = Client::remote_request( $args );

if ( is_wp_error( $response ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check if the HTTP status code is >= 200? For example, if there are no tokens, the server will respond with status 500 - Internal Server Error. - {"code":"wpcom_partner_token_not_associated","message":"No token found associated with the client ID and user.","data":null}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed here. Thx ece73b1

@@ -3,9 +3,14 @@

namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\API\WP;

use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Ads;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed here thx. 436881c

@ianlin
Copy link
Contributor

ianlin commented Jun 14, 2024

after authorizing the WPCOM App, it seems Google is redirecting to the wrong URL.

Had a look at it and I found they tried to redirect us to this URL:

https://shoppingdataintegration.googleapis.com/v1/credentials/partners/WooCommerce/https%3A%2F%2Ftest-site.example.com%2Fwp-admin%2Fadmin.php%3Fpage%3Dwc-admin%26path%3D%2Fgoogle%2Fsettings%3Fnonce=XXXXX&google_wpcom_app_status=approved

We should ask them to remove https://shoppingdataintegration.googleapis.com/v1/credentials/partners/WooCommerce/ so they could redirect to the correct URL.

However I also noticed there were something wrong in the merchant site's URL:

The URL decoded version of the merchant site's URL is:

https://test-site.example.com/wp-admin/admin.php?page=wc-admin&path=/google/settings?nonce=XXXXX&google_wpcom_app_status=approved

where it added a question mark ? before nonce parameter which it would cause the page not found. We should also ask them the replace that ? with & so it would return to the merchant site correctly.

@puntope
Copy link
Contributor Author

puntope commented Jun 14, 2024

Thanks, @ianlin and @jorgemd24 for your suggestions and investigation. They fixed the error from Google and now seems to work. Also, I addressed the review comments. Can you test again?

@puntope puntope requested a review from jorgemd24 June 14, 2024 14:06
@puntope puntope marked this pull request as ready for review June 14, 2024 14:06
@jorgemd24
Copy link
Contributor

@puntope, I tested it, and it is now redirecting me back to my site. Is there anything else we need to wait for, or can I proceed with the review? Also, I noticed some PHPCS errors.

@puntope
Copy link
Contributor Author

puntope commented Jun 19, 2024

@puntope, I tested it, and it is now redirecting me back to my site. Is there anything else we need to wait for, or can I proceed with the review? Also, I noticed some PHPCS errors.

We need to wait for a fix from google. I will ping you

Copy link

codecov bot commented Jun 19, 2024

Codecov Report

Attention: Patch coverage is 64.70588% with 18 lines in your changes missing coverage. Please review.

Project coverage is 64.3%. Comparing base (aec6d57) to head (e42f728).
Report is 481 commits behind head on feature/google-api-project.

Additional details and impacted files

Impacted file tree graph

@@                      Coverage Diff                       @@
##             feature/google-api-project   #2424     +/-   ##
==============================================================
+ Coverage                          64.2%   64.3%   +0.1%     
- Complexity                         4484    4549     +65     
==============================================================
  Files                               471     794    +323     
  Lines                             18862   22795   +3933     
  Branches                              0    1219   +1219     
==============================================================
+ Hits                              12117   14665   +2548     
- Misses                             6745    7963   +1218     
- Partials                              0     167    +167     
Flag Coverage Δ
js-unit-tests 63.5% <ø> (?)
php-unit-tests 64.6% <64.7%> (+0.3%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
src/API/Google/Middleware.php 95.0% <100.0%> (+8.2%) ⬆️
src/MerchantCenter/AccountService.php 98.3% <100.0%> (+2.1%) ⬆️
...rc/API/Site/Controllers/RestAPI/AuthController.php 92.4% <0.0%> (-1.0%) ⬇️
src/API/WP/OAuthService.php 66.7% <33.3%> (-14.0%) ⬇️

... and 512 files with indirect coverage changes

@puntope
Copy link
Contributor Author

puntope commented Jun 24, 2024

@jorgemd24 @ianlin This seems to be ready for another look. Google deployed their last changes

Copy link
Contributor

@jorgemd24 jorgemd24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @puntope for working on this. I tested the happy path and everything works well. However, if the response from WCS is anything other than 200, I get redirected to wp-admin/undefined.

$request = Client::remote_request( $args );

if ( is_wp_error( $request ) ) {
throw new Exception( $request->get_error_message(), $request->get_error_code() );
Copy link
Contributor

@jorgemd24 jorgemd24 Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Exception expects the second parameter to be an int, but wp_error accepts the code as either a string or int so if wp_error had set the code as a string it will throw a fatal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweaked here. I will return just 400

e42f728

}

/**
* Calls an API by Google via WCS to get required information in order to form an auth URL.
*
* @return array{client_id: string, redirect_uri: string, nonce: string} An associative array contains required information that is retrived from Google.
* client_id: Google's WPCOM app client ID, will be used to form the authorization URL.
* redirect_uri: A Google's URL that will be redirected to when the merchant approve the app access. Note that it needs to be matched with the Google WPCOM app client settings.
* nonce: A string returned by Google that we will put it in the auth URL and the redirect_uri. Google will use it to verify the call.
*/
protected function get_data_from_google(): array {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_sdi_auth_params can return an exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tweaked here

e42f728

$result = $client->get( $this->get_sdi_auth_endpoint() );
$response = json_decode( $result->getBody()->getContents(), true );

if ( 200 !== $result->getStatusCode() ) {
Copy link
Contributor

@jorgemd24 jorgemd24 Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simulated a response other than 200, but I don't see any error and I get redirected to: /wp-admin/undefined

dHe4yg.gif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jorgemd24 How did you simulate the bad response?

When I do so I cannot reproduce the error you mentioned.

Screen.Recording.2024-06-26.at.14.01.27.mov

I did the simulation doing:

$result = new Response(400 );
$response = [];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry! I realized that my simulation was not correct, because I changed this line from if ( 200 !== $result->getStatusCode() ) { to if ( 200 === $result->getStatusCode() ) { but then the Exception was thrown as 200 and that's why I was getting that result.

@jorgemd24
Copy link
Contributor

Also, what do you think about setting the default value of this filter to true? Or do we want the merchant to enable this manually?

return apply_filters( 'woocommerce_gla_notifications_enabled', false );

@jorgemd24
Copy link
Contributor

jorgemd24 commented Jun 25, 2024

One more thought: how will Google return any errors through the OAuth flow, and how are we going to handle them?

@puntope
Copy link
Contributor Author

puntope commented Jun 26, 2024

Or do we want the merchant to enable this manually?

If nothing has changed for now I think the first version requires setting the filter. But not sure if that changed after dark launch discussion

@puntope
Copy link
Contributor Author

puntope commented Jun 26, 2024

One more thought: how will Google return any errors through the OAuth flow, and how are we going to handle them?

The discussion between @ianlin and Google was that they would return google_wpcom_app_status=error. Can you confirm @ianlin ?

@puntope
Copy link
Contributor Author

puntope commented Jun 26, 2024

Thanks @jorgemd24. Can you take another look ?

Copy link
Contributor

@jorgemd24 jorgemd24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the adjustments, LGTM 👍

If nothing has changed for now I think the first version requires setting the filter. But not sure if that changed after dark launch discussion

Okay I will ask them, for me it looks a bit redundant.

The discussion between @ianlin and Google was that they would return google_wpcom_app_status=error. Can you confirm @ianlin ?

If so, I can work on a PR to display those errors to the user.

@puntope puntope merged commit b085f64 into feature/google-api-project Jun 26, 2024
15 checks passed
@puntope puntope deleted the add/auth-flow branch June 26, 2024 17:45
@puntope
Copy link
Contributor Author

puntope commented Jun 26, 2024

Thanks for the adjustments, LGTM 👍

If nothing has changed for now I think the first version requires setting the filter. But not sure if that changed after dark launch discussion

Okay I will ask them, for me it looks a bit redundant.

Sounds good. @jorgemd24 Go ahead and we can remove if so.

The discussion between @ianlin and Google was that they would return google_wpcom_app_status=error. Can you confirm @ianlin ?

If so, I can work on a PR to display those errors to the user.

@jorgemd24 We do display a notice on the card banner and allow granting again. Is this what you mean?

p.s You can also check the tracking topic if you are looking for issues.

@ianlin
Copy link
Contributor

ianlin commented Jun 27, 2024

One more thought: how will Google return any errors through the OAuth flow, and how are we going to handle them?

The discussion between @ianlin and Google was that they would return google_wpcom_app_status=error. Can you confirm @ianlin ?

@puntope @jorgemd24 Yeah correct, they will just set the query param to google_wpcom_app_status=error without what error it is.

@jorgemd24 jorgemd24 mentioned this pull request Jun 28, 2024
35 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants