WordPress REST API OAuth 2.0 Authentication Method



WordPress REST API OAuth 2.0 Authentication Method involves the use of OAuth protocol to obtain the access or Id token and that token will be used to authenticate the REST APIs. Each time a request to access the API will be made, the authentication will be done against that access token/id token, and on the basis of the verification of that API token, the resources for that API request will be allowed to access.

Download And Installation

  • Log into your WordPress instance as an admin.
  • Go to the WordPress Dashboard -> Plugins and click on Add New.
  • Search for a WordPress REST API Authentication plugin and click on Install Now.
  • Once installed click on Activate.

Use Case: How to protect/secure WordPress REST API endpoints using OAuth 2.0 method.

    1. Password Grant:

    Use Case: Authenticate/ protect WordPress REST API Endpoints securely using user credentials obtained on user registration or login into WordPress

    Suppose you have a login form in your application and want to authenticate the WordPress REST API endpoints on the basis of the user credentials and their capabilities, then you can easily authenticate the REST API endpoints very securely using this method. This method follows the standard OAuth 2.0 flow/protocol and our plugin acts both as a OAuth 2.0 Identity provider(Server) that will be used to provide the access/id token REST API request and REST API authenticator for authentication of the REST APIs endpoints on the basis of the token.

    2. Client Credentials:

    Use Case: Authenticate/ protect WordPress REST API Endpoints securely without using user credentials.

    If in case you do not want to send user credentials in the WordPress REST API endpoint request to obtain method then you can opt for this method as it follows the OAuth 2.0 client credentials flow/protocol and our plugin acts both as a OAuth 2.0 Identity provider(Server) which provides the token and REST APIs authenticator for authentication of these WordPressREST API endpoints on the basis of the token. Hence it provides utmost security to obtain the token and that token can be used to authenticate the REST API request.

  • The flow for WordPress REST API authentication can be achieved using the OAuth 2.0 method is explained below:
    • 1. The REST API request will be made with appropriate parameters to obtain the token for our plugin. Our plugin will act as an OAuth 2.0 Identity provider and provides the access token.

      2. The actual REST API request to access the resource will be made with the access token received from the last step passed in the Authorization header with token type as Bearer. The plugin now acts as Authenticator to authenticate the API on the basis of token validity. If the token is validated successfully then the API requester will be allowed to access the resource else on the failed validation the error response will be returned.

WordPress REST API OAuth 2.0 using Password Grant:

  • Select yourAuthentication method → OAuth 2.0 and OAuth 2.0 Grant Type → Password Grant and Token Type → Access Token/JWT Token based on your choice and click on Save Configuration.
  • WordPress REST API OAuth 2.0 Authentication method
  • Once you click on the save configuration, you will get the Client ID, Client Secret and Token Endpoint.
  • Here you would need to make two calls:
  • I : Get the Token

    • To get the access token/JWT Token, you would need to make an API Call to Token endpoint as below
    • Request: POST https://<domain-name>/wp-json/api/v1/token
      Body:
      grant_type = <password>
      username = <wordpress username>
      password = <wordpress password>
      client_id = <client id>
      
    • Using Refresh Token
    • Request: POST https://<domain-name>/wp-json/api/v1/token
      Body:
      grant_type = <password>
      refresh_token = <Refresh Token>
      

    II : Send API Request

    • Once you get the access_token / id_token, you can use it to request the access to the WordPress REST APIs as shown below:
    • Request: GET https://<domain-name>/wp-json/wp/v2/posts
      
      Header: 
      Authorization : Bearer <access_token /id_token>
      
    • NOTE:Above token is valid for 1 hour by default. Users have to create a token each time they want to request the API access.

WordPress REST API OAuth 2.0 using Client Credentials Grant:

  • Select yourAuthentication method → OAuth 2.0 and OAuth 2.0 Grant Type → Client Credentials Grant and Token Type → Access Token/JWT Token based on your choice and click on Save Configuration.
  • WordPress REST API OAuth 2.0 Authentication method using jwt
  • Once you click on the save configuration, you will get the Client ID, Client Secret and Token Endpoint.
  • Here you would need to make two calls:
  • I : Get the Token

    • To get the access token/JWT Token, you would need to make an API Call to Token endpoint as below
    • Request: POST https://<domain-name>/wp-json/api/v1/token
      Body:
      grant_type = <client_credentials>
      client_id = <client id>
      client_secret = <client secret>
      
    • Using Refresh Token
    • Request: POST https://<domain-name>/wp-json/api/v1/token
      Body:
      grant_type = <refresh_token>
      refresh_token = <Refresh Token>
      

    II : Send API Request

    • Once you get the access_token / id_token, you can use it to request the access to the WordPress REST APIs as shown below:
    • Request: GET https://<domain-name>/wp-json/wp/v2/posts
      
      Header: 
      Authorization : Bearer <access_token /id_token>
      
    • NOTE:Above token is valid for 1 hour by default. Users have to create a token each time they want to request the API access.
  • Check out the developer documentation for more details.

Postman Samples:

  • OAuth 2.0 password Grant method:
    • Sample request to obtain token:
    • You can download the postman request sample from here.
    • Now extract the zip file and import the extracted json file into the postman application.
    • WordPress REST API OAuth 2.0 Authentication method postman implementation
    • Example
    • WordPress REST API OAuth 2.0 Authentication method postman replace url actual resource
  • Sample request format to request resources using the token obtained in the last step.
  • You can download the postman request sample from here.
  • Now extract the zip file and import the extracted json file into the postman application.
  • WordPress REST API OAuth 2.0 Authentication method postman implementation
  • Example
  • WordPress REST API OAuth 2.0 Authentication method postman replace url actual resource
  • OAuth 2.0 Client Credentials Grant Method:
    • Sample request to obtain token:
    • You can download the postman request sample from here.
    • Now extract the zip file and import the extracted json file into the postman application.
    • WordPress REST API OAuth 2.0 Authentication method postman implementation
    • Example
    • WordPress REST API OAuth 2.0 Authentication method postman replace url actual resource
  • REST API request to obtain the actual resource:
    • You can download the postman request sample from here.
    • Now extract the zip file and import the extracted json file into the postman application.
    • WordPress REST API OAuth 2.0 Authentication method postman implementation
    • Example
    • WordPress REST API OAuth 2.0 Authentication method postman url actual resource

Feature Description

    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?

    • First, go to the plugin ‘Advanced Settings’ tab.
    • Then, in the Role based Restriction section, all the roles by default will be allowed to access the APIs. You can enable the checkbox of the roles for which you want to restrict access.
    • WordPress REST API Basic Authentication method postman implementation
    • In the above screenshot, the subscriber role checkbox is enabled. So whenever an API request is made by the user with his role as subscriber then that user won’t be allowed to access the requested resource.

    Note: The Role based restriction feature is valid for Basic authentication(Username: password), JWT method, and OAuth 2.0 (Password grant).

    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?

    • First, go to the plugin ‘Advanced Settings’ tab.
    • Then in the ‘Custom Header’ section, you can edit the textbox to enter the custom name you want.
    • WordPress REST API Basic Authentication method postman implementation

    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?

    • First, go to the plugin ‘Advanced Settings’ tab.
    • Then in the ‘Exclude REST APIs’, you can enter your APIs in the prescribed format which needs to be whitelisted for public access.
    • WordPress REST API Basic Authentication method postman implementation
    • Example: Suppose if you want to exclude the REST API ‘/wp-json/wp/v2/posts’ then you have to enter ‘/wp/v2/posts’ in the textbox.

    4. Custom Token Expiry

    This feature is applicable for JWT and OAuth 2.0 methods which uses time based tokens to authenticate the WordPress REST API endpoints. This feature allows you to set the custom expiry for the tokens such that the token will no longer be valid once the token expires.


    How to configure it?

    • First, go to the plugin ‘Advanced Settings’ tab.
    • Then in the ‘Token Expiry Configuration’ section, the access token validity and refresh token(used for OAuth 2.0 method) can be altered. By default the access token expiry time is set to 60 minutes and the refresh token expiry time is set to 14 days. Hence with this feature, the expiry can be adjusted accordingly as per the requirements.
    • WordPress REST API Basic Authentication method postman implementation

    Hence, with this custom token expiry feature, the security is increased furthermore.

    5. Enable Advanced Encryption for the tokens using HMAC

    This feature is available with the Basic Authentication method in which by default the token is encrypted using Base64 encoding technique but with the advanced feature, the token can be encrypted with highly secure HMAC encryption which is very secure.

    WordPress REST API Basic Authentication method postman implementation

    6. Signature Validation for JWT based tokens

    This feature allows a secure signing of the JWT signature for the JWT token such that your JWT token is much more secure and the signature can only be decoded using the client secret/certificate. It means your signature is private and can not be seen by others.

    WordPress REST API Basic Authentication method postman implementation

    We provide the support for 2 Signing algorithms: HS256 and RS256.So, any of the signing algorithms can be chosen from the dropdown as shown in the above image.

    Also, you need to add your client secret or certificate from which is used to sign the signature of the JWT.

    7. Create User Specific API key/tokens

    • This feature is available within the API key method in which the tokens can be generated in accordance with the user information rather than a randomly generated token which is a universal key.
    • With the Universal API key/token, the user can not have permission to certain WordPress REST APIs with request method as POST,PUT,DELETE like creating users, posts, pages etc, in which a particular user permissions/role is required to perform operations via use of the REST API request as the universal key is randomly generated and does not contain the user based description.
    • So this User based API key/token feature allows the user to access the REST APIs with request method as POST,PUT,DELETE in WordPress which requires user credentials or certain roles to perform the operation such that the when the WordPress REST API request is made with the user-based key then the role of the user is obtained and he will be allowed to access the API only if he has permission to do so.
    • For Example: Only users with administrator and editor roles have permissions to create/edit/delete a post.
    • So, if a request is made to this API to create/delete/edit the post, the API response will result in “You are not allowed to perform this operation”.
    • Now, if a request is made with the user-based token generated for the user that has administrator or editor role then only they have access to this API and are able to perform the operation(create/update/delete) via the API call.
    • How to use this feature:

    • Select the user from the dropdown and click on the Create API Key button.
    • WordPress REST API Basic Authentication method postman implementation
    • A pop up will appear on the screen, you just need to click on the OK button to copy the token.
    • WordPress REST API Basic Authentication method postman implementation
    • Now this token can be used with the API request just like the universal key is used to make the API request.

Need Help?

Mail us on oauthsupport@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.


Hello there!

Need Help? We are right here!

support
Contact miniOrange Support
success

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