Obtain service credentials without relying on REST API
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_retrievalin thesupportsparameter it sends to the authentication service. - The plugin should be able to receive a
googlesitekit_site_codeparameter alongside agooglesitekit_site_nonceparameter in aGETrequest. Thenonceparameter should be the same nonce originally passed asnonceto 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 aPOSTrequest tositekit.withgoogle.com/o/oauth/site/, with the value ofgooglesitekit_site_codeassite_codeparameter, and the value ofgooglesitekit_codeascodeparameter. The response would be a JSON object with eithersite_idandsite_secreton success, or anerroron 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.
- Since this request is now relying on the user being logged-in to WP Admin, we can actually use
- The above request is always "intertwined" with the request that also includes the
googlesitekit_codeparameter (for redirecting back to the proxy), and it may also include thegooglesitekit_verification_token,googlesitekit_verification_nonce, andgooglesitekit_verification_token_typeparameters. This is to reduce the back-and-forth being made. The request will always include anaction=googlesitekit_proxy_setupparameter. Precisely, we need to support three different types of requests:- Auth service sends
googlesitekit_site_code(plus related nonce), andgooglesitekit_code. This is when no site credentials are stored on the site yet, and the user is already verified. - 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 thegooglesitekit_site_codewould need to be made. - 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.)
- Auth service sends
- 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_idandcodeparameter (thegooglesitekit_codethat 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_retrievaltoOAuth_Client::get_proxy_setup_supports - Update
noncein proxy setup URL to use a WP nonce- Generate using
wp_create_nonce( 'googlesitekit_proxy_setup' )
- Generate using
Receiving data from proxy
- The proxy should send a new
action=googlesitekit_proxy_setupquery parameter in the request to WP admin for receiving credentials and other fields from the proxy if the site declarescredentials_retrieval - Hook
Site_Verification::handle_verification_tokenonadmin_action_googlesitekit_proxy_setuppriority0- 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 theverification_nonceas well rather than passing an error string directly into the setup URL function
- Hook new
Authentication::handle_site_codeonadmin_action_googlesitekit_proxy_setuppriority1- 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-urlencodedcontent type - with body parameters
site_code=SITECODE
whereSITECODEis the urlencoded value of$_GET['googlesitekit_site_code']code=CODE
whereCODEis the urlencoded value of$_GET['googlesitekit_code']
- The response will be a JSON object with
site_idandsite_secreton success, or anerroron 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_CODEand 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.
- as
- Use
- Hook new
Authentication::redirect_to_proxyonadmin_action_googlesitekit_proxy_setuppriority10- Redirects back to the authentication service with
site_idandcodeif no error. - Return redirect query parameters (not complete URL) should be filterable so that
Site_Verificationand other handlers can add parameters dynamically as necessary.
e.g.googlesitekit_proxy_setup_url_params
- Redirects back to the authentication service with
Splash
- Add the user's setup error to
googlesitekit.setup.errorCodeinAuthentication::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/siteendpoint fromREST_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.
@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
POSTrequest, the plugin should also send thegooglesitekit_codevalue ascodeparameter, in addition to the "site code". So that's just another parameter to include. - Regarding the
POSTrequest 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_paramsfor 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.
IB updated; back to you @felixarntz .
IB ✅
Tested Disabled API via the plugin below

Ran through a series of positive tests with setup, revoking access, disabling the plugin.
All looking good, no errors.
Passed QA ✅