---
title: Authenticating MQTT Devices with HiveMQ and Microsoft Entra
description: Looking to authenticating MQTT Devices? Explore how to use HiveMQ Enterprise Security Extension for MQTT client authentication using Microsoft Entra ID.
date: 2024-04-08
published: 2024-04-08
author: Harminder Jandu
tags:
  - HiveMQ Extension
  - Security
source: https://www.hivemq.com/blog/authenticating-mqtt-devices-hivemq-microsoft-entra-safegaurding-iot
---

# Authenticating MQTT Devices with HiveMQ and Microsoft Entra

In Enterprise IoT deployments, ensuring robust security measures is paramount. [HiveMQ](/platform) is a leading Industrial Data Platform that offers advanced security features tailored to the needs of large-scale IoT deployments. Welcome to the blog series on ***Safeguarding Your IoT Ecosystem***!

HiveMQ’s exemplary solution for implementing security is with the [HiveMQ Enterprise Security Extension (ESE)](/products/extensions/enterprise-security-extension)[.](/products/extensions/enterprise-security-extension) The HiveMQ ESE provides comprehensive security functionalities, including authentication, authorization, and encryption. It enables organizations to enforce fine-grained access control policies, ensuring that only authorized devices and users can access IoT resources.

HiveMQ ESE offers seamless integration with existing enterprise authentication systems, simplifying the management of user credentials and access permissions. In Part 1 of this two-part blog series, we look at the HiveMQ ESE setup for MQTT client authentication using Microsoft Entra ID.

The setup entails leveraging the [OAuth 2.0/JSON Web Token (JWT) authentication setup using HiveMQ ESE and Microsoft Entra](https://docs.hivemq.com/hivemq-enterprise-security-extension/latest/ese.html#jwt-mqtt-clients) as the IDP/JWT provider. For further information on Oauth2 flow being used from Microsoft Entra, you can reference details [here](https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-client-creds-grant-flow).

## Prerequisites to Set Up HiveMQ ESE and Microsoft Entra ID 

Before getting started, there are a few items that you will need in order to set up a test. The following are required:

- A Microsoft Entra ID account or access to Entra ID in your Azure instance
- A running HiveMQ Broker in order to set up, which you can [download for free](/download/). Note that the HiveMQ trial comes with ESE for free (remember it runs for 5 hrs, but you can restart HiveMQ to re-run it).

The steps outlined further in this blog post will guide you through setting up your Microsoft Entra ID environment with an Application Registration. The information within the Application Registration will be required as input for HiveMQ ESE configuration. For the setup of HiveMQ ESE configuration, the details on what specific entries are required on the corresponding `config.xml` will be explained.

## Microsoft Entra ID Setup

1. Go to Microsoft Entra and create an account [](https://www.hivemq.com/) or go to Entra within your Azure instance.
2. Navigate to ***Applications*** and select ***App registrations****.*

**Step 1:**

Select the ***New registration*** tab.

![Setting Up HiveMQ ESE and Microsoft Entra ID](https://a.storyblok.com/f/243938/1449x710/11ca503355/authenticating-mqtt-devices-hivemq-microsoft-entra-safegaurding-iot-image-1.webp)

In the ***Register an application*** page that opens, fill out the ***Name field*** and click **Register**.

![Registering a new application on Microsoft Entra](https://a.storyblok.com/f/243938/1999x1673/8f5ae28dc4/authenticating-mqtt-devices-hivemq-microsoft-entra-safegaurding-iot-image-2.webp)

**Step 2:**

On the ***App registrations*** page, note down the following as shown below:

- Application (client) ID
- Directory (tenant) ID

![Copying Application client ID and Directory Tenant ID on Microsoft Entra](https://a.storyblok.com/f/243938/1560x468/7191a73c11/authenticating-mqtt-devices-hivemq-microsoft-entra-safegaurding-iot-image-3.webp)

You will need these for ESE config setup.

**Step 3**:

Create the app role.

1. Navigate to the ***App roles*** and create the necessary roles.
2. Enter ***Display name*** and ***Value***.
3. ***For Allowed member types*** *-* ***Both****.*
4. ***Value*** - this entry will be your scope, so keep note of it for ESE authorization configuration.

![HiveMQ ESE](https://a.storyblok.com/f/243938/1999x952/9af5cc7006/authenticating-mqtt-devices-hivemq-microsoft-entra-safegaurding-iot-image-4.webp)

**Step 4:**

Expose Application ID URI.

Click the ***Add*** beside ***Application ID URI***  box to generate.

You will need this API in order to get the ***Scope*** as part of the client token request.

![HiveMQ ESE - exposing an API](https://a.storyblok.com/f/243938/1999x765/4362e98d82/authenticating-mqtt-devices-hivemq-microsoft-entra-safegaurding-iot-image-5.webp)

**Step 5**:

Select the requested ***API permissions*****.**

- Navigate to ***API permissions***.
- Click on ***APIs my organization uses*** and search for your exposed API just created.
- Select the ***Permissions*** for your app role.

![Request API permissions](https://a.storyblok.com/f/243938/1222x780/bf1c6e3351/authenticating-mqtt-devices-hivemq-microsoft-entra-safegaurding-iot-image-6.webp)

In the ***API permissions*** window, click on ***Grant admin consent for Default Directory****.*

![HiveMQ ESE API Permissions](https://a.storyblok.com/f/243938/1999x926/6331a24115/authenticating-mqtt-devices-hivemq-microsoft-entra-safegaurding-iot-image-7.webp)

## Setting Up the HiveMQ ESE Configuration

Now with the Entra ID setup in place, the next step is to generate the corresponding config.xml for ESE to be applied on the HiveMQ Broker node(s).

As we are using Oauth 2.0/JWT authentication, the ESE configuration setup will be with a `jwt-realm` , which specifies the Entra ID and tenant ID setup in the previous steps. Below is a snippet of the **ESE config.xml**, and enter your appropriate settings for:

- `jwks-endpoint`

## Authorization Preprocessing - Allow All 

Below is a snippet of the ESE configuration that has the `jwt-authentication `manager sourcing the `jwt-realm` setup above and authorization to enable all for testing purposes. You can further adjust the authorization to set up either file-based or SQL DB authorization realm.

### File Authorization Manager Setup

Below is a further example of the ESE configuration that has the `jwt-authentication` manager sourcing the `jwt-realm` setup above and File Authorization Manager setup using file-based realm to apply authorization policies based on the role.

### ESE File Realm - Example for Authorization Based on Role Key

## Test to Get Client Token 

You can test your setup and verify the request of a client token with third-party tools like Postman or curl command. Below are steps to use Postman for that purpose:

1. [Download Postman](https://www.postman.com/)
2. Go to ***Post***
3. Click on ***Body*** section
4. Click ***x-www-form-urlencoded***

Add the below details:

- **Post,** enter your Entra Tenant ID URL `https://login.microsoftonline.com/[TENANTID]/oauth2/v2.0/token`
- **scope** = api://`[YOUR API URL]/.default`

Click ***Send***, and if all is successful, you get the access\_token (screenshot sample below).

![Testing your setup and verifying the request of a client token with third-party tools like Postman](https://a.storyblok.com/f/243938/1999x1135/3ce008039c/authenticating-mqtt-devices-hivemq-microsoft-entra-safegaurding-iot-image-8.webp)

You can copy the access token, which you can then use with any MQTT test client, like the [HiveMQ MQTT CLI](https://hivemq.github.io/mqtt-cli/)[ , ](https://hivemq.github.io/mqtt-cli/)as the password to test authentication. Optionally, you can navigate to `jwt.io` and decode the token to check valid information for claim and scopes as you have set up in Entra ID.

## Conclusion

In conclusion, the HiveMQ Enterprise Security Extension (ESE) stands as a critical solution for securing large-scale IoT deployments, offering robust authentication, authorization, and encryption capabilities alongside seamless integration with enterprise systems such as Microsoft Entra for OAuth 2.0/JWT authentication.

In Part 2 of the ***Safeguarding Your IoT Ecosystem*** series, [Securing MQTT Devices with OIDC Authentication, HiveMQ, and Microsoft Entra, ](/blog/securing-mqtt-devices-oidc-authentication-hivemq-entra-safegaurding-iot)we will show how to set up Single Sign On (SSO) for users to access the HiveMQ Control Center portal with Microsoft Entra ID.  Read on!
