/
SASQueryParameters.ts
748 lines (708 loc) · 23 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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { UserDelegationKey } from "../models";
import { ipRangeToString, SasIPRange } from "./SasIPRange";
import { truncatedISO8061Date } from "../utils/utils.common";
/**
* Protocols for generated SAS.
*
* @export
* @enum {number}
*/
export enum SASProtocol {
/**
* Protocol that allows HTTPS only
*/
Https = "https",
/**
* Protocol that allows both HTTPS and HTTP
*/
HttpsAndHttp = "https,http"
}
/**
* Options interfac to construct {@link SASQueryParameters}.
*
* @export
* @interface SASQueryParametersOptions
*/
export interface SASQueryParametersOptions {
/**
* Optional only when identifier is provided.
* Please refer to {@link AccountSASPermissions}, {@link BlobSASPermissions}, or {@link ContainerSASPermissions} for
* more details.
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
permissions?: string;
/**
* Optional. The storage services being accessed (only for Account SAS). Please refer to {@link AccountSASServices}
* for more details.
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
services?: string;
/**
* Optional. The storage resource types being accessed (only for Account SAS). Please refer to
* {@link AccountSASResourceTypes} for more details.
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
resourceTypes?: string;
/**
* Optional. The allowed HTTP protocol(s).
*
* @type {SASProtocol}
* @memberof SASQueryParametersOptions
*/
protocol?: SASProtocol;
/**
* Optional. The start time for this SAS token.
*
* @type {Date}
* @memberof SASQueryParametersOptions
*/
startsOn?: Date;
/**
* Optional only when identifier is provided. The expiry time for this SAS token.
*
* @type {Date}
* @memberof SASQueryParametersOptions
*/
expiresOn?: Date;
/**
* Optional. IP ranges allowed in this SAS.
*
* @type {SasIPRange}
* @memberof SASQueryParametersOptions
*/
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
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
identifier?: string;
/**
* Optional. The storage container or blob (only for {@link BlobSASSignatureValues}).
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
resource?: string;
/**
* Value for cache-control header in Blob/File Service SAS.
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
cacheControl?: string;
/**
* Value for content-disposition header in Blob/File Service SAS.
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
contentDisposition?: string;
/**
* Value for content-encoding header in Blob/File Service SAS.
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
contentEncoding?: string;
/**
* Value for content-length header in Blob/File Service SAS.
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
contentLanguage?: string;
/**
* Value for content-type header in Blob/File Service SAS.
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
contentType?: string;
/**
* User delegation key properties.
*
* @type {UserDelegationKey}
* @memberof SASQueryParametersOptions
*/
userDelegationKey?: UserDelegationKey;
/**
* Indicate the depth of the directory specified in the canonicalizedresource field of the string-to-sign.
* The depth of the directory is the number of directories beneath the root folder.
*
* @type {number}
* @memberof SASQueryParametersOptions
*/
directoryDepth?: number;
/**
* 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}.
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
preauthorizedAgentObjectId?: string;
/**
* Unauthorized AAD Object Id in GUID format. The AAD Object Id of a user that is assumed to be unauthorized by the owner of the User Delegation Key.
* The Azure Storage Service will perform an additional POSIX ACL check to determine if the user is authorized to perform the requested operation.
* This cannot be used in conjuction with {@link signedAuthorizedUserObjectId}.
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
agentObjectId?: 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.
*
* @type {string}
* @memberof SASQueryParametersOptions
*/
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 {@code 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
*/
export class SASQueryParameters {
/**
* The storage API version.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly version: string;
/**
* Optional. The allowed HTTP protocol(s).
*
* @type {SASProtocol}
* @memberof SASQueryParameters
*/
public readonly protocol?: SASProtocol;
/**
* Optional. The start time for this SAS token.
*
* @type {Date}
* @memberof SASQueryParameters
*/
public readonly startsOn?: Date;
/**
* Optional only when identifier is provided. The expiry time for this SAS token.
*
* @type {Date}
* @memberof SASQueryParameters
*/
public readonly expiresOn?: Date;
/**
* Optional only when identifier is provided.
* Please refer to {@link AccountSASPermissions}, {@link BlobSASPermissions}, or {@link ContainerSASPermissions} for
* more details.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly permissions?: string;
/**
* Optional. The storage services being accessed (only for Account SAS). Please refer to {@link AccountSASServices}
* for more details.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly services?: string;
/**
* Optional. The storage resource types being accessed (only for Account SAS). Please refer to
* {@link AccountSASResourceTypes} for more details.
*
* @type {string}
* @memberof SASQueryParameters
*/
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
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly identifier?: string;
/**
* Optional. The storage container or blob (only for {@link BlobSASSignatureValues}).
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly resource?: string;
/**
* The signature for the SAS token.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly signature: string;
/**
* Value for cache-control header in Blob/File Service SAS.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly cacheControl?: string;
/**
* Value for content-disposition header in Blob/File Service SAS.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly contentDisposition?: string;
/**
* Value for content-encoding header in Blob/File Service SAS.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly contentEncoding?: string;
/**
* Value for content-length header in Blob/File Service SAS.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly contentLanguage?: string;
/**
* Value for content-type header in Blob/File Service SAS.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly contentType?: string;
/**
* Inner value of getter ipRange.
*
* @private
* @type {SasIPRange}
* @memberof SASQueryParameters
*/
private readonly ipRangeInner?: SasIPRange;
/**
* The Azure Active Directory object ID in GUID format.
* Property of user delegation key.
*
* @private
* @type {string}
* @memberof SASQueryParameters
*/
private readonly signedOid?: string;
/**
* The Azure Active Directory tenant ID in GUID format.
* Property of user delegation key.
*
* @private
* @type {string}
* @memberof SASQueryParameters
*/
private readonly signedTenantId?: string;
/**
* The date-time the key is active.
* Property of user delegation key.
*
* @private
* @type {Date}
* @memberof SASQueryParameters
*/
private readonly signedStartsOn?: Date;
/**
* The date-time the key expires.
* Property of user delegation key.
*
* @private
* @type {Date}
* @memberof SASQueryParameters
*/
private readonly signedExpiresOn?: Date;
/**
* Abbreviation of the Azure Storage service that accepts the user delegation key.
* Property of user delegation key.
*
* @private
* @type {string}
* @memberof SASQueryParameters
*/
private readonly signedService?: string;
/**
* The service version that created the user delegation key.
* Property of user delegation key.
*
* @private
* @type {string}
* @memberof SASQueryParameters
*/
private readonly signedVersion?: string;
/**
* Indicate the depth of the directory specified in the canonicalizedresource field of the string-to-sign.
* The depth of the directory is the number of directories beneath the root folder.
*
* @type {number}
* @memberof SASQueryParameters
*/
public readonly directoryDepth?: number;
/**
* 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}.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly preauthorizedAgentObjectId?: string;
/**
* Unauthorized AAD Object Id in GUID format. The AAD Object Id of a user that is assumed to be unauthorized by the owner of the User Delegation Key.
* The Azure Storage Service will perform an additional POSIX ACL check to determine if the user is authorized to perform the requested operation.
* This cannot be used in conjuction with {@link signedAuthorizedUserObjectId}.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly agentObjectId?: 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.
*
* @type {string}
* @memberof SASQueryParameters
*/
public readonly correlationId?: string;
/**
* Optional. IP range allowed for this SAS.
*
* @readonly
* @type {(SasIPRange | undefined)}
* @memberof SASQueryParameters
*/
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 {string} version Representing the storage version
* @param {string} signature Representing the signature for the SAS token
* @param {string} [permissions] Representing the storage permissions
* @param {string} [services] Representing the storage services being accessed (only for Account SAS)
* @param {string} [resourceTypes] Representing the storage resource types being accessed (only for Account SAS)
* @param {SASProtocol} [protocol] Representing the allowed HTTP protocol(s)
* @param {Date} [startsOn] Representing the start time for this SAS token
* @param {Date} [expiresOn] Representing the expiry time for this SAS token
* @param {SasIPRange} [ipRange] Representing the range of valid IP addresses for this SAS token
* @param {string} [identifier] Representing the signed identifier (only for Service SAS)
* @param {string} [resource] Representing the storage container or blob (only for Service SAS)
* @param {string} [cacheControl] Representing the cache-control header (only for Blob/File Service SAS)
* @param {string} [contentDisposition] Representing the content-disposition header (only for Blob/File Service SAS)
* @param {string} [contentEncoding] Representing the content-encoding header (only for Blob/File Service SAS)
* @param {string} [contentLanguage] Representing the content-language header (only for Blob/File Service SAS)
* @param {string} [contentType] Representing the content-type header (only for Blob/File Service SAS)
* @param {userDelegationKey} [userDelegationKey] Representing the user delegation key properties
* @memberof SASQueryParameters
*/
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,
directoryDepth?: number,
preauthorizedAgentObjectId?: string,
agentObjectId?: string,
correlationId?: string
);
/**
* Creates an instance of SASQueryParameters.
*
* @param {string} version Representing the storage version
* @param {string} signature Representing the signature for the SAS token
* @param {SASQueryParametersOptions} [options] Optional. Options to construct the SASQueryParameters.
* @memberof 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,
directoryDepth?: number,
preauthorizedAgentObjectId?: string,
agentObjectId?: string,
correlationId?: string
) {
this.version = version;
this.signature = signature;
if (permissionsOrOptions !== undefined && typeof permissionsOrOptions !== "string") {
// SASQueryParametersOptions
const options = permissionsOrOptions;
this.services = options.services;
this.resourceTypes = options.resourceTypes;
this.expiresOn = options.expiresOn;
this.permissions = options.permissions;
this.protocol = options.protocol;
this.startsOn = options.startsOn;
this.ipRangeInner = options.ipRange;
this.identifier = options.identifier;
this.resource = options.resource;
this.cacheControl = options.cacheControl;
this.contentDisposition = options.contentDisposition;
this.contentEncoding = options.contentEncoding;
this.contentLanguage = options.contentLanguage;
this.contentType = options.contentType;
this.directoryDepth = options.directoryDepth;
this.preauthorizedAgentObjectId = options.preauthorizedAgentObjectId;
this.agentObjectId = options.agentObjectId;
this.correlationId = options.correlationId;
if (options.userDelegationKey) {
this.signedOid = options.userDelegationKey.signedObjectId;
this.signedTenantId = options.userDelegationKey.signedTenantId;
this.signedStartsOn = options.userDelegationKey.signedStartsOn;
this.signedExpiresOn = options.userDelegationKey.signedExpiresOn;
this.signedService = options.userDelegationKey.signedService;
this.signedVersion = options.userDelegationKey.signedVersion;
}
} else {
this.services = services;
this.resourceTypes = resourceTypes;
this.expiresOn = expiresOn;
this.permissions = permissionsOrOptions;
this.protocol = protocol;
this.startsOn = startsOn;
this.ipRangeInner = ipRange;
this.identifier = identifier;
this.resource = resource;
this.cacheControl = cacheControl;
this.contentDisposition = contentDisposition;
this.contentEncoding = contentEncoding;
this.contentLanguage = contentLanguage;
this.contentType = contentType;
this.directoryDepth = directoryDepth;
this.preauthorizedAgentObjectId = preauthorizedAgentObjectId;
this.agentObjectId = agentObjectId;
this.correlationId = correlationId;
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;
}
}
}
/**
* Encodes all SAS query parameters into a string that can be appended to a URL.
*
* @returns {string}
* @memberof SASQueryParameters
*/
public toString(): string {
const params: string[] = [
"sv",
"ss",
"srt",
"spr",
"st",
"se",
"sip",
"si",
"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",
"sdd",
"saoid",
"suoid",
"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 "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 "sdd":
this.tryAppendQueryParameter(queries, param, this.directoryDepth?.toString());
break;
case "saoid":
this.tryAppendQueryParameter(queries, param, this.preauthorizedAgentObjectId);
break;
case "suoid":
this.tryAppendQueryParameter(queries, param, this.agentObjectId);
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.
*
* @private
* @param {string[]} queries
* @param {string} key
* @param {string} [value]
* @returns {void}
* @memberof SASQueryParameters
*/
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}`);
}
}
}