Search Results :
×API Key Authentication Method for WordPress REST API endpoints authentication involves the WordPress REST APIs access on validation against the API key (Bearer token). Whenever a request is made to access the REST API endpoints, authentication will be done against the key (Bearer token), and on the basis of the verification of the API key (Bearer token), the resources for that REST API endpoints request will be allowed to access. An API key is an authentication protocol designed to allow developers to generate authentication keys that could be used for resource owners, such as server-side processes, mobile phone applications, and desktop computers. This method makes sure to secure REST API. The API key can be regenerated in case it is compromised such that all the existing generated keys will expire automatically and this newly generated key will be used for WP REST API authentication. Hence, security will not be any concern. If you don’t have a secure REST API, then it’s easy for manipulators to access your system and steal your data.
This document will take you through step by step process to install and setup WordPress REST API Authentication to secure REST API in few minutes.
1.If you want to protect your WordPress REST API Endpoints (eg. post, pages, or any other REST APIs) from unauthenticated users and you don’t want to share user’s WP login credentials or client id and client secret to authenticate the REST API, then you can use API Key authentication, which will generate a random authentication key for you. Using this key, you can authenticate any WordPress REST API on your site. WordPress REST API Key is the simplest and one of the most popular ways of securing REST API with API Key authentication method. This will make you secure REST API with API key on your website.
2.Suppose you have an Android/ iOS Blog Application and you have already posted all your blogs on WordPress. Now you can get all the posts/ blogs from the WordPress REST APIs but it is publicly accessible. So, whenever you want to protect your GET requests from public users you should use WordPress REST API Key Authentication Method so that your endpoints remain secure.
I.Universal API Key - The Universal API key will be most suitable to authenticate the WP REST APIs which involve HTTP GET method and which does not require WordPress user capabilities. Please note that this key does not involve user capabilities hence can not be used to access those APIs for which WordPress expects user permissions.
Example - If you just want to use the GET APIs for fetching general WordPress posts, comments etc.
II. User-specific API Key - The user-based API will be the most suitable to authenticate the WP REST APIs which involve any of the HTTP methods like - GET, POST, PUT, DELETE and especially in those cases in which you want to perform operations that involve user capabilities.
Example - If you want to perform any operations like fetching WordPress posts based on user capabilities (their WP roles), users data or want to create new users, new posts etc.
Request: GET https://<domain-name>/wp-json/wp/v2/posts
Header:Authorization: Bearer <token>
Sample request: GET https://<domain-name>/wp-json/wp/v2/posts
Header:Authorization: Bearer kGUfhhzXZuWisofgnkAsuHGDyfw7gfhg5s
Sample curl Request Format-
curl -H "Authorization:Bearer <token-value>"
-X GET http://<wp_base_url>/wp-json/wp/v2/posts
I. Authorization :
The HTTP Authorization request header contains the credentials or token type and token value to authenticate a user agent with a server, usually after unsuccessful authentication the server has responded with a 401 Unauthorized status.
II. Bearer <token-value>:
The Bearer <token-value> is created by the Authentication server. When a client application request the authentication server then server authenticate that token and give response to client application accordingly.
var client = new RestClient("http://<wp_base_url>/wp-json/wp/v2/posts ");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer <token-value>");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder()
.url("http://<wp_base_url>/wp-json/wp/v2/posts ")
.method("GET", null)
.addHeader = ("Authorization", "Bearer <token-value>")
.build();
Response responseclient.newCall(request).execute();
var settings = {
"url": "http://<wp_base_url>/wp-json/wp/v2/posts ",
"method": "GET",
"timeout": 0,
"headers": {
"Authorization": "Bearer <token-value >"
},
};
$.ajax(settings).done(function (response) {
console.log(response);
});
<?php
$curl = curl_init();
curl_setopt_array($curl, array
(
CURLOPT_URL => 'http://%3Cwp_base_url%3E/wp-json/wp/v2/posts',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer <token-value>'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
import http.client
conn = http.client.HTTPSConnection("<wp_base_url>")
payload= "
headers = {
'Authorization': 'Bearer <token-value>'
}
conn.request("GET", "/wp-json/wp/v2/posts ", payload, headers)
res= conn.getresponse()
data = res.read()
print (data.decode("utf-8"))
Follow the steps below to make REST API request using Postman:
1. Role Based REST API restriction:
This feature allows restricting the REST API access based on the user roles. You can whitelist the roles for which you want to allow access to the requested resource for the REST APIs. So whenever a REST API request is made by a user, his role will be fetched and only allowed to access the resource if his role is whitelisted.
How to configure it?
Note: The Role based restriction feature is valid for Basic authentication(Username: password), JWT method, OAuth 2.0 (Password grant), and API Key Auth(User specific API key).
2. Custom Header
This feature provides an option to choose a custom header rather than the default ‘Authorization’ header.
It will increase the security as you have the header named with your ‘custom name’, so if someone makes the REST API request with a header as ‘Authorization’ then he won’t be able to access the APIs.
How to configure it?
3. Exclude REST APIs
This feature allows you to whitelist your REST APIs so these can be accessed directly without any authentication. Hence all these whitelisted REST APIs are publicly available.
How to configure it?
4. Create User Specific API key/tokens
How to use this feature:
Congratulations! You have successfully configured the WordPress REST API Key Authentication using this guide. Now, your WordPress REST API endpoints are secure and data is protected from unauthorized access.
Mail us on apisupport@xecurify.com for quick guidance (via email/meeting) on your requirement and our team will help you to select the best suitable solution/plan as per your requirement.
Need Help? We are right here!
Thanks for your inquiry.
If you dont hear from us within 24 hours, please feel free to send a follow up email to info@xecurify.com
This privacy statement applies to miniorange websites describing how we handle the personal information. When you visit any website, it may store or retrieve the information on your browser, mostly in the form of the cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not directly identify you, but it can give you a more personalized web experience. Click on the category headings to check how we handle the cookies. For the privacy statement of our solutions you can refer to the privacy policy.
Necessary cookies help make a website fully usable by enabling the basic functions like site navigation, logging in, filling forms, etc. The cookies used for the functionality do not store any personal identifiable information. However, some parts of the website will not work properly without the cookies.
These cookies only collect aggregated information about the traffic of the website including - visitors, sources, page clicks and views, etc. This allows us to know more about our most and least popular pages along with users' interaction on the actionable elements and hence letting us improve the performance of our website as well as our services.