moon0326 / wp-openapi

19 stars 5 forks source link

auth option #55

Open Morriz opened 1 week ago

Morriz commented 1 week ago

Hi, how do we provide auth token for requests? Or is that not possible because your plugin wraps many different endpoints with different behavior?

moon0326 commented 1 week ago

Hi @Morriz

Or is that not possible because your plugin wraps many different endpoints with different behavior?

Are you referring to Try it functionality?

Screenshot 2024-10-31 at 10 50 40 AM

If so, I think we can add a filter to add additional request payload conditionally.

I'll look into it this weekend.

moon0326 commented 3 days ago

@Morriz It looks like this can be done without adding an additional filter to this plugin.

You can use rest_pre_dispatch filter.

Here's an example:

function my_custom_rest_request_headers($result, $server, $request) {
    // Check if this is the specific route you want to modify
    if ($request->get_route() === '/wc-admin/features') {
        $params = $request->get_params();
        $params['extra_data'] = 'AdditionalPayloadData';
        $request->set_query_params($params);
    }

    return $result;
}
add_filter('rest_pre_dispatch', 'my_custom_rest_request_headers', 10, 3);

Please let me know if you need a different approach.

Thanks!

Morriz commented 1 day ago

I am now just using one api key for all of wp's api as well as the plugin api's as it is shared....didn't know that ;p

moon0326 commented 11 hours ago

I am now just using one api key for all of wp's api as well as the plugin api's as it is shared....didn't know that ;p

We can also add an option to use any authentication method defined in the openapi spec. I'll look into that.