| | 1 | | // |
| | 2 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 3 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | 4 | | // |
| | 5 | |
|
| | 6 | | namespace Microsoft.Azure.Batch.Protocol |
| | 7 | | { |
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Collections.Specialized; |
| | 11 | | using System.Globalization; |
| | 12 | | using System.Linq; |
| | 13 | | using System.Net; |
| | 14 | | using System.Net.Http; |
| | 15 | | using System.Net.Http.Headers; |
| | 16 | | using System.Security.Cryptography; |
| | 17 | | using System.Text; |
| | 18 | | using System.Threading; |
| | 19 | | using System.Threading.Tasks; |
| | 20 | | using Microsoft.Azure.Batch.Utils; |
| | 21 | | using Rest; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Shared key credentials for an Azure Batch account. |
| | 25 | | /// </summary> |
| | 26 | | public class BatchSharedKeyCredential : ServiceClientCredentials |
| | 27 | | { |
| | 28 | | private const string OCPDateHeaderString = "ocp-date"; |
| | 29 | |
|
| 1 | 30 | | private static readonly byte[] EmptyArray = new byte[0]; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Gets the Batch account name. |
| | 34 | | /// </summary> |
| 158 | 35 | | public string AccountName { get; private set; } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Gets the account access key, as a Base64-encoded string. |
| | 39 | | /// </summary> |
| 149 | 40 | | public string KeyValue { get; private set; } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Initializes a new instance of the <see cref="BatchSharedKeyCredential"/> class. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="accountName">The name of the Batch account.</param> |
| | 46 | | /// <param name="keyValue">The access key of the Batch account, as a Base64-encoded string.</param> |
| 148 | 47 | | public BatchSharedKeyCredential(string accountName, string keyValue) |
| | 48 | | { |
| 148 | 49 | | this.AccountName = accountName; |
| 148 | 50 | | this.KeyValue = keyValue; |
| 148 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Signs a HTTP request with the current credentials. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="httpRequest">The HTTP request</param> |
| | 57 | | /// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken"/> for the request.</param> |
| | 58 | | /// <returns>A <see cref="Task"/> representing the asynchronous signing operation.</returns> |
| | 59 | | public override Task ProcessHttpRequestAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken |
| | 60 | | { |
| 9 | 61 | | if (httpRequest == null) |
| | 62 | | { |
| 0 | 63 | | return Async.CompletedTask; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | //First set ocp-date always |
| 9 | 67 | | if (!httpRequest.Headers.Contains(OCPDateHeaderString)) |
| | 68 | | { |
| 9 | 69 | | httpRequest.Headers.TryAddWithoutValidation(OCPDateHeaderString, string.Format(CultureInfo.InvariantCult |
| | 70 | | } |
| | 71 | |
|
| | 72 | | // Set Headers |
| 9 | 73 | | var signature = new StringBuilder(); |
| 9 | 74 | | signature.Append(httpRequest.Method).Append('\n'); |
| 9 | 75 | | signature.Append(httpRequest.Content != null && httpRequest.Content.Headers.Contains("Content-Encoding") ? h |
| 9 | 76 | | signature.Append(httpRequest.Content != null && httpRequest.Content.Headers.Contains("Content-Language") ? h |
| | 77 | |
|
| | 78 | | // Handle content length |
| 9 | 79 | | long? contentLength = httpRequest.Content?.Headers?.ContentLength; |
| | 80 | |
|
| 9 | 81 | | if (contentLength == null) |
| | 82 | | { |
| | 83 | | // C# in .NET Framework adds a content-lenth = 0 reader for DELETE, PATH, OPTIONS, and POST, so we need |
| | 84 | | // Because of https://github.com/dotnet/corefx/issues/31172 netstandard/netcore has different behavior d |
| | 85 | | // httoRequest.Content to an empty array to froce inclusion of content-length = 0 on all versions. |
| 9 | 86 | | if (httpRequest.Method == HttpMethod.Delete || httpRequest.Method == new HttpMethod("PATCH") || httpRequ |
| | 87 | | { |
| 4 | 88 | | httpRequest.Content = new ByteArrayContent(EmptyArray); |
| 4 | 89 | | contentLength = 0; |
| | 90 | | } |
| | 91 | | } |
| 9 | 92 | | signature.Append(contentLength).Append('\n'); |
| | 93 | |
|
| 9 | 94 | | signature.Append(httpRequest.Content != null && httpRequest.Content.Headers.Contains("Content-MD5") ? httpRe |
| 9 | 95 | | signature.Append(httpRequest.Content != null && httpRequest.Content.Headers.Contains("Content-Type") ? httpR |
| 9 | 96 | | signature.Append(httpRequest.Headers.Contains("Date") ? httpRequest.Headers.GetValues("Date").FirstOrDefault |
| 9 | 97 | | signature.Append(httpRequest.Headers.Contains("If-Modified-Since") ? httpRequest.Headers.GetValues("If-Modif |
| 9 | 98 | | signature.Append(httpRequest.Headers.Contains("If-Match") ? httpRequest.Headers.GetValues("If-Match").FirstO |
| 9 | 99 | | signature.Append(httpRequest.Headers.Contains("If-None-Match") ? httpRequest.Headers.GetValues("If-None-Matc |
| 9 | 100 | | signature.Append(httpRequest.Headers.Contains("If-Unmodified-Since") ? httpRequest.Headers.GetValues("If-Unm |
| 9 | 101 | | signature.Append(httpRequest.Headers.Contains("Range") ? httpRequest.Headers.GetValues("Range").FirstOrDefau |
| | 102 | |
|
| 9 | 103 | | List<string> customHeaders = new List<string>(); |
| 58 | 104 | | foreach (KeyValuePair<string, IEnumerable<string>> header in httpRequest.Headers) |
| | 105 | | { |
| 20 | 106 | | if (header.Key.StartsWith("ocp-", StringComparison.OrdinalIgnoreCase)) |
| | 107 | | { |
| 9 | 108 | | customHeaders.Add(header.Key.ToLowerInvariant()); |
| | 109 | | } |
| | 110 | | } |
| | 111 | |
|
| 9 | 112 | | if (httpRequest.Content != null) |
| | 113 | | { |
| 0 | 114 | | foreach (KeyValuePair<string, IEnumerable<string>> contentHeader in httpRequest.Content.Headers) |
| | 115 | | { |
| 0 | 116 | | if (contentHeader.Key.StartsWith("ocp-", StringComparison.OrdinalIgnoreCase)) |
| | 117 | | { |
| 0 | 118 | | customHeaders.Add(contentHeader.Key.ToLowerInvariant()); |
| | 119 | | } |
| | 120 | | } |
| | 121 | | } |
| 9 | 122 | | customHeaders.Sort(StringComparer.Ordinal); |
| | 123 | |
|
| 36 | 124 | | foreach (string canonicalHeader in customHeaders) |
| | 125 | | { |
| 9 | 126 | | string value = httpRequest.Headers.GetValues(canonicalHeader).FirstOrDefault(); |
| 9 | 127 | | value = value.Replace('\n', ' ').Replace('\r', ' ').TrimStart(); |
| 9 | 128 | | signature.Append(canonicalHeader).Append(':').Append(value).Append('\n'); |
| | 129 | | } |
| | 130 | |
|
| 9 | 131 | | signature.Append('/').Append(AccountName).Append('/').Append(httpRequest.RequestUri.AbsolutePath.TrimStart(' |
| 9 | 132 | | if (!string.IsNullOrEmpty(httpRequest.RequestUri.Query)) |
| | 133 | | { |
| | 134 | | #if FullNetFx |
| | 135 | | NameValueCollection queryVariables = System.Web.HttpUtility.ParseQueryString(httpRequest.RequestUri.Quer |
| | 136 | | List<string> queryVariableKeys = new List<string>(queryVariables.AllKeys); |
| | 137 | | #else |
| 2 | 138 | | Dictionary<string, Extensions.Primitives.StringValues> queryVariables = Microsoft.AspNetCore.WebUtilitie |
| 2 | 139 | | List<string> queryVariableKeys = new List<string>(queryVariables.Keys); |
| | 140 | | #endif |
| | 141 | |
|
| 2 | 142 | | queryVariableKeys.Sort(StringComparer.OrdinalIgnoreCase); |
| | 143 | |
|
| 8 | 144 | | foreach (string queryKey in queryVariableKeys) |
| | 145 | | { |
| | 146 | | string lowercaseQueryKey; |
| 2 | 147 | | if (queryKey != null) |
| | 148 | | { |
| 2 | 149 | | lowercaseQueryKey = queryKey.ToLowerInvariant(); |
| | 150 | | } |
| | 151 | | else |
| | 152 | | { |
| 0 | 153 | | lowercaseQueryKey = null; |
| | 154 | | } |
| 2 | 155 | | signature.Append('\n').Append(lowercaseQueryKey).Append(':').Append(queryVariables[queryKey]); |
| | 156 | | } |
| | 157 | | } |
| | 158 | |
|
| 9 | 159 | | string signedSignature = null; |
| | 160 | |
|
| 9 | 161 | | using (HashAlgorithm hashAlgorithm = new HMACSHA256(Convert.FromBase64String(this.KeyValue))) |
| | 162 | | { |
| 9 | 163 | | signedSignature = Convert.ToBase64String(hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(signature.ToSt |
| 9 | 164 | | } |
| 9 | 165 | | httpRequest.Headers.Authorization = new AuthenticationHeaderValue("SharedKey", this.AccountName + ":" + sign |
| | 166 | |
|
| 9 | 167 | | return Async.CompletedTask; |
| | 168 | | } |
| | 169 | | } |
| | 170 | | } |