azure-sdk-for-js/sdk/communication/communication-identity
Matthew Podwysocki 6b0ec887ea
[communication] Migrate @azure/communication-identity to ESM/vitest (#31769)
### Packages impacted by this PR

- @azure/communication-identity

### Issues associated with this PR

- https://github.com/Azure/azure-sdk-for-js/issues/31338

### Describe the problem that is addressed by this PR

Migrates the @azure/communication-identity package to ESM/vitest

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
2024-11-14 15:08:29 -05:00
..
.vscode [Communication] communication-identity test separation for min/max testing (#14902) 2021-04-16 17:56:30 -07:00
review [eslint-plugin] add rule "@typescript-eslint/consistent-type-imports": "warn" 2024-10-30 15:48:52 +00:00
samples/v1 [EngSys] move to vendored version of cross-env via dev-tool 2024-11-02 00:48:06 +00:00
samples-dev [communication] Migrate @azure/communication-identity to ESM/vitest (#31769) 2024-11-14 15:08:29 -05:00
src [communication] Migrate @azure/communication-identity to ESM/vitest (#31769) 2024-11-14 15:08:29 -05:00
swagger Post release automated changes for communication releases (#29061) 2024-03-26 14:42:10 +02:00
test [communication] Migrate @azure/communication-identity to ESM/vitest (#31769) 2024-11-14 15:08:29 -05:00
.gitignore Remove @microsoft/api-documenter depencency (#14462) 2021-03-24 21:52:42 -07:00
CHANGELOG.md Post release automated changes for communication releases (#29061) 2024-03-26 14:42:10 +02:00
LICENSE [communication] Extract identity client into its own communication-identity package (#13415) 2021-01-27 00:29:47 +00:00
README.md [eslint] fix linting issues in communication README.md files (#30817) 2024-08-21 10:34:57 -07:00
api-extractor.json [communication] Migrate @azure/communication-identity to ESM/vitest (#31769) 2024-11-14 15:08:29 -05:00
assets.json push communication-identity recordings (#28698) 2024-02-29 14:46:21 +08:00
package.json [communication] Migrate @azure/communication-identity to ESM/vitest (#31769) 2024-11-14 15:08:29 -05:00
sample.env [JS] Adding Teams.ManageChats to the scope list (#22781) 2022-08-04 16:02:46 +02:00
test.env [JS] Adding Teams.ManageChats to the scope list (#22781) 2022-08-04 16:02:46 +02:00
tests.yml Switch to TME test subscription (#31329) 2024-10-14 18:19:12 -07:00
tsconfig.browser.config.json [communication] Migrate @azure/communication-identity to ESM/vitest (#31769) 2024-11-14 15:08:29 -05:00
tsconfig.json [communication] Migrate @azure/communication-identity to ESM/vitest (#31769) 2024-11-14 15:08:29 -05:00
vitest.browser.config.ts [communication] Migrate @azure/communication-identity to ESM/vitest (#31769) 2024-11-14 15:08:29 -05:00
vitest.config.ts [communication] Migrate @azure/communication-identity to ESM/vitest (#31769) 2024-11-14 15:08:29 -05:00

README.md

Azure Communication Identity client library for JavaScript

The identity library is used for managing users and tokens for Azure Communication Services.

Getting started

Prerequisites

Installing

npm install @azure/communication-identity

Browser support

JavaScript Bundle

To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our bundling documentation.

Key concepts

Clients

The CommunicationIdentityClient provides methods to manage users and their tokens.

Examples

Authentication

You can get a key and/or connection string from your Communication Services resource in Azure Portal. Once you have a key, you can authenticate the CommunicationIdentityClient with any of the following methods:

Create KeyCredential with AzureKeyCredential before initializing the client

import { AzureKeyCredential } from "@azure/core-auth";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const credential = new AzureKeyCredential(KEY);
const client = new CommunicationIdentityClient(ENDPOINT, credential);

Using a connection string

import { CommunicationIdentityClient } from "@azure/communication-identity";

const connectionString = `endpoint=ENDPOINT;accessKey=KEY`;
const client = new CommunicationIdentityClient(connectionString);

Using a TokenCredential

import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";

const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(ENDPOINT, credential);

If you use a key to initialize the client you will also need to provide the appropriate endpoint. You can get this endpoint from your Communication Services resource in Azure Portal.

Usage

Creating an instance of CommunicationIdentityClient

import { CommunicationIdentityClient } from "@azure/communication-identity";

const client = new CommunicationIdentityClient(CONNECTION_STRING);

Creating a new user

Use the createUser method to create a new user.

const user = await client.createUser();

Creating and refreshing a user token

Use the getToken method to issue or refresh a token for an existing user. The method also takes in a list of communication token scopes. Scope options include:

  • chat (Use this for full access to Chat APIs)
  • voip (Use this for full access to Calling APIs)
  • chat.join (Access to Chat APIs but without the authorization to create, delete or update chat threads)
  • chat.join.limited (A more limited version of chat.join that doesn't allow to add or remove participants)
  • voip.join (Access to Calling APIs but without the authorization to start new calls)
const { token } = await client.getToken(user, ["chat"]);

To refresh the user token, issue another token with the same user.

const { token } = await client.getToken(user, ["chat"]);

Creating a user token with custom expiration

It's also possible to create a Communication Identity access token by customizing the expiration time. Validity period of the token must be within [60,1440] minutes range. If not provided, the default value of 1440 minutes (24 hours) will be used.

const tokenOptions: GetTokenOptions = { tokenExpiresInMinutes: 60 };
const { token } = await client.getToken(user, ["chat"], tokenOptions);

Creating a user and a token in a single request

For convenience, use createUserAndToken to create a new user and issue a token with one function call. This translates into a single web request as opposed to creating a user first and then issuing a token.

const { user, token } = await client.createUserAndToken(["chat"]);

Creating a user and a token with custom expiration in a single request

It's also possible to create a Communication Identity access token by customizing the expiration time. Validity period of the token must be within [60,1440] minutes range. If not provided, the default value of 1440 minutes (24 hours) will be used.

const userAndTokenOptions: CreateUserAndTokenOptions = { tokenExpiresInMinutes: 60 };
const { user, token } = await client.createUserAndToken(["chat"], userAndTokenOptions);

Revoking tokens for a user

Use the revokeTokens method to revoke all issued tokens for a user.

await client.revokeTokens(user);

Deleting a user

Use the deleteUser method to delete a user.

await client.deleteUser(user);

Exchanging Azure AD access token of a Teams User for a Communication access token

Use getTokenForTeamsUser method to exchange an Azure AD access token of a Teams user for a new CommunicationAccessToken with a matching expiration time.

await client.getTokenForTeamsUser({
  teamsUserAadToken: "<aad-access-token-of-a-teams-user>",
  clientId: "<cliend-id-of-an-aad-application>",
  userObjectId: "<aad-object-id-of-a-teams-user>",
});

Troubleshooting

Next steps

Please take a look at the samples directory for detailed examples on how to use this library.

Contributing

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.

Impressions