@@ -32,6 +32,8 @@ namespace Microsoft.WindowsAzure.Commands.Storage.Common
32
32
using global ::Azure . Storage . Files . DataLake ;
33
33
using global ::Azure . Storage . Files . Shares ;
34
34
using global ::Azure . Storage . Files . Shares . Models ;
35
+ using global ::Azure . Storage . Queues . Models ;
36
+ using global ::Azure . Storage . Queues ;
35
37
36
38
internal class SasTokenHelper
37
39
{
@@ -266,7 +268,7 @@ public static BlobSignedIdentifier GetBlobSignedIdentifier(BlobContainerClient c
266
268
}
267
269
268
270
/// <summary>
269
- /// Get a ShareSignedIdentifier from contaienr with a specific Id
271
+ /// Get a ShareSignedIdentifier from share with a specific Id
270
272
/// </summary>
271
273
public static ShareSignedIdentifier GetShareSignedIdentifier ( ShareClient share , string identifierId , CancellationToken cancellationToken )
272
274
{
@@ -281,10 +283,25 @@ public static ShareSignedIdentifier GetShareSignedIdentifier(ShareClient share,
281
283
throw new ArgumentException ( string . Format ( Resources . InvalidAccessPolicy , identifierId ) ) ;
282
284
}
283
285
286
+ /// <summary>
287
+ /// Get a QueueSignedIdentifier from queue with a specific id
288
+ /// </summary>
289
+ public static QueueSignedIdentifier GetQueueSignedIdentifier ( QueueClient queue , string identifierId , CancellationToken cancellationToken )
290
+ {
291
+ IEnumerable < QueueSignedIdentifier > signedIdentifiers = queue . GetAccessPolicy ( cancellationToken : cancellationToken ) . Value ;
292
+ foreach ( QueueSignedIdentifier identifier in signedIdentifiers )
293
+ {
294
+ if ( identifier . Id == identifierId )
295
+ {
296
+ return identifier ;
297
+ }
298
+ }
299
+ throw new ArgumentException ( string . Format ( Resources . InvalidAccessPolicy , identifierId ) ) ;
300
+ }
284
301
285
302
286
303
/// <summary>
287
- /// Create a blob SAS build from Blob Object
304
+ /// Create a share SAS build from file Object
288
305
/// </summary>
289
306
public static ShareSasBuilder SetShareSasBuilder_FromFile ( ShareFileClient file ,
290
307
ShareSignedIdentifier signedIdentifier = null ,
@@ -306,7 +323,7 @@ public static ShareSasBuilder SetShareSasBuilder_FromFile(ShareFileClient file,
306
323
}
307
324
308
325
/// <summary>
309
- /// Create a blob SAS build from container Object
326
+ /// Create a share SAS build from share Object
310
327
/// </summary>
311
328
public static ShareSasBuilder SetShareSasBuilder_FromShare ( ShareClient share ,
312
329
ShareSignedIdentifier signedIdentifier = null ,
@@ -328,7 +345,115 @@ public static ShareSasBuilder SetShareSasBuilder_FromShare(ShareClient share,
328
345
}
329
346
330
347
/// <summary>
331
- /// Create a blob SAS build from Blob Object
348
+ /// Create a Queue SAS builder
349
+ /// </summary>
350
+ public static QueueSasBuilder SetQueueSasbuilder ( QueueClient queue ,
351
+ QueueSignedIdentifier signedIdentifier = null ,
352
+ string permission = null ,
353
+ DateTime ? startTime = null ,
354
+ DateTime ? expiryTime = null ,
355
+ string iPAddressOrRange = null ,
356
+ SharedAccessProtocol ? protocol = null )
357
+ {
358
+ QueueSasBuilder sasBuilder = new QueueSasBuilder
359
+ {
360
+ QueueName = queue . Name ,
361
+ } ;
362
+
363
+ if ( signedIdentifier != null )
364
+ {
365
+ sasBuilder . Identifier = signedIdentifier . Id ;
366
+
367
+ if ( startTime != null )
368
+ {
369
+ if ( signedIdentifier . AccessPolicy . StartsOn != DateTimeOffset . MinValue && signedIdentifier . AccessPolicy . StartsOn != null )
370
+ {
371
+ throw new InvalidOperationException ( Resources . SignedStartTimeMustBeOmitted ) ;
372
+ }
373
+ else
374
+ {
375
+ sasBuilder . StartsOn = startTime . Value . ToUniversalTime ( ) ;
376
+ }
377
+ }
378
+ if ( expiryTime != null )
379
+ {
380
+ if ( signedIdentifier . AccessPolicy . ExpiresOn != DateTimeOffset . MinValue && signedIdentifier . AccessPolicy . ExpiresOn != null )
381
+ {
382
+ throw new ArgumentException ( Resources . SignedExpiryTimeMustBeOmitted ) ;
383
+ }
384
+ else
385
+ {
386
+ sasBuilder . ExpiresOn = expiryTime . Value . ToUniversalTime ( ) ;
387
+ }
388
+ }
389
+ // Set up expiry time if it is not set by user input or the policy
390
+ else if ( signedIdentifier . AccessPolicy . ExpiresOn == DateTimeOffset . MinValue || signedIdentifier . AccessPolicy . ExpiresOn == null )
391
+ {
392
+ if ( sasBuilder . StartsOn != DateTimeOffset . MinValue && sasBuilder . StartsOn != null )
393
+ {
394
+ sasBuilder . ExpiresOn = sasBuilder . StartsOn . ToUniversalTime ( ) . AddHours ( 1 ) ;
395
+ }
396
+ else
397
+ {
398
+ sasBuilder . ExpiresOn = DateTimeOffset . UtcNow . AddHours ( 1 ) ;
399
+ }
400
+ }
401
+ if ( permission != null )
402
+ {
403
+ if ( signedIdentifier . AccessPolicy . Permissions != null )
404
+ {
405
+ throw new ArgumentException ( Resources . SignedPermissionsMustBeOmitted ) ;
406
+ }
407
+ else
408
+ {
409
+ sasBuilder . SetPermissions ( permission , true ) ;
410
+ }
411
+ }
412
+ }
413
+ else
414
+ {
415
+ sasBuilder . SetPermissions ( permission , true ) ;
416
+
417
+ if ( startTime != null )
418
+ {
419
+ sasBuilder . StartsOn = startTime . Value . ToUniversalTime ( ) ;
420
+ }
421
+ if ( expiryTime != null )
422
+ {
423
+ sasBuilder . ExpiresOn = expiryTime . Value . ToUniversalTime ( ) ;
424
+ }
425
+ else
426
+ {
427
+ if ( sasBuilder . StartsOn != DateTimeOffset . MinValue )
428
+ {
429
+ sasBuilder . ExpiresOn = sasBuilder . StartsOn . AddHours ( 1 ) . ToUniversalTime ( ) ;
430
+ }
431
+ else
432
+ {
433
+ sasBuilder . ExpiresOn = DateTimeOffset . UtcNow . AddHours ( 1 ) ;
434
+ }
435
+ }
436
+ }
437
+ if ( iPAddressOrRange != null )
438
+ {
439
+ sasBuilder . IPRange = Util . SetupIPAddressOrRangeForSASTrack2 ( iPAddressOrRange ) ;
440
+ }
441
+ if ( protocol != null )
442
+ {
443
+ if ( protocol . Value == SharedAccessProtocol . HttpsOrHttp )
444
+ {
445
+ sasBuilder . Protocol = SasProtocol . HttpsAndHttp ;
446
+ }
447
+ else //HttpsOnly
448
+ {
449
+ sasBuilder . Protocol = SasProtocol . Https ;
450
+ }
451
+ }
452
+ return sasBuilder ;
453
+ }
454
+
455
+ /// <summary>
456
+ /// Create a share SAS builder
332
457
/// </summary>
333
458
public static ShareSasBuilder SetShareSasBuilder ( string shareName ,
334
459
string filePath = null ,
@@ -373,7 +498,7 @@ public static ShareSasBuilder SetShareSasBuilder(string shareName,
373
498
sasBuilder . ExpiresOn = ExpiryTime . Value . ToUniversalTime ( ) ;
374
499
}
375
500
}
376
- else if ( signedIdentifier . AccessPolicy . PolicyExpiresOn == DateTimeOffset . MinValue && signedIdentifier . AccessPolicy . PolicyExpiresOn ! = null )
501
+ else if ( signedIdentifier . AccessPolicy . PolicyExpiresOn == DateTimeOffset . MinValue || signedIdentifier . AccessPolicy . PolicyExpiresOn = = null )
377
502
{
378
503
if ( sasBuilder . StartsOn != DateTimeOffset . MinValue && sasBuilder . StartsOn != null )
379
504
{
@@ -458,6 +583,21 @@ public static string GetFileSharedAccessSignature(AzureStorageContext context, S
458
583
}
459
584
}
460
585
586
+ /// <summary>
587
+ /// Get Queue SAS string
588
+ /// </summary>
589
+ public static string GetQueueSharedAccessSignature ( AzureStorageContext context , QueueSasBuilder sasBuilder , CancellationToken cancellationToken )
590
+ {
591
+ if ( context != null && context . StorageAccount != null && context . StorageAccount . Credentials != null && context . StorageAccount . Credentials . IsSharedKey )
592
+ {
593
+ return sasBuilder . ToSasQueryParameters ( new StorageSharedKeyCredential ( context . StorageAccountName , context . StorageAccount . Credentials . ExportBase64EncodedKey ( ) ) ) . ToString ( ) ;
594
+ }
595
+ else
596
+ {
597
+ throw new InvalidOperationException ( "Create Queue service SAS only supported with SharedKey credentail." ) ;
598
+ }
599
+ }
600
+
461
601
462
602
/// <summary>
463
603
/// Create a blob SAS build from Blob Object
@@ -561,7 +701,7 @@ public static BlobSasBuilder SetBlobSasBuilder(string containerName,
561
701
sasBuilder . ExpiresOn = ExpiryTime . Value . ToUniversalTime ( ) ;
562
702
}
563
703
}
564
- else if ( signedIdentifier . AccessPolicy . PolicyExpiresOn == DateTimeOffset . MinValue && signedIdentifier . AccessPolicy . PolicyExpiresOn ! = null )
704
+ else if ( signedIdentifier . AccessPolicy . PolicyExpiresOn == DateTimeOffset . MinValue || signedIdentifier . AccessPolicy . PolicyExpiresOn = = null )
565
705
{
566
706
if ( sasBuilder . StartsOn != DateTimeOffset . MinValue && sasBuilder . StartsOn != null )
567
707
{
@@ -741,7 +881,7 @@ public static string GetDatalakeGen2SharedAccessSignature(AzureStorageContext co
741
881
}
742
882
743
883
/// <summary>
744
- /// Create a blob SAS build from Blob Object
884
+ /// Create a account SAS builder
745
885
/// </summary>
746
886
public static AccountSasBuilder SetAccountSasBuilder ( SharedAccessAccountServices Service ,
747
887
SharedAccessAccountResourceTypes type ,
0 commit comments