| | 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.Net.Http.Headers; |
| | 10 | | using System.Text; |
| | 11 | | using Azure.Core; |
| | 12 | | using Azure.Core.Pipeline; |
| | 13 | |
|
| | 14 | | namespace Azure.Data.Tables |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// HttpPipelinePolicy to sign requests using an Azure Storage shared key. |
| | 18 | | /// </summary> |
| | 19 | | internal sealed class TableSharedKeyPipelinePolicy : HttpPipelineSynchronousPolicy |
| | 20 | | { |
| | 21 | | private class InternalStorageCredential : TableSharedKeyCredential |
| | 22 | | { |
| 0 | 23 | | public static InternalStorageCredential Instance = new InternalStorageCredential(); |
| 0 | 24 | | public InternalStorageCredential() : base(string.Empty, string.Empty) |
| | 25 | | { |
| 0 | 26 | | } |
| | 27 | |
|
| | 28 | | public static string GetSas(TableSharedKeyCredential credential, string message) |
| | 29 | | { |
| 4008 | 30 | | return ComputeSasSignature(credential, message); |
| | 31 | | } |
| | 32 | | } |
| | 33 | | /// <summary> |
| | 34 | | /// Shared key credentials used to sign requests |
| | 35 | | /// </summary> |
| | 36 | | private readonly TableSharedKeyCredential _credentials; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Create a new SharedKeyPipelinePolicy |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="credentials">SharedKeyCredentials to authenticate requests.</param> |
| 416 | 42 | | public TableSharedKeyPipelinePolicy(TableSharedKeyCredential credentials) |
| 416 | 43 | | => _credentials = credentials; |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Sign the request using the shared key credentials. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="message">The message with the request to sign.</param> |
| | 49 | | public override void OnSendingRequest(HttpMessage message) |
| | 50 | | { |
| 4008 | 51 | | base.OnSendingRequest(message); |
| | 52 | |
|
| 4008 | 53 | | var date = DateTimeOffset.UtcNow.ToString("r", CultureInfo.InvariantCulture); |
| 4008 | 54 | | message.Request.Headers.SetValue(TableConstants.HeaderNames.Date, date); |
| | 55 | |
|
| 4008 | 56 | | var stringToSign = BuildStringToSign(message); |
| 4008 | 57 | | var signature = InternalStorageCredential.GetSas(_credentials, stringToSign); |
| | 58 | |
|
| 4008 | 59 | | var key = new AuthenticationHeaderValue(TableConstants.HeaderNames.SharedKey, _credentials.AccountName + ":" |
| 4008 | 60 | | message.Request.Headers.SetValue(TableConstants.HeaderNames.Authorization, key); |
| 4008 | 61 | | } |
| | 62 | |
|
| | 63 | | private string BuildStringToSign(HttpMessage message) |
| | 64 | | { |
| | 65 | | // https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key |
| | 66 | |
|
| 4008 | 67 | | message.Request.Headers.TryGetValue(TableConstants.HeaderNames.Date, out var date); |
| | 68 | |
|
| 4008 | 69 | | var stringToSign = string.Join("\n", |
| 4008 | 70 | | date, |
| 4008 | 71 | | BuildCanonicalizedResource(message.Request.Uri.ToUri())); |
| 4008 | 72 | | return stringToSign; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | private string BuildCanonicalizedResource(Uri resource) |
| | 76 | | { |
| | 77 | | // https://docs.microsoft.com/en-us/rest/api/storageservices/authentication-for-the-azure-storage-services |
| 4008 | 78 | | StringBuilder cr = new StringBuilder("/").Append(_credentials.AccountName); |
| 4008 | 79 | | if (resource.AbsolutePath.Length > 0) |
| | 80 | | { |
| | 81 | | // Any portion of the CanonicalizedResource string that is derived from |
| | 82 | | // the resource's URI should be encoded exactly as it is in the URI. |
| | 83 | | // -- https://msdn.microsoft.com/en-gb/library/azure/dd179428.aspx |
| 4008 | 84 | | cr.Append(resource.AbsolutePath);//EscapedPath() |
| | 85 | | } |
| | 86 | | else |
| | 87 | | { |
| | 88 | | // a slash is required to indicate the root path |
| 0 | 89 | | cr.Append('/'); |
| | 90 | | } |
| | 91 | |
|
| 4008 | 92 | | System.Collections.Generic.IDictionary<string, string> parameters = GetQueryParameters(resource); // Returns |
| 4008 | 93 | | if (parameters.Count > 0) |
| | 94 | | { |
| 19740 | 95 | | foreach (var name in parameters.Keys.OrderBy(key => key, StringComparer.Ordinal)) |
| | 96 | | { |
| | 97 | | // If the request URI addresses a component of the resource, append the appropriate query string. |
| | 98 | | // The query string should include the question mark and the comp parameter (for example, ?comp=meta |
| | 99 | | // No other parameters should be included on the query string. |
| | 100 | | // https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key#shared-key-li |
| 4324 | 101 | | if (name == "comp") |
| | 102 | | { |
| | 103 | | #pragma warning disable CA1308 // Normalize strings to uppercase |
| 24 | 104 | | cr.Append('?').Append(name.ToLowerInvariant()).Append('=').Append(parameters[name]); |
| | 105 | | #pragma warning restore CA1308 // Normalize strings to uppercase |
| | 106 | | } |
| | 107 | | } |
| | 108 | | } |
| 4008 | 109 | | return cr.ToString(); |
| | 110 | | } |
| | 111 | | public static IDictionary<string, string> GetQueryParameters(Uri uri) |
| | 112 | | { |
| 4008 | 113 | | var parameters = new Dictionary<string, string>(); |
| 4008 | 114 | | var query = uri.Query ?? ""; |
| 4008 | 115 | | if (!string.IsNullOrEmpty(query)) |
| | 116 | | { |
| 3384 | 117 | | if (query.StartsWith("?", true, CultureInfo.InvariantCulture)) |
| | 118 | | { |
| 3384 | 119 | | query = query.Substring(1); |
| | 120 | | } |
| 15416 | 121 | | foreach (var param in query.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries)) |
| | 122 | | { |
| 4324 | 123 | | var parts = param.Split(new[] { '=' }, 2); |
| 4324 | 124 | | var name = WebUtility.UrlDecode(parts[0]); |
| 4324 | 125 | | if (parts.Length == 1) |
| | 126 | | { |
| 0 | 127 | | parameters.Add(name, default); |
| | 128 | | } |
| | 129 | | else |
| | 130 | | { |
| 4324 | 131 | | parameters.Add(name, WebUtility.UrlDecode(parts[1])); |
| | 132 | | } |
| | 133 | | } |
| | 134 | | } |
| 4008 | 135 | | return parameters; |
| | 136 | | } |
| | 137 | |
|
| | 138 | | } |
| | 139 | | } |