Skip to content

Commit 5743a22

Browse files
committed
New content for Node.js Entra include file
1 parent 7a48eed commit 5743a22

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

includes/iot-hub-howto-connect-service-iothub-entra-node.md

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,84 @@ ms.manager: lizross
1111
ms.date: 11/06/2024
1212
---
1313

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+
1416
For an overview of Node.js SDK authentication, see:
1517

1618
* [Getting started with user authentication on Azure](/azure/developer/javascript/how-to/with-authentication/getting-started)
1719
* [Azure Identity client library for JavaScript](/javascript/api/overview/azure/identity-readme)
1820

19-
##### Microsoft Entra token credential
20-
21-
Use [DefaultAzureCredential](/javascript/api/@azure/identity/defaultazurecredential) to generate a token. The token is supplied to `fromTokenCredential`.
21+
##### Configure Microsoft Entra app
2222

23-
To create required Microsoft Entra app parameters for `DefaultAzureCredential`, create a Microsoft Entra app registration that contains your selected authentication mechanism:
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:
2424

2525
* Client secret
2626
* Certificate
2727
* Federated identity credential
2828

29-
For more information, see [Quickstart: Register an application with the Microsoft identity platform](/entra/identity-platform/quickstart-register-app).
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).
3030

31-
Microsoft Entra apps may require permissions depending on operations 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 [Azure built-in roles](/azure/role-based-access-control/built-in-roles#internet-of-things).
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).
3232

33-
##### Connect to IoT Hub
33+
##### Authenticate using DefaultAzureCredential
3434

35-
Use [fromTokenCredential](/javascript/api/azure-iothub/registry?#azure-iothub-registry-fromtokencredential) to create a service connection to IoT Hub using a Microsoft Entra token credential.
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)
3638

37-
`fromTokenCredential` requires two parameters:
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 pakage:
3842

39-
* hostname - The Azure service URL
40-
* tokenCredential - The Azure credential token
43+
```shell
44+
npm install --save @azure/identity
45+
```
4146

42-
In this example, the Azure credential is obtained using `DefaultAzureCredential`. The Azure domain URL and credential are then supplied to `KeyClient`.
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.
4348

4449
```javascript
4550
import { DefaultAzureCredential } from "@azure/identity";
46-
import { KeyClient } from "@azure/keyvault-keys";
4751

48-
// Configure vault URL
49-
const vaultUrl = "https://<your-unique-keyvault-name>.vault.azure.net";
5052
// Azure SDK clients accept the credential as a parameter
5153
const credential = new DefaultAzureCredential();
52-
// Create authenticated client
53-
const client = new KeyClient(vaultUrl, credential);
54+
```
55+
56+
The resulting credential token can then be passed to a connection method to connect to IoT Hub method 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+
##### Connect to IoT Hub
63+
64+
Use [fromTokenCredential](/javascript/api/azure-iothub/registry?#azure-iothub-registry-fromtokencredential) to create a service connection to IoT Hub using a Microsoft Entra token credential.
65+
66+
`fromTokenCredential` requires two parameters:
67+
68+
* 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`.
69+
* The Azure credential token
70+
71+
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.
72+
73+
```javascript
74+
const { DefaultAzureCredential } = require("@azure/identity");
75+
76+
let Registry = require('azure-iothub').Registry;
77+
78+
// Define the client secret values
79+
clientSecretValue = 'xxxxxxxxxxxxxxx'
80+
clientID = 'xxxxxxxxxxxxxx'
81+
tenantID = 'xxxxxxxxxxxxx'
82+
83+
// Set environment variables
84+
process.env['AZURE_CLIENT_SECRET'] = clientSecretValue;
85+
process.env['AZURE_CLIENT_ID'] = clientID;
86+
process.env['AZURE_TENANT_ID'] = tenantID;
87+
88+
// Acquire a credential object
89+
const credential = new DefaultAzureCredential()
90+
91+
// Create an instance of the IoTHub registry
92+
hostName = 'MyAzureDomain.azure-devices.net';
93+
let registry = Registry.fromTokenCredential(hostName,credential);
5494
```

includes/iot-hub-howto-connect-service-iothub-entra-python.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ os.environ['AZURE_TENANT_ID'] = tenantID
8585
# Acquire a credential object
8686
credential = DefaultAzureCredential()
8787

88-
# Use Entra to auth IoT Hub service
88+
# Use Entra to authorize IoT Hub service
8989
print("Connecting to IoTHubRegistryManager...")
9090
iothub_registry_manager = IoTHubRegistryManager.from_token_credential(
91-
url="{Your Entra domain URL}.azure-devices.net",
91+
url="MyAzureDomain.azure-devices.net",
9292
token_credential=credential)
9393
```

0 commit comments

Comments
 (0)