다음을 통해 공유


JavaScript용 Azure Communication Identity 클라이언트 라이브러리 - 버전 1.3.1

ID 라이브러리는 Azure Communication Services 대한 사용자 및 토큰을 관리하는 데 사용됩니다.

시작

필수 구성 요소

설치

npm install @azure/communication-identity

브라우저 지원

JavaScript 번들

브라우저에서 이 클라이언트 라이브러리를 사용하려면 먼저 번들러를 사용해야 합니다. 이 작업을 수행하는 방법에 대한 자세한 내용은 번들링 설명서를 참조하세요.

주요 개념

클라이언트

CommunicationIdentityClient 사용자 및 해당 토큰을 관리하는 메서드를 제공합니다.

예제

인증

Azure Portal의 Communication Services 리소스에서 키 및/또는 연결 문자열 가져올 수 있습니다. 키가 있으면 다음 방법 중 원하는 방법으로 를 인증 CommunicationIdentityClient 할 수 있습니다.

클라이언트를 초기화하기 전에 를 AzureKeyCredential 사용하여 만들기 KeyCredential

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

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

연결 문자열 사용

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

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

TokenCredential 사용

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

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

키를 사용하여 클라이언트를 초기화하는 경우 적절한 엔드포인트도 제공해야 합니다. Azure Portal의 Communication Services 리소스에서 이 엔드포인트를 가져올 수 있습니다.

사용량

CommunicationIdentityClient의 instance 만들기

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

const client = new CommunicationIdentityClient(CONNECTION_STRING);

새 사용자 만들기

메서드를 createUser 사용하여 새 사용자를 만듭니다.

const user = await client.createUser();

사용자 토큰 만들기 및 새로 고침

메서드를 getToken 사용하여 기존 사용자에 대한 토큰을 발급하거나 새로 고칩니다. 또한 메서드는 통신 토큰 범위 목록을 사용합니다. 범위 옵션은 다음과 같습니다.

  • chat (채팅 API에 대한 모든 액세스에 사용)
  • voip (호출 API에 대한 전체 액세스에 사용)
  • chat.join (채팅 API에 액세스하지만 채팅 스레드를 만들거나 삭제하거나 업데이트할 수 있는 권한 없음)
  • chat.join.limited (참가자를 추가하거나 제거할 수 없는 더 제한된 버전의 chat.join)
  • voip.join (호출 API에 액세스하지만 새 호출을 시작할 수 있는 권한 없음)
let { token } = await client.getToken(user, ["chat"]);

사용자 토큰을 새로 고치려면 동일한 사용자와 다른 토큰을 발급합니다.

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

사용자 지정 만료를 사용하여 사용자 토큰 만들기

만료 시간을 사용자 지정하여 통신 ID 액세스 토큰을 만들 수도 있습니다. 토큰의 유효 기간은 [60,1440] 분 범위 내에 있어야 합니다. 제공되지 않으면 기본값인 1440분(24시간)이 사용됩니다.

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

단일 요청에서 사용자 및 토큰 만들기

편의를 위해 를 사용하여 createUserAndToken 새 사용자를 만들고 하나의 함수 호출로 토큰을 발급합니다. 이는 사용자를 먼저 만든 다음 토큰을 발급하는 것이 아니라 단일 웹 요청으로 변환됩니다.

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

단일 요청에서 사용자 지정 만료가 있는 사용자 및 토큰 만들기

만료 시간을 사용자 지정하여 통신 ID 액세스 토큰을 만들 수도 있습니다. 토큰의 유효 기간은 [60,1440] 분 범위 내에 있어야 합니다. 제공되지 않으면 기본값인 1440분(24시간)이 사용됩니다.

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

사용자에 대한 토큰 해지

메서드를 revokeTokens 사용하여 사용자에 대해 발급된 모든 토큰을 해지합니다.

await client.revokeTokens(user);

사용자를 삭제하는 중

메서드를 deleteUser 사용하여 사용자를 삭제합니다.

await client.deleteUser(user);

통신 액세스 토큰에 대한 Teams 사용자의 Azure AD 액세스 토큰 교환

메서드를 사용하여 getTokenForTeamsUser 만료 시간이 일치하는 새 CommunicationAccessToken Teams 사용자의 Azure AD 액세스 토큰을 교환합니다.

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>",
});

문제 해결

다음 단계

이 라이브러리를 사용하는 방법에 대한 자세한 예제는 샘플 디렉터리를 참조하세요.

참여

이 라이브러리에 기여하려면 기여 가이드 를 참조하여 코드를 빌드하고 테스트하는 방법에 대해 자세히 알아보세요.

Impressions