2020-03-19 11:50:51 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
2021-02-01 17:09:18 +00:00
|
|
|
// Stores the current nonce for the middleware.
|
|
|
|
let currentNonce = '';
|
|
|
|
let currentTimestamp = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const storedNonceValue = window.localStorage.getItem( 'storeApiNonce' );
|
|
|
|
const storedNonce = storedNonceValue ? JSON.parse( storedNonceValue ) : {};
|
|
|
|
currentNonce = storedNonce?.nonce || '';
|
|
|
|
currentTimestamp = storedNonce?.timestamp || 0;
|
|
|
|
} catch {
|
|
|
|
// We can ignore an error from JSON parse.
|
|
|
|
}
|
2020-03-19 11:50:51 +00:00
|
|
|
|
|
|
|
/**
|
2021-04-20 15:44:49 +00:00
|
|
|
* Returns whether or not this is a wc/store API request.
|
2020-03-19 11:50:51 +00:00
|
|
|
*
|
|
|
|
* @param {Object} options Fetch options.
|
|
|
|
*
|
|
|
|
* @return {boolean} Returns true if this is a store request.
|
|
|
|
*/
|
2021-04-20 15:44:49 +00:00
|
|
|
const isStoreApiRequest = ( options ) => {
|
2020-03-19 11:50:51 +00:00
|
|
|
const url = options.url || options.path;
|
|
|
|
if ( ! url || ! options.method || options.method === 'GET' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return /wc\/store\//.exec( url ) !== null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current nonce from a header object.
|
|
|
|
*
|
|
|
|
* @param {Object} headers Headers object.
|
|
|
|
*/
|
|
|
|
const setNonce = ( headers ) => {
|
2021-04-20 15:44:49 +00:00
|
|
|
const nonce =
|
|
|
|
typeof headers?.get === 'function'
|
|
|
|
? headers.get( 'X-WC-Store-API-Nonce' )
|
|
|
|
: headers[ 'X-WC-Store-API-Nonce' ];
|
|
|
|
const timestamp =
|
|
|
|
typeof headers?.get === 'function'
|
|
|
|
? headers.get( 'X-WC-Store-API-Nonce-Timestamp' )
|
|
|
|
: headers[ 'X-WC-Store-API-Nonce-Timestamp' ];
|
2021-02-01 17:09:18 +00:00
|
|
|
|
|
|
|
if ( nonce ) {
|
|
|
|
updateNonce( nonce, timestamp );
|
2020-03-19 11:50:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-01 17:09:18 +00:00
|
|
|
/**
|
|
|
|
* Updates the stored nonce within localStorage so it is persisted between page loads.
|
|
|
|
*
|
|
|
|
* @param {string} nonce Incoming nonce string.
|
|
|
|
* @param {number} timestamp Timestamp from server of nonce.
|
|
|
|
*/
|
|
|
|
const updateNonce = ( nonce, timestamp ) => {
|
|
|
|
// If the "new" nonce matches the current nonce, we don't need to update.
|
|
|
|
if ( nonce === currentNonce ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only update the nonce if newer. It might be coming from cache.
|
|
|
|
if ( currentTimestamp && timestamp < currentTimestamp ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentNonce = nonce;
|
|
|
|
currentTimestamp = timestamp || Date.now() / 1000; // Convert ms to seconds to match php time()
|
|
|
|
|
|
|
|
// Update the persisted values.
|
|
|
|
window.localStorage.setItem(
|
|
|
|
'storeApiNonce',
|
|
|
|
JSON.stringify( {
|
|
|
|
nonce: currentNonce,
|
|
|
|
timestamp: currentTimestamp,
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-04-20 15:44:49 +00:00
|
|
|
const appendNonceHeader = ( request ) => {
|
|
|
|
const headers = request.headers || {};
|
|
|
|
request.headers = {
|
|
|
|
...headers,
|
|
|
|
'X-WC-Store-API-Nonce': currentNonce,
|
|
|
|
};
|
|
|
|
return request;
|
|
|
|
};
|
|
|
|
|
2020-03-19 11:50:51 +00:00
|
|
|
/**
|
2021-04-20 15:44:49 +00:00
|
|
|
* Nonce middleware which appends the current nonce to store API requests.
|
2020-03-19 11:50:51 +00:00
|
|
|
*
|
|
|
|
* @param {Object} options Fetch options.
|
|
|
|
* @param {Function} next The next middleware or fetchHandler to call.
|
|
|
|
* @return {*} The evaluated result of the remaining middleware chain.
|
|
|
|
*/
|
2020-03-27 20:56:48 +00:00
|
|
|
const storeNonceMiddleware = ( options, next ) => {
|
2021-04-20 15:44:49 +00:00
|
|
|
if ( isStoreApiRequest( options ) ) {
|
|
|
|
options = appendNonceHeader( options );
|
|
|
|
|
|
|
|
// Add nonce to sub-requests
|
|
|
|
if ( Array.isArray( options?.data?.requests ) ) {
|
|
|
|
options.data.requests = options.data.requests.map(
|
|
|
|
appendNonceHeader
|
|
|
|
);
|
|
|
|
}
|
2020-03-19 11:50:51 +00:00
|
|
|
}
|
|
|
|
return next( options, next );
|
|
|
|
};
|
|
|
|
|
|
|
|
apiFetch.use( storeNonceMiddleware );
|
|
|
|
apiFetch.setNonce = setNonce;
|
2021-02-01 17:09:18 +00:00
|
|
|
|
|
|
|
// @ts-ignore wcStoreApiNonce is window global cache for the initial nonce initialized from hydration.
|
|
|
|
// @ts-ignore wcStoreApiNonceTimestamp is window global cache for the initial nonce initialized from hydration.
|
|
|
|
updateNonce( wcStoreApiNonce, wcStoreApiNonceTimestamp );
|