Search code examples
node.jsazure-functionsazure-logic-appspublic-key-encryption

Azure Logic Apps - generated HMAC-SHA256 method signature


I'm looking to build a Logic App workflow to connect to a REST API to GET a list of Products (retuned as JSON) from the Unleashed API ( https://apidocs.unleashedsoftware.com/AuthenticationHelp ).

I've prototyped the GET request in postmaan successfully generated the required HMAC-SHA256 encrypted method signature in the pre-request script. Now I need to find a way to do the same thing in my Logic Apps workflow.

Having figured out that the Logic Apps Inline Code component wouldn't give me anything but basic Javascript (no access to crypto functions), I thought of writing an Azure Function in node.js - node.js chosen primarily in order to be able to reuse my pre-request script code. Unfortunately, I'm getting absolutely nowhere with this - vertical learning curve doesn't quite cover it!

For one thing, isn't there some kind of inline trigger to allow me to access the function rather than having to send a HTTP request? And how do I populate the output bindings to get the hash value back?

My pre-request script is as follows

var urlParam = "modifiedSince2021-04-01";
var url = "https://api.unleashedsoftware.com/" + "Products?" + urlParam;
var hash = CryptoJS.HmacSHA256(urlParam, 'Customer API-KEY here');
var hash64 = CryptoJS.enc.Base64.stringify(hash);

pm.environment.set("BaseURI", url);
pm.environment.set("hmacKey", hash64);

Any help with constructing my node.js, or any other suggestions for turning this into a solution in Azure would be greatly appreciated.


Solution

  • If you want to run your own node code in the azure logic app, you can use Inline Code action or azure function action. But if we want to run node code with some third-party packages, we can only use Azure function action. Meanwhile, when we use the Azure function action, the function must use the HTTP trigger template. For more details, please refer to here and here.

    For example

    1. Define Azure function in your function app
    const CryptoJS = require("crypto-js");
    module.exports = async function (context, req) {
        context.log('JavaScript HTTP trigger function processed a request.');
        //get the data used to sign from request body
          const data = req.body.data
    
       // sign
       const str = CryptoJS.HmacSHA256(
         CryptoJS.enc.Utf8.parse(req.body.data),,
    
        "key"
      );
         const sig = CryptoJS.enc.Base64.stringify(str);
    
         context.res = {
           
            body: sig // return the hash
         };
    }
    
    1. Call the function in Azure logic app

    enter image description here

    1. Use the hash data which returns from azure function to call the API. We can use the expression @{body('<action name>')} to get the hash data. For example enter image description here

    2. Test Result enter image description here