-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtoken.js
More file actions
30 lines (26 loc) · 1.11 KB
/
token.js
File metadata and controls
30 lines (26 loc) · 1.11 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
exports.handler = function (context, event, callback) {
// verifyExpiry.handler calls callback synchronously when the app is expired.
// We intercept the callback to detect this and short-circuit before proceeding.
// NOTE: This pattern breaks if verifyExpiry.handler ever becomes asynchronous.
const verifyExpiry = require(Runtime.getAssets()['/verify_expiry.js'].path);
let expiryHandled = false;
verifyExpiry.handler(context, event, function (err, result) {
expiryHandled = true;
return callback(err, result);
});
if (expiryHandled) {
return;
}
const { verifyRecaptcha } = require(Runtime.getAssets()['/verify_recaptcha.js'].path);
verifyRecaptcha(context, event, callback, function () {
const AccessToken = Twilio.jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
const videoGrant = new VideoGrant();
const token = new AccessToken(context.ACCOUNT_SID, context.API_KEY, context.API_SECRET, {
ttl: 60,
identity: context.VIDEO_IDENTITY,
});
token.addGrant(videoGrant);
callback(null, { token: token.toJwt() });
}, ['token_check', 'preflight']);
};