アイデンティティライブラリはAzure Communication Servicesのユーザーおよびトークン管理に使用されます。
作業の開始
前提条件
- Azure サブスクリプション。
- 既存の Communication Services リソース。 リソースを作成する必要がある場合は、Azure portal、Azure PowerShell、または Azure CLIを使用できます。
[インストール中]
npm install @azure/communication-identity
ブラウザーのサポート
JavaScript バンドル
ブラウザーでこのクライアント ライブラリを使用するには、まず、バンドルを使用する必要があります。 これを行う方法の詳細については、
主な概念
Clients
CommunicationIdentityClientはユーザーとそのトークンを管理する方法を提供しています。
Examples
Authentication
Azure portalのCommunication Servicesリソースからキーや接続文字列を取得することができます。 鍵を手に入れたら、以下のいずれかの方法で CommunicationIdentityClient を認証できます。
クライアントを初期化する前にAzureKeyCredentialでKeyCredentialを作成してください
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(チャット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);
ユーザーとトークンを1つのリクエストで作成する方法
利便性のために、 createUserAndToken を使って新しいユーザーを作成し、1つの関数呼び出しでトークンを発行します。 これは、まずユーザーを作成してからトークンを発行するのではなく、単一のウェブリクエストに変換されます。
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"]);
ユーザーとカスタム期限を持つトークンを1つのリクエストで作成する
また、有効期限をカスタマイズすることで通信識別トークンを作成することも可能です。 トークンの有効期間は[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アクセストークンを通信アクセストークンに交換する
getTokenForTeamsUser方法を使って、TeamsユーザーのAzure ADアクセストークンを有効期限が一致する新しいCommunicationAccessTokenと交換します。
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
このライブラリに貢献したい場合は、コードのビルドやテスト方法について詳しく知るために、contributing guideをお読みください。
関連プロジェクト
Azure SDK for JavaScript