-
Notifications
You must be signed in to change notification settings - Fork 22
[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
[API Pull] Auth Flow #2424
Conversation
There was a problem hiding this 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.
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.
I left some minor comments about the code too.
src/API/WP/OAuthService.php
Outdated
|
||
$response = Client::remote_request( $args ); | ||
|
||
if ( is_wp_error( $response ) ) { |
There was a problem hiding this comment.
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}
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused dependency
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here thx. 436881c
Had a look at it and I found they tried to redirect us to this URL:
We should ask them to remove 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:
where it added a question mark |
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, 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 |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
|
@jorgemd24 @ianlin This seems to be ready for another look. Google deployed their last changes |
There was a problem hiding this 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
.
src/API/WP/OAuthService.php
Outdated
$request = Client::remote_request( $args ); | ||
|
||
if ( is_wp_error( $request ) ) { | ||
throw new Exception( $request->get_error_message(), $request->get_error_code() ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
} | ||
|
||
/** | ||
* 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tweaked here
$result = $client->get( $this->get_sdi_auth_endpoint() ); | ||
$response = json_decode( $result->getBody()->getContents(), true ); | ||
|
||
if ( 200 !== $result->getStatusCode() ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 = [];
There was a problem hiding this comment.
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.
Also, what do you think about setting the default value of this filter to
|
One more thought: how will Google return any errors through the OAuth flow, and how are we going to handle them? |
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 |
Thanks @jorgemd24. Can you take another look ? |
There was a problem hiding this 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.
Sounds good. @jorgemd24 Go ahead and we can remove if so.
@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. |
@puntope @jorgemd24 Yeah correct, they will just set the query param to |
Changes proposed in this Pull Request:
Detailed test instructions:
Additional details:
get_data_from_google
function