-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsync.js
61 lines (51 loc) · 1.59 KB
/
sync.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
57
58
59
60
61
const { flags } = require('@oclif/command');
const { TwilioClientCommand } = require('@twilio/cli-core').baseCommands;
const Twilio = require('twilio');
const createToken = require('../../helpers/accessToken.js');
const globalFlags = require('../../helpers/globalFlags.js');
class SyncTokenGenerator extends TwilioClientCommand {
constructor(argv, config) {
super(argv, config);
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 accessToken = createToken.call(this);
if (!this.validateSyncServiceSid()) {
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'],
});
accessToken.addGrant(syncGrant);
this.logger.info('Copy/paste this sync token into your test application:');
this.output({ jwt: accessToken.toJwt() }, undefined, {
showHeaders: false,
});
}
}
const SyncTokenGeneratorFlags = {
identity: flags.string({
description: 'The user identity for this Sync Service',
required: true,
}),
'sync-service-sid': flags.string({
description: 'The service SID for the Sync, starts with ISXXX',
required: true,
}),
};
SyncTokenGenerator.flags = Object.assign(
SyncTokenGeneratorFlags,
TwilioClientCommand.flags,
globalFlags
);
module.exports = SyncTokenGenerator;