WP REST API: Setting Up and Using Basic Authentication

In the introductory part of this series, we had a quick refresher on REST architecture and how it can help us create better applications. We then explored the history of REST APIs in WordPress and introduced ourselves to the latest addition: the WP REST API plugin. We set up a basic working environment for testing with the plugin, which included plugin installation and an HTTP client for sending requests or viewing the server response.

In the current part of the series, we will set up a basic authentication protocol on the server to send authenticated requests to perform various tasks through the REST API.

To be specific, in this part we will:

  • look at various authentication methods available when using the REST API plugin
  • set up basic authentication on the server
  • send authenticated request using Postman
  • send authenticated request using JavaScript framework
  • send authenticated request using command line
  • send authenticated request using WP HTTP API

But let’s first take a look at the authentication itself.

What Is Authentication?

In its most basic definition, authentication is the process of determining the identity of a person.

According to Webopedia:

The process of identifying an individual, usually based on a username and password. In security systems, authentication is distinct from authorization , which is the process of giving individuals access to system objects based on their identity. Authentication merely ensures that the individual is who he or she claims to be, but says nothing about the access rights of the individual.

When talking about WP REST API, a user with sufficient privileges can perform various CRUD tasks such as creating a post, retrieving all the users of the site or revoke the rights of a user. But for all these actions one must prove his/her identity to the server, and that’s where authentication plays its part.

Without proper authentication, it would be very easy for someone with mischievous ambitions to mess around with the site, so authentication provides a necessary layer of security to restrict the rights of a user and the actions that could be performed.

Authentication With the WP REST API

The WP REST API provides three options for authentication, each intended for a specific purpose. These options are:

  • basic authentication
  • OAuth authentication
  • cookie authentication

Right now, the native way to authenticate with WordPress is authentication by cookies. This is how WordPress determines the identity of a user and what actions it can perform. To use the other two authentication methods listed above with the WP REST API, we need to install their respective plugins provided by the WP REST API team available on GitHub. Hopefully, these two methods will also be included in the WordPress core with the REST API plugin itself.

Basic authentication is the most basic type of HTTP authentication, in which login credentials are sent along with the headers of the request.

How Basic Authentication Works

In basic authentication, the client requests a URL that requires authentication. The server requests the client (or user agent) to authenticate itself by sending a 401-Not Authorized code. The client, in return, sends back the same request but with login credentials as a base64 encoded string in the format username:password. This string is sent in the Authorization header field as the following:

So if the username is tutsplus and the password is 123456, the following header field would be sent with the request:

Since the base64 encoded string can easily be decoded, this method is highly insecure to be used on an open network. Hence this method should only be used for debugging and development purposes when the connection between the server and the client is trusted.

Installing the Plugin

As mentioned above, the plugin is available on GitHub from the WP REST API team. So all we need to do is to clone it in our plugins directory and activate it.

Head over to your /wp-content/plugins/ directory and clone the plugin for which you may need sudo rights to run the command. To do so, issue the following:

The terminal will ask for your password. Enter your password and let the repository be cloned into a directory.

After cloning the plugin, activate it by going to your WP Admin. The basic HTTP authentication method can now be used with the REST API plugin.

Sending Authenticated Requests Using Postman

Most HTTP clients support sending a request using the basic authentication method natively, and so does Postman for Chrome. To send an authenticated request, go to the Authorization tab below the address bar:

Authorization Tab

Now select Basic Auth from the drop-down menu. You will be asked to enter your username and password. After entering your credentials, click the Update request button.

Basic Authentication

After updating the authentication option, you will see a change in the Headers tab, and it now includes a header field containing the encoded username and password string:

Authorization Header

That’s all about how we set up basic authentication with Postman. Now you can send a test request like deleting a post, which requires authentication:

Where dev-server is the path to your development server.

If all goes well, the server will return a 200 OK status code, indicating that the post with an id of 52 has been deleted:

Post created

Don’t worry about the request we made here—we will be going through it in more detail in the future parts of the series.

Sending Authenticated Requests From the Command Line

We can use the command line to send authenticated requests using this method. Consider the following curl equivalent of the above request:

The following response would be sent by the server, indicating that everything is okay:

The --request option specifies the request method to be used, which in our case is DELETE. You can also use -X as an alternate to the --request option.

The -I option only fetches the HTTP headers sent by the server. The alternate to -I is the --head option.

Sending Authenticated Requests Using JavaScript

If you are using a client-side JavaScript framework, such as jQuery, to interact remotely with a WordPress site having WP API enabled, you can send the authorization headers in an AJAX request. Consider the following DELETE request sent through the jQuery.ajax() method:

Where Base64 is an object used for encoding and decoding a base64 string. It’s defined as follows, prior to the above jQuery.ajax() method call:

I found it on StackOverflow, and it’s a cross browser way of encoding and decoding a base64 string in JavaScript.

In the above request, we set the Authorization header using the setRequestHeader() method of the xhr object passed as an argument to the beforeSend() method.

In addition to the above request, the Access-Control-Allow-Headers headers should allow the Authorization field on the server. This can be enabled by adding the following line of code in your WordPress .htaccess file:

The above request, when completed, will echo out the response in your browser’s console as shown in the figure below:

Server response

The 200 status response code returned by the server shows that the post with an id of 52 has been deleted successfully.

Sending Authenticated Requests Using WP HTTP API

If you are interacting remotely with another WordPress site from your WordPress installation, the most appropriate way to send HTTP requests is the WP HTTP API.

Consider the following code that sends a DELETE request to another WordPress installation with WP REST API and basic authentication enabled:

Here we used the wp_remote_request() function which accepts two arguments:

  • $url: the URL of the request
  • $args: an array of additional arguments to be passed

The $method defined in the $args array is DELETE, and it should always be written in uppercase. The $headers array takes key value pairs of all the header fields to be passed with the request. We have passed the Authorization key with a base64 encoded username and password string as its value.

The response would be saved in the $wp_delete_post_response variable, which we can use with the wp_remote_retrieve_response_code() and wp_remote_retrieve_response_message() functions. These two functions are helper functions in the WP HTTP API, and they extract the status code and status message from the response respectively.

If the post is deleted successfully by the above request, the following text will be echoed out:

That’s all about the basic authentication method supported by WP REST API. We will be using the same authentication method in our future parts for retrieving, creating, or modifying data due to its simplicity, unless mentioned otherwise.

Conclusion

In the current part of the series, we looked closely at the basic HTTP authentication method supported by WP REST API. However, it shouldn’t be used in live production environments due to the fact that the base64 encoded string could easily be decoded and your credentials might fall into the wrong hands.

After having successfully set up and tested the HTTP basic authentication method, we are ready to take a step further and set up a more sophisticated way of authentication—the OAuth 1.0a method. We will do that in the very next part of the series, so stay tuned!