Skip to content

Commit 61cdcba

Browse files
author
Tim Mendoza
committed
Add verification for user_identity parameter
1 parent 4202885 commit 61cdcba

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/video-token-server.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ module.exports.handler = (context, event, callback) => {
4646
return;
4747
}
4848

49+
if (!user_identity) {
50+
response.setStatusCode(400);
51+
response.setBody({
52+
error: {
53+
message: 'missing user_identity',
54+
explanation: 'The user_identity parameter is missing.',
55+
},
56+
});
57+
callback(null, response);
58+
return;
59+
}
60+
4961
const token = new AccessToken(TWILIO_ACCOUNT_SID, TWILIO_API_KEY_SID, TWILIO_API_KEY_SECRET, {
5062
ttl: MAX_ALLOWED_SESSION_DURATION,
5163
});

test/video-token-server.test.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('the video-token-server', () => {
1616
it('should return an "unauthorized" error when the passcode is incorrect', () => {
1717
Date.now = () => 5;
1818

19-
handler(mockContext, { passcode: '9876543210' }, callback);
19+
handler(mockContext, { passcode: '9876543210', user_identity: 'test identity' }, callback);
2020

2121
expect(callback).toHaveBeenCalledWith(null, {
2222
body: {
@@ -33,7 +33,7 @@ describe('the video-token-server', () => {
3333
it('should return an "expired" error when the current time is past the API_PASSCODE_EXPIRY time', () => {
3434
Date.now = () => 15;
3535

36-
handler(mockContext, { passcode: '1234566789' }, callback);
36+
handler(mockContext, { passcode: '1234566789', user_identity: 'test identity'}, callback);
3737

3838
expect(callback).toHaveBeenCalledWith(null, {
3939
body: {
@@ -48,11 +48,29 @@ describe('the video-token-server', () => {
4848
});
4949
});
5050

51-
it('should return a token when only the passcode is supplied', () => {
51+
it('should return a "missing user_identity" error when the "user_identity" parameter is not supplied', () => {
5252
Date.now = () => 5;
5353

5454
handler(mockContext, { passcode: '1234566789' }, callback);
5555

56+
expect(callback).toHaveBeenCalledWith(null, {
57+
body: {
58+
error: {
59+
message: 'missing user_identity',
60+
explanation:
61+
'The user_identity parameter is missing.',
62+
},
63+
},
64+
headers: { 'Content-Type': 'application/json' },
65+
statusCode: 400,
66+
});
67+
});
68+
69+
it('should return a token when no room_name is supplied', () => {
70+
Date.now = () => 5;
71+
72+
handler(mockContext, { passcode: '1234566789', user_identity: 'test identity' }, callback);
73+
5674
expect(callback).toHaveBeenCalledWith(null, {
5775
body: { token: expect.any(String) },
5876
headers: { 'Content-Type': 'application/json' },
@@ -62,6 +80,7 @@ describe('the video-token-server', () => {
6280
expect(jwt.decode(callback.mock.calls[0][1].body.token)).toEqual({
6381
exp: 14400,
6482
grants: {
83+
identity: "test identity",
6584
video: {},
6685
},
6786
iat: 0,

0 commit comments

Comments
 (0)