-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
AccountSASPermissions.ts
352 lines (325 loc) · 8.52 KB
/
AccountSASPermissions.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/**
* ONLY AVAILABLE IN NODE.JS RUNTIME.
*
* This is a helper class to construct a string representing the permissions granted by an AccountSAS. Setting a value
* to true means that any SAS which uses these permissions will grant permissions for that operation. Once all the
* values are set, this should be serialized with toString and set as the permissions field on an
* {@link AccountSASSignatureValues} object. It is possible to construct the permissions string without this class, but
* the order of the permissions is particular and this class guarantees correctness.
*
* @export
* @class AccountSASPermissions
*/
export class AccountSASPermissions {
/**
* Parse initializes the AccountSASPermissions fields from a string.
*
* @static
* @param {string} permissions
* @returns {AccountSASPermissions}
* @memberof AccountSASPermissions
*/
public static parse(permissions: string): AccountSASPermissions {
const accountSASPermissions = new AccountSASPermissions();
for (const c of permissions) {
switch (c) {
case "r":
accountSASPermissions.read = true;
break;
case "w":
accountSASPermissions.write = true;
break;
case "d":
accountSASPermissions.delete = true;
break;
case "x":
accountSASPermissions.deleteVersion = true;
break;
case "l":
accountSASPermissions.list = true;
break;
case "a":
accountSASPermissions.add = true;
break;
case "c":
accountSASPermissions.create = true;
break;
case "u":
accountSASPermissions.update = true;
break;
case "p":
accountSASPermissions.process = true;
break;
case "t":
accountSASPermissions.tag = true;
break;
case "f":
accountSASPermissions.filter = true;
break;
default:
throw new RangeError(`Invalid permission character: ${c}`);
}
}
return accountSASPermissions;
}
/**
* Creates a {@link AccountSASPermissions} from a raw object which contains same keys as it
* and boolean values for them.
*
* @static
* @param {AccountSASPermissionsLike} permissionLike
* @returns {AccountSASPermissions}
* @memberof AccountSASPermissions
*/
public static from(permissionLike: AccountSASPermissionsLike): AccountSASPermissions {
const accountSASPermissions = new AccountSASPermissions();
if (permissionLike.read) {
accountSASPermissions.read = true;
}
if (permissionLike.write) {
accountSASPermissions.write = true;
}
if (permissionLike.delete) {
accountSASPermissions.delete = true;
}
if (permissionLike.deleteVersion) {
accountSASPermissions.deleteVersion = true;
}
if (permissionLike.filter) {
accountSASPermissions.filter = true;
}
if (permissionLike.tag) {
accountSASPermissions.tag = true;
}
if (permissionLike.list) {
accountSASPermissions.list = true;
}
if (permissionLike.add) {
accountSASPermissions.add = true;
}
if (permissionLike.create) {
accountSASPermissions.create = true;
}
if (permissionLike.update) {
accountSASPermissions.update = true;
}
if (permissionLike.process) {
accountSASPermissions.process = true;
}
return accountSASPermissions;
}
/**
* Permission to read resources and list queues and tables granted.
*
* @type {boolean}
* @memberof AccountSASPermissions
*/
public read: boolean = false;
/**
* Permission to write resources granted.
*
* @type {boolean}
* @memberof AccountSASPermissions
*/
public write: boolean = false;
/**
* Permission to create blobs and files granted.
*
* @type {boolean}
* @memberof AccountSASPermissions
*/
public delete: boolean = false;
/**
* Permission to delete versions granted.
*
* @type {boolean}
* @memberof AccountSASPermissions
*/
public deleteVersion: boolean = false;
/**
* Permission to list blob containers, blobs, shares, directories, and files granted.
*
* @type {boolean}
* @memberof AccountSASPermissions
*/
public list: boolean = false;
/**
* Permission to add messages, table entities, and append to blobs granted.
*
* @type {boolean}
* @memberof AccountSASPermissions
*/
public add: boolean = false;
/**
* Permission to create blobs and files granted.
*
* @type {boolean}
* @memberof AccountSASPermissions
*/
public create: boolean = false;
/**
* Permissions to update messages and table entities granted.
*
* @type {boolean}
* @memberof AccountSASPermissions
*/
public update: boolean = false;
/**
* Permission to get and delete messages granted.
*
* @type {boolean}
* @memberof AccountSASPermissions
*/
public process: boolean = false;
/**
* Specfies Tag access granted.
*
* @type {boolean}
* @memberof AccountSASPermissions
*/
public tag: boolean = false;
/**
* Permission to filter blobs.
*
* @type {boolean}
* @memberof AccountSASPermissions
*/
public filter: boolean = false;
/**
* Produces the SAS permissions string for an Azure Storage account.
* Call this method to set AccountSASSignatureValues Permissions field.
*
* Using this method will guarantee the resource types are in
* an order accepted by the service.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-an-account-sas
*
* @returns {string}
* @memberof AccountSASPermissions
*/
public toString(): string {
// The order of the characters should be as specified here to ensure correctness:
// https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-an-account-sas
// Use a string array instead of string concatenating += operator for performance
const permissions: string[] = [];
if (this.read) {
permissions.push("r");
}
if (this.write) {
permissions.push("w");
}
if (this.delete) {
permissions.push("d");
}
if (this.deleteVersion) {
permissions.push("x");
}
if (this.filter) {
permissions.push("f");
}
if (this.tag) {
permissions.push("t");
}
if (this.list) {
permissions.push("l");
}
if (this.add) {
permissions.push("a");
}
if (this.create) {
permissions.push("c");
}
if (this.update) {
permissions.push("u");
}
if (this.process) {
permissions.push("p");
}
return permissions.join("");
}
}
/**
* A type that looks like an account SAS permission.
* Used in {@link AccountSASPermissions} to parse SAS permissions from raw objects.
*/
export interface AccountSASPermissionsLike {
/**
* Permission to read resources and list queues and tables granted.
*
* @type {boolean}
* @memberof AccountSASPermissionsLike
*/
read?: boolean;
/**
* Permission to write resources granted.
*
* @type {boolean}
* @memberof AccountSASPermissionsLike
*/
write?: boolean;
/**
* Permission to create blobs and files granted.
*
* @type {boolean}
* @memberof AccountSASPermissionsLike
*/
delete?: boolean;
/**
* Permission to delete versions granted.
*
* @type {boolean}
* @memberof AccountSASPermissionsLike
*/
deleteVersion?: boolean;
/**
* Permission to list blob containers, blobs, shares, directories, and files granted.
*
* @type {boolean}
* @memberof AccountSASPermissionsLike
*/
list?: boolean;
/**
* Permission to add messages, table entities, and append to blobs granted.
*
* @type {boolean}
* @memberof AccountSASPermissionsLike
*/
add?: boolean;
/**
* Permission to create blobs and files granted.
*
* @type {boolean}
* @memberof AccountSASPermissionsLike
*/
create?: boolean;
/**
* Permissions to update messages and table entities granted.
*
* @type {boolean}
* @memberof AccountSASPermissionsLike
*/
update?: boolean;
/**
* Permission to get and delete messages granted.
*
* @type {boolean}
* @memberof AccountSASPermissionsLike
*/
process?: boolean;
/**
* Specfies Tag access granted.
*
* @type {boolean}
* @memberof AccountSASPermissionsLike
*/
tag?: boolean;
/**
* Permission to filter blobs.
*
* @type {boolean}
* @memberof AccountSASPermissionsLike
*/
filter?: boolean;
}