/
TaskRouterCapability.ts
147 lines (133 loc) · 3.76 KB
/
TaskRouterCapability.ts
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
import jwt from "jsonwebtoken";
export interface TaskRouterCapabilityOptions {
accountSid: string;
authToken: string;
workspaceSid: string;
channelId: string;
friendlyName?: string;
ttl?: number;
version?: string;
}
export interface PolicyOptions {
/** Policy URL */
url?: string;
/** HTTP Method */
method?: string;
/** Request query filter allowances */
queryFilter?: object;
/** Request post filter allowances */
postFilter?: object;
/** Allow the policy */
allow?: boolean;
}
export interface PolicyPayload {
url: string;
method: string;
query_filter: object;
post_filter: object;
allow: boolean;
}
/**
* Create a new Policy
*/
export class Policy {
url: string;
method: string;
queryFilter: object;
postFilter: object;
allow: boolean;
/**
* Create a new Policy instance
*
* @param options - ...
* @param options.url - Policy URL
* @param options.method - HTTP Method
* @param options.queryFilter - Request query filter allowances
* @param options.postFilter - Request post filter allowances
* @param options.allowed - Allow the policy
*/
constructor(options?: PolicyOptions) {
options = options || {};
this.url = options.url || "";
this.method = options.method || "GET";
this.queryFilter = options.queryFilter || {};
this.postFilter = options.postFilter || {};
this.allow = options.allow || true;
}
payload(): PolicyPayload {
return {
url: this.url,
method: this.method,
query_filter: this.queryFilter,
post_filter: this.postFilter,
allow: this.allow,
};
}
}
export default class TaskRouterCapability {
accountSid: string;
authToken: string;
workspaceSid: string;
channelId: string;
ttl: number;
version: string;
policies: Policy[];
friendlyName?: string;
/**
* @param options - ...
* @param options.accountSid - account sid
* @param options.authToken - auth token
* @param options.workspaceSid - workspace sid
* @param options.channelId - taskrouter channel id
* @param options.friendlyName - friendly name for the jwt
* @param options.ttl - time to live
* @param options.version - taskrouter version
*/
constructor(options: TaskRouterCapabilityOptions) {
if (!options) {
throw new Error('Required parameter "options" missing.');
}
if (!options.accountSid) {
throw new Error('Required parameter "options.accountSid" missing.');
}
if (!options.authToken) {
throw new Error('Required parameter "options.authToken" missing.');
}
if (!options.workspaceSid) {
throw new Error('Required parameter "options.workspaceSid" missing.');
}
if (!options.channelId) {
throw new Error('Required parameter "options.channelId" missing.');
}
this.accountSid = options.accountSid;
this.authToken = options.authToken;
this.workspaceSid = options.workspaceSid;
this.channelId = options.channelId;
this.friendlyName = options.friendlyName;
this.ttl = options.ttl || 3600;
this.version = options.version || "v1";
this.policies = [];
}
static Policy = Policy;
addPolicy(policy: Policy) {
this.policies.push(policy);
}
toJwt(): string {
var payload: any = {
iss: this.accountSid,
exp: Math.floor(new Date().valueOf() / 1000) + this.ttl,
version: this.version,
friendly_name: this.friendlyName,
account_sid: this.accountSid,
channel: this.channelId,
workspace_sid: this.workspaceSid,
policies: this.policies.map((policy) => policy.payload()),
};
if (this.channelId.startsWith("WK")) {
payload.worker_sid = this.channelId;
} else if (this.channelId.startsWith("WQ")) {
payload.taskqueue_sid = this.channelId;
}
return jwt.sign(payload, this.authToken);
}
}