-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathchat.js
56 lines (47 loc) · 1.49 KB
/
chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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) {
super(argv, config);
this.showHeaders = true;
}
async run() {
await super.run();
const chatServiceSid = await this.flags['chat-service-sid'];
const accessToken = createToken.call(this);
if (!validateSid('IS', chatServiceSid)) {
this.logger.error(
'Invalid Chat Service SID, must look like ISxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
);
process.exit(1);
}
const chatGrant = new Twilio.jwt.AccessToken.ChatGrant({
serviceSid: chatServiceSid,
});
accessToken.addGrant(chatGrant);
this.logger.info('Copy/paste this chat token into your test application:');
this.output({ jwt: accessToken.toJwt() }, undefined, {
showHeaders: false,
});
}
}
const ChatTokenGeneratorFlags = {
identity: Flags.string({
description: 'The user identity for this Chat',
required: true,
}),
'chat-service-sid': Flags.string({
description: 'The service SID for the Chat, starts with ISXXX',
required: true,
}),
};
ChatTokenGenerator.flags = Object.assign(
ChatTokenGeneratorFlags,
TwilioClientCommand.flags,
globalFlags,
);
module.exports = ChatTokenGenerator;