twilio-node
twilio-node copied to clipboard
toJWT() throws a TypeError when calling
Issue Summary
Hi guys!
I'm trying to create a JWT with VideoGrant since I'm creating a Mobile App that needs video calls. I've followed this guide.
Steps to Reproduce
- Create a service to generate an accessToken based on the code snippet I show below.
- Run the service with some arbitrary values like userId with value "pedro" and roomSid with value "test"
- When calling toJWT the call will throw a TypeError exception related to no initializacion of an empty object before assigning values.
Code Snippet
import * as twilio from 'twilio';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import CreateVideoCallTokenDto from './twilio.dto';
import { TwilioJwtResponse } from './twilio.controller';
let AccessToken = twilio.jwt.AccessToken;
let VideoGrant = AccessToken.VideoGrant;
@Injectable()
export class TwilioService {
constructor(private readonly configService: ConfigService) {}
public async createVideoCallToken({
userId,
roomSid,
}: CreateVideoCallTokenDto): Promise<TwilioJwtResponse> {
let token = new AccessToken(
this.configService.get<string>('TWILIO_ACCOUNT_SID'),
this.configService.get<string>('TWILIO_API_KEY_SID'),
this.configService.get<string>('TWILIO_API_KEY_SECRET'),
{
identity: userId,
},
);
let grant = new VideoGrant({ room: roomSid });
token.addGrant(grant);
return {
accessToken: token.toJwt(),
};
}
}
Exception/Log
TypeError: Cannot create property 'video' on string 'pedro'
at /Users/jonatansalas/Desktop/unmano-backend-2/node_modules/twilio/lib/jwt/AccessToken.js:281:25
at arrayEach (/Users/jonatansalas/Desktop/unmano-backend-2/node_modules/lodash/lodash.js:530:11)
at Function.forEach (/Users/jonatansalas/Desktop/unmano-backend-2/node_modules/lodash/lodash.js:9410:14)
at AccessToken.toJwt (/Users/jonatansalas/Desktop/unmano-backend-2/node_modules/twilio/lib/jwt/AccessToken.js:280:7)
at TwilioService.createVideoCallToken (/Users/jonatansalas/Desktop/unmano-backend-2/src/twilio/twilio.service.ts:36:26)
at TwilioController.createVideoCallToken (/Users/jonatansalas/Desktop/unmano-backend-2/src/twilio/twilio.controller.ts:15:37)
at /Users/jonatansalas/Desktop/unmano-backend-2/node_modules/@nestjs/core/router/router-execution-context.js:38:29
at processTicksAndRejections (internal/process/task_queues.js:95:5)
Technical details:
- twilio-node version: 3.81.0
- node version: v14.18.2
I've reviewed the code, the v3.81.0 explodes in the following lines:
var grants = {};
if (_.isInteger(this.identity) || _.isString(this.identity)) { grants = String(this.identity);}
_.each(this.grants, function (grant) {
grants[grant.key] = grant.toPayload();
});
I've also reviewed the v4.0.0-rc branch that has a fix:
var grants = {};
if (_.isInteger(this.identity) || _.isString(this.identity)) { grants.identity = String(this.identity); }
_.each(this.grants, function(grant) {
grants[grant.key] = grant.toPayload();
});
The problem is that grants
is being overwritten in case of setting identity
with a string
, and then when trying to access an index with a key it get out of bounds and results in throwing an error.
The JWT generation code is the same between the main and RC branches, which is not matching up with the first snippet above: https://github.com/twilio/twilio-node/blob/39d15cd357f90e749fe97d1901398722a9687812/lib/jwt/AccessToken.js#L275-L280
Maybe I'm missing something?
Closing due to inactivity.