신원 라이브러리는 Azure Communication Services의 사용자 및 토큰 관리를 위해 사용됩니다.
시작하기
Prerequisites
- Azure 구독.
- 기존 Communication Services 리소스입니다. 리소스를 만들어야 하는 경우 Azure Portal, Azure PowerShell또는 azure CLI 사용할 수 있습니다.
설치
npm install @azure/communication-identity
브라우저 지원
JavaScript 번들
브라우저에서 이 클라이언트 라이브러리를 사용하려면 먼저 번들러를 사용해야 합니다. 이 작업을 수행하는 방법에 대한 자세한 내용은 번들링 설명서참조하세요.
주요 개념
Clients
사용자 CommunicationIdentityClient 와 토큰을 관리하는 방법을 제공합니다.
예시
Authentication
Azure Portal의 Communication Services 리소스에서 키나 연결 문자열을 받을 수 있습니다. 키를 얻으면 다음 방법 중 하나로 인증 CommunicationIdentityClient 할 수 있습니다:
클라이언트를 초기화하기 전에 를 생성 KeyCredential 하세요 AzureKeyCredential
import { AzureKeyCredential } from "@azure/core-auth";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const key = "<some-key>";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new AzureKeyCredential(key);
const client = new CommunicationIdentityClient(endpoint, credential);
연결 문자열 사용
import { CommunicationIdentityClient } from "@azure/communication-identity";
// Example connection string
const connectionString =
"endpoint=https://contoso.eastus.communications.azure.net/;accesskey=secret";
const client = new CommunicationIdentityClient(connectionString);
TokenCredential을(를) 사용하여
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
키를 사용하여 클라이언트를 초기화하는 경우 적절한 엔드포인트도 제공해야 합니다. Azure Portal Communication Services 리소스에서 이 엔드포인트를 가져올 수 있습니다.
Usage
새 사용자 생성
이 메서드를 createUser 사용해 새 사용자를 생성하세요.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const user = await client.createUser();
사용자 토큰 생성 및 갱신
기존 사용자를 위해 토큰을 발행하거나 갱신하는 방법을 사용 getToken 하세요. 이 방법은 통신 토큰 범위 목록도 포함합니다. 범위 옵션은 다음과 같습니다.
-
chat(Chat API에 완전한 접근 권한을 위해 이 기능을 사용하세요) -
voip(Calling API에 완전한 접근 권한을 위해 이 기능을 사용하세요) -
chat.join(채팅 API 접근 권한 없이, 채팅 스레드를 생성, 삭제 또는 업데이트할 권한이 없음) -
chat.join.limited(참여자 추가나 삭제가 불가능한 chat.join의 더 제한된 버전) -
voip.join(호출 API 접근이지만 새 호출 시작 권한은 없음)
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const user = await client.createUser();
const { token } = await client.getToken(user, ["chat"]);
사용자 토큰을 새로고침하려면 같은 사용자로 또 다른 토큰을 발행하세요.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const user = await client.createUser();
let { token } = await client.getToken(user, ["chat"]);
// Refresh the token again
({ token } = await client.getToken(user, ["chat"]));
사용자 토큰 생성 시 사용자 만료 시 사용자 사용 시
만료 시간을 맞춤 설정하여 통신 신원 액세스 토큰을 만드는 것도 가능합니다. 토큰의 유효 기간은 [60,1440] 분 범위 내에 있어야 합니다. 제공되지 않은 경우, 기본 값인 1440분(24시간)이 사용됩니다.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const user = await client.createUser();
const tokenOptions = { tokenExpiresInMinutes: 60 };
const { token } = await client.getToken(user, ["chat"], tokenOptions);
단일 요청에서 사용자와 토큰을 생성하기
편의를 위해, 새 사용자를 생성하고 하나의 함수 호출로 토큰을 발행하는 데 사용 createUserAndToken 하세요. 이는 사용자를 먼저 만들고 토큰을 발행하는 대신 단일 웹 요청으로 전환됩니다.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const { user, token } = await client.createUserAndToken(["chat"]);
사용자 및 토큰 생성 중 하나에 사용자 만기가 커스텀 만료됩니다
만료 시간을 맞춤 설정하여 통신 신원 액세스 토큰을 만드는 것도 가능합니다. 토큰의 유효 기간은 [60,1440] 분 범위 내에 있어야 합니다. 제공되지 않은 경우, 기본 값인 1440분(24시간)이 사용됩니다.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const userAndTokenOptions = { tokenExpiresInMinutes: 60 };
const { user, token } = await client.createUserAndToken(["chat"], userAndTokenOptions);
사용자를 위한 토큰 취소
이 메서드를 revokeTokens 사용해 사용자에게 발행된 모든 토큰을 취소하세요.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
// Create user
const user = await client.createUser();
// Later when you want to revoke the user's tokens
await client.revokeTokens(user);
사용자 삭제
사용자를 삭제하는 deleteUser 방법을 사용하세요.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
// Create user
const user = await client.createUser();
// Later when you want to delete the user
await client.deleteUser(user);
Teams 사용자의 Azure AD 접근 토큰을 커뮤니케이션 액세스 토큰으로 교환하기
Teams 사용자의 Azure AD 액세스 토큰을 만료 기한이 맞는 새 CommunicationAccessToken 토큰으로 교환하는 방법을 사용 getTokenForTeamsUser 하세요.
import { DefaultAzureCredential } from "@azure/identity";
import { CommunicationIdentityClient } from "@azure/communication-identity";
const endpoint = "https://contoso.eastus.communications.azure.net";
const credential = new DefaultAzureCredential();
const client = new CommunicationIdentityClient(endpoint, credential);
const { token, expiresOn } = 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
Logging
로깅을 사용하도록 설정하면 오류에 대한 유용한 정보를 파악하는 데 도움이 될 수 있습니다. HTTP 요청 및 응답 로그를 보려면 AZURE_LOG_LEVEL 환경 변수를 info설정합니다. 또는 setLogLevel@azure/logger 호출하여 런타임에 로깅을 사용하도록 설정할 수 있습니다.
import { setLogLevel } from "@azure/logger";
setLogLevel("info");
다음 단계
이 라이브러리를 사용하는 방법에 대한 자세한 예제는 샘플 디렉터리를 살펴보세요.
Contributing
이 라이브러리에 기여하고 싶으시다면, 코드 빌드 및 테스트 방법에 대해 더 알고 싶다면 기여 가이드를 읽어보시기 바랍니다.
관련 프로젝트
- Javascript용 Microsoft Azure SDK
Azure SDK for JavaScript