| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.Net; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace Azure.Data.Tables.Sas |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Extension methods for Sas. |
| | | 13 | | /// </summary> |
| | | 14 | | internal static partial class TableSasExtensions |
| | | 15 | | { |
| | | 16 | | private const string NoneName = null; |
| | | 17 | | private const string HttpsName = "https"; |
| | | 18 | | private const string HttpsAndHttpName = "https,http"; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets a string representation of the protocol. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <returns>A string representation of the protocol.</returns> |
| | | 24 | | internal static string ToProtocolString(this TableSasProtocol protocol) |
| | | 25 | | { |
| | | 26 | | switch (protocol) |
| | | 27 | | { |
| | | 28 | | case TableSasProtocol.Https: |
| | 0 | 29 | | return HttpsName; |
| | | 30 | | case TableSasProtocol.HttpsAndHttp: |
| | 0 | 31 | | return HttpsAndHttpName; |
| | | 32 | | case TableSasProtocol.None: |
| | | 33 | | default: |
| | 40 | 34 | | return null; |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Parse a string representation of a protocol. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="s">A string representation of a protocol.</param> |
| | | 42 | | /// <returns>A <see cref="TableSasProtocol"/>.</returns> |
| | | 43 | | public static TableSasProtocol ParseProtocol(string s) |
| | | 44 | | { |
| | | 45 | | switch (s) |
| | | 46 | | { |
| | | 47 | | case NoneName: |
| | | 48 | | case "": |
| | 0 | 49 | | return TableSasProtocol.None; |
| | | 50 | | case HttpsName: |
| | 0 | 51 | | return TableSasProtocol.Https; |
| | | 52 | | case HttpsAndHttpName: |
| | 0 | 53 | | return TableSasProtocol.HttpsAndHttp; |
| | | 54 | | default: |
| | 0 | 55 | | throw Errors.InvalidSasProtocol(nameof(s), nameof(TableSasProtocol)); |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Parse a string representing which resource types are accessible |
| | | 61 | | /// from a shared access signature. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="s"> |
| | | 64 | | /// A string representing which resource types are accessible. |
| | | 65 | | /// </param> |
| | | 66 | | /// <returns> |
| | | 67 | | /// An <see cref="TableAccountSasResourceTypes"/> instance. |
| | | 68 | | /// </returns> |
| | | 69 | | /// <remarks> |
| | | 70 | | /// The order here matches the order used by the portal when generating SAS signatures. |
| | | 71 | | /// </remarks> |
| | | 72 | | internal static TableAccountSasResourceTypes ParseResourceTypes(string s) |
| | | 73 | | { |
| | 0 | 74 | | TableAccountSasResourceTypes types = default; |
| | 0 | 75 | | foreach (var ch in s) |
| | | 76 | | { |
| | 0 | 77 | | types |= ch switch |
| | 0 | 78 | | { |
| | 0 | 79 | | TableConstants.Sas.TableAccountResources.Service => TableAccountSasResourceTypes.Service, |
| | 0 | 80 | | TableConstants.Sas.TableAccountResources.Container => TableAccountSasResourceTypes.Container, |
| | 0 | 81 | | TableConstants.Sas.TableAccountResources.Object => TableAccountSasResourceTypes.Object, |
| | 0 | 82 | | _ => throw Errors.InvalidResourceType(ch), |
| | 0 | 83 | | }; |
| | | 84 | | } |
| | 0 | 85 | | return types; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// FormatTimesForSASSigning converts a time.Time to a snapshotTimeFormat string suitable for a |
| | | 90 | | /// SASField's StartTime or ExpiryTime fields. Returns "" if value.IsZero(). |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="time"></param> |
| | | 93 | | /// <returns></returns> |
| | | 94 | | internal static string FormatTimesForSasSigning(DateTimeOffset time) => |
| | | 95 | | // "yyyy-MM-ddTHH:mm:ssZ" |
| | 80 | 96 | | (time == new DateTimeOffset()) ? "" : time.ToString(TableConstants.Sas.SasTimeFormat, CultureInfo.InvariantC |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Helper method to add query param key value pairs to StringBuilder |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="sb">StringBuilder instance</param> |
| | | 102 | | /// <param name="key">query key</param> |
| | | 103 | | /// <param name="value">query value</param> |
| | | 104 | | internal static void AddToBuilder(StringBuilder sb, string key, string value) => |
| | 0 | 105 | | sb |
| | 0 | 106 | | .Append(sb.Length > 0 ? "&" : "") |
| | 0 | 107 | | .Append(key) |
| | 0 | 108 | | .Append('=') |
| | 0 | 109 | | .Append(value); |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Builds the query parameter string for the SasQueryParameters instance. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <param name="parameters"></param> |
| | | 115 | | /// <param name="stringBuilder"> |
| | | 116 | | /// StringBuilder instance to add the query params to |
| | | 117 | | /// </param> |
| | | 118 | | internal static void AppendProperties(this TableAccountSasQueryParameters parameters, StringBuilder stringBuilde |
| | | 119 | | { |
| | 40 | 120 | | if (!string.IsNullOrWhiteSpace(parameters.Version)) |
| | | 121 | | { |
| | 40 | 122 | | stringBuilder.AppendQueryParameter(TableConstants.Sas.Parameters.Version, parameters.Version); |
| | | 123 | | } |
| | | 124 | | |
| | 40 | 125 | | if (!(parameters is TableSasQueryParameters)) |
| | | 126 | | { |
| | 32 | 127 | | stringBuilder.AppendQueryParameter(TableConstants.Sas.Parameters.Services, TableConstants.Sas.TableAccou |
| | | 128 | | } |
| | | 129 | | |
| | 40 | 130 | | if (parameters.ResourceTypes != null) |
| | | 131 | | { |
| | 32 | 132 | | stringBuilder.AppendQueryParameter(TableConstants.Sas.Parameters.ResourceTypes, parameters.ResourceTypes |
| | | 133 | | } |
| | | 134 | | |
| | 40 | 135 | | if (parameters.Protocol != default) |
| | | 136 | | { |
| | 0 | 137 | | stringBuilder.AppendQueryParameter(TableConstants.Sas.Parameters.Protocol, parameters.Protocol.ToProtoco |
| | | 138 | | } |
| | | 139 | | |
| | 40 | 140 | | if (parameters.StartsOn != DateTimeOffset.MinValue) |
| | | 141 | | { |
| | 0 | 142 | | stringBuilder.AppendQueryParameter(TableConstants.Sas.Parameters.StartTime, WebUtility.UrlEncode(paramet |
| | | 143 | | } |
| | | 144 | | |
| | 40 | 145 | | if (parameters.ExpiresOn != DateTimeOffset.MinValue) |
| | | 146 | | { |
| | 40 | 147 | | stringBuilder.AppendQueryParameter(TableConstants.Sas.Parameters.ExpiryTime, WebUtility.UrlEncode(parame |
| | | 148 | | } |
| | | 149 | | |
| | 40 | 150 | | var ipr = parameters.IPRange.ToString(); |
| | 40 | 151 | | if (ipr.Length > 0) |
| | | 152 | | { |
| | 0 | 153 | | stringBuilder.AppendQueryParameter(TableConstants.Sas.Parameters.IPRange, ipr); |
| | | 154 | | } |
| | | 155 | | |
| | 40 | 156 | | if (!string.IsNullOrWhiteSpace(parameters.Identifier)) |
| | | 157 | | { |
| | 0 | 158 | | stringBuilder.AppendQueryParameter(TableConstants.Sas.Parameters.Identifier, parameters.Identifier); |
| | | 159 | | } |
| | | 160 | | |
| | 40 | 161 | | if (!string.IsNullOrWhiteSpace(parameters.Resource)) |
| | | 162 | | { |
| | 0 | 163 | | stringBuilder.AppendQueryParameter(TableConstants.Sas.Parameters.Resource, parameters.Resource); |
| | | 164 | | } |
| | | 165 | | |
| | 40 | 166 | | if (!string.IsNullOrWhiteSpace(parameters.Permissions)) |
| | | 167 | | { |
| | 40 | 168 | | stringBuilder.AppendQueryParameter(TableConstants.Sas.Parameters.Permissions, parameters.Permissions); |
| | | 169 | | } |
| | | 170 | | |
| | 40 | 171 | | if (!string.IsNullOrWhiteSpace(parameters.Signature)) |
| | | 172 | | { |
| | 40 | 173 | | stringBuilder.AppendQueryParameter(TableConstants.Sas.Parameters.Signature, WebUtility.UrlEncode(paramet |
| | | 174 | | } |
| | 40 | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Appends a query parameter to the string builder. |
| | | 179 | | /// </summary> |
| | | 180 | | /// <param name="sb">string builder instance.</param> |
| | | 181 | | /// <param name="key">query parameter key.</param> |
| | | 182 | | /// <param name="value">query parameter value.</param> |
| | | 183 | | internal static void AppendQueryParameter(this StringBuilder sb, string key, string value) => |
| | 232 | 184 | | sb |
| | 232 | 185 | | .Append(sb.Length > 0 ? "&" : "") |
| | 232 | 186 | | .Append(key) |
| | 232 | 187 | | .Append('=') |
| | 232 | 188 | | .Append(value); |
| | | 189 | | } |
| | | 190 | | } |