Last active
April 8, 2021 02:44
-
-
Save andhikayuana/3cfb5a7b6921cbe8cc3ffa345056b7c2 to your computer and use it in GitHub Desktop.
postman prescript for generate hmac 256 signature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author andhikayuana | |
*/ | |
const moment = require('moment'); | |
function getPath(url) { | |
var pathRegex = /.+?\:\/\/.+?(\/.+?)(?:#|\?|$)/; | |
var result = url.match(pathRegex); | |
return result && result.length > 1 ? result[1] : ''; | |
} | |
function getQueryString(url) { | |
var arrSplit = url.split('?'); | |
return arrSplit.length > 1 ? url.substring(url.indexOf('?')+1) : ''; | |
} | |
function getTimestamp() { | |
return moment().format('YYYY-MM-DDTHH:mm:ss.SSZ'); | |
} | |
function generateSignature() { | |
const apiKey = pm.environment.get("API_KEY"); | |
const secretKey = pm.environment.get("SECRET_KEY"); | |
const queryString = getQueryString(request['url']); | |
const fromUrl = queryString.length == 0 ? getPath(request['url']) : `${getPath(request['url'])}?${queryString}`; | |
const input = `${request['method']}:${fromUrl}:${apiKey}:${getTimestamp()}`; | |
const hash = CryptoJS.HmacSHA256(input, secretKey); | |
const hashInBase64 = CryptoJS.enc.Base64.stringify(hash); | |
return hashInBase64; | |
} | |
postman.setEnvironmentVariable("SIGNATURE", generateSignature()); | |
postman.setEnvironmentVariable("TIMESTAMP", getTimestamp()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author andhikayuana | |
*/ | |
const moment = require('moment'); | |
function getPath(url) { | |
var pathRegex = /.+?\:\/\/.+?(\/.+?)(?:#|\?|$)/; | |
var result = url.match(pathRegex); | |
return result && result.length > 1 ? result[1] : ''; | |
} | |
function getQueryString(url) { | |
var arrSplit = url.split('?'); | |
return arrSplit.length > 1 ? url.substring(url.indexOf('?')+1) : ''; | |
} | |
function getTimestamp() { | |
return moment().format('YYYY-MM-DDTHH:mm:ss.SSSZ'); | |
} | |
function generateSignature() { | |
const apiKey = pm.environment.get("API_KEY"); | |
const secretKey = pm.environment.get("SECRET_KEY"); | |
const queryString = getQueryString(request['url']); | |
const fromUrl = queryString.length == 0 ? getPath(request['url']) : `${getPath(request['url'])}?${queryString}`; | |
const timestamp = getTimestamp(); | |
postman.setEnvironmentVariable("TIMESTAMP", timestamp); | |
let input = `${request['method']}:${fromUrl}:${apiKey}:${timestamp}`; | |
if (request['method'] == 'POST') { | |
const requestData = JSON.stringify(JSON.parse(request['data'])); | |
input = `${input}:${requestData}`; | |
} | |
const hash = CryptoJS.HmacSHA256(input, secretKey); | |
const hashInBase64 = CryptoJS.enc.Base64.stringify(hash); | |
return hashInBase64; | |
} | |
postman.setEnvironmentVariable("SIGNATURE", generateSignature()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment