-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoken.js
More file actions
155 lines (137 loc) · 4.97 KB
/
token.js
File metadata and controls
155 lines (137 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* global Twilio Runtime */
'use strict';
const AccessToken = Twilio.jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
const ChatGrant = AccessToken.ChatGrant;
const MAX_ALLOWED_SESSION_DURATION = 14400;
module.exports.handler = async (context, event, callback) => {
const { ACCOUNT_SID, TWILIO_API_KEY_SID, TWILIO_API_KEY_SECRET, ROOM_TYPE, CONVERSATIONS_SERVICE_SID } = context;
const authHandler = require(Runtime.getAssets()['/auth-handler.js'].path);
authHandler(context, event, callback);
const { user_identity, room_name, create_room = true, create_conversation = false } = event;
let response = new Twilio.Response();
response.appendHeader('Content-Type', 'application/json');
if (typeof create_room !== 'boolean') {
response.setStatusCode(400);
response.setBody({
error: {
message: 'invalid parameter',
explanation: 'A boolean value must be provided for the create_room parameter',
},
});
return callback(null, response);
}
if (typeof create_conversation !== 'boolean') {
response.setStatusCode(400);
response.setBody({
error: {
message: 'invalid parameter',
explanation: 'A boolean value must be provided for the create_conversation parameter',
},
});
return callback(null, response);
}
if (!user_identity) {
response.setStatusCode(400);
response.setBody({
error: {
message: 'missing user_identity',
explanation: 'The user_identity parameter is missing.',
},
});
return callback(null, response);
}
if (!room_name && create_room) {
response.setStatusCode(400);
response.setBody({
error: {
message: 'missing room_name',
explanation: 'The room_name parameter is missing. room_name is required when create_room is true.',
},
});
return callback(null, response);
}
if (create_room) {
const client = context.getTwilioClient();
let room;
try {
// See if a room already exists
room = await client.video.v1.rooms(room_name).fetch();
} catch (e) {
try {
// If room doesn't exist, create it
room = await client.video.v1.rooms.create({ uniqueName: room_name, type: ROOM_TYPE });
} catch (e) {
console.error('Error creating room:');
console.error(e);
response.setStatusCode(500);
response.setBody({
error: {
message: 'error creating room',
explanation: 'Something went wrong when creating a room.',
},
});
return callback(null, response);
}
}
if (create_conversation) {
const conversationsClient = client.conversations.v1.services(CONVERSATIONS_SERVICE_SID);
try {
// See if conversation already exists
await conversationsClient.conversations(room.sid).fetch();
} catch (e) {
try {
// If conversation doesn't exist, create it.
// Here we add a timer to close the conversation after the maximum length of a room (24 hours).
// This helps to clean up old conversations since there is a limit that a single participant
// can not be added to more than 1,000 open conversations.
await conversationsClient.conversations.create({ uniqueName: room.sid, 'timers.closed': 'P1D' });
} catch (e) {
console.error('Error creating conversation:');
console.error(e);
response.setStatusCode(500);
response.setBody({
error: {
message: 'error creating conversation',
explanation: 'Something went wrong when creating a conversation.',
},
});
return callback(null, response);
}
}
try {
// Add participant to conversation
await conversationsClient.conversations(room.sid).participants.create({ identity: user_identity });
} catch (e) {
// Ignore "Participant already exists" error (50433)
if (e.code !== 50433) {
console.error('Error creating conversation participant:');
console.error(e);
response.setStatusCode(500);
response.setBody({
error: {
message: 'error creating conversation participant',
explanation: 'Something went wrong when creating a conversation participant.',
},
});
return callback(null, response);
}
}
}
}
// Create token
const token = new AccessToken(ACCOUNT_SID, TWILIO_API_KEY_SID, TWILIO_API_KEY_SECRET, {
ttl: MAX_ALLOWED_SESSION_DURATION,
identity: user_identity,
});
// Add video grant to token
const videoGrant = new VideoGrant({ room: room_name });
token.addGrant(videoGrant);
// Add chat grant to token
const chatGrant = new ChatGrant({ serviceSid: CONVERSATIONS_SERVICE_SID });
token.addGrant(chatGrant);
// Return token
response.setStatusCode(200);
response.setBody({ token: token.toJwt(), room_type: ROOM_TYPE });
return callback(null, response);
};