{"size": 844, "ext": "js", "max_stars_repo_path": "tests/lib/token.js", "max_stars_repo_name": "cmcaboy/twilio-voice.js", "max_stars_repo_stars_event_min_datetime": "2019-05-21T20:02:14.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-26T02:11:55.000Z", "max_issues_repo_path": "tests/lib/token.js", "max_issues_repo_name": "cmcaboy/twilio-voice.js", "max_issues_repo_issues_event_min_datetime": "2019-05-24T19:42:22.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-16T23:06:54.000Z", "max_forks_repo_path": "tests/lib/token.js", "max_forks_repo_name": "cmcaboy/twilio-voice.js", "max_forks_repo_forks_event_min_datetime": "2019-06-29T00:23:49.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-16T20:56:18.000Z"}
{"max_stars_count": 66, "max_issues_count": 130, "max_forks_count": 38, "avg_line_length": 25.5757575758, "max_line_length": 77, "alphanum_fraction": 0.7227488152}
const Twilio = require('twilio');
const env = require('../env.js');

function generateAccessToken(identity, ttl, appSid) {
  const accessToken = new Twilio.jwt.AccessToken(env.accountSid,
    env.apiKeySid,
    env.apiKeySecret,
    { ttl: ttl || 300, identity });

  accessToken.addGrant(new Twilio.jwt.AccessToken.VoiceGrant({
    incomingAllow: true,
    outgoingApplicationSid: appSid || env.appSid,
  }));

  return accessToken.toJwt();
}

function generateCapabilityToken() {
  const outgoingScope = new Twilio.jwt.ClientCapability.OutgoingClientScope({
    applicationSid: env.appSid
  });

  const token = new Twilio.jwt.ClientCapability({
    accountSid: env.accountSid,
    authToken: env.authToken,
  });

  token.addScope(outgoingScope);
  return token.toJwt();
}

module.exports = { generateAccessToken, generateCapabilityToken };
