| | | 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.Text; |
| | | 9 | | |
| | | 10 | | namespace Azure.Data.Tables.Sas |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// A <see cref="TableAccountSasQueryParameters"/> object represents the components |
| | | 14 | | /// that make up an Azure Storage Shared Access Signature's query |
| | | 15 | | /// parameters. You can construct a new instance using |
| | | 16 | | /// <see cref="TableAccountSasBuilder"/>. |
| | | 17 | | /// |
| | | 18 | | /// For more information, <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/create-account-sas">C |
| | | 19 | | /// </summary> |
| | | 20 | | public class TableAccountSasQueryParameters |
| | | 21 | | { |
| | | 22 | | // sv |
| | | 23 | | private readonly string _version; |
| | | 24 | | |
| | | 25 | | // srt |
| | | 26 | | private TableAccountSasResourceTypes? _resourceTypes; |
| | | 27 | | |
| | | 28 | | // spr |
| | | 29 | | private readonly TableSasProtocol _protocol; |
| | | 30 | | |
| | | 31 | | // st |
| | | 32 | | private DateTimeOffset _startTime; |
| | | 33 | | |
| | | 34 | | // se |
| | | 35 | | private DateTimeOffset _expiryTime; |
| | | 36 | | |
| | | 37 | | // sip |
| | | 38 | | private readonly TableSasIPRange _ipRange; |
| | | 39 | | |
| | | 40 | | // si |
| | | 41 | | private readonly string _identifier; |
| | | 42 | | |
| | | 43 | | // sr |
| | | 44 | | private readonly string _resource; |
| | | 45 | | |
| | | 46 | | // sp |
| | | 47 | | private readonly string _permissions; |
| | | 48 | | |
| | | 49 | | // sig |
| | | 50 | | private readonly string _signature; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// The default service version to use for Shared Access Signatures. |
| | | 54 | | /// </summary> |
| | | 55 | | internal const string DefaultSasVersion = TableConstants.Sas.DefaultSasVersion; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the storage service version to use to authenticate requests |
| | | 59 | | /// made with this shared access signature, and the service version to |
| | | 60 | | /// use when handling requests made with this shared access signature. |
| | | 61 | | /// </summary> |
| | 80 | 62 | | public string Version => _version ?? TableConstants.Sas.DefaultSasVersion; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets which resources are accessible via the shared access signature. |
| | | 66 | | /// </summary> |
| | 72 | 67 | | public TableAccountSasResourceTypes? ResourceTypes => _resourceTypes; |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Optional. Specifies the protocol permitted for a request made with |
| | | 71 | | /// the shared access signature. |
| | | 72 | | /// </summary> |
| | 40 | 73 | | public TableSasProtocol Protocol => _protocol; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets the optional time at which the shared access signature becomes |
| | | 77 | | /// valid. If omitted, start time for this call is assumed to be the |
| | | 78 | | /// time when the storage service receives the request. |
| | | 79 | | /// <see cref="DateTimeOffset.MinValue"/> means not set. |
| | | 80 | | /// </summary> |
| | 40 | 81 | | public DateTimeOffset StartsOn => _startTime; |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the time at which the shared access signature becomes invalid. |
| | | 85 | | /// <see cref="DateTimeOffset.MinValue"/> means not set. |
| | | 86 | | /// </summary> |
| | 80 | 87 | | public DateTimeOffset ExpiresOn => _expiryTime; |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets the optional IP address or a range of IP addresses from which |
| | | 90 | | /// to accept requests. When specifying a range, note that the range |
| | | 91 | | /// is inclusive. |
| | | 92 | | /// </summary> |
| | 40 | 93 | | public TableSasIPRange IPRange => _ipRange; |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Gets the optional unique value up to 64 characters in length that |
| | | 97 | | /// correlates to an access policy specified for the blob container, queue, |
| | | 98 | | /// or share. |
| | | 99 | | /// </summary> |
| | 40 | 100 | | public string Identifier => _identifier ?? string.Empty; |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Gets the resources are accessible via the shared access signature. |
| | | 104 | | /// </summary> |
| | 40 | 105 | | public string Resource => _resource ?? string.Empty; |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Gets the permissions associated with the shared access signature. |
| | | 109 | | /// The user is restricted to operations allowed by the permissions. |
| | | 110 | | /// This field must be omitted if it has been specified in an |
| | | 111 | | /// associated stored access policy. |
| | | 112 | | /// </summary> |
| | 80 | 113 | | public string Permissions => _permissions ?? string.Empty; |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// The signature is an HMAC computed over the string-to-sign and key |
| | | 117 | | /// using the SHA256 algorithm, and then encoded using Base64 encoding. |
| | | 118 | | /// </summary> |
| | 80 | 119 | | public string Signature => _signature ?? string.Empty; |
| | | 120 | | |
| | | 121 | | internal TableAccountSasQueryParameters() |
| | 0 | 122 | | : base() |
| | | 123 | | { |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Creates a new TableAccountSasQueryParameters instance. |
| | | 128 | | /// </summary> |
| | 40 | 129 | | internal TableAccountSasQueryParameters( |
| | 40 | 130 | | string version, |
| | 40 | 131 | | TableAccountSasResourceTypes? resourceTypes, |
| | 40 | 132 | | TableSasProtocol protocol, |
| | 40 | 133 | | DateTimeOffset startsOn, |
| | 40 | 134 | | DateTimeOffset expiresOn, |
| | 40 | 135 | | TableSasIPRange ipRange, |
| | 40 | 136 | | string identifier, |
| | 40 | 137 | | string resource, |
| | 40 | 138 | | string permissions, |
| | 40 | 139 | | string signature) |
| | | 140 | | { |
| | 40 | 141 | | _version = version; |
| | 40 | 142 | | _resourceTypes = resourceTypes; |
| | 40 | 143 | | _protocol = protocol; |
| | 40 | 144 | | _startTime = startsOn; |
| | 40 | 145 | | _expiryTime = expiresOn; |
| | 40 | 146 | | _ipRange = ipRange; |
| | 40 | 147 | | _identifier = identifier; |
| | 40 | 148 | | _resource = resource; |
| | 40 | 149 | | _permissions = permissions; |
| | 40 | 150 | | _signature = signature; |
| | | 151 | | |
| | 40 | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Creates a new instance of the <see cref="TableAccountSasQueryParameters"/> |
| | | 156 | | /// type based on the supplied query parameters <paramref name="values"/>. |
| | | 157 | | /// All SAS-related query parameters will be removed from |
| | | 158 | | /// <paramref name="values"/>. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <param name="values">URI query parameters</param> |
| | 0 | 161 | | internal TableAccountSasQueryParameters( |
| | 0 | 162 | | IDictionary<string, string> values) |
| | | 163 | | { |
| | | 164 | | // make copy, otherwise we'll get an exception when we remove |
| | 0 | 165 | | IEnumerable<KeyValuePair<string, string>> kvps = values.ToArray(); |
| | 0 | 166 | | foreach (KeyValuePair<string, string> kv in kvps) |
| | | 167 | | { |
| | | 168 | | // these are already decoded |
| | 0 | 169 | | var isSasKey = true; |
| | 0 | 170 | | switch (kv.Key.ToUpperInvariant()) |
| | | 171 | | { |
| | | 172 | | case TableConstants.Sas.Parameters.VersionUpper: |
| | 0 | 173 | | _version = kv.Value; |
| | 0 | 174 | | break; |
| | | 175 | | case TableConstants.Sas.Parameters.ResourceTypesUpper: |
| | 0 | 176 | | _resourceTypes = TableSasExtensions.ParseResourceTypes(kv.Value); |
| | 0 | 177 | | break; |
| | | 178 | | case TableConstants.Sas.Parameters.ProtocolUpper: |
| | 0 | 179 | | _protocol = TableSasExtensions.ParseProtocol(kv.Value); |
| | 0 | 180 | | break; |
| | | 181 | | case TableConstants.Sas.Parameters.StartTimeUpper: |
| | 0 | 182 | | _startTime = DateTimeOffset.ParseExact(kv.Value, TableConstants.Sas.SasTimeFormat, CultureInfo.I |
| | 0 | 183 | | break; |
| | | 184 | | case TableConstants.Sas.Parameters.ExpiryTimeUpper: |
| | 0 | 185 | | _expiryTime = DateTimeOffset.ParseExact(kv.Value, TableConstants.Sas.SasTimeFormat, CultureInfo. |
| | 0 | 186 | | break; |
| | | 187 | | case TableConstants.Sas.Parameters.IPRangeUpper: |
| | 0 | 188 | | _ipRange = TableSasIPRange.Parse(kv.Value); |
| | 0 | 189 | | break; |
| | | 190 | | case TableConstants.Sas.Parameters.IdentifierUpper: |
| | 0 | 191 | | _identifier = kv.Value; |
| | 0 | 192 | | break; |
| | | 193 | | case TableConstants.Sas.Parameters.ResourceUpper: |
| | 0 | 194 | | _resource = kv.Value; |
| | 0 | 195 | | break; |
| | | 196 | | case TableConstants.Sas.Parameters.PermissionsUpper: |
| | 0 | 197 | | _permissions = kv.Value; |
| | 0 | 198 | | break; |
| | | 199 | | case TableConstants.Sas.Parameters.SignatureUpper: |
| | 0 | 200 | | _signature = kv.Value; |
| | 0 | 201 | | break; |
| | | 202 | | |
| | | 203 | | // We didn't recognize the query parameter |
| | | 204 | | default: |
| | 0 | 205 | | isSasKey = false; |
| | | 206 | | break; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | // Remove the query parameter if it's part of the SAS |
| | 0 | 210 | | if (isSasKey) |
| | | 211 | | { |
| | 0 | 212 | | values.Remove(kv.Key); |
| | | 213 | | } |
| | | 214 | | } |
| | 0 | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Convert the SAS query parameters into a URL encoded query string. |
| | | 219 | | /// </summary> |
| | | 220 | | /// <returns> |
| | | 221 | | /// A URL encoded query string representing the SAS. |
| | | 222 | | /// </returns> |
| | | 223 | | public override string ToString() |
| | | 224 | | { |
| | 32 | 225 | | StringBuilder sb = new StringBuilder(); |
| | 32 | 226 | | this.AppendProperties(sb); |
| | 32 | 227 | | return sb.ToString(); |
| | | 228 | | } |
| | | 229 | | } |
| | | 230 | | } |