Skip to content

Commit cb76098

Browse files
committed
Utilize validate_request Rest API nonce method
1 parent 56c8ef2 commit cb76098

2 files changed

Lines changed: 26 additions & 12 deletions

File tree

php/class-rest-api.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
*/
1313
class REST_API {
1414

15+
/**
16+
* Base path for the REST API endpoints.
17+
*
18+
* @var string
19+
*/
1520
const BASE = 'cloudinary/v1';
1621

1722
/**
@@ -21,6 +26,13 @@ class REST_API {
2126
*/
2227
public $endpoints;
2328

29+
/**
30+
* The nonce key used for WordPress REST API authentication.
31+
*
32+
* @var string
33+
*/
34+
const NONCE_KEY = 'wp_rest';
35+
2436
/**
2537
* REST_API constructor.
2638
*
@@ -81,7 +93,7 @@ public function background_request( $endpoint, $params = array(), $method = 'POS
8193

8294
$url = Utils::rest_url( static::BASE . '/' . $endpoint );
8395
// Setup a call for a background sync.
84-
$params['nonce'] = wp_create_nonce( 'wp_rest' );
96+
$params['nonce'] = wp_create_nonce( static::NONCE_KEY );
8597
$args = array(
8698
'timeout' => 0.1,
8799
'blocking' => false,
@@ -115,4 +127,15 @@ public function background_request( $endpoint, $params = array(), $method = 'POS
115127
// Send request.
116128
wp_remote_request( $url, $args );
117129
}
130+
131+
/**
132+
* Validation for request.
133+
*
134+
* @param \WP_REST_Request $request The original request.
135+
*
136+
* @return bool
137+
*/
138+
public static function validate_request( $request ) {
139+
return wp_verify_nonce( $request->get_header( 'x_wp_nonce' ), self::NONCE_KEY );
140+
}
118141
}

php/ui/class-state.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,13 @@ public function rest_endpoints( $endpoints ) {
9898
'method' => \WP_REST_Server::CREATABLE,
9999
'callback' => array( $this, 'set_state' ),
100100
'args' => array(),
101-
'permission_callback' => array( $this, 'validate_request' ),
101+
'permission_callback' => array( 'Cloudinary\REST_API', 'validate_request' ),
102102
);
103103

104104
return $endpoints;
105105
}
106106

107-
/**
108-
* Validation for request.
109-
*
110-
* @param \WP_REST_Request $request The original request.
111-
*
112-
* @return bool
113-
*/
114-
public function validate_request( $request ) {
115-
return wp_verify_nonce( $request->get_header( 'x_wp_nonce' ), 'wp_rest' );
116-
}
107+
117108

118109
/**
119110
* Set the UI state.

0 commit comments

Comments
 (0)