/
SASQueryParameters.ts
575 lines (533 loc) · 19.1 KB
/
SASQueryParameters.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { SasIPRange, ipRangeToString } from "./SasIPRange";
import { truncatedISO8061Date } from "../utils/utils.common";
import { UserDelegationKey } from "../BlobServiceClient";
/**
* Protocols for generated SAS.
*/
export enum SASProtocol {
/**
* Protocol that allows HTTPS only
*/
Https = "https",
/**
* Protocol that allows both HTTPS and HTTP
*/
HttpsAndHttp = "https,http",
}
/**
* Options to construct {@link SASQueryParameters}.
*/
export interface SASQueryParametersOptions {
/**
* Optional only when identifier is provided.
* Please refer to {@link AccountSASPermissions}, {@link BlobSASPermissions}, or {@link ContainerSASPermissions} for
* more details.
*/
permissions?: string;
/**
* Optional. The storage services being accessed (only for Account SAS). Please refer to {@link AccountSASServices}
* for more details.
*/
services?: string;
/**
* Optional. The storage resource types being accessed (only for Account SAS). Please refer to
* {@link AccountSASResourceTypes} for more details.
*/
resourceTypes?: string;
/**
* Optional. The allowed HTTP protocol(s).
*/
protocol?: SASProtocol;
/**
* Optional. The start time for this SAS token.
*/
startsOn?: Date;
/**
* Optional only when identifier is provided. The expiry time for this SAS token.
*/
expiresOn?: Date;
/**
* Optional. IP ranges allowed in this SAS.
*/
ipRange?: SasIPRange;
/**
* Optional. The signed identifier (only for {@link BlobSASSignatureValues}).
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/establishing-a-stored-access-policy
*/
identifier?: string;
/**
* Optional. Encryption scope to use when sending requests authorized with this SAS URI.
*/
encryptionScope?: string;
/**
* Optional. Specifies which resources are accessible via the SAS (only for {@link BlobSASSignatureValues}).
* @see https://docs.microsoft.com/rest/api/storageservices/create-service-sas#specifying-the-signed-resource-blob-service-only
*/
resource?: string;
/**
* Value for cache-control header in Blob/File Service SAS.
*/
cacheControl?: string;
/**
* Value for content-disposition header in Blob/File Service SAS.
*/
contentDisposition?: string;
/**
* Value for content-encoding header in Blob/File Service SAS.
*/
contentEncoding?: string;
/**
* Value for content-length header in Blob/File Service SAS.
*/
contentLanguage?: string;
/**
* Value for content-type header in Blob/File Service SAS.
*/
contentType?: string;
/**
* User delegation key properties.
*/
userDelegationKey?: UserDelegationKey;
/**
* Authorized AAD Object ID in GUID format. The AAD Object ID of a user authorized by the owner of the User Delegation Key
* to perform the action granted by the SAS. The Azure Storage service will ensure that the owner of the user delegation key
* has the required permissions before granting access but no additional permission check for the user specified in
* this value will be performed. This cannot be used in conjuction with {@link signedUnauthorizedUserObjectId}.
* This is only used for User Delegation SAS.
*/
preauthorizedAgentObjectId?: string;
/**
* A GUID value that will be logged in the storage diagnostic logs and can be used to correlate SAS generation with storage resource access.
* This is only used for User Delegation SAS.
*/
correlationId?: string;
}
/**
* Represents the components that make up an Azure Storage SAS' query parameters. This type is not constructed directly
* by the user; it is only generated by the {@link AccountSASSignatureValues} and {@link BlobSASSignatureValues}
* types. Once generated, it can be encoded into a {@link String} and appended to a URL directly (though caution should
* be taken here in case there are existing query parameters, which might affect the appropriate means of appending
* these query parameters).
*
* NOTE: Instances of this class are immutable.
*/
export class SASQueryParameters {
/**
* The storage API version.
*/
public readonly version: string;
/**
* Optional. The allowed HTTP protocol(s).
*/
public readonly protocol?: SASProtocol;
/**
* Optional. The start time for this SAS token.
*/
public readonly startsOn?: Date;
/**
* Optional only when identifier is provided. The expiry time for this SAS token.
*/
public readonly expiresOn?: Date;
/**
* Optional only when identifier is provided.
* Please refer to {@link AccountSASPermissions}, {@link BlobSASPermissions}, or {@link ContainerSASPermissions} for
* more details.
*/
public readonly permissions?: string;
/**
* Optional. The storage services being accessed (only for Account SAS). Please refer to {@link AccountSASServices}
* for more details.
*/
public readonly services?: string;
/**
* Optional. The storage resource types being accessed (only for Account SAS). Please refer to
* {@link AccountSASResourceTypes} for more details.
*/
public readonly resourceTypes?: string;
/**
* Optional. The signed identifier (only for {@link BlobSASSignatureValues}).
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/establishing-a-stored-access-policy
*/
public readonly identifier?: string;
/**
* Optional. Encryption scope to use when sending requests authorized with this SAS URI.
*/
public readonly encryptionScope?: string;
/**
* Optional. Specifies which resources are accessible via the SAS (only for {@link BlobSASSignatureValues}).
* @see https://docs.microsoft.com/rest/api/storageservices/create-service-sas#specifying-the-signed-resource-blob-service-only
*/
public readonly resource?: string;
/**
* The signature for the SAS token.
*/
public readonly signature: string;
/**
* Value for cache-control header in Blob/File Service SAS.
*/
public readonly cacheControl?: string;
/**
* Value for content-disposition header in Blob/File Service SAS.
*/
public readonly contentDisposition?: string;
/**
* Value for content-encoding header in Blob/File Service SAS.
*/
public readonly contentEncoding?: string;
/**
* Value for content-length header in Blob/File Service SAS.
*/
public readonly contentLanguage?: string;
/**
* Value for content-type header in Blob/File Service SAS.
*/
public readonly contentType?: string;
/**
* Inner value of getter ipRange.
*/
private readonly ipRangeInner?: SasIPRange;
/**
* The Azure Active Directory object ID in GUID format.
* Property of user delegation key.
*/
private readonly signedOid?: string;
/**
* The Azure Active Directory tenant ID in GUID format.
* Property of user delegation key.
*/
private readonly signedTenantId?: string;
/**
* The date-time the key is active.
* Property of user delegation key.
*/
private readonly signedStartsOn?: Date;
/**
* The date-time the key expires.
* Property of user delegation key.
*/
private readonly signedExpiresOn?: Date;
/**
* Abbreviation of the Azure Storage service that accepts the user delegation key.
* Property of user delegation key.
*/
private readonly signedService?: string;
/**
* The service version that created the user delegation key.
* Property of user delegation key.
*/
private readonly signedVersion?: string;
/**
* Authorized AAD Object ID in GUID format. The AAD Object ID of a user authorized by the owner of the User Delegation Key
* to perform the action granted by the SAS. The Azure Storage service will ensure that the owner of the user delegation key
* has the required permissions before granting access but no additional permission check for the user specified in
* this value will be performed. This is only used for User Delegation SAS.
*/
public readonly preauthorizedAgentObjectId?: string;
/**
* A GUID value that will be logged in the storage diagnostic logs and can be used to correlate SAS generation with storage resource access.
* This is only used for User Delegation SAS.
*/
public readonly correlationId?: string;
/**
* Optional. IP range allowed for this SAS.
*
* @readonly
*/
public get ipRange(): SasIPRange | undefined {
if (this.ipRangeInner) {
return {
end: this.ipRangeInner.end,
start: this.ipRangeInner.start,
};
}
return undefined;
}
/**
* Creates an instance of SASQueryParameters.
*
* @param version - Representing the storage version
* @param signature - Representing the signature for the SAS token
* @param permissions - Representing the storage permissions
* @param services - Representing the storage services being accessed (only for Account SAS)
* @param resourceTypes - Representing the storage resource types being accessed (only for Account SAS)
* @param protocol - Representing the allowed HTTP protocol(s)
* @param startsOn - Representing the start time for this SAS token
* @param expiresOn - Representing the expiry time for this SAS token
* @param ipRange - Representing the range of valid IP addresses for this SAS token
* @param identifier - Representing the signed identifier (only for Service SAS)
* @param resource - Representing the storage container or blob (only for Service SAS)
* @param cacheControl - Representing the cache-control header (only for Blob/File Service SAS)
* @param contentDisposition - Representing the content-disposition header (only for Blob/File Service SAS)
* @param contentEncoding - Representing the content-encoding header (only for Blob/File Service SAS)
* @param contentLanguage - Representing the content-language header (only for Blob/File Service SAS)
* @param contentType - Representing the content-type header (only for Blob/File Service SAS)
* @param userDelegationKey - Representing the user delegation key properties
* @param preauthorizedAgentObjectId - Representing the authorized AAD Object ID (only for User Delegation SAS)
* @param correlationId - Representing the correlation ID (only for User Delegation SAS)
* @param encryptionScope -
*/
constructor(
version: string,
signature: string,
permissions?: string,
services?: string,
resourceTypes?: string,
protocol?: SASProtocol,
startsOn?: Date,
expiresOn?: Date,
ipRange?: SasIPRange,
identifier?: string,
resource?: string,
cacheControl?: string,
contentDisposition?: string,
contentEncoding?: string,
contentLanguage?: string,
contentType?: string,
userDelegationKey?: UserDelegationKey,
preauthorizedAgentObjectId?: string,
correlationId?: string,
encryptionScope?: string
);
/**
* Creates an instance of SASQueryParameters.
*
* @param version - Representing the storage version
* @param signature - Representing the signature for the SAS token
* @param options - Optional. Options to construct the SASQueryParameters.
*/
constructor(version: string, signature: string, options?: SASQueryParametersOptions);
constructor(
version: string,
signature: string,
permissionsOrOptions?: string | SASQueryParametersOptions,
services?: string,
resourceTypes?: string,
protocol?: SASProtocol,
startsOn?: Date,
expiresOn?: Date,
ipRange?: SasIPRange,
identifier?: string,
resource?: string,
cacheControl?: string,
contentDisposition?: string,
contentEncoding?: string,
contentLanguage?: string,
contentType?: string,
userDelegationKey?: UserDelegationKey,
preauthorizedAgentObjectId?: string,
correlationId?: string,
encryptionScope?: string
) {
this.version = version;
this.signature = signature;
if (permissionsOrOptions !== undefined && typeof permissionsOrOptions !== "string") {
// SASQueryParametersOptions
this.permissions = permissionsOrOptions.permissions;
this.services = permissionsOrOptions.services;
this.resourceTypes = permissionsOrOptions.resourceTypes;
this.protocol = permissionsOrOptions.protocol;
this.startsOn = permissionsOrOptions.startsOn;
this.expiresOn = permissionsOrOptions.expiresOn;
this.ipRangeInner = permissionsOrOptions.ipRange;
this.identifier = permissionsOrOptions.identifier;
this.encryptionScope = permissionsOrOptions.encryptionScope;
this.resource = permissionsOrOptions.resource;
this.cacheControl = permissionsOrOptions.cacheControl;
this.contentDisposition = permissionsOrOptions.contentDisposition;
this.contentEncoding = permissionsOrOptions.contentEncoding;
this.contentLanguage = permissionsOrOptions.contentLanguage;
this.contentType = permissionsOrOptions.contentType;
if (permissionsOrOptions.userDelegationKey) {
this.signedOid = permissionsOrOptions.userDelegationKey.signedObjectId;
this.signedTenantId = permissionsOrOptions.userDelegationKey.signedTenantId;
this.signedStartsOn = permissionsOrOptions.userDelegationKey.signedStartsOn;
this.signedExpiresOn = permissionsOrOptions.userDelegationKey.signedExpiresOn;
this.signedService = permissionsOrOptions.userDelegationKey.signedService;
this.signedVersion = permissionsOrOptions.userDelegationKey.signedVersion;
this.preauthorizedAgentObjectId = permissionsOrOptions.preauthorizedAgentObjectId;
this.correlationId = permissionsOrOptions.correlationId;
}
} else {
this.services = services;
this.resourceTypes = resourceTypes;
this.expiresOn = expiresOn;
this.permissions = permissionsOrOptions;
this.protocol = protocol;
this.startsOn = startsOn;
this.ipRangeInner = ipRange;
this.encryptionScope = encryptionScope;
this.identifier = identifier;
this.resource = resource;
this.cacheControl = cacheControl;
this.contentDisposition = contentDisposition;
this.contentEncoding = contentEncoding;
this.contentLanguage = contentLanguage;
this.contentType = contentType;
if (userDelegationKey) {
this.signedOid = userDelegationKey.signedObjectId;
this.signedTenantId = userDelegationKey.signedTenantId;
this.signedStartsOn = userDelegationKey.signedStartsOn;
this.signedExpiresOn = userDelegationKey.signedExpiresOn;
this.signedService = userDelegationKey.signedService;
this.signedVersion = userDelegationKey.signedVersion;
this.preauthorizedAgentObjectId = preauthorizedAgentObjectId;
this.correlationId = correlationId;
}
}
}
/**
* Encodes all SAS query parameters into a string that can be appended to a URL.
*
*/
public toString(): string {
const params: string[] = [
"sv",
"ss",
"srt",
"spr",
"st",
"se",
"sip",
"si",
"ses",
"skoid", // Signed object ID
"sktid", // Signed tenant ID
"skt", // Signed key start time
"ske", // Signed key expiry time
"sks", // Signed key service
"skv", // Signed key version
"sr",
"sp",
"sig",
"rscc",
"rscd",
"rsce",
"rscl",
"rsct",
"saoid",
"scid",
];
const queries: string[] = [];
for (const param of params) {
switch (param) {
case "sv":
this.tryAppendQueryParameter(queries, param, this.version);
break;
case "ss":
this.tryAppendQueryParameter(queries, param, this.services);
break;
case "srt":
this.tryAppendQueryParameter(queries, param, this.resourceTypes);
break;
case "spr":
this.tryAppendQueryParameter(queries, param, this.protocol);
break;
case "st":
this.tryAppendQueryParameter(
queries,
param,
this.startsOn ? truncatedISO8061Date(this.startsOn, false) : undefined
);
break;
case "se":
this.tryAppendQueryParameter(
queries,
param,
this.expiresOn ? truncatedISO8061Date(this.expiresOn, false) : undefined
);
break;
case "sip":
this.tryAppendQueryParameter(
queries,
param,
this.ipRange ? ipRangeToString(this.ipRange) : undefined
);
break;
case "si":
this.tryAppendQueryParameter(queries, param, this.identifier);
break;
case "ses":
this.tryAppendQueryParameter(queries, param, this.encryptionScope);
break;
case "skoid": // Signed object ID
this.tryAppendQueryParameter(queries, param, this.signedOid);
break;
case "sktid": // Signed tenant ID
this.tryAppendQueryParameter(queries, param, this.signedTenantId);
break;
case "skt": // Signed key start time
this.tryAppendQueryParameter(
queries,
param,
this.signedStartsOn ? truncatedISO8061Date(this.signedStartsOn, false) : undefined
);
break;
case "ske": // Signed key expiry time
this.tryAppendQueryParameter(
queries,
param,
this.signedExpiresOn ? truncatedISO8061Date(this.signedExpiresOn, false) : undefined
);
break;
case "sks": // Signed key service
this.tryAppendQueryParameter(queries, param, this.signedService);
break;
case "skv": // Signed key version
this.tryAppendQueryParameter(queries, param, this.signedVersion);
break;
case "sr":
this.tryAppendQueryParameter(queries, param, this.resource);
break;
case "sp":
this.tryAppendQueryParameter(queries, param, this.permissions);
break;
case "sig":
this.tryAppendQueryParameter(queries, param, this.signature);
break;
case "rscc":
this.tryAppendQueryParameter(queries, param, this.cacheControl);
break;
case "rscd":
this.tryAppendQueryParameter(queries, param, this.contentDisposition);
break;
case "rsce":
this.tryAppendQueryParameter(queries, param, this.contentEncoding);
break;
case "rscl":
this.tryAppendQueryParameter(queries, param, this.contentLanguage);
break;
case "rsct":
this.tryAppendQueryParameter(queries, param, this.contentType);
break;
case "saoid":
this.tryAppendQueryParameter(queries, param, this.preauthorizedAgentObjectId);
break;
case "scid":
this.tryAppendQueryParameter(queries, param, this.correlationId);
break;
}
}
return queries.join("&");
}
/**
* A private helper method used to filter and append query key/value pairs into an array.
*
* @param queries -
* @param key -
* @param value -
*/
private tryAppendQueryParameter(queries: string[], key: string, value?: string): void {
if (!value) {
return;
}
key = encodeURIComponent(key);
value = encodeURIComponent(value);
if (key.length > 0 && value.length > 0) {
queries.push(`${key}=${value}`);
}
}
}