Skip to content

Commit 13e66fa

Browse files
committed
added js sample app
1 parent aca86ec commit 13e66fa

File tree

2 files changed

+149
-2
lines changed

2 files changed

+149
-2
lines changed

articles/communication-services/quickstarts/identity/entra-id-authentication-integration.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,11 @@ The developer's required actions are shown in following diagram:
175175
1. The Contoso developer initialize any implementation of `TokenCredential` from Azure Identity SDK which is capable of obtaining a Microsoft Entra user token for the application that was created earlier by the Administrator.
176176
1. The Contoso developer initializes `AzureCommunicationTokenCredential` from Communication Services Common SDK with `TokenCredential` created in the step 1. The `AzureCommunicationTokenCredential` credential exchanges the incoming Microsoft Entra user token for the access token of Teams user seamlessly in the background.
177177

178-
179178
> [!NOTE]
180179
> The following sections describe how to create `AzureCommunicationTokenCredential`.
181180
182181
::: zone pivot="programming-language-javascript"
183-
[!INCLUDE [JavaScript]()]
182+
[!INCLUDE [JavaScript](./includes/entra-id/support-entra-id-users-js.md)]
184183
::: zone-end
185184

186185

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
---
2+
title: include file
3+
description: include file
4+
services: azure-communication-services
5+
author: aigerim
6+
manager: soricos
7+
8+
ms.service: azure-communication-services
9+
ms.subservice: azure-communication-services
10+
ms.date: 05/06/2025
11+
ms.topic: include
12+
ms.custom: include file
13+
ms.author: aigerimb
14+
---
15+
16+
## Set up prerequisites
17+
18+
- [Node.js](https://nodejs.org/) Active LTS and Maintenance LTS versions (8.11.1 and 10.14.1 recommended).
19+
- [Azure Identify SDK for JavaScript](https://www.npmjs.com/package/@azure/identity) to authenticate with Microsoft Entra ID.
20+
- [Azure Communication Services Common SDK for JavaScript](https://www.npmjs.com/package/@azure/communication-common) to obtain Azure Communication Services access tokens for Microsoft Entra ID user.
21+
22+
## Final code
23+
Find the finalized code for this quickstart on [GitHub](https://github.com/Azure-Samples/communication-services-javascript-quickstarts/tree/main/entra-id-users-support-quickstart).
24+
25+
## Set up
26+
27+
### Create a new Node.js Application
28+
29+
Open your terminal or command window create a new directory for your app, and navigate to it.
30+
31+
```console
32+
mkdir entra-id-users-support-quickstart && cd entra-id-users-support-quickstart
33+
```
34+
35+
Run `npm init -y` to create a `package.json` file with default settings.
36+
37+
```console
38+
npm init -y
39+
```
40+
41+
### Install the package
42+
43+
Use the `npm install` command to install the Azure Identity and Azure Communication Services Common SDKs for JavaScript.
44+
45+
```console
46+
npm install @azure/[email protected] --save
47+
npm install --save @azure/identity
48+
npm install express --save
49+
npm install dotenv --save
50+
```
51+
52+
The `--save` option lists the library as a dependency in your **package.json** file.
53+
54+
## Set up the app framework
55+
56+
From the project directory:
57+
58+
1. Open a new text file in your code editor
59+
1. Create the structure for the program, including basic exception handling and importing following SDK classes:
60+
61+
```javascript
62+
const { AzureCommunicationTokenCredential } = require('@azure/communication-common');
63+
const {InteractiveBrowserCredential} = require('@azure/identity');
64+
const express = require("express");
65+
66+
// You will need to set environment variables in .env
67+
const SERVER_PORT = process.env.PORT || 80;
68+
69+
// Quickstart code goes here
70+
71+
app.listen(SERVER_PORT, () => console.log(`Communication access token application started on ${SERVER_PORT}!`))
72+
```
73+
74+
You can import any implementation of the [TokenCredential](/javascript/api/%40azure/core-auth/tokencredential) interface from the [Azure Identity SDK for JavaScript](https://www.npmjs.com/package/@azure/identity) to authenticate with Microsoft Entra ID. In this quickstart, we use the `InteractiveBrowserCredential` class, which is suitable for browser basic authentication scenarios. For a full list of the credentials offered, see [Credential Classes](/javascript/api/overview/azure/identity-readme?view=azure-node-latest#credential-classes).
75+
76+
1. Save the new file as `obtain-access-token-for-entra-id-user.js` in the `entra-id-users-support-quickstart` directory.
77+
78+
<a name='step-1-obtain-entra-user-token-via-the-identity-library'></a>
79+
80+
### Step 1: Initialize implementation of TokenCredential from Azure Identity SDK
81+
82+
The first step in obtaining Communication Services access token for Entra ID user is getting an Entra ID access token for your Entra ID user by using [Azure Identity](/javascript/api/overview/azure/identity-readme) SDK. The code below retrieves Microsoft Entra client ID and tenant ID from environment variables named `ENTRA_CLIENT_ID` and `ENTRA_TENANT_ID`.
83+
84+
```javascript
85+
// This code demonstrates how to fetch your Microsoft Entra client ID and tenant ID from environment variables.
86+
const clientId = process.env['ENTRA_CLIENT_ID'];
87+
const tenantId = process.env['ENTRA_TENANT_ID'];
88+
89+
// Initialize InteractiveBrowserCredential for use with AzureCommunicationTokenCredential.
90+
const entraTokenCredential = new InteractiveBrowserCredential({
91+
tenantId: tenantId,
92+
clientId: clientId,
93+
});
94+
```
95+
96+
### Step 2: Initialize AzureCommunicationTokenCredential
97+
98+
Instantiate a `AzureCommunicationTokenCredential` with the TokenCredential created above and your Communication Services resource endpoint URI. The code below retrieves the endpoint for the resource from an environment variable named `COMMUNICATION_SERVICES_RESOURCE_ENDPOINT`.
99+
100+
```javascript
101+
const app = express();
102+
103+
app.get('/', async (req, res) => {
104+
try {
105+
console.log("Azure Communication Services - Obtain Access Token for Entra ID User Quickstart");
106+
// This code demonstrates how to fetch your Azure Communication Services resource endpoint URI
107+
// from an environment variable.
108+
const resourceEndpoint = process.env['COMMUNICATION_SERVICES_RESOURCE_ENDPOINT'];
109+
110+
// Set up AzureCommunicationTokenCredential to request a Communication Services access token for a Microsoft Entra ID user.
111+
const entraTokenCredentialOptions = {
112+
resourceEndpoint: resourceEndpoint,
113+
tokenCredential: entraTokenCredential,
114+
scopes: ["https://communication.azure.com/clients/VoIP"],
115+
};
116+
const entraCommunicationTokenCredential = new AzureCommunicationTokenCredential(
117+
entraTokenCredentialOptions
118+
);
119+
120+
// Obtain an access token for the Entra ID user code goes here.
121+
122+
} catch (err) {
123+
console.error("Error obtaining token:", err);
124+
res.status(500).send("Failed to obtain token");
125+
}
126+
});
127+
```
128+
Providing scopes is optional. When not specified, the .default scope is automatically used, requesting all API permissions for Communication Services Clients.
129+
130+
<a name='step-3-obtain-acs-access-token-of-the-entra-id-user'></a>
131+
132+
### Step 3: Obtain Azure Communication Services access token for Microsoft Entra ID user
133+
134+
Use the `getToken` method to obtain an access token for the Entra ID user. The `AzureCommunicationTokenCredential` can be used with the Azure Communication Services SDKs.
135+
136+
```javascript
137+
// To obtain a Communication Services access token for Microsoft Entra ID call getToken() function.
138+
let accessToken = await entraCommunicationTokenCredential.getToken();
139+
console.log("Token:", accessToken);
140+
```
141+
142+
## Run the code
143+
144+
From a console prompt, navigate to the directory containing the *obtain-access-token-for-entra-id-user.js* file, then execute the following `node` command to run the app.
145+
146+
```console
147+
node ./obtain-access-token-for-entra-id-user.js
148+
```

0 commit comments

Comments
 (0)