Setup Basic Authentication in Drupal
Basic Authentication is a simple and straightforward method of authentication. In this approach, the client sends HTTP requests with an Authorization header that contains the Drupal site’s username and password, encoded in Base64 format. With Basic Authentication, the Drupal API Authentication module verifies the user’s credentials against their Drupal username and password. Only if the authentication is successful, the user will be allowed to perform the requested operation. This module is fully compatible with Drupal 8, Drupal 9, Drupal 10, and Drupal 11.
- Using Composer
- Using Drush
- Manual Installation
Pre-requisites:
- REST UI: This module provides a user-friendly interface to configure the REST module.
- Enable the following Web Services modules from the Extend section (/admin/modules) of your Drupal site:
- JSON:API
- REST UI
- RESTful Web Services
- Serialization
Enable API and Assign Methods:
- The first step is to enable the API and assign the methods and operations allowed for that API. This can be done using the REST UI module, or by directly modifying the Drupal config.
- Click on the Enable API button.
- To enable the API using the REST UI module, click the Configure button (as shown below).
- In our example, we need to enable the /entity/user API. To do this, click the Enable option in front of it.
- Since our goal is to create a user in Drupal, select the following configurations:
- Method: POST
- Format: JSON
- Authentication Provider: rest_api_authentication
- This allows the miniOrange API Authentication module to authenticate the API. Click the Save Configuration button to continue.
Configuration Steps
1. Setup Basic Authentication Method:
- In this step, we will set up Basic Authentication as an API authentication method. To do this, navigate to the API Authentication tab of the module (/admin/config/people/rest_api_authentication/auth_settings).
- Under Basic Configuration, enable the Enable Authentication toggle.
- Enter the Application Name and click on the Save Configuration button.
- Scroll down to the Authentication Method section on the same tab and select Basic Authentication method.
- You have successfully configured the Basic Authentication method.
Note: Use the application-specific unique header when authenticating the API.
Grant Role Permissions to Create Users
- If needed, you can allow non-admin Drupal roles to create users. To do this, assign the Administer users permission to the desired roles from the Permissions page (/admin/people/permissions) of your Drupal site.
2. Example:
- For better understanding, let’s look at an example of using Basic Authentication to create a user in Drupal with the REST API.
- To create a user in Drupal, you need to make a POST request using the Drupal username and password. The username and password must be encoded in Base64 format. Refer to the example below for making the call.
Note: The /entity/user API in Drupal is used to create a new user.
HTML Request Format-
Request: POST <your_drupal_base_url>/entity/user?_format=json
Header:
AUTH-METHOD: application_id
Accept: application/json
Content-Type: application/json
Authorization: Basic base64encoded <username:password>
Body:
{
"name": {
"value": "<username>"
},
"mail": {
"value": "<email>"
},
"pass": {
"value": "<password>"
},
"status": {
"value": "1"
}
}
CURL Request Format-
curl --location --request POST ‘<your_drupal_base_url>/entity/user?_format=json' \
--header 'AUTH-METHOD: application_id' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic base64encoded <username:password'>’ \
--data-raw '
{
"name": [
{ "value": "Username" }
],
"mail": [
{ "value": "email" }
],
"pass": [
{ "value": "Password" }
],
"status": [
{ "value": "1" }
]
}
- A successful response will return the details of the user you created (see the image below).
Congratulations! You have successfully set up the Basic Authentication method using the Drupal API Authentication module.
If the configuration was not successful, please contact us at drupalsupport@xecurify.com. Kindly include a screenshot of the error window, and we will assist you in resolving the issue and guide you through the setup.
Thank you for your response. We will get back to you soon.
Something went wrong. Please submit your query again
