I have a C# script embedded in an ‘Execute CSharp Script Code’ object on an Azure workflow.
This takes an external HMAC key passed in the header and some JSON text in the body as input and needs to calculate a new HMAC locally and compare the two for authenticity.
My HMAC strings aren’t matching because the output body type is as shown below when the original is formatted.
My testing script:
// Add the required libraries
#r "Newtonsoft.Json"
#r "Microsoft.Azure.Workflows.Scripting"
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Workflows.Scripting;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// Executes the inline csharp code.
/// </summary>
/// <param name="context">The workflow context.</param>
/// <remarks> This is the entry-point to your code. The function signature should remain unchanged.</remarks>
public static async Task<Results> Run(WorkflowContext context, ILogger log)
{
//String Variables (for clarity)
string compare = string.Empty;
string _origonalMessage = string.Empty;
string _origonalHmac = string.Empty;
string _calculatedHmac = string.Empty;
////Define the trigger output.
var triggerOutputs = (await context.GetTriggerResults().ConfigureAwait(false)).Outputs;
////Set the local HMAC key from the Azure Key Vault.
string secretKey = "HMAC-GG-TEST"; // <-- Temporary for testing.
//I need to make a key vault get the secret from this.
//Maybe something like: var secretKeysTEST = await client.GetSecretAsync(HMAC);
////Set the whole JSON body text from the trigger.
var origonalMessage = triggerOutputs["body"].ToString();
_origonalMessage = origonalMessage;
////Set the remote HMAC encrypted text from the Azure header. (needs to be the input header)
var receievedHmac = triggerOutputs?["headers"]?["HMACEncripted"]?.ToString();
_origonalHmac = receievedHmac;
////Encrypt the body text with the local HMAC key and compare with the remote Encrypted text.
//convert key and message to byte arrays
byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);
byte[] messageBytes = Encoding.UTF8.GetBytes(origonalMessage);
//Create an HMAC SHA256 instance with the local key
using (HMACSHA256 hmac = new HMACSHA256(keyBytes))
{
//Compute the HMAC and convert it to a string
byte[] hashBytes = hmac.ComputeHash(messageBytes);
var localHmac = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
_calculatedHmac = localHmac;
}
//Compare the computed HMAC with the received HMAC
string testString = "GAURAV";
if (_calculatedHmac.Equals(receievedHmac, StringComparison.OrdinalIgnoreCase))
{
compare = "Match";
}
else
{
compare = "noMatch: Calc=" + _calculatedHmac + " Orig=" + _origonalHmac + " Body=" + _origonalMessage;
;
}
return new Results
{
Message = compare
};
}
public class Results
{
public string Message {get; set;}
}
Output from the script:
{
"body": {
"message": "noMatch: Calc=62c72a210402ffc1cefed5547192fd14e44ef1dfdc012f3279940ab5f35105b8
Orig=af9d5fe5216e4a3c731cc864ddc0dba66ae22b68605678fd2a3c381dc5c72025
Body={rn "id": 13,rn "baseprofile": {rn "id": 3,rn "value": "Accountant",rn "profile": "https://api.icims.com/customers/1234/jobs/3/"rn },rn "associatedprofile": {rn "id": 5,rn "value": "JohnSmith",rn "profile": "https://api.icims.com/customers/1234/people/5/"rn },rn "status": {rn "id": "D5432",rn "value": "InterviewScheduled"rn },rn "source": "iCIMS.com",rn "sourcename": "Internal"rn}"
}
}
Origonal formated JSON is like this:
{
"id": 13,
"baseprofile": {
"id": 3,
"value": "Accountant",
"profile": "https://api.icims.com/customers/1234/jobs/3/"
},
"associatedprofile": {
"id": 5,
"value": "JohnSmith",
"profile": "https://api.icims.com/customers/1234/people/5/"
},
"status": {
"id": "D5432",
"value": "InterviewScheduled"
},
"source": "iCIMS.com",
"sourcename": "Internal"
}
So, how do you compare the local and remote HMAC strings specifically in an Azure Workflow ‘CSharp Script Code’ object calculated on the input body text, whilst preserving the formatting? When looking at the input and output JSON text through Postman the formatting is preserved and the text looks the same.
Any help would be appreciated.
1