Important Notes
- OpenTok.js 2.27.0+ では、以前のバージョンの OpenTok.js を使用しているクライアントに接続する場合、エンドツーエンドの暗号化は機能しません。OpenTok.js 2.27.0+ を使用するようにアプリをアップグレードする際、アプリがエンドツーエンド暗号化を使用する場合は、すべてのクライアントが OpenTok.js 2.27.0+ を使用していることを確認してください。
- WebKitベースのブラウザ(macOS/iOSのSafariやiOSのChromeなど)でのエンドツーエンドの暗号化サポートは、バージョンOpenTok.js 2.30.0以降でのみ利用可能です。
Note about Content Security Policies (CSP)
もし script-src ディレクティブが設定されていることを確認してください。 'wasm-unsafe-eval' が指定されている。そうでない場合、エンドツーエンドの暗号化に必要なWebAssemblyは、ページ上での読み込みと実行がブロックされます。
Initializing a session with a secret
エンドツーエンドの暗号化セッションは、サーバーAPIを使用して作成されます。 REST API).ウェブクライアントがエンドツーエンドで暗号化されたセッションに参加するには OT.initSession() メソッドを使用する:
const session = OT.initSession('api-key', 'session-id', {
encryptionSecret: 'initialEncryptionSecret',
});
有効な秘密は8文字以上256文字以下の文字列である。秘密は後で Session.setEncryptionSecret() メソッド ( シークレット以下同様)。
Checking whether the browser supports end-to-end encryption
を使用する。 OT.hasEndToEndEncryptionSupport() メソッドを使用して、クライアントのブラウザがエンドツーエンドの暗号化をサポートしているかどうかをチェックします:
if (OT.hasEndToEndEncryptionSupport()) {
// Proceed with connecting to the session
}
else {
// Notify the user that they cannot join the session
}
エンドツーエンドの暗号化は現在Firefoxではサポートされていません。
Changing the secret
シークレットは Session.setEncryptionSecret() メソッドを呼び出す:
await session.setEncryptionSecret('newEncryptionSecret');
セッションでエンドツーエンド暗号化が有効になっていない場合、シークレットを設定すると、 OT_ENCRYPTION_NOT_ENABLED エラー。参照: エンドツーエンド暗号化なしのセッションでシークレットを設定する.
Events and errors
イベントとエラーは、ユーザー主導の暗号化の動作を管理するために不可欠である。エンドツーエンドの暗号化は、共有秘密モデルを使用します。セッション内の全員が、自分のメディアを暗号化し、他の全員のメディアを復号化するために、同じ秘密を使用することが期待されています。
サブスクライバオブジェクトは encryptionSecretMismatch イベントは、加入者がストリームのメディアをデコードできないときに発生する。接続障害やオーディオ/ビデオのバグが原因ではなく、暗号化の不一致が原因でメディ アが受信できないことをユーザーに伝えることが重要である:
subscriber.on('encryptionSecretMismatch', () => {
// Activate a UI element communicating that there's been an encryption secret mismatch.
});
また、暗号化が正常に有効になったことをユーザーに伝えることも重要である。サブスクライバ・オブジェクトは、以前の不一致の後にサブスクライバがストリームのメ ディアをデコードできるようになると、encryptionSecretMatch イベントをディスパッチします。
また、暗号化が正常に有効になったことをユーザーに伝えることも重要である。サブスクライバ・オブジェクトは、以前の不一致の後にサブスクライバがストリームのメ ディアをデコードできるようになると、encryptionSecretMatch イベントをディスパッチします。
subscriber.on('encryptionSecretMatch', () => {
// Activate a UI element communicating that the media is being properly decrypted.
});
について Session.connect() クライアントが無効な暗号化シークレットで初期化されたエンドツーエンド暗号化セッションに接続しようとすると、コールバックはエラーとともに呼び出される。有効なシークレットは8文字以上256文字以下の文字列である。最良のユーザーエクスペリエンスを得るために、アプリケーションは無効なシークレットを検出してから OT.initSession() メソッドを使用します。以下の例では、セッションが空の(つまり無効な)secretで初期化され、接続時にエラーが発生します:
const session = OT.initSession(
'api-key',
'e2ee-session-id',
{
encryptionSecret: '',
}
)
session.connect('token', (error) => {
if (error && error.name === 'OT_INVALID_ENCRYPTION_SECRET') {
/*
The application should communicate that the secret was invalid.
*/
}
});
について Session.connect() コールバックは、エンドツーエンド暗号化をサポートしていないブラウザでエンドツーエンド暗号化セッションに接続しようとすると、エラーとともに呼び出されます。
const session = OT.initSession(
'api-key',
'e2ee-session-id',
{
encryptionSecret: 'validEncryptionSecret',
}
)
session.connect('token', (error) => {
if (error && error.name === 'OT_UNSUPPORTED_BROWSER') {
/*
The error will have the message 'Tried to connect an e2ee session but the browser does not support e2ee'
The application should communicate that the browser does not support encryption.
*/
}
});
ユーザーが暗号化シークレットを指定せずにエンドツーエンドで暗号化されたセッションで公開しようとすると Session.publish() コールバックはエラーで呼び出される。最高のユーザーエクスペリエンスを得るためには、アプリケーションは session.publish() メソッドを使用する:
session.publish(publisher, (error) => {
if (error && error.name === 'OT_STREAM_CREATE_FAILED') {
/*
The error will have the message 'Tried to publish to an e2ee session but encryption secret was not set'
The application should communicate that the secret was not set.
*/
}
});
もしユーザーが暗号化シークレットを指定せずにエンドツーエンドで暗号化されたセッションに参加しようとした場合、次のようになる。 Session.subscribe() コールバックはエラーで呼び出される。最高のユーザーエクスペリエンスを得るためには、アプリケーションは Session.subscribe() メソッドを使用する:
subscriber = session.subscribe(stream, target, opts, (error) => {
if (error && error.name === 'OT_UNABLE_TO_SUBSCRIBE') {
/*
The error will have the message 'Tried to subscribe to an e2ee stream but encryption secret was not set'
The application should communicate that the secret was not set.
*/
}
});
Setting a secret in a session without end-to-end encryption
注: この機能は、SDK バージョン 2.36.0 以降で利用可能です。
エンドツーエンド暗号化が有効な状態でセッションが作成されなかった場合、そのセッションに暗号化シークレットを設定すると、 OT_ENCRYPTION_NOT_ENABLED エラー。エラーが発生する場所は、接続、公開、および購読に対して、シークレットがいつ設定されたかによって異なります。
注: このセクションのシナリオでは、クライアントのブラウザがエンドツーエンド暗号化に対応していることを前提としています。対応していないブラウザでエンドツーエンド暗号化を使用すると、 OT_UNSUPPORTED_BROWSER 代わりにエラーが発生します。詳しくは ブラウザがエンドツーエンド暗号化に対応しているかを確認する そして イベントとエラー.
Setting the secret before connecting
接続する前に秘密鍵が設定されている場合( encryptionSecret ~の選択肢 OT.initSession() またはお電話にて Session.setEncryptionSecret() — セッションの接続に成功しました。エラーは Session.publish() そして Session.subscribe() コールバック:
// Option 1: set the secret when initializing the session
const session = OT.initSession('api-key', 'non-e2ee-session-id', {
encryptionSecret: 'encryptionSecret',
});
// Option 2: set the secret before connecting
// await session.setEncryptionSecret('encryptionSecret');
session.connect('token', (error) => {
if (error) {
return;
}
session.publish(publisher, (error) => {
if (error && error.name === 'OT_ENCRYPTION_NOT_ENABLED') {
/*
The application should communicate that end-to-end encryption
is not enabled for this session.
*/
}
});
session.subscribe(stream, target, opts, (error) => {
if (error && error.name === 'OT_ENCRYPTION_NOT_ENABLED') {
// Same handling as for the publisher.
}
});
});
Setting the secret after connecting
もし Session.setEncryptionSecret() セッションの接続完了後にこのメソッドが呼び出されると、このメソッドはエラーを返して呼び出しを拒否します。その後の Session.publish() または Session.subscribe() また、そのコールバック内でエラーを返します:
session.connect('token', async (error) => {
if (error) {
return;
}
try {
await session.setEncryptionSecret('encryptionSecret');
} catch (error) {
if (error.name === 'OT_ENCRYPTION_NOT_ENABLED') {
/*
The application should communicate that end-to-end encryption
is not enabled for this session.
*/
}
}
});
Setting the secret while publishing or subscribing
もし Session.setEncryptionSecret() クライアントがすでにパブリッシュまたはサブスクライブを行っている状態でこのメソッドが呼び出されると、メソッドはエラーを返して処理を拒否し、アクティブなすべての Publisher および Subscriber オブジェクトが encryptionNotEnabled イベント。その後、SDKはそれらのパブリッシャーとサブスクライバーを自動的に停止するため、 Session.unpublish() または Session.unsubscribe():
publisher.on('encryptionNotEnabled', (event) => {
// The publisher is stopped by the SDK. No need to call session.unpublish().
});
subscriber.on('encryptionNotEnabled', (event) => {
// The subscriber is stopped by the SDK. No need to call session.unsubscribe().
});
try {
await session.setEncryptionSecret('encryptionSecret');
} catch (error) {
if (error.name === 'OT_ENCRYPTION_NOT_ENABLED') {
// An encryptionNotEnabled event is also dispatched on every
// active publisher and subscriber (handled above).
}
}
Recovering from the error
これらのエラーが発生しても、セッションは有効なままです。再度パブリッシュまたはサブスクライブするには、以下を呼び出してシークレットをクリアしてください。 Session.setEncryptionSecret() と null. その後、その同じ Publisher オブジェクトを Session.publish() 再び
await session.setEncryptionSecret(null);
session.publish(publisher, (error) => {
// Publishing proceeds without end-to-end encryption.
});