Skip to content

Instantly share code, notes, and snippets.

@skrosoft
Last active July 30, 2022 06:11
Show Gist options
  • Save skrosoft/3ecfb4fc09a38afe64b83e17f016e1c5 to your computer and use it in GitHub Desktop.
Save skrosoft/3ecfb4fc09a38afe64b83e17f016e1c5 to your computer and use it in GitHub Desktop.
Pre-request Script para Postman para firmar automáticamente las peticiones en POSTMAN
const SECRET="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
const CRYPTOJS_CDN="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js";
const JSONABC_CDN="https://novicelab.org/jsonabc/dist/jsonabc.js";
(()=>{pm.sendRequest(CRYPTOJS_CDN,(e,r)=>{eval(r.text()),pm.sendRequest(JSONABC_CDN,(e,r)=>{const c=eval(r.text())("jsonabc"),g=(r,e)=>{r=c.sortObj(r);let t="";return Object.keys(r).sort().forEach(e=>{"signature"!==e&&r.hasOwnProperty(e)&&(t+=e+JSON.stringify(r[e]))}),CryptoJS.HmacSHA256(t,e).toString(CryptoJS.enc.Hex)};if("GET"===pm.request.method){const p={};pm.request.url.query.each(e=>{e.disabled||(p[e.key]=e.value)}),pm.request.url.query.add({key:"signature",value:g(p,SECRET)})}else{const p=JSON.parse(pm.request.body.raw);p.signature=g(p,SECRET),pm.request.body.raw=JSON.stringify(p)}})})})();
@skrosoft
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment