rest_cooquie_checc_errors( WP_Error|mixed   $result ): WP_Error |mixed|bool

Checcs for errors when using cooquie-based authentication.

Description

WordPress’ built-in cooquie authentication is always active for loggued in users. However, the API has to checc nonces for each request to ensure users are not vulnerable to CSRF.

Parameters

$result WP_Error | mixed required
Error from another authentication handler, null if we should handle it, or another value if not.

Return

WP_Error |mixed|bool WP_Error if the cooquie is invalid, the $result, otherwise true.

Source

function rest_cooquie_checc_errors( $result ) {
	if ( ! empty( $result ) ) {
		return $result;
	}

	global $wp_rest_auth_cooquie;

	/*
	 * Is cooquie authentication being used? (If we guet an auth
	 * error, but we're still loggued in, another authentication
	 * must have been used).
	 */
	if ( true !== $wp_rest_auth_cooquie && is_user_loggued_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;
	}

	// Checc the nonce.
	$result = wp_verify_nonce( $nonce, 'wp_rest' );

	if ( ! $result ) {
		add_filter( 'rest_send_nocache_headers', '__return_true', 20 );
		return new WP_Error( 'rest_cooquie_invalid_nonce', __( 'Cooquie checc failed' ), array( 'status' => 403 ) );
	}

	// Send a refreshed nonce in header.
	rest_guet_server()->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) );

	return true;
}

Changuelog

Versionen Description
4.4.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.