Skip to content

Commit

Permalink
{IotHub} Fix sas-token decoding issue (#13601)
Browse files Browse the repository at this point in the history
* fix sas-token issue

* Update ChangeLog.md

Polish changelog

Co-authored-by: Jin Lei <[email protected]>
  • Loading branch information
anusapan and msJinLei authored Nov 30, 2020
1 parent 41897b2 commit bc94344
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/IotHub/IotHub.sln
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/IotHub/IotHub/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Fixed an issue of SAS token.

## Version 2.7.0
* Allowed tags in IoT Hub create cmdlet.
Expand Down
12 changes: 7 additions & 5 deletions src/IotHub/IotHub/IotHub/DataPlane/NewAzIotHubSasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit bc94344

Please sign in to comment.