|
| 1 | +--- |
| 2 | +title: How to connect a service to IoT Hub using Microsoft Entra (Node.js) |
| 3 | +titleSuffix: Azure IoT Hub |
| 4 | +description: Learn how to connect a service to IoT Hub using Microsoft Entra and the Azure IoT Hub SDK for Node.js. |
| 5 | +author: kgremban |
| 6 | +ms.author: kgremban |
| 7 | +ms.service: iot-hub |
| 8 | +ms.devlang: javascript |
| 9 | +ms.topic: include |
| 10 | +ms.manager: lizross |
| 11 | +ms.date: 11/19/2024 |
| 12 | +--- |
| 13 | + |
| 14 | +A backend app that uses Microsoft Entra must successfully authenticate and obtain a security token credential before connecting to IoT Hub. This token is passed to a IoT Hub connection method. For general information about setting up and using Microsoft Entra for IoT Hub, see [Control access to IoT Hub by using Microsoft Entra ID](/azure/iot-hub/authenticate-authorize-azure-ad). |
| 15 | + |
| 16 | +For an overview of Node.js SDK authentication, see: |
| 17 | + |
| 18 | +* [Getting started with user authentication on Azure](/azure/developer/javascript/how-to/with-authentication/getting-started) |
| 19 | +* [Azure Identity client library for JavaScript](/javascript/api/overview/azure/identity-readme) |
| 20 | + |
| 21 | +##### Configure Microsoft Entra app |
| 22 | + |
| 23 | +You must set up a Microsoft Entra app that is configured for your preferred authentication credential. The app contains parameters such as client secret that are used by the backend application to authenticate. The available app authentication configurations are: |
| 24 | + |
| 25 | +* Client secret |
| 26 | +* Certificate |
| 27 | +* Federated identity credential |
| 28 | + |
| 29 | +Microsoft Entra apps may require specific role permissions depending on operations being performed. For example, [IoT Hub Twin Contributor](/azure/role-based-access-control/built-in-roles/internet-of-things#iot-hub-twin-contributor) is required to enable read and write access to a IoT Hub device and module twins. For more information, see [Manage access to IoT Hub by using Azure RBAC role assignment](/azure/iot-hub/authenticate-authorize-azure-ad?#manage-access-to-iot-hub-by-using-azure-rbac-role-assignment). |
| 30 | + |
| 31 | +For more information about setting up a Microsoft Entra app, see [Quickstart: Register an application with the Microsoft identity platform](/entra/identity-platform/quickstart-register-app). |
| 32 | + |
| 33 | +##### Authenticate using DefaultAzureCredential |
| 34 | + |
| 35 | +The easiest way to use Microsoft Entra to authenticate a backend application is to use [DefaultAzureCredential](/javascript/api/@azure/identity/defaultazurecredential), but it's recommended to use a different method in a production environment including a specific `TokenCredential` or pared-down `ChainedTokenCredential`. For simplicity, this section describes authentication using `DefaultAzureCredential` and Client secret. |
| 36 | +For more information about the pros and cons of using `DefaultAzureCredential`, see |
| 37 | +[Credential chains in the Azure Identity client library for JavaScript](/azure/developer/javascript/sdk/credential-chains#use-defaultazurecredential-for-flexibility) |
| 38 | + |
| 39 | +[DefaultAzureCredential](/javascript/api/@azure/identity/defaultazurecredential) supports different authentication mechanisms and determines the appropriate credential type based on the environment it's executing in. It attempts to use multiple credential types in an order until it finds a working credential. |
| 40 | + |
| 41 | +Microsoft Entra requires this package: |
| 42 | + |
| 43 | +```shell |
| 44 | +npm install --save @azure/identity |
| 45 | +``` |
| 46 | + |
| 47 | +In this example, Microsoft Entra app registration client secret, client ID, and tenant ID have been added to environment variables. These environment variables are used by `DefaultAzureCredential` to authenticate the application. The result of a successful Microsoft Entra authentication is a security token credential that is passed to an IoT Hub connection method. |
| 48 | + |
| 49 | +```javascript |
| 50 | +import { DefaultAzureCredential } from "@azure/identity"; |
| 51 | + |
| 52 | +// Azure SDK clients accept the credential as a parameter |
| 53 | +const credential = new DefaultAzureCredential(); |
| 54 | +``` |
| 55 | + |
| 56 | +The resulting credential token can then be passed to [fromTokenCredential](/javascript/api/azure-iothub/registry?#azure-iothub-registry-fromtokencredential) to connect to IoT Hub for any SDK client that accepts Microsoft Entra credentials: |
| 57 | + |
| 58 | +* [Registry](/javascript/api/azure-iothub/registry?#azure-iothub-registry-fromtokencredential) |
| 59 | +* [Client](/javascript/api/azure-iothub/client?#azure-iothub-client-fromtokencredential) |
| 60 | +* [JobClient](/javascript/api/azure-iothub/jobclient?#azure-iothub-jobclient-fromtokencredential) |
| 61 | + |
| 62 | +`fromTokenCredential` requires two parameters: |
| 63 | + |
| 64 | +* The Azure service URL - The Azure service URL should be in the format `{Your Entra domain URL}.azure-devices.net` without a `https://` prefix. For example, `MyAzureDomain.azure-devices.net`. |
| 65 | +* The Azure credential token |
| 66 | + |
| 67 | +In this example, the Azure credential is obtained using `DefaultAzureCredential`. The Azure domain URL and credential are then supplied to `Registry.fromTokenCredential` to create the connection to IoT Hub. |
| 68 | + |
| 69 | +```javascript |
| 70 | +const { DefaultAzureCredential } = require("@azure/identity"); |
| 71 | + |
| 72 | +let Registry = require('azure-iothub').Registry; |
| 73 | + |
| 74 | +// Define the client secret values |
| 75 | +clientSecretValue = 'xxxxxxxxxxxxxxx' |
| 76 | +clientID = 'xxxxxxxxxxxxxx' |
| 77 | +tenantID = 'xxxxxxxxxxxxx' |
| 78 | + |
| 79 | +// Set environment variables |
| 80 | +process.env['AZURE_CLIENT_SECRET'] = clientSecretValue; |
| 81 | +process.env['AZURE_CLIENT_ID'] = clientID; |
| 82 | +process.env['AZURE_TENANT_ID'] = tenantID; |
| 83 | + |
| 84 | +// Acquire a credential object |
| 85 | +const credential = new DefaultAzureCredential() |
| 86 | + |
| 87 | +// Create an instance of the IoTHub registry |
| 88 | +hostName = 'MyAzureDomain.azure-devices.net'; |
| 89 | +let registry = Registry.fromTokenCredential(hostName,credential); |
| 90 | +``` |
| 91 | + |
| 92 | +##### Code samples |
| 93 | + |
| 94 | +For working samples of Microsoft Entra service authentication, see [Azure identity examples](https://github.com/Azure/azure-sdk-for-js/blob/@azure/identity_4.5.0/sdk/identity/identity/samples/AzureIdentityExamples.md). |
0 commit comments