site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Obtain service credentials without relying on REST API

Open felixarntz opened this issue 7 years ago • 4 comments

Feature Description

The Site Kit plugin currently receives its credentials to communicate with the authentication service via a POST request to the WP REST API. While this has been a secure mechanism to send these credentials, several hosting providers and plugins completely block access to the REST API for non-logged-in users, being the most common point of failure when setting up the plugin. Let's replace this with another mechanism.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • The plugin should include a credentials_retrieval in the supports parameter it sends to the authentication service.
  • The plugin should be able to receive a googlesitekit_site_code parameter alongside a googlesitekit_site_nonce parameter in a GET request. The nonce parameter should be the same nonce originally passed as nonce to the authentication service.
    • Since this request is now relying on the user being logged-in to WP Admin, we can actually use wp_create_nonce() now and don't need to rely on an option for that.
    • The plugin should check the nonce with wp_verify_nonce() (see above), and then issue a POST request to sitekit.withgoogle.com/o/oauth/site/, with the value of googlesitekit_site_code as site_code parameter, and the value of googlesitekit_code as code parameter. The response would be a JSON object with either site_id and site_secret on success, or an error on failure.
    • On success, the site ID and secret should be stored. On error, we should display the error and prompt the user to restart the process / try again from the admin.
  • The above request is always "intertwined" with the request that also includes the googlesitekit_code parameter (for redirecting back to the proxy), and it may also include the googlesitekit_verification_token, googlesitekit_verification_nonce, and googlesitekit_verification_token_type parameters. This is to reduce the back-and-forth being made. The request will always include an action=googlesitekit_proxy_setup parameter. Precisely, we need to support three different types of requests:
    1. Auth service sends googlesitekit_site_code (plus related nonce), and googlesitekit_code. This is when no site credentials are stored on the site yet, and the user is already verified.
    2. In addition to the above, the request includes googlesitekit_verification_token (and the other verification token-related parameters). This is when no site credentials are stored yet, and the user needs to be verified in order to obtain them. In that case, the verification token needs to be stored first so that on the next check the site would be verified, and afterwards the above API call to get the site credentials with the googlesitekit_site_code would need to be made.
    3. The request includes the verification token data and googlesitekit_code, but not the site code and site nonce. This is when a user needs to be verified when the site credentials are already on the site (e.g. a secondary user). (This is what we're already taking care of today.)
  • In all of the above cases, at the end the request should redirect back to the authentication service, the same way as today, with site_id and code parameter (the googlesitekit_code that is present in all of the above 3 cases).
  • The REST API endpoint to receive credentials, and related functionality should be removed.

Implementation Brief

  • Add credentials_retrieval to OAuth_Client::get_proxy_setup_supports
  • Update nonce in proxy setup URL to use a WP nonce
    • Generate using wp_create_nonce( 'googlesitekit_proxy_setup' )

Receiving data from proxy

  • The proxy should send a new action=googlesitekit_proxy_setup query parameter in the request to WP admin for receiving credentials and other fields from the proxy if the site declares credentials_retrieval
  • Hook Site_Verification::handle_verification_token on admin_action_googlesitekit_proxy_setup priority 0
    • Remove current redirect at the end
    • Add filter to add query parameters in return URL accordingly (see below)
      This is a much cleaner way to provide the verification_nonce as well rather than passing an error string directly into the setup URL function
  • Hook new Authentication::handle_site_code on admin_action_googlesitekit_proxy_setup priority 1
    • Use googlesitekit_site_(code|nonce) parameters to make a request to the proxy to obtain site id and secret
      POST sitekit.withgoogle.com/o/oauth/site/
      • as application/x-www-form-urlencoded content type
      • with body parameters
        • site_code=SITECODE
          where SITECODE is the urlencoded value of $_GET['googlesitekit_site_code']
        • code=CODE
          where CODE is the urlencoded value of $_GET['googlesitekit_code']
      • The response will be a JSON object with site_id and site_secret on success, or an error on failure.
      • On success, the site ID and secret should be stored using Credentials::set().
      • On error, save the code to the user's options as OAuth_Client::OPTION_ERROR_CODE and redirect to the splash screen to display the error and prompt the user to restart the process and try again.
        This supersedes the normal redirect back to the proxy at the end.
  • Hook new Authentication::redirect_to_proxy on admin_action_googlesitekit_proxy_setup priority 10
    • Redirects back to the authentication service with site_id and code if no error.
    • Return redirect query parameters (not complete URL) should be filterable so that Site_Verification and other handlers can add parameters dynamically as necessary.
      e.g. googlesitekit_proxy_setup_url_params

Splash

  • Add the user's setup error to googlesitekit.setup.errorCode in Authentication::inline_js_setup_data
  • Add notice for conditionally displaying setup error

Testing

In order to properly test this, we need to implement a way to set query parameters during tests. As such, #886 will be a necessary dependency which can be done simultaneously.

Cleanup

  • Remove googlesitekit_proxy_nonce - keep in Reset
  • Remove oauth/site endpoint from REST_Routes

Changelog entry

  • Introduce new mechanism for a site to receive its credentials from the authentication service, replacing the previous mechanism using the REST API that was error-prone on certain environments.

felixarntz avatar Nov 25 '19 06:11 felixarntz

@aaemnnosttv I like the clean solution in the IB. There are only a few minor details to be updated:

  • I missed a detail in the ACs which I added just now: When requesting the site credentials with the POST request, the plugin should also send the googlesitekit_code value as code parameter, in addition to the "site code". So that's just another parameter to include.
  • Regarding the POST request to /o/oauth/site/, can you update that to use regular URL-encoded body parameters instead of a JSON body? Only the response should be JSON.
  • I'd prefer to use googlesitekit_proxy_setup_url_params for the filter name.

I also updated the AC based on your IB, to include the action parameter. That is just for my own reference for the proxy implementation.

felixarntz avatar Nov 26 '19 06:11 felixarntz

IB updated; back to you @felixarntz .

aaemnnosttv avatar Nov 26 '19 10:11 aaemnnosttv

IB ✅

felixarntz avatar Nov 27 '19 06:11 felixarntz

Tested Disabled API via the plugin below

image

disable-json-api.zip

Ran through a series of positive tests with setup, revoking access, disabling the plugin.

All looking good, no errors.

Passed QA ✅

cole10up avatar Dec 05 '19 17:12 cole10up