/// <summary>
        /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
        /// shared access signature values to produce the proper SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// The storage account's <see cref="StorageSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// The <see cref="DataLakeSasQueryParameters"/> used for authenticating
        /// requests.
        /// </returns>
        public DataLakeSasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            EnsureState();

            var startTime  = SasExtensions.FormatTimesForSasSigning(StartsOn);
            var expiryTime = SasExtensions.FormatTimesForSasSigning(ExpiresOn);

            // See http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            var stringToSign = String.Join("\n",
                                           Permissions,
                                           startTime,
                                           expiryTime,
                                           GetCanonicalName(sharedKeyCredential.AccountName, FileSystemName ?? String.Empty, Path ?? String.Empty),
                                           Identifier,
                                           IPRange.ToString(),
                                           SasExtensions.ToProtocolString(Protocol),
                                           Version,
                                           Resource,
                                           null, // snapshot
                                           CacheControl,
                                           ContentDisposition,
                                           ContentEncoding,
                                           ContentLanguage,
                                           ContentType);

            var signature = StorageSharedKeyCredentialInternals.ComputeSasSignature(sharedKeyCredential, stringToSign);

            var p = new DataLakeSasQueryParameters(
                version: Version,
                services: default,
        /// <summary>
        /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
        /// shared access signature values to produce the proper SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// The storage account's <see cref="StorageSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// The <see cref="SasQueryParameters"/> used for authenticating requests.
        /// </returns>
        public SasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            string resource;

            if (string.IsNullOrEmpty(FilePath))
            {
                // Make sure the permission characters are in the correct order
                Permissions = ShareSasPermissions.Parse(Permissions).ToString();
                resource    = Constants.Sas.Resource.Share;
            }
            else
            {
                // Make sure the permission characters are in the correct order
                Permissions = FileSasPermissions.Parse(Permissions).ToString();
                resource    = Constants.Sas.Resource.File;
            }

            if (string.IsNullOrEmpty(Version))
            {
                Version = SasQueryParameters.DefaultSasVersion;
            }

            var startTime  = SasQueryParameters.FormatTimesForSasSigning(StartTime);
            var expiryTime = SasQueryParameters.FormatTimesForSasSigning(ExpiryTime);

            // String to sign: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            var stringToSign = string.Join("\n",
                                           Permissions,
                                           startTime,
                                           expiryTime,
                                           GetCanonicalName(sharedKeyCredential.AccountName, ShareName ?? string.Empty, FilePath ?? string.Empty),
                                           Identifier,
                                           IPRange.ToString(),
                                           Protocol.ToString(),
                                           Version,
                                           CacheControl,
                                           ContentDisposition,
                                           ContentEncoding,
                                           ContentLanguage,
                                           ContentType);

            var signature = sharedKeyCredential.ComputeHMACSHA256(stringToSign);

            var p = new SasQueryParameters(
                version: Version,
                services: null,
                resourceTypes: null,
                protocol: Protocol,
                startTime: StartTime,
                expiryTime: ExpiryTime,
                ipRange: IPRange,
                identifier: Identifier,
                resource: resource,
                permissions: Permissions,
                signature: signature);

            return(p);
        }
Example #3
0
        /// <summary>
        /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
        /// shared access signature values to produce the proper SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// The storage account's <see cref="StorageSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// The <see cref="SasQueryParameters"/> used for authenticating
        /// requests.
        /// </returns>
        public SasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));
            if (ExpiresOn == default)
            {
                throw Errors.SasMissingData(nameof(ExpiresOn));
            }
            if (string.IsNullOrEmpty(Permissions))
            {
                throw Errors.SasMissingData(nameof(Permissions));
            }
            if (string.IsNullOrEmpty(Version))
            {
                Version = SasQueryParameters.DefaultSasVersion;
            }

            var startTime  = SasExtensions.FormatTimesForSasSigning(StartsOn);
            var expiryTime = SasExtensions.FormatTimesForSasSigning(ExpiresOn);

            // String to sign: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            var stringToSign = string.Join("\n",
                                           Permissions,
                                           startTime,
                                           expiryTime,
                                           GetCanonicalName(sharedKeyCredential.AccountName, QueueName ?? string.Empty),
                                           Identifier,
                                           IPRange.ToString(),
                                           SasExtensions.ToProtocolString(Protocol),
                                           Version);
            var signature = StorageSharedKeyCredentialInternals.ComputeSasSignature(sharedKeyCredential, stringToSign);
            var p         = SasQueryParametersInternals.Create(
                version: Version,
                services: default,
        /// <summary>
        /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
        /// shared access signature values to produce the proper SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// The storage account's <see cref="StorageSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// The <see cref="BlobSasQueryParameters"/> used for authenticating
        /// requests.
        /// </returns>
        public BlobSasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            EnsureState();

            var startTime  = SasQueryParameters.FormatTimesForSasSigning(StartsOn);
            var expiryTime = SasQueryParameters.FormatTimesForSasSigning(ExpiresOn);

            // See http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            var stringToSign = String.Join("\n",
                                           Permissions,
                                           startTime,
                                           expiryTime,
                                           GetCanonicalName(sharedKeyCredential.AccountName, BlobContainerName ?? String.Empty, BlobName ?? String.Empty),
                                           Identifier,
                                           IPRange.ToString(),
                                           Protocol.ToProtocolString(),
                                           Version,
                                           Resource,
                                           Snapshot,
                                           CacheControl,
                                           ContentDisposition,
                                           ContentEncoding,
                                           ContentLanguage,
                                           ContentType);

            var signature = sharedKeyCredential.ComputeHMACSHA256(stringToSign);

            var p = new BlobSasQueryParameters(
                version: Version,
                services: default,
        /// <summary>
        /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
        /// shared access signature values to produce the proper SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// The storage account's <see cref="StorageSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// The <see cref="DataLakeSasQueryParameters"/> used for authenticating
        /// requests.
        /// </returns>
        public DataLakeSasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            EnsureState();

            string startTime  = SasExtensions.FormatTimesForSasSigning(StartsOn);
            string expiryTime = SasExtensions.FormatTimesForSasSigning(ExpiresOn);

            // See http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            string stringToSign;

            // TODO https://github.com/Azure/azure-sdk-for-net/issues/23369
            if (SasQueryParametersInternals.DefaultSasVersionInternal == "2020-12-06")
            {
                stringToSign = string.Join("\n",
                                           Permissions,
                                           startTime,
                                           expiryTime,
                                           GetCanonicalName(sharedKeyCredential.AccountName, FileSystemName ?? string.Empty, Path ?? string.Empty),
                                           Identifier,
                                           IPRange.ToString(),
                                           SasExtensions.ToProtocolString(Protocol),
                                           Version,
                                           Resource,
                                           null, // snapshot
                                           null, // encryption scope
                                           CacheControl,
                                           ContentDisposition,
                                           ContentEncoding,
                                           ContentLanguage,
                                           ContentType);
            }
            else
            {
                stringToSign = string.Join("\n",
                                           Permissions,
                                           startTime,
                                           expiryTime,
                                           GetCanonicalName(sharedKeyCredential.AccountName, FileSystemName ?? string.Empty, Path ?? string.Empty),
                                           Identifier,
                                           IPRange.ToString(),
                                           SasExtensions.ToProtocolString(Protocol),
                                           Version,
                                           Resource,
                                           null, // snapshot
                                           CacheControl,
                                           ContentDisposition,
                                           ContentEncoding,
                                           ContentLanguage,
                                           ContentType);
            }

            string signature = StorageSharedKeyCredentialInternals.ComputeSasSignature(sharedKeyCredential, stringToSign);

            DataLakeSasQueryParameters p = new DataLakeSasQueryParameters(
                version: Version,
                services: default,
Example #6
0
 public override int GetHashCode() =>
 ExpiryTime.GetHashCode() ^
 IPRange.GetHashCode() ^
 (Permissions?.GetHashCode() ?? 0) ^
 Protocol.GetHashCode() ^
 (ResourceTypes?.GetHashCode() ?? 0) ^
 (Services?.GetHashCode() ?? 0) ^
 StartTime.GetHashCode() ^
 (Version?.GetHashCode() ?? 0);
Example #7
0
 public override int GetHashCode() =>
 ExpiryTime.GetHashCode() ^
 Identifier.GetHashCode() ^
 IPRange.GetHashCode() ^
 Permissions.GetHashCode() ^
 Protocol.GetHashCode() ^
 QueueName.GetHashCode() ^
 StartTime.GetHashCode() ^
 Version.GetHashCode();
        /// <summary>
        /// Use an account's <see cref="UserDelegationKey"/> to sign this
        /// shared access signature values to produce the propery SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="userDelegationKey">
        /// A <see cref="UserDelegationKey"/> returned from
        /// <see cref="Azure.Storage.Blobs.BlobServiceClient.GetUserDelegationKeyAsync"/>.
        /// </param>
        /// <param name="accountName">The name of the storage account.</param>
        /// <returns>
        /// The <see cref="BlobSasQueryParameters"/> used for authenticating requests.
        /// </returns>
        public BlobSasQueryParameters ToSasQueryParameters(UserDelegationKey userDelegationKey, string accountName)
        {
            userDelegationKey = userDelegationKey ?? throw Errors.ArgumentNull(nameof(userDelegationKey));

            EnsureState();

            var startTime    = SasQueryParameters.FormatTimesForSasSigning(StartTime);
            var expiryTime   = SasQueryParameters.FormatTimesForSasSigning(ExpiryTime);
            var signedStart  = SasQueryParameters.FormatTimesForSasSigning(userDelegationKey.SignedStart);
            var signedExpiry = SasQueryParameters.FormatTimesForSasSigning(userDelegationKey.SignedExpiry);

            // See http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            var stringToSign = String.Join("\n",
                                           Permissions,
                                           startTime,
                                           expiryTime,
                                           GetCanonicalName(accountName, ContainerName ?? String.Empty, BlobName ?? String.Empty),
                                           userDelegationKey.SignedOid,
                                           userDelegationKey.SignedTid,
                                           signedStart,
                                           signedExpiry,
                                           userDelegationKey.SignedService,
                                           userDelegationKey.SignedVersion,
                                           IPRange.ToString(),
                                           Protocol.ToString(),
                                           Version,
                                           Resource,
                                           Snapshot,
                                           CacheControl,
                                           ContentDisposition,
                                           ContentEncoding,
                                           ContentLanguage,
                                           ContentType);

            var signature = ComputeHMACSHA256(userDelegationKey.Value, stringToSign);

            var p = new BlobSasQueryParameters(
                version: Version,
                services: null,
                resourceTypes: null,
                protocol: Protocol,
                startTime: StartTime,
                expiryTime: ExpiryTime,
                ipRange: IPRange,
                identifier: null,
                resource: Resource,
                permissions: Permissions,
                keyOid: userDelegationKey.SignedOid,
                keyTid: userDelegationKey.SignedTid,
                keyStart: userDelegationKey.SignedStart,
                keyExpiry: userDelegationKey.SignedExpiry,
                keyService: userDelegationKey.SignedService,
                keyVersion: userDelegationKey.SignedVersion,
                signature: signature);

            return(p);
        }
Example #9
0
        /// <summary>
        /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
        /// shared access signature values to produce the proper SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// The storage account's <see cref="StorageSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// The <see cref="SasQueryParameters"/> used for authenticating requests.
        /// </returns>
        public SasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));
            if (ExpiresOn == default)
            {
                throw Errors.SasMissingData(nameof(ExpiresOn));
            }
            if (string.IsNullOrEmpty(Permissions))
            {
                throw Errors.SasMissingData(nameof(Permissions));
            }

            string resource;

            if (string.IsNullOrEmpty(FilePath))
            {
                resource = Constants.Sas.Resource.Share;
            }
            else
            {
                resource = Constants.Sas.Resource.File;
            }

            if (string.IsNullOrEmpty(Version))
            {
                Version = SasQueryParameters.DefaultSasVersion;
            }

            var startTime  = SasQueryParameters.FormatTimesForSasSigning(StartsOn);
            var expiryTime = SasQueryParameters.FormatTimesForSasSigning(ExpiresOn);

            // String to sign: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            var stringToSign = string.Join("\n",
                                           Permissions,
                                           startTime,
                                           expiryTime,
                                           GetCanonicalName(sharedKeyCredential.AccountName, ShareName ?? string.Empty, FilePath ?? string.Empty),
                                           Identifier,
                                           IPRange.ToString(),
                                           Protocol.ToProtocolString(),
                                           Version,
                                           CacheControl,
                                           ContentDisposition,
                                           ContentEncoding,
                                           ContentLanguage,
                                           ContentType);

            var signature = sharedKeyCredential.ComputeHMACSHA256(stringToSign);

            var p = new SasQueryParameters(
                version: Version,
                services: default,
 public override int GetHashCode() =>
 BlobName.GetHashCode() ^
 CacheControl.GetHashCode() ^
 BlobContainerName.GetHashCode() ^
 ContentDisposition.GetHashCode() ^
 ContentEncoding.GetHashCode() ^
 ContentLanguage.GetHashCode() ^
 ContentType.GetHashCode() ^
 ExpiryTime.GetHashCode() ^
 Identifier.GetHashCode() ^
 IPRange.GetHashCode() ^
 Permissions.GetHashCode() ^
 Protocol.GetHashCode() ^
 StartTime.GetHashCode() ^
 Version.GetHashCode();
Example #11
0
 /// <summary>
 /// Creates a new instance of the <see cref="BlobSasQueryParameters"/>
 /// type.
 ///
 /// Expects decoded values.
 /// </summary>
 internal BlobSasQueryParameters(
     string version,
     string services,
     string resourceTypes,
     SasProtocol protocol,
     DateTimeOffset startTime,
     DateTimeOffset expiryTime,
     IPRange ipRange,
     string identifier,
     string resource,
     string permissions,
     string signature,
     string keyOid             = default,
     string keyTid             = default,
     DateTimeOffset keyStart   = default,
     DateTimeOffset keyExpiry  = default,
     string keyService         = default,
     string keyVersion         = default,
     string cacheControl       = default,
     string contentDisposition = default,
     string contentEncoding    = default,
     string contentLanguage    = default,
     string contentType        = default)
     : base(
         version,
         services,
         resourceTypes,
         protocol,
         startTime,
         expiryTime,
         ipRange,
         identifier,
         resource,
         permissions,
         signature,
         keyOid,
         keyTid,
         keyStart,
         keyExpiry,
         keyService,
         keyVersion,
         cacheControl,
         contentDisposition,
         contentEncoding,
         contentLanguage,
         contentType)
 {
 }
Example #12
0
 /// <summary>
 /// Creates a new instance of the <see cref="SasQueryParameters"/> type.
 ///
 /// Expects decoded values.
 /// </summary>
 internal SasQueryParameters(
     string version,
     string services,
     string resourceTypes,
     SasProtocol protocol,
     DateTimeOffset startTime,
     DateTimeOffset expiryTime,
     IPRange ipRange,
     string identifier,
     string resource,
     string permissions,
     string signature,
     string keyOid             = default,
     string keyTid             = default,
     DateTimeOffset keyStart   = default,
     DateTimeOffset keyExpiry  = default,
     string keyService         = default,
     string keyVersion         = default,
     string cacheControl       = default,
     string contentDisposition = default,
     string contentEncoding    = default,
     string contentLanguage    = default,
     string contentType        = default)
 {
     // Assume URL-decoded
     _version            = version ?? DefaultSasVersion;
     _services           = services ?? string.Empty;
     _resourceTypes      = resourceTypes ?? string.Empty;
     _protocol           = protocol;
     _startTime          = startTime;
     _expiryTime         = expiryTime;
     _ipRange            = ipRange;
     _identifier         = identifier ?? string.Empty;
     _resource           = resource ?? string.Empty;
     _permissions        = permissions ?? string.Empty;
     _signature          = signature ?? string.Empty; // Should never be null
     _keyObjectId        = keyOid;
     _keyTenantId        = keyTid;
     _keyStart           = keyStart;
     _keyExpiry          = keyExpiry;
     _keyService         = keyService;
     _keyVersion         = keyVersion;
     _cacheControl       = cacheControl;
     _contentDisposition = contentDisposition;
     _contentEncoding    = contentEncoding;
     _contentLanguage    = contentLanguage;
     _contentType        = contentType;
 }
Example #13
0
        /// <summary>
        /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
        /// shared access signature values to produce the proper SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// The storage account's <see cref="StorageSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// The <see cref="SasQueryParameters"/> used for authenticating
        /// requests.
        /// </returns>
        public SasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
        {
            // https://docs.microsoft.com/en-us/rest/api/storageservices/Constructing-an-Account-SAS
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            if (ExpiryTime == default || string.IsNullOrEmpty(Permissions) || string.IsNullOrEmpty(ResourceTypes) || string.IsNullOrEmpty(Services))
            {
                throw Errors.AccountSasMissingData();
            }
            if (string.IsNullOrEmpty(Version))
            {
                Version = SasQueryParameters.DefaultSasVersion;
            }
            // Make sure the permission characters are in the correct order
            Permissions = AccountSasPermissions.Parse(Permissions).ToString();
            var startTime  = SasQueryParameters.FormatTimesForSasSigning(StartTime);
            var expiryTime = SasQueryParameters.FormatTimesForSasSigning(ExpiryTime);

            // String to sign: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            var stringToSign = string.Join("\n",
                                           sharedKeyCredential.AccountName,
                                           Permissions,
                                           Services,
                                           ResourceTypes,
                                           startTime,
                                           expiryTime,
                                           IPRange.ToString(),
                                           Protocol.ToString(),
                                           Version,
                                           ""); // That's right, the account SAS requires a terminating extra newline

            var signature = sharedKeyCredential.ComputeHMACSHA256(stringToSign);
            var p         = new SasQueryParameters(
                Version,
                Services,
                ResourceTypes,
                Protocol,
                StartTime,
                ExpiryTime,
                IPRange,
                null, // Identifier
                null, // Resource
                Permissions,
                signature);

            return(p);
        }
        /// <summary>
        /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
        /// shared access signature values to produce the proper SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// The storage account's <see cref="StorageSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// The <see cref="SasQueryParameters"/> used for authenticating
        /// requests.
        /// </returns>
        public SasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
        {
            // https://docs.microsoft.com/en-us/rest/api/storageservices/Constructing-an-Account-SAS
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            if (ExpiresOn == default || string.IsNullOrEmpty(Permissions) || ResourceTypes == default || Services == default)
            {
                throw Errors.AccountSasMissingData();
            }

            Version = SasQueryParametersInternals.DefaultSasVersionInternal;

            string startTime  = SasExtensions.FormatTimesForSasSigning(StartsOn);
            string expiryTime = SasExtensions.FormatTimesForSasSigning(ExpiresOn);

            // String to sign: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            string stringToSign = string.Join("\n",
                                              sharedKeyCredential.AccountName,
                                              Permissions,
                                              Services.ToPermissionsString(),
                                              ResourceTypes.ToPermissionsString(),
                                              startTime,
                                              expiryTime,
                                              IPRange.ToString(),
                                              Protocol.ToProtocolString(),
                                              Version,
                                              EncryptionScope,
                                              string.Empty); // That's right, the account SAS requires a terminating extra newline

            string             signature = sharedKeyCredential.ComputeHMACSHA256(stringToSign);
            SasQueryParameters p         = SasQueryParametersInternals.Create(
                Version,
                Services,
                ResourceTypes,
                Protocol,
                StartsOn,
                ExpiresOn,
                IPRange,
                identifier: null,
                resource: null,
                Permissions,
                signature,
                encryptionScope: EncryptionScope);

            return(p);
        }
Example #15
0
        /// <summary>
        /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
        /// shared access signature values to produce the proper SAS query
        /// parameters for authenticating requests.
        /// </summary>
        /// <param name="sharedKeyCredential">
        /// The storage account's <see cref="StorageSharedKeyCredential"/>.
        /// </param>
        /// <returns>
        /// The <see cref="SasQueryParameters"/> used for authenticating
        /// requests.
        /// </returns>
        public SasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
        {
            sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));

            Permissions = QueueAccountSasPermissions.Parse(Permissions).ToString();
            if (string.IsNullOrEmpty(Version))
            {
                Version = SasQueryParameters.DefaultSasVersion;
            }

            var startTime  = SasQueryParameters.FormatTimesForSasSigning(StartTime);
            var expiryTime = SasQueryParameters.FormatTimesForSasSigning(ExpiryTime);

            // String to sign: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
            var stringToSign = string.Join("\n",
                                           Permissions,
                                           startTime,
                                           expiryTime,
                                           GetCanonicalName(sharedKeyCredential.AccountName, QueueName ?? string.Empty),
                                           Identifier,
                                           IPRange.ToString(),
                                           Protocol.ToString(),
                                           Version);
            var signature = sharedKeyCredential.ComputeHMACSHA256(stringToSign);
            var p         = new SasQueryParameters(
                version: Version,
                services: null,
                resourceTypes: null,
                protocol: Protocol,
                startTime: StartTime,
                expiryTime: ExpiryTime,
                ipRange: IPRange,
                identifier: Identifier,
                resource: null,
                permissions: Permissions,
                signature: signature);

            return(p);
        }
Example #16
0
 /// <summary>
 /// Creates a new instance of the <see cref="SasQueryParameters"/> type.
 ///
 /// Expects decoded values.
 /// </summary>
 internal SasQueryParameters(
     string version,
     string services,
     string resourceTypes,
     SasProtocol protocol,
     DateTimeOffset startTime,
     DateTimeOffset expiryTime,
     IPRange ipRange,
     string identifier,
     string resource,
     string permissions,
     string signature,
     string keyOid            = default,
     string keyTid            = default,
     DateTimeOffset keyStart  = default,
     DateTimeOffset keyExpiry = default,
     string keyService        = default,
     string keyVersion        = default)
 {
     // Assume URL-decoded
     this.version       = version ?? DefaultSasVersion;
     this.services      = services ?? String.Empty;
     this.resourceTypes = resourceTypes ?? String.Empty;
     this.protocol      = protocol;
     this.startTime     = startTime;
     this.expiryTime    = expiryTime;
     this.ipRange       = ipRange;
     this.identifier    = identifier ?? String.Empty;
     this.resource      = resource ?? String.Empty;
     this.permissions   = permissions ?? String.Empty;
     this.signature     = signature ?? String.Empty; // Should never be null
     this.keyObjectId   = keyOid;
     this.keyTenantId   = keyTid;
     this.keyStart      = keyStart;
     this.keyExpiry     = keyExpiry;
     this.keyService    = keyService;
     this.keyVersion    = keyVersion;
 }
Example #17
0
 /// <summary>
 /// Creates a new instance of the <see cref="BlobSasQueryParameters"/>
 /// type.
 ///
 /// Expects decoded values.
 /// </summary>
 internal BlobSasQueryParameters(
     string version,
     string services,
     string resourceTypes,
     SasProtocol protocol,
     DateTimeOffset startTime,
     DateTimeOffset expiryTime,
     IPRange ipRange,
     string identifier,
     string resource,
     string permissions,
     string signature,
     string keyOid            = default,
     string keyTid            = default,
     DateTimeOffset keyStart  = default,
     DateTimeOffset keyExpiry = default,
     string keyService        = default,
     string keyVersion        = default)
     : base(
         version,
         services,
         resourceTypes,
         protocol,
         startTime,
         expiryTime,
         ipRange,
         identifier,
         resource,
         permissions,
         signature,
         keyOid,
         keyTid,
         keyStart,
         keyExpiry,
         keyService,
         keyVersion)
 {
 }
Example #18
0
        /// <summary>
        /// Creates a new instance of the <see cref="SasQueryParameters"/> type
        /// based on the supplied query parameters <paramref name="values"/>.
        /// All SAS-related query parameters will be removed from
        /// <paramref name="values"/>.
        /// </summary>
        /// <param name="values">URI query parameters</param>
        /// <param name="includeBlobParameters">
        /// Optional flag indicating whether to process blob-specific query
        /// parameters.  The default value is false.
        /// </param>
        internal SasQueryParameters(
            UriQueryParamsCollection values,
            bool includeBlobParameters = false)
        {
            // make copy, otherwise we'll get an exception when we remove
            IEnumerable <KeyValuePair <string, string> > kvps = values.ToArray();;

            foreach (var kv in kvps)
            {
                // these are already decoded
                var isSasKey = true;
                switch (kv.Key.ToUpperInvariant())
                {
                case Constants.Sas.Parameters.VersionUpper: this.version = kv.Value; break;

                case Constants.Sas.Parameters.ServicesUpper: this.services = kv.Value; break;

                case Constants.Sas.Parameters.ResourceTypesUpper: this.resourceTypes = kv.Value; break;

                case Constants.Sas.Parameters.ProtocolUpper: this.protocol = SasProtocol.Parse(kv.Value); break;

                case Constants.Sas.Parameters.StartTimeUpper: this.startTime = DateTimeOffset.ParseExact(kv.Value, TimeFormat, CultureInfo.InvariantCulture); break;

                case Constants.Sas.Parameters.ExpiryTimeUpper: this.expiryTime = DateTimeOffset.ParseExact(kv.Value, TimeFormat, CultureInfo.InvariantCulture); break;

                case Constants.Sas.Parameters.IPRangeUpper: this.ipRange = IPRange.Parse(kv.Value); break;

                case Constants.Sas.Parameters.IdentifierUpper: this.identifier = kv.Value; break;

                case Constants.Sas.Parameters.ResourceUpper: this.resource = kv.Value; break;

                case Constants.Sas.Parameters.PermissionsUpper: this.permissions = kv.Value; break;

                case Constants.Sas.Parameters.SignatureUpper: this.signature = kv.Value; break;

                // Optionally include Blob parameters
                case Constants.Sas.Parameters.KeyOidUpper:
                    if (includeBlobParameters)
                    {
                        this.keyObjectId = kv.Value;
                    }
                    else
                    {
                        isSasKey = false;
                    }
                    break;

                case Constants.Sas.Parameters.KeyTidUpper:
                    if (includeBlobParameters)
                    {
                        this.keyTenantId = kv.Value;
                    }
                    else
                    {
                        isSasKey = false;
                    }
                    break;

                case Constants.Sas.Parameters.KeyStartUpper:
                    if (includeBlobParameters)
                    {
                        this.keyStart = DateTimeOffset.ParseExact(kv.Value, TimeFormat, CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        isSasKey = false;
                    }
                    break;

                case Constants.Sas.Parameters.KeyExpiryUpper:
                    if (includeBlobParameters)
                    {
                        this.keyExpiry = DateTimeOffset.ParseExact(kv.Value, TimeFormat, CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        isSasKey = false;
                    }
                    break;

                case Constants.Sas.Parameters.KeyServiceUpper:
                    if (includeBlobParameters)
                    {
                        this.keyService = kv.Value;
                    }
                    else
                    {
                        isSasKey = false;
                    }
                    break;

                case Constants.Sas.Parameters.KeyVersionUpper:
                    if (includeBlobParameters)
                    {
                        this.keyVersion = kv.Value;
                    }
                    else
                    {
                        isSasKey = false;
                    }
                    break;

                // We didn't recognize the query parameter
                default: isSasKey = false; break;
                }

                // Remove the query parameter if it's part of the SAS
                if (isSasKey)
                {
                    values.Remove(kv.Key);
                }
            }
        }