2013-11-04 01:06:28 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce API Authentication Class
|
|
|
|
*
|
2015-05-16 00:16:53 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category API
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\API
|
2015-05-16 00:16:53 +00:00
|
|
|
* @since 2.1.0
|
|
|
|
* @version 2.4.0
|
2013-11-04 01:06:28 +00:00
|
|
|
*/
|
|
|
|
|
2014-09-20 19:24:20 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
|
|
|
class WC_API_Authentication {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup class
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
// To disable authentication, hook into this filter at a later priority and return a valid WP_User
|
2013-11-23 18:28:26 +00:00
|
|
|
add_filter( 'woocommerce_api_check_authentication', array( $this, 'authenticate' ), 0 );
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate the request. The authentication method varies based on whether the request was made over SSL or not.
|
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @param WP_User $user
|
|
|
|
* @return null|WP_Error|WP_User
|
|
|
|
*/
|
|
|
|
public function authenticate( $user ) {
|
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
// Allow access to the index by default
|
2015-01-19 05:33:38 +00:00
|
|
|
if ( '/' === WC()->api->server->path ) {
|
|
|
|
return new WP_User( 0 );
|
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-06-18 15:21:11 +00:00
|
|
|
try {
|
2015-01-19 05:33:38 +00:00
|
|
|
if ( is_ssl() ) {
|
2015-05-16 01:52:00 +00:00
|
|
|
$keys = $this->perform_ssl_authentication();
|
2015-01-19 05:33:38 +00:00
|
|
|
} else {
|
2015-05-16 01:52:00 +00:00
|
|
|
$keys = $this->perform_oauth_authentication();
|
2015-01-19 05:33:38 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
// Check API key-specific permission
|
2015-05-16 01:52:00 +00:00
|
|
|
$this->check_api_key_permissions( $keys['permissions'] );
|
2013-11-19 08:04:00 +00:00
|
|
|
|
2015-05-16 01:52:00 +00:00
|
|
|
$user = $this->get_user_by_id( $keys['user_id'] );
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-07-16 18:42:00 +00:00
|
|
|
$this->update_api_key_last_access( $keys['key_id'] );
|
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
} catch ( Exception $e ) {
|
2013-11-06 06:54:19 +00:00
|
|
|
$user = new WP_Error( 'woocommerce_api_authentication_error', $e->getMessage(), array( 'status' => $e->getCode() ) );
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-04 18:24:14 +00:00
|
|
|
* SSL-encrypted requests are not subject to sniffing or man-in-the-middle
|
|
|
|
* attacks, so the request can be authenticated by simply looking up the user
|
|
|
|
* associated with the given consumer key and confirming the consumer secret
|
|
|
|
* provided is valid
|
2013-11-04 01:06:28 +00:00
|
|
|
*
|
|
|
|
* @since 2.1
|
2015-05-16 00:16:53 +00:00
|
|
|
* @return array
|
2013-11-04 01:06:28 +00:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
private function perform_ssl_authentication() {
|
2014-04-04 18:24:14 +00:00
|
|
|
$params = WC()->api->server->params['GET'];
|
|
|
|
|
2015-06-18 15:21:11 +00:00
|
|
|
// if the $_GET parameters are present, use those first
|
|
|
|
if ( ! empty( $params['consumer_key'] ) && ! empty( $params['consumer_secret'] ) ) {
|
|
|
|
$keys = $this->get_keys_by_consumer_key( $params['consumer_key'] );
|
2014-04-04 18:24:14 +00:00
|
|
|
|
2015-06-18 15:21:11 +00:00
|
|
|
if ( ! $this->is_consumer_secret_valid( $keys['consumer_secret'], $params['consumer_secret'] ) ) {
|
2016-10-12 10:16:30 +00:00
|
|
|
throw new Exception( __( 'Consumer secret is invalid.', 'woocommerce' ), 401 );
|
2015-06-18 15:21:11 +00:00
|
|
|
}
|
2014-04-04 18:24:14 +00:00
|
|
|
|
2015-06-18 15:21:11 +00:00
|
|
|
return $keys;
|
2014-04-04 18:24:14 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-06-18 15:21:11 +00:00
|
|
|
// if the above is not present, we will do full basic auth
|
|
|
|
if ( empty( $_SERVER['PHP_AUTH_USER'] ) || empty( $_SERVER['PHP_AUTH_PW'] ) ) {
|
|
|
|
$this->exit_with_unauthorized_headers();
|
2014-04-04 18:24:14 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-06-18 15:21:11 +00:00
|
|
|
$keys = $this->get_keys_by_consumer_key( $_SERVER['PHP_AUTH_USER'] );
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-06-18 15:21:11 +00:00
|
|
|
if ( ! $this->is_consumer_secret_valid( $keys['consumer_secret'], $_SERVER['PHP_AUTH_PW'] ) ) {
|
|
|
|
$this->exit_with_unauthorized_headers();
|
2014-04-04 18:24:14 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-05-16 01:52:00 +00:00
|
|
|
return $keys;
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 15:21:11 +00:00
|
|
|
/**
|
|
|
|
* If the consumer_key and consumer_secret $_GET parameters are NOT provided
|
|
|
|
* and the Basic auth headers are either not present or the consumer secret does not match the consumer
|
|
|
|
* key provided, then return the correct Basic headers and an error message.
|
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
private function exit_with_unauthorized_headers() {
|
2016-10-11 01:39:13 +00:00
|
|
|
$auth_message = __( 'WooCommerce API. Use a consumer key in the username field and a consumer secret in the password field.', 'woocommerce' );
|
2015-06-18 15:27:17 +00:00
|
|
|
header( 'WWW-Authenticate: Basic realm="' . $auth_message . '"' );
|
2015-06-18 15:21:11 +00:00
|
|
|
header( 'HTTP/1.0 401 Unauthorized' );
|
2016-10-11 01:39:13 +00:00
|
|
|
throw new Exception( __( 'Consumer Secret is invalid.', 'woocommerce' ), 401 );
|
2015-06-18 15:21:11 +00:00
|
|
|
}
|
|
|
|
|
2013-11-04 01:06:28 +00:00
|
|
|
/**
|
|
|
|
* Perform OAuth 1.0a "one-legged" (http://oauthbible.com/#oauth-10a-one-legged) authentication for non-SSL requests
|
|
|
|
*
|
|
|
|
* This is required so API credentials cannot be sniffed or intercepted when making API requests over plain HTTP
|
|
|
|
*
|
|
|
|
* This follows the spec for simple OAuth 1.0a authentication (RFC 5849) as closely as possible, with two exceptions:
|
|
|
|
*
|
2013-11-19 08:03:39 +00:00
|
|
|
* 1) There is no token associated with request/responses, only consumer keys/secrets are used
|
2013-11-04 01:06:28 +00:00
|
|
|
*
|
|
|
|
* 2) The OAuth parameters are included as part of the request query string instead of part of the Authorization header,
|
|
|
|
* This is because there is no cross-OS function within PHP to get the raw Authorization header
|
|
|
|
*
|
|
|
|
* @link http://tools.ietf.org/html/rfc5849 for the full spec
|
|
|
|
* @since 2.1
|
2015-05-16 00:16:53 +00:00
|
|
|
* @return array
|
2013-11-04 01:06:28 +00:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
private function perform_oauth_authentication() {
|
|
|
|
|
|
|
|
$params = WC()->api->server->params['GET'];
|
|
|
|
|
2016-08-27 06:14:06 +00:00
|
|
|
$param_names = array( 'oauth_consumer_key', 'oauth_timestamp', 'oauth_nonce', 'oauth_signature', 'oauth_signature_method' );
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
// Check for required OAuth parameters
|
2013-11-04 01:06:28 +00:00
|
|
|
foreach ( $param_names as $param_name ) {
|
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
if ( empty( $params[ $param_name ] ) ) {
|
2013-11-23 18:28:26 +00:00
|
|
|
throw new Exception( sprintf( __( '%s parameter is missing', 'woocommerce' ), $param_name ), 404 );
|
2015-05-16 00:16:53 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
// Fetch WP user by consumer key
|
2015-05-16 01:52:00 +00:00
|
|
|
$keys = $this->get_keys_by_consumer_key( $params['oauth_consumer_key'] );
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
// Perform OAuth validation
|
2015-05-16 01:52:00 +00:00
|
|
|
$this->check_oauth_signature( $keys, $params );
|
|
|
|
$this->check_oauth_timestamp_and_nonce( $keys, $params['oauth_timestamp'], $params['oauth_nonce'] );
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
// Authentication successful, return user
|
2015-05-16 01:52:00 +00:00
|
|
|
return $keys;
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-16 01:52:00 +00:00
|
|
|
* Return the keys for the given consumer key
|
2013-11-04 01:06:28 +00:00
|
|
|
*
|
2015-05-16 00:16:53 +00:00
|
|
|
* @since 2.4.0
|
2013-11-04 01:06:28 +00:00
|
|
|
* @param string $consumer_key
|
2015-05-16 00:16:53 +00:00
|
|
|
* @return array
|
2013-11-04 01:06:28 +00:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-05-16 01:52:00 +00:00
|
|
|
private function get_keys_by_consumer_key( $consumer_key ) {
|
2015-05-16 00:16:53 +00:00
|
|
|
global $wpdb;
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-06-08 22:58:38 +00:00
|
|
|
$consumer_key = wc_api_hash( sanitize_text_field( $consumer_key ) );
|
|
|
|
|
2015-05-16 01:52:00 +00:00
|
|
|
$keys = $wpdb->get_row( $wpdb->prepare( "
|
2015-07-16 18:42:00 +00:00
|
|
|
SELECT key_id, user_id, permissions, consumer_key, consumer_secret, nonces
|
2015-05-16 00:16:53 +00:00
|
|
|
FROM {$wpdb->prefix}woocommerce_api_keys
|
|
|
|
WHERE consumer_key = '%s'
|
2015-06-08 22:58:38 +00:00
|
|
|
", $consumer_key ), ARRAY_A );
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-05-16 01:52:00 +00:00
|
|
|
if ( empty( $keys ) ) {
|
2016-10-12 10:16:30 +00:00
|
|
|
throw new Exception( __( 'Consumer key is invalid.', 'woocommerce' ), 401 );
|
2015-05-16 00:16:53 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 01:52:00 +00:00
|
|
|
return $keys;
|
2015-05-16 00:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get user by ID
|
|
|
|
*
|
2015-11-17 12:51:42 +00:00
|
|
|
* @since 2.4.0
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2015-11-17 12:51:42 +00:00
|
|
|
* @param int $user_id
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
|
|
|
* @return WP_User
|
2015-11-17 12:51:42 +00:00
|
|
|
* @throws Exception
|
2015-05-16 00:16:53 +00:00
|
|
|
*/
|
|
|
|
private function get_user_by_id( $user_id ) {
|
|
|
|
$user = get_user_by( 'id', $user_id );
|
|
|
|
|
|
|
|
if ( ! $user ) {
|
|
|
|
throw new Exception( __( 'API user is invalid', 'woocommerce' ), 401 );
|
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
return $user;
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-19 08:03:39 +00:00
|
|
|
* Check if the consumer secret provided for the given user is valid
|
2013-11-04 01:06:28 +00:00
|
|
|
*
|
|
|
|
* @since 2.1
|
2015-05-16 01:52:00 +00:00
|
|
|
* @param string $keys_consumer_secret
|
2013-11-19 08:03:39 +00:00
|
|
|
* @param string $consumer_secret
|
2013-11-04 01:06:28 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-05-16 01:52:00 +00:00
|
|
|
private function is_consumer_secret_valid( $keys_consumer_secret, $consumer_secret ) {
|
|
|
|
return hash_equals( $keys_consumer_secret, $consumer_secret );
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify that the consumer-provided request signature matches our generated signature, this ensures the consumer
|
2013-11-19 08:03:39 +00:00
|
|
|
* has a valid key/secret
|
2013-11-04 01:06:28 +00:00
|
|
|
*
|
2015-05-16 01:52:00 +00:00
|
|
|
* @param array $keys
|
2013-11-04 01:06:28 +00:00
|
|
|
* @param array $params the request parameters
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-05-16 01:52:00 +00:00
|
|
|
private function check_oauth_signature( $keys, $params ) {
|
2013-11-04 01:06:28 +00:00
|
|
|
$http_method = strtoupper( WC()->api->server->method );
|
|
|
|
|
2015-06-19 16:05:23 +00:00
|
|
|
$server_path = WC()->api->server->path;
|
|
|
|
|
|
|
|
// if the requested URL has a trailingslash, make sure our base URL does as well
|
2015-07-21 23:08:19 +00:00
|
|
|
if ( isset( $_SERVER['REDIRECT_URL'] ) && '/' === substr( $_SERVER['REDIRECT_URL'], -1 ) ) {
|
2015-06-19 16:05:23 +00:00
|
|
|
$server_path .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
$base_request_uri = rawurlencode( untrailingslashit( get_woocommerce_api_url( '' ) ) . $server_path );
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
// Get the signature provided by the consumer and remove it from the parameters prior to checking the signature
|
2017-03-16 17:23:45 +00:00
|
|
|
$consumer_signature = rawurldecode( str_replace( ' ', '+', $params['oauth_signature'] ) );
|
2013-11-04 01:06:28 +00:00
|
|
|
unset( $params['oauth_signature'] );
|
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
// Sort parameters
|
2014-04-03 20:10:08 +00:00
|
|
|
if ( ! uksort( $params, 'strcmp' ) ) {
|
2016-10-12 10:16:30 +00:00
|
|
|
throw new Exception( __( 'Invalid signature - failed to sort parameters.', 'woocommerce' ), 401 );
|
2014-04-03 20:10:08 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-06-19 23:03:45 +00:00
|
|
|
// Normalize parameter key/values
|
|
|
|
$params = $this->normalize_parameters( $params );
|
|
|
|
$query_parameters = array();
|
2013-11-04 01:06:28 +00:00
|
|
|
foreach ( $params as $param_key => $param_value ) {
|
2015-06-19 23:03:45 +00:00
|
|
|
if ( is_array( $param_value ) ) {
|
|
|
|
foreach ( $param_value as $param_key_inner => $param_value_inner ) {
|
|
|
|
$query_parameters[] = $param_key . '%255B' . $param_key_inner . '%255D%3D' . $param_value_inner;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$query_parameters[] = $param_key . '%3D' . $param_value; // join with equals sign
|
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
2015-06-19 23:03:45 +00:00
|
|
|
$query_string = implode( '%26', $query_parameters ); // join with ampersand
|
2013-11-04 01:06:28 +00:00
|
|
|
|
|
|
|
$string_to_sign = $http_method . '&' . $base_request_uri . '&' . $query_string;
|
|
|
|
|
2016-09-09 00:14:28 +00:00
|
|
|
if ( 'HMAC-SHA1' !== $params['oauth_signature_method'] && 'HMAC-SHA256' !== $params['oauth_signature_method'] ) {
|
2016-10-12 10:16:30 +00:00
|
|
|
throw new Exception( __( 'Invalid signature - signature method is invalid.', 'woocommerce' ), 401 );
|
2014-04-03 20:10:08 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
|
|
|
$hash_algorithm = strtolower( str_replace( 'HMAC-', '', $params['oauth_signature_method'] ) );
|
|
|
|
|
2015-06-19 15:05:51 +00:00
|
|
|
$secret = $keys['consumer_secret'] . '&';
|
|
|
|
$signature = base64_encode( hash_hmac( $hash_algorithm, $string_to_sign, $secret, true ) );
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-01-19 05:34:09 +00:00
|
|
|
if ( ! hash_equals( $signature, $consumer_signature ) ) {
|
2016-10-12 10:16:30 +00:00
|
|
|
throw new Exception( __( 'Invalid signature - provided signature does not match.', 'woocommerce' ), 401 );
|
2014-04-03 20:10:08 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-03 20:11:51 +00:00
|
|
|
* Normalize each parameter by assuming each parameter may have already been
|
|
|
|
* encoded, so attempt to decode, and then re-encode according to RFC 3986
|
|
|
|
*
|
|
|
|
* Note both the key and value is normalized so a filter param like:
|
|
|
|
*
|
|
|
|
* 'filter[period]' => 'week'
|
|
|
|
*
|
|
|
|
* is encoded to:
|
|
|
|
*
|
|
|
|
* 'filter%5Bperiod%5D' => 'week'
|
|
|
|
*
|
|
|
|
* This conforms to the OAuth 1.0a spec which indicates the entire query string
|
|
|
|
* should be URL encoded
|
2013-11-04 01:06:28 +00:00
|
|
|
*
|
|
|
|
* @since 2.1
|
|
|
|
* @see rawurlencode()
|
2017-07-17 10:10:52 +00:00
|
|
|
* @param array $parameters un-normalized parameters
|
2014-04-03 20:11:51 +00:00
|
|
|
* @return array normalized parameters
|
2013-11-04 01:06:28 +00:00
|
|
|
*/
|
2014-04-03 20:11:51 +00:00
|
|
|
private function normalize_parameters( $parameters ) {
|
2015-06-19 23:03:45 +00:00
|
|
|
$keys = WC_API_Authentication::urlencode_rfc3986( array_keys( $parameters ) );
|
|
|
|
$values = WC_API_Authentication::urlencode_rfc3986( array_values( $parameters ) );
|
|
|
|
$parameters = array_combine( $keys, $values );
|
|
|
|
return $parameters;
|
|
|
|
}
|
2014-04-03 20:11:51 +00:00
|
|
|
|
2015-06-19 23:03:45 +00:00
|
|
|
/**
|
|
|
|
* Encodes a value according to RFC 3986. Supports multidimensional arrays.
|
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
* @param string|array $value The value to encode
|
|
|
|
* @return string|array Encoded values
|
|
|
|
*/
|
|
|
|
public static function urlencode_rfc3986( $value ) {
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
return array_map( array( 'WC_API_Authentication', 'urlencode_rfc3986' ), $value );
|
|
|
|
} else {
|
2015-05-16 00:16:53 +00:00
|
|
|
// Percent symbols (%) must be double-encoded
|
2015-06-19 23:03:45 +00:00
|
|
|
return str_replace( '%', '%25', rawurlencode( rawurldecode( $value ) ) );
|
2014-04-03 20:11:51 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify that the timestamp and nonce provided with the request are valid. This prevents replay attacks where
|
|
|
|
* an attacker could attempt to re-send an intercepted request at a later time.
|
|
|
|
*
|
|
|
|
* - A timestamp is valid if it is within 15 minutes of now
|
|
|
|
* - A nonce is valid if it has not been used within the last 15 minutes
|
|
|
|
*
|
2015-05-16 01:52:00 +00:00
|
|
|
* @param array $keys
|
2013-11-04 01:06:28 +00:00
|
|
|
* @param int $timestamp the unix timestamp for when the request was made
|
|
|
|
* @param string $nonce a unique (for the given user) 32 alphanumeric string, consumer-generated
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-05-16 01:52:00 +00:00
|
|
|
private function check_oauth_timestamp_and_nonce( $keys, $timestamp, $nonce ) {
|
2015-05-16 00:16:53 +00:00
|
|
|
global $wpdb;
|
2013-11-04 01:06:28 +00:00
|
|
|
|
|
|
|
$valid_window = 15 * 60; // 15 minute window
|
|
|
|
|
2015-05-16 01:52:00 +00:00
|
|
|
if ( ( $timestamp < time() - $valid_window ) || ( $timestamp > time() + $valid_window ) ) {
|
2016-10-11 01:39:13 +00:00
|
|
|
throw new Exception( __( 'Invalid timestamp.', 'woocommerce' ), 401 );
|
2015-05-16 00:16:53 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-05-16 01:52:00 +00:00
|
|
|
$used_nonces = maybe_unserialize( $keys['nonces'] );
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
if ( empty( $used_nonces ) ) {
|
2013-11-04 01:06:28 +00:00
|
|
|
$used_nonces = array();
|
2015-05-16 00:16:53 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
if ( in_array( $nonce, $used_nonces ) ) {
|
2016-10-11 01:39:13 +00:00
|
|
|
throw new Exception( __( 'Invalid nonce - nonce has already been used.', 'woocommerce' ), 401 );
|
2015-05-16 00:16:53 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
|
|
|
|
$used_nonces[ $timestamp ] = $nonce;
|
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
// Remove expired nonces
|
|
|
|
foreach ( $used_nonces as $nonce_timestamp => $nonce ) {
|
|
|
|
if ( $nonce_timestamp < ( time() - $valid_window ) ) {
|
2013-11-04 01:06:28 +00:00
|
|
|
unset( $used_nonces[ $nonce_timestamp ] );
|
2015-05-16 00:16:53 +00:00
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 00:16:53 +00:00
|
|
|
$used_nonces = maybe_serialize( $used_nonces );
|
|
|
|
|
|
|
|
$wpdb->update(
|
|
|
|
$wpdb->prefix . 'woocommerce_api_keys',
|
|
|
|
array( 'nonces' => $used_nonces ),
|
2015-05-16 01:52:00 +00:00
|
|
|
array( 'key_id' => $keys['key_id'] ),
|
2015-05-16 00:16:53 +00:00
|
|
|
array( '%s' ),
|
|
|
|
array( '%d' )
|
|
|
|
);
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 08:04:00 +00:00
|
|
|
/**
|
|
|
|
* Check that the API keys provided have the proper key-specific permissions to either read or write API resources
|
|
|
|
*
|
2015-05-16 00:16:53 +00:00
|
|
|
* @param string $key_permissions
|
2013-11-19 08:04:00 +00:00
|
|
|
* @throws Exception if the permission check fails
|
|
|
|
*/
|
2015-05-16 00:16:53 +00:00
|
|
|
public function check_api_key_permissions( $key_permissions ) {
|
2013-11-19 08:04:00 +00:00
|
|
|
switch ( WC()->api->server->method ) {
|
|
|
|
|
|
|
|
case 'HEAD':
|
|
|
|
case 'GET':
|
|
|
|
if ( 'read' !== $key_permissions && 'read_write' !== $key_permissions ) {
|
2016-10-11 01:39:13 +00:00
|
|
|
throw new Exception( __( 'The API key provided does not have read permissions.', 'woocommerce' ), 401 );
|
2013-11-19 08:04:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'POST':
|
|
|
|
case 'PUT':
|
|
|
|
case 'PATCH':
|
|
|
|
case 'DELETE':
|
|
|
|
if ( 'write' !== $key_permissions && 'read_write' !== $key_permissions ) {
|
2016-10-11 01:39:13 +00:00
|
|
|
throw new Exception( __( 'The API key provided does not have write permissions.', 'woocommerce' ), 401 );
|
2013-11-19 08:04:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-16 18:42:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updated API Key last access datetime
|
|
|
|
*
|
|
|
|
* @since 2.4.0
|
|
|
|
*
|
|
|
|
* @param int $key_id
|
|
|
|
*/
|
|
|
|
private function update_api_key_last_access( $key_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$wpdb->update(
|
|
|
|
$wpdb->prefix . 'woocommerce_api_keys',
|
|
|
|
array( 'last_access' => current_time( 'mysql' ) ),
|
|
|
|
array( 'key_id' => $key_id ),
|
|
|
|
array( '%s' ),
|
|
|
|
array( '%d' )
|
|
|
|
);
|
|
|
|
}
|
2013-11-04 01:06:28 +00:00
|
|
|
}
|