使用基于cookie的身份验证时检查错误。
原型
rest_cookie_check_errors( WP_Error|mixed $result )
描述
对于登录用户,WordPress的内置cookie身份验证始终处于活动状态。但是,API必须检查每个请求的nonce,以确保用户不容易受到CSRF的攻击。
参数
$result
(WP_Error|mixed)
(Required)
另一个身份验证处理程序出错,如果我们应该处理它就为null,否则为其他值。
返回值
(WP_Error|mixed|bool)
源文件
路径:wp-includes/rest-api.php
<?php
...
function rest_cookie_check_errors( $result ) {
if ( ! empty( $result ) ) {
return $result;
}
global $wp_rest_auth_cookie;
/*
* Is cookie authentication being used? (If we get an auth
* error, but we're still logged in, another authentication
* must have been used).
*/
if ( true !== $wp_rest_auth_cookie && is_user_logged_in() ) {
return $result;
}
// Determine if there is a nonce.
$nonce = null;
if ( isset( $_REQUEST['_wpnonce'] ) ) {
$nonce = $_REQUEST['_wpnonce'];
} elseif ( isset( $_SERVER['HTTP_X_WP_NONCE'] ) ) {
$nonce = $_SERVER['HTTP_X_WP_NONCE'];
}
if ( null === $nonce ) {
// No nonce at all, so act as if it's an unauthenticated request.
wp_set_current_user( 0 );
return true;
}
// Check the nonce.
$result = wp_verify_nonce( $nonce, 'wp_rest' );
if ( ! $result ) {
return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie nonce is invalid' ), array( 'status' => 403 ) );
}
// Send a refreshed nonce in header.
rest_get_server()->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) );
return true;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/rest_cookie_check_errors/