Skip to content

Instantly share code, notes, and snippets.

@xlewkanx
Last active December 22, 2018 15:59
Show Gist options
  • Select an option

  • Save xlewkanx/52e6c7f8f36bcf49ef5504ad512ea473 to your computer and use it in GitHub Desktop.

Select an option

Save xlewkanx/52e6c7f8f36bcf49ef5504ad512ea473 to your computer and use it in GitHub Desktop.
e3kit twilio js
async function createChannel(twilioChat, uniqueName, friendlyName) {
return await twilioChat.createChannel({
uniqueName,
friendlyName
});
}
async function joinChannel(twilioChat, name) {
const paginator = await twilioChat.getPublicChannelDescriptors();
for (i = 0; i < paginator.items.length; i++) {
const channelDescriptor = paginator.items[i];
if (channelDescriptor.uniqueName === name) {
const channel = channelDescriptor.getChannel();
return channel.join();
}
}
throw new Error('channel not found');
}
async function getMessages(e3kit, channel) {
const messages = await channel.getMessages();
const totalMessages = messages.items.length;
for (i = 0; i < totalMessages; i++) {
const message = messages.items[i];
const authorPublicKey = await e3kit.lookupPublicKeys(message.author);
message.decryptedMessage = await e3kit.decrypt(message.body, authorPublicKey);
}
return messages.items;
}
// This function returns a token that will be used to authenticate requests
// to your backend.
// This is a simplified solution without any real protection, so here you need use your
// application authentication mechanism.
async function authenticate(identity) {
const response = await fetch('http://localhost:3000/authenticate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
identity: identity
})
});
if (!response.ok) {
throw new Error(`Error code: ${response.status} \nMessage: ${response.statusText}`);
}
return response.json().then(data => data.authToken);
}
// This function makes authenticated request to GET /virgil-jwt endpoint
// The token serves to make authenticated requests to Virgil Cloud
async function getVirgilToken(authToken) {
const response = await fetch('http://localhost:3000/virgil-jwt', {
headers: {
// We use bearer authorization, but you can use any other mechanism.
// The point is only, this endpoint should be protected.
Authorization: `Bearer ${authToken}`,
}
})
if (!response.ok) {
throw new Error(`Error code: ${response.status} \nMessage: ${response.statusText}`);
}
// If request was successful we return Promise which will resolve with token string.
return response.json().then(data => data.virgilToken);
}
// This function makes authenticated request to GET /twilio-jwt endpoint
// Returned token is used by twilio library
async function getTwilioToken(authToken) {
const response = await fetch('http://localhost:3000/twilio-jwt', {
headers: {
Authorization: `Bearer ${authToken}`,
}
})
if (!response.ok) {
throw new Error(`Error code: ${response.status} \nMessage: ${response.statusText}`);
}
return response.json().then(data => data.twilioToken);
}
async function initialize(identity) {
// E3kit will call this callback function and wait for the Promise resolve.
// When it receives Virgil JWT it can do authorized requests to Virgil Cloud.
// E3kit uses the identity encoded in the JWT as the current user's identity.
const authToken = await authenticate(identity);
const [e3kit, twilioChat] = await Promise.all([
E3kit.EThree.initialize(() => getVirgilToken(authToken)),
getTwilioToken(authToken).then(twilioToken => Twilio.Chat.Client.create(twilioToken))
]);
return { e3kit, twilioChat };
}
async function sendMessage(e3kit, channel, message) {
const membersIdentities = await channel.getMembers().then(members => members.map(member => member.identity));
const publicKeys = await e3kit.lookupPublicKeys(membersIdentities);
const encryptedMessage = await e3kit.encrypt(message, publicKeys);
return channel.sendMessage(encryptedMessage);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment