Skip to content

Commit 9427911

Browse files
authored
Merge pull request #6062 from AsrOneSdk/cikTokenFix
[RecoveryServices][SiteRecovery] CIK token serialize using System.Web.Script.Serialization.JavaScriptSerializer.
2 parents 3a2c7f8 + 1fec877 commit 9427911

File tree

3 files changed

+36
-7
lines changed
  • src/ResourceManager/RecoveryServices.SiteRecovery

3 files changed

+36
-7
lines changed

src/ResourceManager/RecoveryServices.SiteRecovery/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21+
* Fixed Authentication Header
2122
* Set minimum dependency of module to PowerShell 5.0
2223

2324
## Version 0.2.4

src/ResourceManager/RecoveryServices.SiteRecovery/Commands.RecoveryServices.SiteRecovery.Test/ScenarioTests/Common/AsrTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
1516
using Xunit;
1617
using Xunit.Abstractions;
1718
using Microsoft.Azure.ServiceManagemenet.Common.Models;
1819
using Microsoft.WindowsAzure.Commands.ScenarioTest;
19-
20+
using Microsoft.Azure.Commands.RecoveryServices.SiteRecovery;
21+
using Microsoft.Azure.Portal.RecoveryServices.Models.Common;
2022

2123
namespace RecoveryServices.SiteRecovery.Test
2224
{
@@ -80,5 +82,24 @@ public void V2AGetNotificationTest()
8082
Constants.NewModel,
8183
"Test-NotificationSettings -vaultSettingsFilePath \"" + this.vaultSettingsFilePath + "\"");
8284
}
85+
86+
[Fact]
87+
[Trait(
88+
Category.AcceptanceType,
89+
Category.CheckIn)]
90+
public void CIKTokenValidation()
91+
{
92+
DateTime? dateTime = new DateTime(636604856296924385);
93+
PSRecoveryServicesClient.asrVaultCreds = new ASRVaultCreds();
94+
PSRecoveryServicesClient.asrVaultCreds.ChannelIntegrityKey = "RandomRandom";
95+
96+
var cikToken = PSRecoveryServicesClient.GenerateAgentAuthenticationHeader(
97+
"e5ec3f71-75c6-4688-b557-6ef69d2e7514-2018-04-27 22:43:45Z-Ps",
98+
dateTime);
99+
100+
Assert.Equal(
101+
cikToken,
102+
"{\"NotBeforeTimestamp\":\"\\/Date(1524865429692)\\/\",\"NotAfterTimestamp\":\"\\/Date(1525470229692)\\/\",\"ClientRequestId\":\"e5ec3f71-75c6-4688-b557-6ef69d2e7514-2018-04-27 22:43:45Z-Ps\",\"HashFunction\":\"HMACSHA256\",\"Hmac\":\"cYcaVjQ7BOG/lVrrl7dhwK5WXad6mvQdqm3ce3JSRY4=\",\"Version\":{\"Major\":1,\"Minor\":2,\"Build\":-1,\"Revision\":-1,\"MajorRevision\":-1,\"MinorRevision\":-1},\"PropertyBag\":{}}");
103+
}
83104
}
84105
}

src/ResourceManager/RecoveryServices.SiteRecovery/Commands.RecoveryServices.SiteRecovery/Common/PSAsrClient.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,15 @@ public PSRecoveryServicesClient(
132132
/// Generating that authentication token here and sending it via http headers.
133133
/// </summary>
134134
/// <param name="clientRequestId">Unique identifier for the client's request</param>
135+
/// <param name="dateTime">Optional , datetime used for header genertion</param>
135136
/// <returns>The authentication token for the provider</returns>
136-
public string GenerateAgentAuthenticationHeader(
137-
string clientRequestId)
137+
public static string GenerateAgentAuthenticationHeader(
138+
string clientRequestId,
139+
DateTime? dateTime = null)
138140
{
139141
var cikTokenDetails = new CikTokenDetails();
140142

141-
var currentDateTime = DateTime.Now;
143+
var currentDateTime = dateTime == null ? DateTime.Now:dateTime.Value;
142144
currentDateTime = currentDateTime.AddHours(-1);
143145
cikTokenDetails.NotBeforeTimestamp = TimeZoneInfo.ConvertTimeToUtc(currentDateTime);
144146
cikTokenDetails.NotAfterTimestamp = cikTokenDetails.NotBeforeTimestamp.AddDays(7);
@@ -148,7 +150,12 @@ public string GenerateAgentAuthenticationHeader(
148150
2);
149151
cikTokenDetails.PropertyBag = new Dictionary<string, object>();
150152

151-
var shaInput = JsonConvert.SerializeObject(cikTokenDetails);
153+
JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
154+
{
155+
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
156+
};
157+
158+
var shaInput = JsonConvert.SerializeObject(cikTokenDetails, microsoftDateFormatSettings);
152159

153160
if (null == asrVaultCreds.ChannelIntegrityKey)
154161
{
@@ -160,7 +167,7 @@ public string GenerateAgentAuthenticationHeader(
160167
Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(shaInput)));
161168
cikTokenDetails.HashFunction = CikSupportedHashFunctions.HMACSHA256.ToString();
162169

163-
return JsonConvert.SerializeObject(cikTokenDetails);
170+
return JsonConvert.SerializeObject(cikTokenDetails, microsoftDateFormatSettings);
164171
}
165172

166173
/// <summary>
@@ -225,7 +232,7 @@ public Dictionary<string, List<string>> GetRequestHeaders(
225232
"Agent-Authentication",
226233
new List<string>
227234
{
228-
this.GenerateAgentAuthenticationHeader(this.ClientRequestId)
235+
GenerateAgentAuthenticationHeader(this.ClientRequestId)
229236
});
230237
}
231238
else

0 commit comments

Comments
 (0)