Quantcast
Channel: WordPress.org Forums » [WooCommerce] Support
Viewing all articles
Browse latest Browse all 102571

What’s the easiest way to enforce OAuth on my custom REST endpoint in PHP?

$
0
0

Replies: 0

I have developed a very simple plugin for WooCommerce that registers a new WP REST API endpoint, and executes some simple tasks on it. The plugin works, but since it changes product data, I would like to enforce the WooCommerce OAuth keys in the code to prevent unauthorized access.

I’ve been browsing WooCommerce’s GitHub page for about an hour, but I still can’t figure out the easiest way to do this. Here you can see a very basic sketch of my plugin.php file:

add_action('rest_api_init', function () {
	register_rest_route('ssv/v1', '/catalog_visibility/(?P<id>\d+)', array(
		'methods' => 'PUT',
		'callback' => array($this, 'set_catalog_visibility')
	));
});

function set_catalog_visibility($data) {
        if(!check_oauth_consumer_keys()) // <-- This is what I'm after
                return "Auth error";

        // The actual function code...
}

I can see that the authentication happens in class-wc-rest-authentication.php in the authentication_fallback function, but I don’t know how to access this function from a separate WP plugin.

  • This topic was modified 1 hour, 23 minutes ago by sparky621.

Viewing all articles
Browse latest Browse all 102571

Trending Articles