I'm creating a single page app with WP REST API and AngularJS. After solving a problem with nonces on this question, now i'm facing something else.
To test the chance of adding fields and making them protected against non-logged users, i tried to create a custom field which exposes the result of wp_verify_nonce
if (isset($_SERVER['HTTP_X_WP_NONCE'])) {
$nonce = $_SERVER['HTTP_X_WP_NONCE'];
$nonce_verified = wp_verify_nonce($_SERVER['HTTP_X_WP_NONCE'], 'wp_rest');
}
return array('nonce' => $nonce,
'nonce_verified' => $nonce_verified);
Of course, this is a test and will remain so. Nonce is exposed and so is nonce_verified
. At this point, i tried to login/logout a user and require this endpoint and users/me. But it happened that my nonce is always verified, either with logged in or logged out user. Nonce itself changes while doing this; still, when logged out, it's the same for all of its lifetime.
To create my nonce, i used a localized code as in my previous question:
wp_localize_script('angularjs', 'params', array(
'nonce' => wp_create_nonce('wp_rest'),
));
which is passed through javascript, as i saw in a tutorial about AngularJS. Although this nonce gets validated, query to users/me
leads to a 403 when not logged, as expected.
My theory is that a nonce for non-logged users is being created, so there is always a valid nonce available.
Should i protect nonce creation with is_user_logged_in()
? Or should i hook the nonce creation somewhere? Thank you.