<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Error while trying to use preprocessing script in postman in APIs</title>
    <link>https://community.cisco.com/t5/apis/error-while-trying-to-use-preprocessing-script-in-postman/m-p/4881904#M392</link>
    <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;Hey thanks for the script, Now it worked for me in postman.&lt;/P&gt;&lt;/DIV&gt;</description>
    <pubDate>Fri, 05 May 2023 19:42:04 GMT</pubDate>
    <dc:creator>Apoorva_Anne</dc:creator>
    <dc:date>2023-05-05T19:42:04Z</dc:date>
    <item>
      <title>Error while trying to use preprocessing script in postman</title>
      <link>https://community.cisco.com/t5/apis/error-while-trying-to-use-preprocessing-script-in-postman/m-p/4881902#M390</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;I am using the below script in preprocessing step in POSTMAN but it is throwing an error&lt;/P&gt;
&lt;P&gt;code": 40103,&lt;BR /&gt;
“message”: “Invalid signature in request credentials”,&lt;BR /&gt;
“stat”: “FAIL”&lt;/P&gt;
&lt;P&gt;“PRE PROCESSING SCRIPT”&lt;/P&gt;
&lt;P&gt;function getAuthHeader(httpMethod, requestUrl, requestBody) { console.log(requestBody); //body data console.log(httpMethod); // http type: POST, GET, ETC&lt;BR /&gt;
var CLIENT_KEY = ‘xxxxxxx’;&lt;BR /&gt;
var SECRET_KEY = ‘xxxxxxx’;&lt;BR /&gt;
//var AUTH_TYPE = ‘HMAC-SHA1’;&lt;BR /&gt;
var moment = require(‘moment’)&lt;BR /&gt;
//const moment= require(‘moment’);&lt;BR /&gt;
/* Uncomment out lines below to use your test for getting correct formatted time and date */&lt;BR /&gt;
var timestamp = moment().format(‘ddd, DD MMM YYYY HH:mm:ss ZZ’);&lt;BR /&gt;
//var timestamp1 = moment().utcOffset(‘timestamp’);&lt;BR /&gt;
//var timestamp1 = timestamp.toUTCString();&lt;BR /&gt;
pm.environment.set(“timestampHeader”,timestamp);&lt;BR /&gt;
//var timestamp = “Mon, 24 Apr 2023 16:38:18 -0600” ;&lt;BR /&gt;
&lt;A href="https://pm.environment.set" rel="noopener nofollow ugc"&gt;//pm.environment.set&lt;/A&gt;(“timestampHeader”,timestamp);&lt;BR /&gt;
var hostname = “&lt;A href="http://api-xxxx.duosecurity.com" rel="noopener nofollow ugc"&gt;■■■■■■■■■■■■■■■■■■■■■■■■&lt;/A&gt;”;&lt;BR /&gt;
var apicall = ‘/admin/v1/users’&lt;BR /&gt;
var method = ‘POST’&lt;BR /&gt;
var body = ‘username=xxxx’ //sample username&lt;BR /&gt;
var requestData = timestamp +‘\n’+method+‘\n’+ hostname +‘\n’+ apicall +‘\n’+ body;&lt;BR /&gt;
//requestData = requestData.normalize([‘NFC’]);&lt;BR /&gt;
console.log(requestData);&lt;BR /&gt;
var hmacDigest = CryptoJS.HmacSHA1(requestData, SECRET_KEY);&lt;BR /&gt;
console.log(hmacDigest);&lt;/P&gt;
&lt;P&gt;var prebase = CLIENT_KEY+“:”+hmacDigest;&lt;BR /&gt;
console.log(prebase);&lt;BR /&gt;
var baseComplete = btoa(prebase);&lt;BR /&gt;
console.log(baseComplete);&lt;BR /&gt;
var authHeader = "Basic "+baseComplete;&lt;BR /&gt;
return authHeader;&lt;BR /&gt;
}&lt;BR /&gt;
postman.setEnvironmentVariable(‘hmacAuthHeader’, getAuthHeader(request[‘method’], request[‘url’], request[‘data’]));&lt;/P&gt;
&lt;P&gt;Headers :&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;DIV class="lightbox-wrapper"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="2X_8_81670d4dda7c1902fc378c84d285c4e9b9682a95.png" style="width: 690px;"&gt;&lt;img src="https://community.cisco.com/t5/image/serverpage/image-id/190615iE20EE80EF6D00CEA/image-size/large?v=v2&amp;amp;px=999" role="button" title="2X_8_81670d4dda7c1902fc378c84d285c4e9b9682a95.png" alt="2X_8_81670d4dda7c1902fc378c84d285c4e9b9682a95.png" /&gt;&lt;/span&gt;&lt;/DIV&gt;&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I have seen few similar posts on this script, but no one is clear on how they made it working.&lt;/P&gt;
&lt;P&gt;So can someone help on what I am missing in the script and to convert time to UTC?&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 27 Apr 2023 17:36:05 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/error-while-trying-to-use-preprocessing-script-in-postman/m-p/4881902#M390</guid>
      <dc:creator>Apoorva_Anne</dc:creator>
      <dc:date>2023-04-27T17:36:05Z</dc:date>
    </item>
    <item>
      <title>Re: Error while trying to use preprocessing script in postman</title>
      <link>https://community.cisco.com/t5/apis/error-while-trying-to-use-preprocessing-script-in-postman/m-p/4881903#M391</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;Hi &lt;A class="mention" href="https://community.duo.com/u/apoorva_anne"&gt;@Apoorva_Anne&lt;/A&gt; ,&lt;/P&gt;
&lt;P&gt;You might have already seen this lengthy thread regarding the topic: &lt;A href="https://community.duo.com/t/preauth-api-failing-but-check-and-ping-are-good/3125/14" class="inline-onebox"&gt;Preauth API failing but check and ping are good - #14 by testdemo_user&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;I will paste the pre-request script that I have been using successfully (taken from the above thread). You should only need to replace the ikey, skey and API hostname. Keep the Headers that you already have.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class="lang-auto"&gt;
//Tested on Postman for Web Version 10.13.6
 
function getAuthHeader(httpMethod, requestUrl, requestBody) {
    console.log(requestUrl); // Full URL
    console.log(httpMethod); // http type: POST, GET, ETC
    console.log(requestBody); //POST Body, currently not used
     
    var IKEY = '&amp;lt;ikey&amp;gt;'; //Auth or Admin API IKEY
    var SKEY = '&amp;lt;skey&amp;gt;'; //Auth or Admin API SKEY
    var API_HOSTNAME = '&amp;lt;api-hostname&amp;gt;'; //Auth or Admin API Hostname
    var AUTH_TYPE = 'HMAC-SHA1';
     
    //Adds a ? at the end of the URL even if there aren't any parameters, makes it easier to find the end of the api_call
    if (requestUrl.indexOf("?") == -1) {
        requestUrl += "?";
    }
     
    var paramsStart = requestUrl.indexOf("?");
    var hostname_length = API_HOSTNAME.length + 8;                  //api_hostname + https://
    var api_call = requestUrl.slice(hostname_length, paramsStart);  //remove hostname and params to get the api_call
    console.log(api_call);
     
    var params_unsorted = "";
    var params_array = [];
    //Create unsorted array of parameters from either URL or Body
    //Assuming POST parameters are in the Body, GET paramters are in URL
    //(technically it looks like POST can be in the URL as well)
 
    if (Object.keys(requestBody).length !== 0) {
        for (var parameter1 in requestBody) {
            params_unsorted = parameter1 + "=" + requestBody[parameter1];
            params_array.push(params_unsorted);
        }
    } else {
        params_unsorted = requestUrl.substring(paramsStart+1);
        params_array = params_unsorted.split("&amp;amp;");
    }
    console.log(params_array);
     
    params_array.sort();                    //lexicographically sort parameters by key
    var encoded_params = "";               
    if (params_array[0] !== ""){            //check if there are any Params to encode and create the string from
        var encoded_params_array = params_array.map(urlEncodeParams);   //create URL-encoded array of key=value pairs from the sorted array
        encoded_params = encoded_params_array.join("&amp;amp;");                //create string of parameters joined by &amp;amp;
    }
    console.log(encoded_params);
     
    //The current time, formatted as RFC 2822. This must be the same string as the "Date" header (or X-Duo-Date header).
    var moment = require('moment');
    var timestamp = moment().format("ddd, DD MMM YYYY HH:mm:ss ZZ");
    pm.environment.set("timestampHeader",timestamp);
 
    //Then concatenate these components with (line feed) newlines
    var requestData =  timestamp+"\n"+httpMethod+"\n"+API_HOSTNAME+"\n"+api_call+"\n"+encoded_params;
    console.log(requestData);
 
    //compute the HMAC-SHA1 of this canonical representation, using your Duo application's secret key as the HMAC key
    var hmacDigest = CryptoJS.HmacSHA1(requestData, SKEY);
    console.log(hmacDigest);
     
    //Use HTTP Basic Authentication for the request, using your integration key as the username and the HMAC-SHA1 signature as the password.
    var prebase = IKEY+":"+ hmacDigest;
    console.log(prebase);
     
    //encodes a string in base-64
    var baseComplete = btoa(prebase);
    console.log(baseComplete);
 
    var authHeader = "Basic "+ baseComplete;
    return authHeader;
}
 
function urlEncodeParams(value, index, array) {
    //split the Key and Value at the first "=", URL-encode the Value, and rejoin them with an "="
    //If the separator in the split() is a regular expression that contains capturing parentheses (), matched results are included in the array
    return value.split(/=(.+)/)[0] + "=" + encodeURIComponent(value.split(/=(.+)/)[1]);
}
 
pm.environment.set("hmacAuthHeader", getAuthHeader(request.method, request.url, request.data));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can then perform the &lt;CODE&gt;GET&lt;/CODE&gt; or &lt;CODE&gt;POST&lt;/CODE&gt; to the endpoint of your choice (which initially appears to be &lt;CODE&gt;https://■■■■■■■■■■■■■■■■■■■■■■■■■■■■/admin/v1/users&lt;/CODE&gt;) in the request field:&lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="2X_a_a294f05ac40c0d56ca238eade7348d819497c8f2.png" style="width: 470px;"&gt;&lt;img src="https://community.cisco.com/t5/image/serverpage/image-id/191230iB132748E6A600F77/image-size/large?v=v2&amp;amp;px=999" role="button" title="2X_a_a294f05ac40c0d56ca238eade7348d819497c8f2.png" alt="2X_a_a294f05ac40c0d56ca238eade7348d819497c8f2.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Hope this helps!&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 29 Apr 2023 01:42:51 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/error-while-trying-to-use-preprocessing-script-in-postman/m-p/4881903#M391</guid>
      <dc:creator>DuoPablo</dc:creator>
      <dc:date>2023-04-29T01:42:51Z</dc:date>
    </item>
    <item>
      <title>Re: Error while trying to use preprocessing script in postman</title>
      <link>https://community.cisco.com/t5/apis/error-while-trying-to-use-preprocessing-script-in-postman/m-p/4881904#M392</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;Hey thanks for the script, Now it worked for me in postman.&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 05 May 2023 19:42:04 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/error-while-trying-to-use-preprocessing-script-in-postman/m-p/4881904#M392</guid>
      <dc:creator>Apoorva_Anne</dc:creator>
      <dc:date>2023-05-05T19:42:04Z</dc:date>
    </item>
  </channel>
</rss>

