| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.ComponentModel; |
| | 7 | | using System.Text; |
| | 8 | | using Azure.Storage.Queues; |
| | 9 | |
|
| | 10 | | namespace Azure.Storage.Sas |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// <see cref="QueueSasBuilder"/> is used to generate a Shared Access |
| | 14 | | /// Signature (SAS) for an Azure Storage queue. |
| | 15 | | /// |
| | 16 | | /// For more information, see |
| | 17 | | /// <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas"> |
| | 18 | | /// Create a Service SAS</see>. |
| | 19 | | /// </summary> |
| | 20 | | public class QueueSasBuilder |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// The storage service version to use to authenticate requests made |
| | 24 | | /// with this shared access signature, and the service version to use |
| | 25 | | /// when handling requests made with this shared access signature. |
| | 26 | | /// </summary> |
| 144 | 27 | | public string Version { get; set; } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// The optional signed protocol field specifies the protocol |
| | 31 | | /// permitted for a request made with the SAS. Possible values are |
| | 32 | | /// <see cref="SasProtocol.HttpsAndHttp"/>, |
| | 33 | | /// <see cref="SasProtocol.Https"/>, and |
| | 34 | | /// <see cref="SasProtocol.None"/>. |
| | 35 | | /// </summary> |
| 84 | 36 | | public SasProtocol Protocol { get; set; } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Optionally specify the time at which the shared access signature |
| | 40 | | /// becomes valid. If omitted when DateTimeOffset.MinValue is used, |
| | 41 | | /// start time for this call is assumed to be the time when the |
| | 42 | | /// storage service receives the request. |
| | 43 | | /// </summary> |
| 96 | 44 | | public DateTimeOffset StartsOn { get; set; } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// The time at which the shared access signature becomes invalid. |
| | 48 | | /// This field must be omitted if it has been specified in an |
| | 49 | | /// associated stored access policy. |
| | 50 | | /// </summary> |
| 112 | 51 | | public DateTimeOffset ExpiresOn { get; set; } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// The permissions associated with the shared access signature. The |
| | 55 | | /// user is restricted to operations allowed by the permissions. This |
| | 56 | | /// field must be omitted if it has been specified in an associated |
| | 57 | | /// stored access policy. <see cref="QueueSasPermissions"/> and |
| | 58 | | /// <see cref="QueueAccountSasPermissions"/> can be used to create the |
| | 59 | | /// permissions string. |
| | 60 | | /// </summary> |
| 108 | 61 | | public string Permissions { get; private set; } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Specifies an IP address or a range of IP addresses from which to |
| | 65 | | /// accept requests. If the IP address from which the request |
| | 66 | | /// originates does not match the IP address or address range |
| | 67 | | /// specified on the SAS token, the request is not authenticated. |
| | 68 | | /// When specifying a range of IP addresses, note that the range is |
| | 69 | | /// inclusive. |
| | 70 | | /// </summary> |
| 80 | 71 | | public SasIPRange IPRange { get; set; } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// An optional unique value up to 64 characters in length that |
| | 75 | | /// correlates to an access policy specified for the container. |
| | 76 | | /// </summary> |
| 116 | 77 | | public string Identifier { get; set; } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// The optional name of the blob being made accessible. |
| | 81 | | /// </summary> |
| 72 | 82 | | public string QueueName { get; set; } |
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// Sets the permissions for a queue SAS. |
| | 86 | | /// </summary> |
| | 87 | | /// <param name="permissions"> |
| | 88 | | /// <see cref="QueueSasPermissions"/> containing the allowed permissions. |
| | 89 | | /// </param> |
| | 90 | | public void SetPermissions(QueueSasPermissions permissions) |
| | 91 | | { |
| 0 | 92 | | Permissions = permissions.ToPermissionsString(); |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Sets the permissions for a queue account level SAS. |
| | 97 | | /// </summary> |
| | 98 | | /// <param name="permissions"> |
| | 99 | | /// <see cref="QueueAccountSasPermissions"/> containing the allowed permissions. |
| | 100 | | /// </param> |
| | 101 | | public void SetPermissions(QueueAccountSasPermissions permissions) |
| | 102 | | { |
| 4 | 103 | | Permissions = permissions.ToPermissionsString(); |
| 4 | 104 | | } |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Sets the permissions for the SAS using a raw permissions string. |
| | 108 | | /// </summary> |
| | 109 | | /// <param name="rawPermissions"> |
| | 110 | | /// Raw permissions string for the SAS. |
| | 111 | | /// </param> |
| | 112 | | /// <param name="normalize"> |
| | 113 | | /// If the permissions should be validated and correctly ordered. |
| | 114 | | /// </param> |
| | 115 | | public void SetPermissions( |
| | 116 | | string rawPermissions, |
| | 117 | | bool normalize = default) |
| | 118 | | { |
| 12 | 119 | | if (normalize) |
| | 120 | | { |
| 12 | 121 | | rawPermissions = SasExtensions.ValidateAndSanitizeRawPermissions( |
| 12 | 122 | | permissions: rawPermissions, |
| 12 | 123 | | validPermissionsInOrder: s_validPermissionsInOrder); |
| | 124 | | } |
| | 125 | |
|
| 8 | 126 | | SetPermissions(rawPermissions); |
| 8 | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Sets the permissions for the SAS using a raw permissions string. |
| | 131 | | /// </summary> |
| | 132 | | /// <param name="rawPermissions">Raw permissions string for the SAS.</param> |
| | 133 | | public void SetPermissions(string rawPermissions) |
| | 134 | | { |
| 24 | 135 | | Permissions = rawPermissions; |
| 24 | 136 | | } |
| | 137 | |
|
| 2 | 138 | | private static readonly List<char> s_validPermissionsInOrder = new List<char> |
| 2 | 139 | | { |
| 2 | 140 | | Constants.Sas.Permissions.Read, |
| 2 | 141 | | Constants.Sas.Permissions.Write, |
| 2 | 142 | | Constants.Sas.Permissions.Delete, |
| 2 | 143 | | Constants.Sas.Permissions.DeleteBlobVersion, |
| 2 | 144 | | Constants.Sas.Permissions.List, |
| 2 | 145 | | Constants.Sas.Permissions.Add, |
| 2 | 146 | | Constants.Sas.Permissions.Create, |
| 2 | 147 | | Constants.Sas.Permissions.Update, |
| 2 | 148 | | Constants.Sas.Permissions.Process, |
| 2 | 149 | | Constants.Sas.Permissions.Tag, |
| 2 | 150 | | Constants.Sas.Permissions.FilterByTags, |
| 2 | 151 | | }; |
| | 152 | |
|
| | 153 | | /// <summary> |
| | 154 | | /// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this |
| | 155 | | /// shared access signature values to produce the proper SAS query |
| | 156 | | /// parameters for authenticating requests. |
| | 157 | | /// </summary> |
| | 158 | | /// <param name="sharedKeyCredential"> |
| | 159 | | /// The storage account's <see cref="StorageSharedKeyCredential"/>. |
| | 160 | | /// </param> |
| | 161 | | /// <returns> |
| | 162 | | /// The <see cref="SasQueryParameters"/> used for authenticating |
| | 163 | | /// requests. |
| | 164 | | /// </returns> |
| | 165 | | public SasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential) |
| | 166 | | { |
| 36 | 167 | | sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential)); |
| | 168 | |
|
| 32 | 169 | | EnsureState(); |
| | 170 | |
|
| 32 | 171 | | var startTime = SasExtensions.FormatTimesForSasSigning(StartsOn); |
| 32 | 172 | | var expiryTime = SasExtensions.FormatTimesForSasSigning(ExpiresOn); |
| | 173 | |
|
| | 174 | | // String to sign: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx |
| 32 | 175 | | var stringToSign = string.Join("\n", |
| 32 | 176 | | Permissions, |
| 32 | 177 | | startTime, |
| 32 | 178 | | expiryTime, |
| 32 | 179 | | GetCanonicalName(sharedKeyCredential.AccountName, QueueName ?? string.Empty), |
| 32 | 180 | | Identifier, |
| 32 | 181 | | IPRange.ToString(), |
| 32 | 182 | | SasExtensions.ToProtocolString(Protocol), |
| 32 | 183 | | Version); |
| 32 | 184 | | var signature = StorageSharedKeyCredentialInternals.ComputeSasSignature(sharedKeyCredential, stringToSign); |
| 32 | 185 | | var p = SasQueryParametersInternals.Create( |
| 32 | 186 | | version: Version, |
| 32 | 187 | | services: default, |
| 32 | 188 | | resourceTypes: default, |
| 32 | 189 | | protocol: Protocol, |
| 32 | 190 | | startsOn: StartsOn, |
| 32 | 191 | | expiresOn: ExpiresOn, |
| 32 | 192 | | ipRange: IPRange, |
| 32 | 193 | | identifier: Identifier, |
| 32 | 194 | | resource: null, |
| 32 | 195 | | permissions: Permissions, |
| 32 | 196 | | signature: signature); |
| 32 | 197 | | return p; |
| | 198 | | } |
| | 199 | |
|
| | 200 | | /// <summary> |
| | 201 | | /// Computes the canonical name for a queue resource for SAS signing. |
| | 202 | | /// </summary> |
| | 203 | | /// <param name="account"> |
| | 204 | | /// Account. |
| | 205 | | /// </param> |
| | 206 | | /// <param name="queueName"> |
| | 207 | | /// Name of queue. |
| | 208 | | /// </param> |
| | 209 | | /// <returns> |
| | 210 | | /// Canonical name as a string. |
| | 211 | | /// </returns> |
| | 212 | | private static string GetCanonicalName(string account, string queueName) => |
| | 213 | | // Queue: "/queue/account/queuename" |
| 32 | 214 | | string.Join("", new[] { "/queue/", account, "/", queueName }); |
| | 215 | |
|
| | 216 | | /// <summary> |
| | 217 | | /// Returns a string that represents the current object. |
| | 218 | | /// </summary> |
| | 219 | | /// <returns>A string that represents the current object.</returns> |
| | 220 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 221 | | public override string ToString() => base.ToString(); |
| | 222 | |
|
| | 223 | | /// <summary> |
| | 224 | | /// Check if two QueueSasBuilder instances are equal. |
| | 225 | | /// </summary> |
| | 226 | | /// <param name="obj">The instance to compare to.</param> |
| | 227 | | /// <returns>True if they're equal, false otherwise.</returns> |
| | 228 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 229 | | public override bool Equals(object obj) => base.Equals(obj); |
| | 230 | |
|
| | 231 | | /// <summary> |
| | 232 | | /// Get a hash code for the QueueSasBuilder. |
| | 233 | | /// </summary> |
| | 234 | | /// <returns>Hash code for the QueueSasBuilder.</returns> |
| | 235 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 236 | | public override int GetHashCode() => base.GetHashCode(); |
| | 237 | |
|
| | 238 | | /// <summary> |
| | 239 | | /// Ensure the <see cref="QueueSasBuilder"/>'s properties are in a |
| | 240 | | /// consistent state. |
| | 241 | | /// </summary> |
| | 242 | | private void EnsureState() |
| | 243 | | { |
| 32 | 244 | | if (Identifier == default) |
| | 245 | | { |
| 16 | 246 | | if (ExpiresOn == default) |
| | 247 | | { |
| 0 | 248 | | throw Errors.SasMissingData(nameof(ExpiresOn)); |
| | 249 | | } |
| 16 | 250 | | if (string.IsNullOrEmpty(Permissions)) |
| | 251 | | { |
| 0 | 252 | | throw Errors.SasMissingData(nameof(Permissions)); |
| | 253 | | } |
| | 254 | | } |
| | 255 | |
|
| 32 | 256 | | if (string.IsNullOrEmpty(Version)) |
| | 257 | | { |
| 24 | 258 | | Version = SasQueryParameters.DefaultSasVersion; |
| | 259 | | } |
| 32 | 260 | | } |
| | 261 | | } |
| | 262 | | } |