Skip to content

[RecoveryServices][SiteRecovery] CIK token serialize using System.Web.Script.Serialization.JavaScriptSerializer. #6062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Current Release
* Fixed Authentication Header
* Set minimum dependency of module to PowerShell 5.0

## Version 0.2.4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using Xunit;
using Xunit.Abstractions;
using Microsoft.Azure.ServiceManagemenet.Common.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;

using Microsoft.Azure.Commands.RecoveryServices.SiteRecovery;
using Microsoft.Azure.Portal.RecoveryServices.Models.Common;

namespace RecoveryServices.SiteRecovery.Test
{
Expand Down Expand Up @@ -80,5 +82,24 @@ public void V2AGetNotificationTest()
Constants.NewModel,
"Test-NotificationSettings -vaultSettingsFilePath \"" + this.vaultSettingsFilePath + "\"");
}

[Fact]
[Trait(
Category.AcceptanceType,
Category.CheckIn)]
public void CIKTokenValidation()
{
DateTime? dateTime = new DateTime(636604856296924385);
PSRecoveryServicesClient.asrVaultCreds = new ASRVaultCreds();
PSRecoveryServicesClient.asrVaultCreds.ChannelIntegrityKey = "RandomRandom";

var cikToken = PSRecoveryServicesClient.GenerateAgentAuthenticationHeader(
"e5ec3f71-75c6-4688-b557-6ef69d2e7514-2018-04-27 22:43:45Z-Ps",
dateTime);

Assert.Equal(
cikToken,
"{\"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\":{}}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,15 @@ public PSRecoveryServicesClient(
/// Generating that authentication token here and sending it via http headers.
/// </summary>
/// <param name="clientRequestId">Unique identifier for the client's request</param>
/// <param name="dateTime">Optional , datetime used for header genertion</param>
/// <returns>The authentication token for the provider</returns>
public string GenerateAgentAuthenticationHeader(
string clientRequestId)
public static string GenerateAgentAuthenticationHeader(
string clientRequestId,
DateTime? dateTime = null)
{
var cikTokenDetails = new CikTokenDetails();

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

var shaInput = JsonConvert.SerializeObject(cikTokenDetails);
JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};

var shaInput = JsonConvert.SerializeObject(cikTokenDetails, microsoftDateFormatSettings);

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

return JsonConvert.SerializeObject(cikTokenDetails);
return JsonConvert.SerializeObject(cikTokenDetails, microsoftDateFormatSettings);
}

/// <summary>
Expand Down Expand Up @@ -225,7 +232,7 @@ public Dictionary<string, List<string>> GetRequestHeaders(
"Agent-Authentication",
new List<string>
{
this.GenerateAgentAuthenticationHeader(this.ClientRequestId)
GenerateAgentAuthenticationHeader(this.ClientRequestId)
});
}
else
Expand Down