Skip to content

Commit

Permalink
Merge pull request #39 from twilio-labs/update-oclif
Browse files Browse the repository at this point in the history
Update oclif & minor refactors
  • Loading branch information
micaswyers-work authored Sep 28, 2022
2 parents c0642f7 + 45ad11a commit 95923a9
Show file tree
Hide file tree
Showing 13 changed files with 1,730 additions and 10,253 deletions.
11,810 changes: 1,654 additions & 10,156 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
"access": "public"
},
"dependencies": {
"@oclif/command": "^1.6.1",
"@oclif/config": "^1.15.1",
"@twilio/cli-core": "^6.0.0"
"@oclif/core": "^1.13.10",
"@twilio/cli-core": "^7.0.0"
},
"devDependencies": {
"@oclif/dev-cli": "^1.22.2",
Expand Down
15 changes: 9 additions & 6 deletions src/commands/token/capability/client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
const ClientCapability = require('twilio').jwt.ClientCapability;
const globalFlags = require('../../../helpers/globalFlags.js');
const { voiceFlags, validateTwimlAppSid } = require('../../../helpers/voiceGlobals.js');
const { voiceFlags } = require('../../../helpers/voiceGlobals.js');
const { validateSid } = require('../../../helpers/validation-helpers.js');

class ClientCapabilityTokenGenerator extends TwilioClientCommand {
constructor(argv, config) {
Expand All @@ -13,27 +14,29 @@ class ClientCapabilityTokenGenerator extends TwilioClientCommand {
async run() {
await super.run();

let ttl = this.flags['ttl'];
const voiceAppSid = await this.flags['voice-app-sid'];
const ttl = await this.flags['ttl'];
const incomingAllow = await this.flags['allow-incoming'];
const identity = await this.flags['identity'];
const capability = new ClientCapability({
accountSid: this.twilioClient.accountSid,
authToken: this.twilioClient.password,
ttl
});

if (!validateTwimlAppSid(this.flags['voice-app-sid'])) {
if (!validateSid('AP', voiceAppSid)) {
this.logger.error(
'Invalid TwiML Application SID, must look like APxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
process.exit(1);
}

capability.addScope(new ClientCapability.OutgoingClientScope({
applicationSid: this.flags['voice-app-sid']
applicationSid: voiceAppSid
}));

let incomingAllow = (this.flags['allow-incoming'] == 'true');
if (incomingAllow) {
capability.addScope(new ClientCapability.IncomingClientScope(this.flags['identity']))
capability.addScope(new ClientCapability.IncomingClientScope(identity))
}

this.logger.info('Copy/paste this voice token into your test application:');
Expand Down
13 changes: 7 additions & 6 deletions src/commands/token/capability/worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
const globalFlags = require('../../../helpers/globalFlags.js');
const { taskrouterFlags, validateWorkerSid, validateWorkspaceSid } = require('../../../helpers/taskrouterGlobals.js');
const { taskrouterFlags } = require('../../../helpers/taskrouterGlobals.js');
const { validateSid } = require('../../../helpers/validation-helpers.js');
const taskrouter = require('twilio').jwt.taskrouter;
const util = taskrouter.util;
const TaskRouterCapability = taskrouter.TaskRouterCapability;
Expand All @@ -16,17 +17,20 @@ class WorkerCapabilityTokenGenerator extends TwilioClientCommand {
async run() {
await super.run();

const workerSid = await this.flags['worker-sid'];
const workspaceSid = await this.flags['workspace-sid'];
let ttl = await this.flags['ttl'];
const TASKROUTER_BASE_URL = 'https://taskrouter.twilio.com';
const version = 'v1';

if (!validateWorkerSid(this.flags['worker-sid'])) {
if (!validateSid('WK', workerSid)) {
this.logger.error(
'Invalid Worker SID, must look like WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
process.exit(1);
}

if (!validateWorkspaceSid(this.flags['workspace-sid'])) {
if (!validateSid('WS', workspaceSid)) {
this.logger.error(
'Invalid Workspace SID, must look like WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
Expand All @@ -35,10 +39,7 @@ class WorkerCapabilityTokenGenerator extends TwilioClientCommand {

const accountSid = this.twilioClient.accountSid;
const authToken = this.twilioClient.password;
const workspaceSid = this.flags['workspace-sid'];
const workerSid = this.flags['worker-sid'];

let ttl = this.flags['ttl'];
const capability = new TaskRouterCapability({
accountSid,
authToken,
Expand Down
23 changes: 9 additions & 14 deletions src/commands/token/chat.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { flags } = require('@oclif/command');
const { Flags } = require('@oclif/core');
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
const Twilio = require('twilio');
const createToken = require('../../helpers/accessToken.js');
const globalFlags = require('../../helpers/globalFlags.js');
const { validateSid } = require('../../helpers/validation-helpers.js');

class ChatTokenGenerator extends TwilioClientCommand {
constructor(argv, config) {
Expand All @@ -11,27 +12,21 @@ class ChatTokenGenerator extends TwilioClientCommand {
this.showHeaders = true;
}

validateChatServiceSid() {
return (
this.flags['chat-service-sid'].startsWith('IS') &&
this.flags['chat-service-sid'].length === 34
);
}

async run() {
await super.run();

const chatServiceSid = await this.flags['chat-service-sid'];
const accessToken = createToken.call(this);

if (!this.validateChatServiceSid()) {
if (!validateSid('IS', chatServiceSid)) {
this.logger.error(
'Invalid Chat Service SID, must look like ISxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
process.exit(1);
}

let chatGrant = new Twilio.jwt.AccessToken.ChatGrant({
serviceSid: this.flags['chat-service-sid'],
serviceSid: chatServiceSid
});
accessToken.addGrant(chatGrant);

Expand All @@ -43,13 +38,13 @@ class ChatTokenGenerator extends TwilioClientCommand {
}

const ChatTokenGeneratorFlags = {
identity: flags.string({
identity: Flags.string({
description: 'The user identity for this Chat',
required: true,
required: true
}),
'chat-service-sid': flags.string({
'chat-service-sid': Flags.string({
description: 'The service SID for the Chat, starts with ISXXX',
required: true,
required: true
}),
};

Expand Down
14 changes: 8 additions & 6 deletions src/commands/token/flex.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { flags } = require('@oclif/command');
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
const { TwilioCliError } = require('@twilio/cli-core').services.error;
const Twilio = require('twilio');
const createToken = require('../../helpers/accessToken.js');
const globalFlags = require('../../helpers/globalFlags.js');
const { taskrouterFlags, validateWorkerSid, validateWorkspaceSid } = require('../../helpers/taskrouterGlobals.js');
const { taskrouterFlags } = require('../../helpers/taskrouterGlobals.js');
const { validateSid } = require('../../helpers/validation-helpers.js');

class FlexTokenGenerator extends TwilioClientCommand {
constructor(argv, config) {
Expand All @@ -16,25 +16,27 @@ class FlexTokenGenerator extends TwilioClientCommand {
async run() {
await super.run();

const workerSid = await this.flags['worker-sid'];
const workspaceSid = await this.flags['workspace-sid'];
const accessToken = createToken.call(this);

if (!validateWorkerSid(this.flags['worker-sid'])) {
if (!validateSid('WK', workerSid)) {
this.logger.error(
'Invalid Worker SID, must look like WKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
process.exit(1);
}

if (!validateWorkspaceSid(this.flags['workspace-sid'])) {
if (!validateSid('WS', workspaceSid)) {
this.logger.error(
'Invalid Workspace SID, must look like WSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
process.exit(1);
}

let flexGrant = new Twilio.jwt.AccessToken.TaskRouterGrant({
workerSid: this.flags['worker-sid'],
workspaceSid: this.flags['workspace-sid'],
workerSid,
workspaceSid,
role: 'worker'
});
accessToken.addGrant(flexGrant);
Expand Down
19 changes: 7 additions & 12 deletions src/commands/token/sync.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { flags } = require('@oclif/command');
const { Flags } = require('@oclif/core');
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
const Twilio = require('twilio');
const createToken = require('../../helpers/accessToken.js');
const globalFlags = require('../../helpers/globalFlags.js');
const { validateSid } = require('../../helpers/validation-helpers.js');

class SyncTokenGenerator extends TwilioClientCommand {
constructor(argv, config) {
Expand All @@ -11,27 +12,21 @@ class SyncTokenGenerator extends TwilioClientCommand {
this.showHeaders = true;
}

validateSyncServiceSid() {
return (
this.flags['sync-service-sid'].startsWith('IS') &&
this.flags['sync-service-sid'].length === 34
);
}

async run() {
await super.run();

const syncServiceSid = await this.flags['sync-service-sid'];
const accessToken = createToken.call(this);

if (!this.validateSyncServiceSid()) {
if (!validateSid('IS', syncServiceSid)) {
this.logger.error(
'Invalid Sync Service SID, must look like ISxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
process.exit(1);
}

let syncGrant = new Twilio.jwt.AccessToken.SyncGrant({
serviceSid: this.flags['sync-service-sid'],
serviceSid: syncServiceSid
});
accessToken.addGrant(syncGrant);

Expand All @@ -43,11 +38,11 @@ class SyncTokenGenerator extends TwilioClientCommand {
}

const SyncTokenGeneratorFlags = {
identity: flags.string({
identity: Flags.string({
description: 'The user identity for this Sync Service',
required: true,
}),
'sync-service-sid': flags.string({
'sync-service-sid': Flags.string({
description: 'The service SID for the Sync, starts with ISXXX',
required: true,
}),
Expand Down
6 changes: 3 additions & 3 deletions src/commands/token/video.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { flags } = require('@oclif/command');
const { Flags } = require('@oclif/core');
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
const Twilio = require('twilio');
const createToken = require('../../helpers/accessToken.js');
Expand All @@ -16,7 +16,7 @@ class VideoTokenGenerator extends TwilioClientCommand {

const accessToken = createToken.call(this);

let room = this.flags['room-name'];
let room = await this.flags['room-name'];
let videoGrant = new Twilio.jwt.AccessToken.VideoGrant({ room });
accessToken.addGrant(videoGrant);

Expand All @@ -28,7 +28,7 @@ class VideoTokenGenerator extends TwilioClientCommand {
}

const VideoTokenGeneratorFlags = {
'room-name': flags.string({
'room-name': Flags.string({
description: 'The name of the room this token grants access to',
required: false,
}),
Expand Down
19 changes: 12 additions & 7 deletions src/commands/token/voice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
const Twilio = require('twilio');
const createToken = require('../../helpers/accessToken.js');
const globalFlags = require('../../helpers/globalFlags.js');
const { voiceFlags, validateTwimlAppSid, validatePushCredentialSid } = require('../../helpers/voiceGlobals.js');
const { voiceFlags } = require('../../helpers/voiceGlobals.js');
const { validateSid } = require('../../helpers/validation-helpers.js');

class VoiceTokenGenerator extends TwilioClientCommand {
constructor(argv, config) {
Expand All @@ -15,27 +16,31 @@ class VoiceTokenGenerator extends TwilioClientCommand {
await super.run();

const accessToken = createToken.call(this);
// all flags
const voiceAppSid = await this.flags['voice-app-sid'];
const pushCredentialSid = await this.flags['push-credential-sid'];
const incomingAllow = await this.flags['allow-incoming'];

if (!validateTwimlAppSid(this.flags['voice-app-sid'])) {
if (!validateSid('AP', voiceAppSid)) {
this.logger.error(
'Invalid TwiML Application SID, must look like APxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
process.exit(1);
}

let pushCredentialSid = this.flags['push-credential-sid'];
if (pushCredentialSid && !validatePushCredentialSid(pushCredentialSid)) {
// logic for optional pushCredential
if (pushCredentialSid && !validateSid('CR', pushCredentialSid)) {
this.logger.error(
'Invalid Push Credential SID, must look like CRxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
process.exit(1);
}

let incomingAllow = (this.flags['allow-incoming'] == 'true');
let voiceGrant = new Twilio.jwt.AccessToken.VoiceGrant({
outgoingApplicationSid: this.flags['voice-app-sid'],
const voiceGrant = new Twilio.jwt.AccessToken.VoiceGrant({
outgoingApplicationSid: voiceAppSid,
incomingAllow
});

if (pushCredentialSid) {
voiceGrant.pushCredentialSid = pushCredentialSid;
}
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/globalFlags.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { flags } = require('@oclif/command');
const { Flags } = require('@oclif/core');

const globalFlags = {
identity: flags.string({
identity: Flags.string({
description: 'The user identity',
required: true,
}),
ttl: flags.integer({
ttl: Flags.integer({
description: 'Optional TTL for token (up to 24 hours) (value in seconds)',
required: false,
})
Expand Down
22 changes: 4 additions & 18 deletions src/helpers/taskrouterGlobals.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
const { flags } = require('@oclif/command');
const { Flags } = require('@oclif/core');

const taskrouterFlags = {
'worker-sid': flags.string({
'worker-sid': Flags.string({
description: 'The Worker SID for this token',
required: true,
}),
'workspace-sid': flags.string({
'workspace-sid': Flags.string({
description: 'The Workspace SID for this token',
required: true,
})
}

const validateWorkerSid = function(sid) {
return (
sid.startsWith('WK') &&
sid.length === 34
);
}

const validateWorkspaceSid = function(sid) {
return (
sid.startsWith('WS') &&
sid.length === 34
);
}

module.exports = { taskrouterFlags, validateWorkerSid, validateWorkspaceSid };
module.exports = { taskrouterFlags };
7 changes: 7 additions & 0 deletions src/helpers/validation-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const validateSid = function (prefix, sid) {
return (sid.startsWith(prefix) &&
sid.length === 34
);
};

module.exports = { validateSid };
Loading

0 comments on commit 95923a9

Please sign in to comment.