/
TaskRouterCapability.js
183 lines (148 loc) · 5.88 KB
/
TaskRouterCapability.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var jwt = require('jsonwebtoken');
var deprecate = require('deprecate');
var _ = require('underscore')
require('string.prototype.startswith');
var taskRouterUrlBase = 'https://taskrouter.twilio.com';
var taskRouterVersion = 'v1';
var eventUrlBase = 'https://event-bridge.twilio.com/v1/wschannels';
function TaskRouterCapability(accountSid, authToken, workspaceSid, channelId) {
this.accountSid = accountSid;
this.authToken = authToken;
this.policies = [];
this.workspaceSid = workspaceSid;
this.channelId = channelId;
this._baseUrl = taskRouterUrlBase + '/' + taskRouterVersion + '/Workspaces/' + this.workspaceSid;
this._validateJWT();
this._setupResource();
var eventsUrl = eventUrlBase + '/' + this.accountSid + '/' + channelId;
// add permissions to GET and POST to the event-bridge channel
this.allow(eventsUrl, 'GET');
this.allow(eventsUrl, 'POST');
// add permission to fetch the instance resource
this.allow(this._resourceUrl, 'GET');
}
TaskRouterCapability.prototype._setupResource = function() {
if(this.channelId.startsWith('WS')) {
this._resourceUrl = this._baseUrl;
}else if(this.channelId.startsWith('WK')) {
this._resourceUrl = this._baseUrl + '/Workers/' + this.channelId;
var activityUrl = this._baseUrl + '/Activities';
this.allow(activityUrl, "GET");
var tasksUrl = this._baseUrl + '/Tasks/**';
this.allow(tasksUrl, "GET");
var workerReservationsUrl = this._resourceUrl + '/Reservations/**';
this.allow(workerReservationsUrl, "GET");
}else if(this.channelId.startsWith('WQ')) {
this._resourceUrl = this._baseUrl + '/TaskQueues/' + this.channelId;
}
}
TaskRouterCapability.prototype._validateJWT = function() {
if(!this.accountSid || !this.accountSid.startsWith('AC')) {
throw "Invalid AccountSid provided: "+this.accountSid;
}
if(!this.workspaceSid || !this.workspaceSid.startsWith('WS')) {
throw "Invalid WorkspaceSid provided: "+this.workspaceSid;
}
if(!this.channelId) {
throw "ChannelId not provided";
}
var prefix = this.channelId.substring(0,2);
if(prefix != 'WS' && prefix != 'WK' && prefix != 'WQ') {
throw "Invalid ChannelId provided: "+this.channelId;
}
}
TaskRouterCapability.prototype.allowFetchSubresources = function() {
this.allow(this._resourceUrl+'/**', 'GET');
}
TaskRouterCapability.prototype.allowUpdates = function() {
this.allow(this._resourceUrl, 'POST');
}
TaskRouterCapability.prototype.allowUpdatesSubresources = function() {
this.allow(this._resourceUrl+'/**', 'POST');
}
TaskRouterCapability.prototype.allowDelete = function() {
this.allow(this._resourceUrl, 'DELETE');
}
TaskRouterCapability.prototype.allowDeleteSubresources = function() {
this.allow(this._resourceUrl+'/**', 'DELETE');
}
TaskRouterCapability.prototype.allowDeleteSubresources = function() {
this.allow(this._resourceUrl+'/**', 'DELETE');
}
TaskRouterCapability.prototype.allowWorkerActivityUpdates = function() {
if(this.channelId.startsWith('WK')) {
deprecate('allowWorkerActivityUpdates is deprecated. Please use TaskRouterWorkerCapability.allowWorkerActivityUpdates() instead');
this.allow(this._resourceUrl, 'POST', {}, {"ActivitySid": {'required': true}});
}else {
throw "Deprecated function not applicable to non Worker";
}
}
TaskRouterCapability.prototype.allowWorkerFetchAttributes = function() {
if(this.channelId.startsWith('WK')) {
deprecate('allowWorkerFetchAttributes is deprecated. Please use TaskRouterWorkerCapability; added automatically in constructor');
this.allow(this._resourceUrl, 'GET');
}else {
throw "Deprecated function not applicable to non Worker";
}
}
TaskRouterCapability.prototype.allowTaskReservationUpdates = function() {
if(this.channelId.startsWith('WK')) {
deprecate('allowTaskReservationUpdates is deprecated. Please use TaskRouterWorkerCapability.allowReservationUpdates() instead');
this.allow(this._baseUrl + "/Tasks/**", "POST");
}else {
throw "Deprecated function not applicable to non Worker";
}
}
TaskRouterCapability.prototype.addPolicy = function(url, method, allowed, queryFilter, postFilter) {
var policy = {
url: url,
method: method
};
if (queryFilter) {
policy.query_filter = queryFilter;
} else {
policy.query_filter = {};
}
if (postFilter) {
policy.post_filter = postFilter;
} else {
policy.post_filter = {};
}
if (typeof allowed !== 'undefined') {
policy.allow = allowed;
} else {
policy.allow = true;
}
this.policies.push(policy);
}
TaskRouterCapability.prototype.allow = function(url, method, queryFilter, postFilter) {
this.addPolicy(url, method, true, queryFilter, postFilter);
}
TaskRouterCapability.prototype.deny = function(url, method, queryFilter, postFilter) {
this.addPolicy(url, method, false, queryFilter, postFilter);
}
TaskRouterCapability.prototype.generate = function(ttl) {
var taskRouterAttributes = {
'account_sid': this.accountSid,
'channel': this.channelId,
'workspace_sid': this.workspaceSid
}
if(this.channelId.startsWith('WK')) {
taskRouterAttributes.worker_sid = this.channelId;
}else if(this.channelId.startsWith('WQ')) {
taskRouterAttributes.taskqueue_sid = this.channelId;
}
return this._generate(ttl, taskRouterAttributes);
}
TaskRouterCapability.prototype._generate = function(ttl, extraAttributes) {
var payload = {
iss: this.accountSid,
exp: (Math.floor(new Date() / 1000) + (ttl || 3600)),
version: taskRouterVersion,
friendly_name: this.channelId,
policies: this.policies,
};
_.extend(payload, extraAttributes);
return jwt.sign(payload, this.authToken);
}
module.exports = TaskRouterCapability;