Authentication with internal WP_REST_Request and rest_do_request()

How can I add authentication for rest_do_request()?

No. This solution to your problems will not work, and it will not save time or effort.

In fact, it will require extensive and concerted effort, and lots of time. This despite the entire purpose being to avoid dealing with undocumented PHP code by using the REST API instead.

The fundamental problem is that rest_do_request handles the request in the same request as the current page load, and so it runs in the same context.

So to accomplish what you want, we need to trick WordPress into thinking you’re logged in, and as someone else who has the privileges to use that route. This then continues for every single API that route uses.

On top of that, plugins can load code differently depending on the context, so you would need to change how they load, but you don’t know if you need to do this yet because shortcodes are processed much later on.

So, you will need to make significant adjustments to WordPress’ internal state, as well as lots of adjustments to the plugins, possibly forking them.

The only practical method, is to make a REST API request using the HTTP APIs, but this will incur a large performance cost.

Either way, there are no advantages to this approach.