Skip to content

Commit 199487e

Browse files
author
Tim Mendoza
committed
Update token server to be backwards compatible
1 parent 07fbd3a commit 199487e

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/video-token-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports.handler = (context, event, callback) => {
1616
} = context;
1717

1818
const { user_identity, room_name, passcode } = event;
19-
const [, appID, serverlessID] = DOMAIN_NAME.match(/-(\d+)-(\d+)(?:-\w+)?.twil.io$/);
19+
const [, appID, serverlessID] = DOMAIN_NAME.match(/-?(\d*)-(\d+)(?:-\w+)?.twil.io$/);
2020

2121
let response = new Twilio.Response();
2222
response.appendHeader('Content-Type', 'application/json');

test/video-token-server.test.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const callback = jest.fn();
66
const mockContext = {
77
ACCOUNT_SID: 'AC1234',
88
TWILIO_API_KEY_SID: 'SK1234',
9-
TWILIO_API_KEY_SECRET: '123456',
9+
TWILIO_API_KEY_SECRET: 'api_secret',
1010
API_PASSCODE: '123456',
1111
API_PASSCODE_EXPIRY: '10',
1212
DOMAIN_NAME: 'video-app-1234-5678-dev.twil.io',
@@ -99,7 +99,7 @@ describe('the video-token-server', () => {
9999
statusCode: 200,
100100
});
101101

102-
expect(jwt.decode(callback.mock.calls[0][1].body.token)).toEqual({
102+
expect(jwt.verify(callback.mock.calls[0][1].body.token, 'api_secret')).toEqual({
103103
exp: 14400,
104104
grants: {
105105
identity: 'test-user',
@@ -113,4 +113,23 @@ describe('the video-token-server', () => {
113113
sub: 'AC1234',
114114
});
115115
});
116+
117+
describe('when using an old form URL "video-app-XXXX-dev.twil.io', () => {
118+
it('should return a valid token when passcode, room_name, and user_identity parameters are supplied', () => {
119+
Date.now = () => 5;
120+
handler(
121+
{ ...mockContext, DOMAIN_NAME: 'video-app-1234-dev.twil.io' },
122+
{ passcode: '1234561234', room_name: 'test-room', user_identity: 'test-user' },
123+
callback
124+
);
125+
126+
expect(callback).toHaveBeenCalledWith(null, {
127+
body: { token: expect.any(String) },
128+
headers: { 'Content-Type': 'application/json' },
129+
statusCode: 200,
130+
});
131+
132+
expect(jwt.verify(callback.mock.calls[0][1].body.token, 'api_secret')).toBeTruthy();
133+
});
134+
});
116135
});

0 commit comments

Comments
 (0)