Skip to content

Commit e139883

Browse files
committed
WIP Managed Identity Stuff
1 parent cf09cd3 commit e139883

File tree

7 files changed

+398
-135
lines changed

7 files changed

+398
-135
lines changed

articles/communication-services/quickstarts/includes/managed-identity-js.md

Lines changed: 0 additions & 56 deletions
This file was deleted.

articles/communication-services/quickstarts/includes/managed-identity-net.md

Lines changed: 0 additions & 71 deletions
This file was deleted.

articles/communication-services/quickstarts/includes/managed-identity-java.md renamed to articles/communication-services/quickstarts/includes/managed-identity/managed-identity-java.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
## Prerequisites
2-
3-
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
1+
## Additional Prerequisites for Java
2+
For Java, you'll also need:
43
- [Java Development Kit (JDK)](https://docs.microsoft.com/azure/developer/java/fundamentals/java-jdk-install) version 8 or above.
54
- [Apache Maven](https://maven.apache.org/download.cgi).
6-
- A deployed Communication Services resource and connection string. [Create a Communication Services resource](../create-communication-resource.md).
75

86
## Add managed identity to your Communication Services solution (Java)
97

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
## Setting Up
2+
3+
### Create a new Node.js Application
4+
5+
Open your terminal or command window create a new directory for your app, and navigate to it.
6+
7+
```console
8+
mkdir managed-identity-quickstart && cd managed-identity--quickstart
9+
```
10+
11+
Run `npm init -y` to create a **package.json** file with default settings.
12+
13+
```console
14+
npm init -y
15+
```
16+
17+
### Install the SDK packages
18+
19+
```console
20+
npm install @azure/communication-identity
21+
npm install @azure/communication-common
22+
npm install @azure/communication-sms
23+
npm install @azure/identity
24+
```
25+
26+
### Create a new file
27+
28+
Open a new file with a text editor and save it as `index.js`, we'll be placing our code inside this file.
29+
30+
### Use the SDK packages
31+
32+
Add the following `require` directives to the top of `index.js` to use the Azure Identity and Azure Storage SDKs.
33+
34+
```JavaScript
35+
const { DefaultAzureCredential } = require("@azure/identity");
36+
const { CommunicationIdentityClient, CommunicationUserToken } = require("@azure/communication-identity");
37+
const { SmsClient, SmsSendRequest } = require("@azure/communication-sms");
38+
```
39+
## Create a DefaultAzureCredential
40+
41+
We'll be using the [DefaultAzureCredential](/javascript/api/@azure/identity/defaultazurecredential) for this quickstart. This credential is suitable for production and development environments. As it is needed for each operation let's create it within the top of our `index.js` file.
42+
43+
```JavaScript
44+
const credential = new DefaultAzureCredential();
45+
```
46+
47+
## Create an identity and issue a token with Managed Identity
48+
49+
Next, we'll write a function which creates a new identity and issues a token for this identity, we'll use this later to test our managed identity setup.
50+
51+
```JavaScript
52+
async function createIdentityAndIssueToken(resourceEndpoint) {
53+
const client = new CommunicationIdentityClient(resourceEndpoint, credential);
54+
return await client.createUserAndToken(["chat"]);
55+
}
56+
```
57+
58+
## Send an SMS with Managed Identity
59+
60+
Now, lets write a function which uses managed identities to send an SMS:
61+
62+
```JavaScript
63+
async function sendSms(resourceEndpoint, fromNumber, toNumber, message) {
64+
const smsClient = new SmsClient(resourceEndpoint, credential);
65+
const sendRequest = {
66+
from: fromNumber,
67+
to: [toNumber],
68+
message: message
69+
};
70+
return await smsClient.send(
71+
sendRequest,
72+
{} //Optional SendOptions
73+
);
74+
}
75+
```
76+
77+
## Write the main method
78+
79+
With our functions created we can now write a main function to call them and demonstrate the use of Managed Identities:
80+
```JavaScript
81+
async function main() {
82+
// You can find your endpoint and access key from your resource in the Azure portal
83+
// e.g. "https://<RESOURCE_NAME>.communication.azure.com";
84+
endpoint = "https://<RESOURCE_NAME>.communication.azure.com/"
85+
86+
87+
console.log("Retrieving new Access Token, using Managed Identities");
88+
const result = await createIdentityAndIssueToken(endpoint);
89+
console.log(`Retrieved Access Token: ${result.token}`);
90+
91+
console.log("Sending SMS using Managed Identities");
92+
93+
// You will need a phone number from your resource to send an SMS.
94+
const smsResult = await sendSms(endpoint, "<FROM NUMBER>", "<TO NUMBER>", "Hello from Managed Identities");
95+
console.log(`SMS ID: ${smsResult[0].messageId}`);
96+
console.log(`Send Result Successful: ${smsResult[0].successful}`);
97+
}
98+
99+
main();
100+
```
101+
102+
The final `index.js` file should look like this:
103+
```JavaScript
104+
const { DefaultAzureCredential } = require("@azure/identity");
105+
const { CommunicationIdentityClient, CommunicationUserToken } = require("@azure/communication-identity");
106+
const { SmsClient, SmsSendRequest } = require("@azure/communication-sms");
107+
108+
const credential = new DefaultAzureCredential();
109+
110+
async function createIdentityAndIssueToken(resourceEndpoint) {
111+
const client = new CommunicationIdentityClient(resourceEndpoint, credential);
112+
return await client.createUserAndToken(["chat"]);
113+
}
114+
115+
async function sendSms(resourceEndpoint, fromNumber, toNumber, message) {
116+
const smsClient = new SmsClient(resourceEndpoint, credential);
117+
const sendRequest = {
118+
from: fromNumber,
119+
to: [toNumber],
120+
message: message
121+
};
122+
return await smsClient.send(
123+
sendRequest,
124+
{} //Optional SendOptions
125+
);
126+
}
127+
128+
async function main() {
129+
// You can find your endpoint and access key from your resource in the Azure portal
130+
// e.g. "https://<RESOURCE_NAME>.communication.azure.com";
131+
endpoint = "https://<RESOURCE_NAME>.communication.azure.com/"
132+
133+
134+
console.log("Retrieving new Access Token, using Managed Identities");
135+
const result = await createIdentityAndIssueToken(endpoint);
136+
console.log(`Retrieved Access Token: ${result.token}`);
137+
138+
console.log("Sending SMS using Managed Identities");
139+
140+
// You will need a phone number from your resource to send an SMS.
141+
const smsResult = await sendSms(endpoint, "<FROM NUMBER>", "<TO NUMBER>", "Hello from Managed Identities");
142+
console.log(`SMS ID: ${smsResult[0].messageId}`);
143+
console.log(`Send Result Successful: ${smsResult[0].successful}`);
144+
}
145+
146+
main();
147+
```
148+
149+
## Run the Program
150+
151+
With everything complete, you can run the file by entering `node index.js` from your project's directory. If everything went well you should see something similar to the following.
152+
153+
```Bash
154+
$ node index.js
155+
Retrieving new Access Token, using Managed Identities
156+
Retrieved Access Token: ey...Q
157+
Sending SMS using Managed Identities
158+
SMS ID: Outgoing_2021040602194...._noam
159+
Send Result Successful: true
160+
```

0 commit comments

Comments
 (0)