| | | 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.Globalization; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Net; |
| | | 9 | | using System.Text; |
| | | 10 | | |
| | | 11 | | namespace Azure.Storage.Sas |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// A <see cref="SasQueryParameters"/> object represents the components |
| | | 15 | | /// that make up an Azure Storage Shared Access Signature's query |
| | | 16 | | /// parameters. It includes components used by all Azure Storage resources |
| | | 17 | | /// (Blob Containers, Blobs, Files, and Queues). You can construct a new instance |
| | | 18 | | /// using the service specific SAS builder types. |
| | | 19 | | /// For more information, |
| | | 20 | | /// <see href="https://docs.microsoft.com/rest/api/storageservices/create-service-sas"> |
| | | 21 | | /// Create a service SAS</see>. |
| | | 22 | | /// </summary> |
| | | 23 | | public partial class SasQueryParameters |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// The default service version to use for Shared Access Signatures. |
| | | 27 | | /// </summary> |
| | | 28 | | public const string DefaultSasVersion = Constants.DefaultSasVersion; |
| | | 29 | | |
| | | 30 | | // sv |
| | | 31 | | private string _version; |
| | | 32 | | |
| | | 33 | | // ss |
| | | 34 | | private AccountSasServices? _services; |
| | | 35 | | |
| | | 36 | | // srt |
| | | 37 | | private AccountSasResourceTypes? _resourceTypes; |
| | | 38 | | |
| | | 39 | | // spr |
| | | 40 | | private SasProtocol _protocol; |
| | | 41 | | |
| | | 42 | | // st |
| | | 43 | | private DateTimeOffset _startTime; |
| | | 44 | | |
| | | 45 | | // se |
| | | 46 | | private DateTimeOffset _expiryTime; |
| | | 47 | | |
| | | 48 | | // sip |
| | | 49 | | private SasIPRange _ipRange; |
| | | 50 | | |
| | | 51 | | // si |
| | | 52 | | private string _identifier; |
| | | 53 | | |
| | | 54 | | // sr |
| | | 55 | | private string _resource; |
| | | 56 | | |
| | | 57 | | // sp |
| | | 58 | | private string _permissions; |
| | | 59 | | |
| | | 60 | | // sig |
| | | 61 | | private string _signature; |
| | | 62 | | |
| | | 63 | | // rscc |
| | | 64 | | private string _cacheControl; |
| | | 65 | | |
| | | 66 | | // rscd |
| | | 67 | | private string _contentDisposition; |
| | | 68 | | |
| | | 69 | | // rsce |
| | | 70 | | private string _contentEncoding; |
| | | 71 | | |
| | | 72 | | // rscl |
| | | 73 | | private string _contentLanguage; |
| | | 74 | | |
| | | 75 | | // rsct |
| | | 76 | | private string _contentType; |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets the storage service version to use to authenticate requests |
| | | 80 | | /// made with this shared access signature, and the service version to |
| | | 81 | | /// use when handling requests made with this shared access signature. |
| | | 82 | | /// </summary> |
| | 2480 | 83 | | public string Version => _version ?? DefaultSasVersion; |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Gets the signed services accessible with an account level shared |
| | | 87 | | /// access signature. |
| | | 88 | | /// </summary> |
| | 1554 | 89 | | public AccountSasServices? Services => _services; |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets which resources are accessible via the shared access signature. |
| | | 93 | | /// </summary> |
| | 1554 | 94 | | public AccountSasResourceTypes? ResourceTypes => _resourceTypes; |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Optional. Specifies the protocol permitted for a request made with |
| | | 98 | | /// the shared access signature. |
| | | 99 | | /// </summary> |
| | 1392 | 100 | | public SasProtocol Protocol => _protocol; |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Gets the optional time at which the shared access signature becomes |
| | | 104 | | /// valid. If omitted, start time for this call is assumed to be the |
| | | 105 | | /// time when the storage service receives the request. |
| | | 106 | | /// <see cref="DateTimeOffset.MinValue"/> means not set. |
| | | 107 | | /// </summary> |
| | 2268 | 108 | | public DateTimeOffset StartsOn => _startTime; |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Gets the time at which the shared access signature becomes invalid. |
| | | 112 | | /// <see cref="DateTimeOffset.MinValue"/> means not set. |
| | | 113 | | /// </summary> |
| | 2396 | 114 | | public DateTimeOffset ExpiresOn => _expiryTime; |
| | | 115 | | /// <summary> |
| | | 116 | | /// Gets the optional IP address or a range of IP addresses from which |
| | | 117 | | /// to accept requests. When specifying a range, note that the range |
| | | 118 | | /// is inclusive. |
| | | 119 | | /// </summary> |
| | 1248 | 120 | | public SasIPRange IPRange => _ipRange; |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Gets the optional unique value up to 64 characters in length that |
| | | 124 | | /// correlates to an access policy specified for the blob container, queue, |
| | | 125 | | /// or share. |
| | | 126 | | /// </summary> |
| | 1310 | 127 | | public string Identifier => _identifier ?? string.Empty; |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets the resources are accessible via the shared access signature. |
| | | 131 | | /// </summary> |
| | 2128 | 132 | | public string Resource => _resource ?? string.Empty; |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Gets the permissions associated with the shared access signature. |
| | | 136 | | /// The user is restricted to operations allowed by the permissions. |
| | | 137 | | /// This field must be omitted if it has been specified in an |
| | | 138 | | /// associated stored access policy. |
| | | 139 | | /// </summary> |
| | 2396 | 140 | | public string Permissions => _permissions ?? string.Empty; |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Gets the Cache-Control response header, which allows for |
| | | 144 | | /// specifying the client-side caching to be used for blob and file downloads. |
| | | 145 | | /// </summary> |
| | 1236 | 146 | | public string CacheControl => _cacheControl ?? string.Empty; |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Gets the Content-Disposition response header, which allows for |
| | | 150 | | /// specifying the way that the blob or file content can be displayed in the browser. |
| | | 151 | | /// </summary> |
| | 1236 | 152 | | public string ContentDisposition => _contentDisposition ?? string.Empty; |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Gets the Content-Encoding response header, which allows for specifying |
| | | 156 | | /// the type of encoding used for blob and file downloads. |
| | | 157 | | /// </summary> |
| | 1236 | 158 | | public string ContentEncoding => _contentEncoding ?? string.Empty; |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Gets the Content-Language response header, which allows for specifying the |
| | | 162 | | /// language of the downloaded blob or file content. |
| | | 163 | | /// </summary> |
| | 1236 | 164 | | public string ContentLanguage => _contentLanguage ?? string.Empty; |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Gets the Content-Type response header, which allows for specifying the |
| | | 168 | | /// type of the downloaded blob or file content. |
| | | 169 | | /// </summary> |
| | 1236 | 170 | | public string ContentType => _contentType ?? string.Empty; |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Gets the string-to-sign, a unique string constructed from the |
| | | 174 | | /// fields that must be verified in order to authenticate the request. |
| | | 175 | | /// The signature is an HMAC computed over the string-to-sign and key |
| | | 176 | | /// using the SHA256 algorithm, and then encoded using Base64 encoding. |
| | | 177 | | /// </summary> |
| | 2444 | 178 | | public string Signature => _signature ?? string.Empty; |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Gets empty shared access signature query parameters. |
| | | 182 | | /// </summary> |
| | 0 | 183 | | public static SasQueryParameters Empty => new SasQueryParameters(); |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Initializes a new instance of the <see cref="SasQueryParameters"/> class. |
| | | 187 | | /// </summary> |
| | 0 | 188 | | protected SasQueryParameters() { } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Creates a new instance of the <see cref="SasQueryParameters"/> type |
| | | 192 | | /// based on the supplied query parameters <paramref name="values"/>. |
| | | 193 | | /// All SAS-related query parameters will be removed from |
| | | 194 | | /// <paramref name="values"/>. |
| | | 195 | | /// </summary> |
| | | 196 | | /// <param name="values">URI query parameters</param> |
| | 626 | 197 | | protected SasQueryParameters(IDictionary<string, string> values) |
| | | 198 | | { |
| | | 199 | | // make copy, otherwise we'll get an exception when we remove |
| | 626 | 200 | | IEnumerable<KeyValuePair<string, string>> kvps = values.ToArray(); |
| | 11080 | 201 | | foreach (KeyValuePair<string, string> kv in kvps) |
| | | 202 | | { |
| | | 203 | | // these are already decoded |
| | 4914 | 204 | | var isSasKey = true; |
| | 4914 | 205 | | switch (kv.Key.ToUpperInvariant()) |
| | | 206 | | { |
| | | 207 | | case Constants.Sas.Parameters.VersionUpper: |
| | 626 | 208 | | _version = kv.Value; |
| | 626 | 209 | | break; |
| | | 210 | | case Constants.Sas.Parameters.ServicesUpper: |
| | 158 | 211 | | _services = SasExtensions.ParseAccountServices(kv.Value); |
| | 158 | 212 | | break; |
| | | 213 | | case Constants.Sas.Parameters.ResourceTypesUpper: |
| | 158 | 214 | | _resourceTypes = SasExtensions.ParseResourceTypes(kv.Value); |
| | 158 | 215 | | break; |
| | | 216 | | case Constants.Sas.Parameters.ProtocolUpper: |
| | 100 | 217 | | _protocol = SasExtensions.ParseProtocol(kv.Value); |
| | 100 | 218 | | break; |
| | | 219 | | case Constants.Sas.Parameters.StartTimeUpper: |
| | 522 | 220 | | _startTime = DateTimeOffset.ParseExact(kv.Value, Constants.SasTimeFormat, CultureInfo.InvariantC |
| | 522 | 221 | | break; |
| | | 222 | | case Constants.Sas.Parameters.ExpiryTimeUpper: |
| | 612 | 223 | | _expiryTime = DateTimeOffset.ParseExact(kv.Value, Constants.SasTimeFormat, CultureInfo.Invariant |
| | 612 | 224 | | break; |
| | | 225 | | case Constants.Sas.Parameters.IPRangeUpper: |
| | 18 | 226 | | _ipRange = SasIPRange.Parse(kv.Value); |
| | 18 | 227 | | break; |
| | | 228 | | case Constants.Sas.Parameters.IdentifierUpper: |
| | 16 | 229 | | _identifier = kv.Value; |
| | 16 | 230 | | break; |
| | | 231 | | case Constants.Sas.Parameters.ResourceUpper: |
| | 470 | 232 | | _resource = kv.Value; |
| | 470 | 233 | | break; |
| | | 234 | | case Constants.Sas.Parameters.PermissionsUpper: |
| | 612 | 235 | | _permissions = kv.Value; |
| | 612 | 236 | | break; |
| | | 237 | | case Constants.Sas.Parameters.SignatureUpper: |
| | 626 | 238 | | _signature = kv.Value; |
| | 626 | 239 | | break; |
| | | 240 | | case Constants.Sas.Parameters.CacheControlUpper: |
| | 2 | 241 | | _cacheControl = kv.Value; |
| | 2 | 242 | | break; |
| | | 243 | | case Constants.Sas.Parameters.ContentDispositionUpper: |
| | 2 | 244 | | _contentDisposition = kv.Value; |
| | 2 | 245 | | break; |
| | | 246 | | case Constants.Sas.Parameters.ContentEncodingUpper: |
| | 2 | 247 | | _contentEncoding = kv.Value; |
| | 2 | 248 | | break; |
| | | 249 | | case Constants.Sas.Parameters.ContentLanguageUpper: |
| | 2 | 250 | | _contentLanguage = kv.Value; |
| | 2 | 251 | | break; |
| | | 252 | | case Constants.Sas.Parameters.ContentTypeUpper: |
| | 2 | 253 | | _contentType = kv.Value; |
| | 2 | 254 | | break; |
| | | 255 | | |
| | | 256 | | // We didn't recognize the query parameter |
| | | 257 | | default: |
| | 986 | 258 | | isSasKey = false; |
| | | 259 | | break; |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | // Remove the query parameter if it's part of the SAS |
| | 4914 | 263 | | if (isSasKey) |
| | | 264 | | { |
| | 3928 | 265 | | values.Remove(kv.Key); |
| | | 266 | | } |
| | | 267 | | } |
| | 626 | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Creates a new SasQueryParameters instance. |
| | | 272 | | /// </summary> |
| | 482 | 273 | | protected SasQueryParameters( |
| | 482 | 274 | | string version, |
| | 482 | 275 | | AccountSasServices? services, |
| | 482 | 276 | | AccountSasResourceTypes? resourceTypes, |
| | 482 | 277 | | SasProtocol protocol, |
| | 482 | 278 | | DateTimeOffset startsOn, |
| | 482 | 279 | | DateTimeOffset expiresOn, |
| | 482 | 280 | | SasIPRange ipRange, |
| | 482 | 281 | | string identifier, |
| | 482 | 282 | | string resource, |
| | 482 | 283 | | string permissions, |
| | 482 | 284 | | string signature, |
| | 482 | 285 | | string cacheControl = default, |
| | 482 | 286 | | string contentDisposition = default, |
| | 482 | 287 | | string contentEncoding = default, |
| | 482 | 288 | | string contentLanguage = default, |
| | 482 | 289 | | string contentType = default) |
| | | 290 | | { |
| | 482 | 291 | | _version = version; |
| | 482 | 292 | | _services = services; |
| | 482 | 293 | | _resourceTypes = resourceTypes; |
| | 482 | 294 | | _protocol = protocol; |
| | 482 | 295 | | _startTime = startsOn; |
| | 482 | 296 | | _expiryTime = expiresOn; |
| | 482 | 297 | | _ipRange = ipRange; |
| | 482 | 298 | | _identifier = identifier; |
| | 482 | 299 | | _resource = resource; |
| | 482 | 300 | | _permissions = permissions; |
| | 482 | 301 | | _signature = signature; |
| | 482 | 302 | | _cacheControl = cacheControl; |
| | 482 | 303 | | _contentDisposition = contentDisposition; |
| | 482 | 304 | | _contentEncoding = contentEncoding; |
| | 482 | 305 | | _contentLanguage = contentLanguage; |
| | 482 | 306 | | _contentType = contentType; |
| | 482 | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary> |
| | | 310 | | /// Creates a new instance of the <see cref="SasQueryParameters"/> type |
| | | 311 | | /// based on the supplied query parameters <paramref name="values"/>. |
| | | 312 | | /// All SAS-related query parameters will be removed from |
| | | 313 | | /// <paramref name="values"/>. |
| | | 314 | | /// </summary> |
| | | 315 | | /// <param name="values">URI query parameters</param> |
| | | 316 | | protected static SasQueryParameters Create(IDictionary<string, string> values) => |
| | 92 | 317 | | new SasQueryParameters(values); |
| | | 318 | | |
| | | 319 | | /// <summary> |
| | | 320 | | /// Creates a new SasQueryParameters instance. |
| | | 321 | | /// </summary> |
| | | 322 | | protected static SasQueryParameters Create( |
| | | 323 | | string version, |
| | | 324 | | AccountSasServices? services, |
| | | 325 | | AccountSasResourceTypes? resourceTypes, |
| | | 326 | | SasProtocol protocol, |
| | | 327 | | DateTimeOffset startsOn, |
| | | 328 | | DateTimeOffset expiresOn, |
| | | 329 | | SasIPRange ipRange, |
| | | 330 | | string identifier, |
| | | 331 | | string resource, |
| | | 332 | | string permissions, |
| | | 333 | | string signature, |
| | | 334 | | string cacheControl = default, |
| | | 335 | | string contentDisposition = default, |
| | | 336 | | string contentEncoding = default, |
| | | 337 | | string contentLanguage = default, |
| | | 338 | | string contentType = default) => |
| | 202 | 339 | | new SasQueryParameters( |
| | 202 | 340 | | version, |
| | 202 | 341 | | services, |
| | 202 | 342 | | resourceTypes, |
| | 202 | 343 | | protocol, |
| | 202 | 344 | | startsOn, |
| | 202 | 345 | | expiresOn, |
| | 202 | 346 | | ipRange, |
| | 202 | 347 | | identifier, |
| | 202 | 348 | | resource, |
| | 202 | 349 | | permissions, |
| | 202 | 350 | | signature, |
| | 202 | 351 | | cacheControl, |
| | 202 | 352 | | contentDisposition, |
| | 202 | 353 | | contentEncoding, |
| | 202 | 354 | | contentLanguage, |
| | 202 | 355 | | contentType); |
| | | 356 | | |
| | | 357 | | /// <summary> |
| | | 358 | | /// Convert the SAS query parameters into a URL encoded query string. |
| | | 359 | | /// </summary> |
| | | 360 | | /// <returns> |
| | | 361 | | /// A URL encoded query string representing the SAS. |
| | | 362 | | /// </returns> |
| | | 363 | | public override string ToString() |
| | | 364 | | { |
| | 278 | 365 | | StringBuilder sb = new StringBuilder(); |
| | 278 | 366 | | this.AppendProperties(sb); |
| | 278 | 367 | | return sb.ToString(); |
| | | 368 | | } |
| | | 369 | | } |
| | | 370 | | } |