Skip to content

Commit eb36989

Browse files
authored
Merge pull request #35 from johnfischelli/refactor-token-handling
Refactor Token handling in Twilio Functions
2 parents c2b55bf + 9b8480b commit eb36989

15 files changed

+65
-335
lines changed
Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
const nodeFetch = require('node-fetch');
2+
const TokenValidator = require('twilio-flex-token-validator').functionValidator;
23

3-
async function getAuthentication(token, context) {
4-
5-
console.log('Validating request token');
6-
7-
const tokenValidationApi = `https://${context.ACCOUNT_SID}:${context.AUTH_TOKEN}@iam.twilio.com/v1/Accounts/${context.ACCOUNT_SID}/Tokens/validate`;
8-
9-
const fetchResponse = await nodeFetch(tokenValidationApi, {
10-
method: 'POST',
11-
headers: {
12-
'Content-Type': 'application/json',
13-
},
14-
body: JSON.stringify({
15-
token
16-
})
17-
});
18-
19-
const tokenResponse = await fetchResponse.json();
20-
return tokenResponse;
21-
}
22-
23-
exports.handler = async function (context, event, callback) {
4+
exports.handler = TokenValidator(async function (context, event, callback) {
245
const response = new Twilio.Response();
256
response.appendHeader('Access-Control-Allow-Origin', '*');
267
response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS POST');
@@ -29,7 +10,7 @@ exports.handler = async function (context, event, callback) {
2910

3011
console.log('add-conference-participant parameters:');
3112
Object.keys(event).forEach(key => {
32-
if (key !== "token") {
13+
if (key !== "token" || key !== "Token") {
3314
console.log(`${key}: ${event[key]}`);
3415
}
3516
});
@@ -40,23 +21,11 @@ exports.handler = async function (context, event, callback) {
4021
}
4122

4223
const {
43-
token,
4424
taskSid,
4525
to,
4626
from
4727
} = event;
4828

49-
const tokenResponse = await getAuthentication(token, context);
50-
if (!tokenResponse.valid) {
51-
response.setStatusCode(401);
52-
response.setBody({
53-
status: 401,
54-
message: 'Your authentication token failed validation',
55-
detail: tokenResponse.message
56-
});
57-
return callback(null, response);
58-
}
59-
6029
console.log(`Adding ${to} to named conference ${taskSid}`);
6130
const client = context.getTwilioClient();
6231
const participantsResponse = await client
@@ -79,4 +48,4 @@ exports.handler = async function (context, event, callback) {
7948
});
8049

8150
return callback(null, response);
82-
};
51+
});
Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,7 @@
11
const nodeFetch = require('node-fetch');
2+
const TokenValidator = require('twilio-flex-token-validator').functionValidator;
23

3-
async function getAuthentication(token, context) {
4-
5-
console.log('Validating request token');
6-
7-
const tokenValidationApi = `https://${context.ACCOUNT_SID}:${context.AUTH_TOKEN}@iam.twilio.com/v1/Accounts/${context.ACCOUNT_SID}/Tokens/validate`;
8-
9-
const fetchResponse = await nodeFetch(tokenValidationApi, {
10-
method: 'POST',
11-
headers: {
12-
'Content-Type': 'application/json',
13-
},
14-
body: JSON.stringify({
15-
token
16-
})
17-
});
18-
19-
const tokenResponse = await fetchResponse.json();
20-
return tokenResponse;
21-
}
22-
23-
24-
exports.handler = async function (context, event, callback) {
4+
exports.handler = TokenValidator(async function (context, event, callback) {
255
const response = new Twilio.Response();
266
response.appendHeader('Access-Control-Allow-Origin', '*');
277
response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS POST');
@@ -30,7 +10,7 @@ exports.handler = async function (context, event, callback) {
3010

3111
console.log('get-call-properties parameters:');
3212
Object.keys(event).forEach(key => {
33-
if (key !== "token") {
13+
if (key !== "token" || key !== "Token") {
3414
console.log(`${key}: ${event[key]}`);
3515
}
3616
});
@@ -41,23 +21,9 @@ exports.handler = async function (context, event, callback) {
4121
}
4222

4323
const {
44-
token,
4524
callSid,
4625
} = event;
4726

48-
console.log('Validating request token');
49-
const tokenResponse = await getAuthentication(event.token, context);
50-
51-
if (!tokenResponse.valid) {
52-
response.setStatusCode(401);
53-
response.setBody({
54-
status: 401,
55-
message: 'Your authentication token failed validation',
56-
detail: tokenResponse.message
57-
});
58-
return callback(null, response);
59-
}
60-
6127
console.log(`Getting properties for call SID ${callSid}`);
6228
const client = context.getTwilioClient();
6329
const callProperties = await client
@@ -74,4 +40,4 @@ exports.handler = async function (context, event, callback) {
7440
});
7541

7642
return callback(null, response);
77-
};
43+
});
Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
const nodeFetch = require('node-fetch');
2+
const TokenValidator = require('twilio-flex-token-validator').functionValidator;
23

3-
async function getAuthentication(token, context) {
4-
5-
console.log('Validating request token');
6-
7-
const tokenValidationApi = `https://${context.ACCOUNT_SID}:${context.AUTH_TOKEN}@iam.twilio.com/v1/Accounts/${context.ACCOUNT_SID}/Tokens/validate`;
8-
9-
const fetchResponse = await nodeFetch(tokenValidationApi, {
10-
method: 'POST',
11-
headers: {
12-
'Content-Type': 'application/json',
13-
},
14-
body: JSON.stringify({
15-
token
16-
})
17-
});
18-
19-
const tokenResponse = await fetchResponse.json();
20-
return tokenResponse;
21-
}
22-
23-
exports.handler = async function (context, event, callback) {
4+
exports.handler = TokenValidator(async function (context, event, callback) {
245
const response = new Twilio.Response();
256
response.appendHeader('Access-Control-Allow-Origin', '*');
267
response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS POST');
@@ -29,7 +10,7 @@ exports.handler = async function (context, event, callback) {
2910

3011
console.log('hold-conference-participant parameters:');
3112
Object.keys(event).forEach(key => {
32-
if (key !== "token") {
13+
if (key !== "token" || key !== "Token") {
3314
console.log(`${key}: ${event[key]}`);
3415
}
3516
});
@@ -40,24 +21,11 @@ exports.handler = async function (context, event, callback) {
4021
}
4122

4223
const {
43-
token,
4424
conference,
4525
participant,
4626
hold
4727
} = event;
4828

49-
console.log('Validating request token');
50-
const tokenResponse = await getAuthentication(event.token, context);
51-
if (!tokenResponse.valid) {
52-
response.setStatusCode(401);
53-
response.setBody({
54-
status: 401,
55-
message: 'Your authentication token failed validation',
56-
detail: tokenResponse.message
57-
});
58-
return callback(null, response);
59-
}
60-
6129
console.log(`${hold ? 'Holding' : 'Unholding'} participant ${participant} `
6230
+ `in conference ${conference}`);
6331
const client = context.getTwilioClient();
@@ -82,4 +50,4 @@ exports.handler = async function (context, event, callback) {
8250
});
8351

8452
return callback(null, response);
85-
};
53+
});
Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
const nodeFetch = require('node-fetch');
2+
const TokenValidator = require('twilio-flex-token-validator').functionValidator;
23

3-
async function getAuthentication(token, context) {
4-
5-
console.log('Validating request token');
6-
7-
const tokenValidationApi = `https://${context.ACCOUNT_SID}:${context.AUTH_TOKEN}@iam.twilio.com/v1/Accounts/${context.ACCOUNT_SID}/Tokens/validate`;
8-
9-
const fetchResponse = await nodeFetch(tokenValidationApi, {
10-
method: 'POST',
11-
headers: {
12-
'Content-Type': 'application/json',
13-
},
14-
body: JSON.stringify({
15-
token
16-
})
17-
});
18-
19-
const tokenResponse = await fetchResponse.json();
20-
return tokenResponse;
21-
}
22-
23-
exports.handler = async function (context, event, callback) {
4+
exports.handler = TokenValidator(async function (context, event, callback) {
245
const response = new Twilio.Response();
256
response.appendHeader('Access-Control-Allow-Origin', '*');
267
response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS POST');
@@ -29,7 +10,7 @@ exports.handler = async function (context, event, callback) {
2910

3011
console.log('remove-conference-participant parameters:');
3112
Object.keys(event).forEach(key => {
32-
if (key !== "token") {
13+
if (key !== "token" || key !== "Token") {
3314
console.log(`${key}: ${event[key]}`);
3415
}
3516
});
@@ -40,23 +21,10 @@ exports.handler = async function (context, event, callback) {
4021
}
4122

4223
const {
43-
token,
4424
conference,
4525
participant
4626
} = event;
4727

48-
console.log('Validating request token');
49-
const tokenResponse = await getAuthentication(event.token, context);
50-
if (!tokenResponse.valid) {
51-
response.setStatusCode(401);
52-
response.setBody({
53-
status: 401,
54-
message: 'Your authentication token failed validation',
55-
detail: tokenResponse.message
56-
});
57-
return callback(null, response);
58-
}
59-
6028
console.log(`Removing participant ${participant} from conference ${conference}`);
6129
const client = context.getTwilioClient();
6230
const participantResponse = await client
@@ -69,4 +37,4 @@ exports.handler = async function (context, event, callback) {
6937
});
7038

7139
return callback(null, response);
72-
};
40+
});
Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
const nodeFetch = require('node-fetch');
2+
const TokenValidator = require('twilio-flex-token-validator').functionValidator;
23

3-
async function getAuthentication(token, context) {
4-
5-
console.log('Validating request token');
6-
7-
const tokenValidationApi = `https://${context.ACCOUNT_SID}:${context.AUTH_TOKEN}@iam.twilio.com/v1/Accounts/${context.ACCOUNT_SID}/Tokens/validate`;
8-
9-
const fetchResponse = await nodeFetch(tokenValidationApi, {
10-
method: 'POST',
11-
headers: {
12-
'Content-Type': 'application/json',
13-
},
14-
body: JSON.stringify({
15-
token
16-
})
17-
});
18-
19-
const tokenResponse = await fetchResponse.json();
20-
return tokenResponse;
21-
}
22-
23-
exports.handler = async function (context, event, callback) {
4+
exports.handler = TokenValidator(async function (context, event, callback) {
245
const response = new Twilio.Response();
256
response.appendHeader('Access-Control-Allow-Origin', '*');
267
response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS POST');
@@ -29,7 +10,7 @@ exports.handler = async function (context, event, callback) {
2910

3011
console.log('update-conference-participant parameters:');
3112
Object.keys(event).forEach(key => {
32-
if (key !== "token") {
13+
if (key !== "token" || key !== "Token") {
3314
console.log(`${key}: ${event[key]}`);
3415
}
3516
});
@@ -40,24 +21,11 @@ exports.handler = async function (context, event, callback) {
4021
}
4122

4223
const {
43-
token,
4424
conference,
4525
participant,
4626
endConferenceOnExit
4727
} = event;
4828

49-
console.log('Validating request token');
50-
const tokenResponse = await getAuthentication(event.token, context);
51-
if (!tokenResponse.valid) {
52-
response.setStatusCode(401);
53-
response.setBody({
54-
status: 401,
55-
message: 'Your authentication token failed validation',
56-
detail: tokenResponse.message
57-
});
58-
return callback(null, response);
59-
}
60-
6129
console.log(`Updating participant ${participant} in conference ${conference}`);
6230
const client = context.getTwilioClient();
6331
const participantResponse = await client
@@ -79,4 +47,4 @@ exports.handler = async function (context, event, callback) {
7947
});
8048

8149
return callback(null, response);
82-
};
50+
});

0 commit comments

Comments
 (0)