From 626b5ec4e2c657a054bea588b2370587b9d55399 Mon Sep 17 00:00:00 2001 From: Sapan Saxena Date: Fri, 27 Nov 2020 17:47:05 -0800 Subject: [PATCH 1/2] fix sas-token issue --- src/IotHub/IotHub.sln | 9 ++++++--- src/IotHub/IotHub/ChangeLog.md | 1 + .../IotHub/IotHub/DataPlane/NewAzIotHubSasToken.cs | 12 +++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/IotHub/IotHub.sln b/src/IotHub/IotHub.sln index 8929d58bed10..e846294e3573 100644 --- a/src/IotHub/IotHub.sln +++ b/src/IotHub/IotHub.sln @@ -1,7 +1,6 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27703.2042 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30427.197 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IotHub", "IotHub\IotHub.csproj", "{78770A60-B18A-4442-A982-0CEE0356F8DB}" EndProject @@ -41,6 +40,10 @@ Global {142D7B0B-388A-4CEB-A228-7F6D423C5C2E}.Debug|Any CPU.Build.0 = Debug|Any CPU {142D7B0B-388A-4CEB-A228-7F6D423C5C2E}.Release|Any CPU.ActiveCfg = Release|Any CPU {142D7B0B-388A-4CEB-A228-7F6D423C5C2E}.Release|Any CPU.Build.0 = Release|Any CPU + {6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Release|Any CPU.Build.0 = Release|Any CPU {1E60D2AC-DEA7-403C-86DC-7B8C47F54668}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {1E60D2AC-DEA7-403C-86DC-7B8C47F54668}.Debug|Any CPU.Build.0 = Debug|Any CPU {1E60D2AC-DEA7-403C-86DC-7B8C47F54668}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/src/IotHub/IotHub/ChangeLog.md b/src/IotHub/IotHub/ChangeLog.md index 627f7afec22d..26fc77c7b7b4 100644 --- a/src/IotHub/IotHub/ChangeLog.md +++ b/src/IotHub/IotHub/ChangeLog.md @@ -18,6 +18,7 @@ - Additional information about change #1 --> ## Upcoming Release +* Fix an issue of SAS token. ## Version 2.7.0 * Allowed tags in IoT Hub create cmdlet. diff --git a/src/IotHub/IotHub/IotHub/DataPlane/NewAzIotHubSasToken.cs b/src/IotHub/IotHub/IotHub/DataPlane/NewAzIotHubSasToken.cs index c7c5b369113a..75f3021c58ac 100644 --- a/src/IotHub/IotHub/IotHub/DataPlane/NewAzIotHubSasToken.cs +++ b/src/IotHub/IotHub/IotHub/DataPlane/NewAzIotHubSasToken.cs @@ -18,9 +18,10 @@ namespace Microsoft.Azure.Commands.Management.IotHub using System.Collections.Generic; using System.Globalization; using System.Management.Automation; + using System.Net; using System.Security.Cryptography; using System.Text; - using System.Web; + using System.Text.RegularExpressions; using Microsoft.Azure.Commands.Management.IotHub.Common; using Microsoft.Azure.Commands.Management.IotHub.Models; using Microsoft.Azure.Devices; @@ -179,15 +180,16 @@ private string createToken(string resourceUri, string keyName, string key, int d { TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1); var expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + duration); - string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry; - HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)); + string stringToSign = WebUtility.UrlEncode(resourceUri) + "\n" + expiry; + stringToSign=Regex.Replace(stringToSign, "(%[0-9A-F]{2})", c => c.Value.ToLowerInvariant()); + HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(key)); var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign))); - var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry); + var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}", WebUtility.UrlEncode(resourceUri), WebUtility.UrlEncode(signature), expiry); if (!string.IsNullOrEmpty(keyName)) { sasToken += String.Format(CultureInfo.InvariantCulture, "&skn={0}", keyName); } - return sasToken; + return Regex.Replace(sasToken, "(%[0-9A-F]{2})", c => c.Value.ToLowerInvariant()); } } } From ab08f405771b64d92c5064ee25d6d5f6a8179f7d Mon Sep 17 00:00:00 2001 From: Jin Lei <54836179+msJinLei@users.noreply.github.com> Date: Mon, 30 Nov 2020 10:34:21 +0800 Subject: [PATCH 2/2] Update ChangeLog.md Polish changelog --- src/IotHub/IotHub/ChangeLog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IotHub/IotHub/ChangeLog.md b/src/IotHub/IotHub/ChangeLog.md index 26fc77c7b7b4..045ad9093e68 100644 --- a/src/IotHub/IotHub/ChangeLog.md +++ b/src/IotHub/IotHub/ChangeLog.md @@ -18,7 +18,7 @@ - Additional information about change #1 --> ## Upcoming Release -* Fix an issue of SAS token. +* Fixed an issue of SAS token. ## Version 2.7.0 * Allowed tags in IoT Hub create cmdlet.