已發(fā)布至github,遠(yuǎn)程依賴即可使用十电,不用將腳本復(fù)制到本地
更加具體使用方式見:鏈接:https://github.com/iimondo/Postman-encryption
問(wèn)題
- postman內(nèi)置加密Api揭保,但不支持RSA加解密碼肥橙。如何用postman進(jìn)入rsa加解密?
- postman中request對(duì)象屬性皆為只讀秸侣,如何把提交時(shí)的明文變?yōu)槊芪?
解決問(wèn)題一
- postman支持eval函數(shù)存筏,我們只要將rsa代碼存入環(huán)境變量中,在需要的時(shí)候調(diào)用eval函數(shù)就可以解決
解決問(wèn)題二
- postman在每次請(qǐng)求時(shí)都會(huì)先執(zhí)行pre-request scripts 中的腳本味榛,在此處我們可以通過(guò)request對(duì)象拿到
此次請(qǐng)求的參數(shù)椭坚,但request中的參數(shù)只可讀。
所以我們只能通過(guò)環(huán)境變量去占位然后在去加密變量中的值搏色,于是在請(qǐng)求時(shí)的內(nèi)容就會(huì)變成加密的內(nèi)容善茎。
針對(duì)postman對(duì){{}}讀取的方式,我們可以先在請(qǐng)求參數(shù)中將要加密的內(nèi)容包裹進(jìn)來(lái)频轿,然后在動(dòng)態(tài)創(chuàng)建此變量垂涯,
并將變量的加密內(nèi)容寫入此環(huán)境變量中,最后在執(zhí)行請(qǐng)求完畢后將此變量清除
例子
AES加密參數(shù)
- 必需用{{}}將內(nèi)容包起來(lái)航邢,因?yàn)樵谶M(jìn)行請(qǐng)求后postman遇到{{}}時(shí)會(huì)從環(huán)境變量中讀取耕赘,如果有該環(huán)境變量則會(huì)動(dòng)態(tài)替換。
-
$符號(hào)后面是我們要加密的內(nèi)容膳殷,可以直接填寫內(nèi)容或?qū)懭氕h(huán)境變量的名稱
動(dòng)態(tài)生成的環(huán)境變量
如果不想在環(huán)境變量夾中顯示動(dòng)態(tài)生成的環(huán)境變量可以將下方tests中的腳本加入到tests中
相關(guān)腳本
- 注意:將腳本加入到collections中會(huì)更好
- Pre-request Scripts
// ------ 通用方法 ------
// 提取{{}}中內(nèi)容
function getBracketStr(text) {
let result = ''
let regex = /\{\{(.+?)\}\}/g;
let options = text.match(regex);
if (options && options.length > 0) {
let option = options[0];
if (option) {
result = option.substring(2, option.length - 2)
}
}
return result
}
// ------ 導(dǎo)入RSA ------
if(!pm.globals.has("forgeJS")){
pm.sendRequest("https://raw.githubusercontent.com/loveiset/RSAForPostman/master/forge.js", (err, res) => {
if (!err) {
pm.globals.set("forgeJS", res.text())
}
})}
eval(postman.getGlobalVariable("forgeJS"));
// ------------ AES 加密 ------------
function aesEncrypt(content){
//console.log('AES: ' + content);
const key = CryptoJS.enc.Utf8.parse("Y5MUIOM7BUWI7BQR");
const iv = CryptoJS.enc.Utf8.parse('S41AXIPFRFVJL73Z');
const encrypted = CryptoJS.AES.encrypt(content, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
return encrypted.toString();
}
// ------------ RSA 加密 ------------
function rsaEncrypt(content){
const pubKey = pm.environment.get("RSA_Public_Key");
if(pubKey){
const publicKey = forge.pki.publicKeyFromPem(pubKey);
const encryptedText = forge.util.encode64(
publicKey.encrypt(content, 'RSAES-PKCS1-V1_5', {
md: forge.md.sha1.create(),
mgf: forge.mgf.mgf1.create(forge.md.sha1.create())
}));
return encryptedText;
}
}
// ------ 存儲(chǔ)所有未加密環(huán)境變量 ------
if(!pm.environment.has('localStore')){
pm.environment.set('localStore', '{}');
}
let localStore = JSON.parse(pm.environment.get('localStore'));
// 獲取當(dāng)前請(qǐng)求中的加密變量
let requestData;
if((typeof request.data) === 'string'){
requestData = JSON.parse(request.data)
} else {
requestData = request.data;
}
requestData = Object.assign(requestData, request.headers);
Object.keys(requestData).map(key => {
let value = requestData[key] + ''; // 內(nèi)容
if (value.indexOf('{{') !== -1) { // 是否為變量
let content = getBracketStr(value);
// 判斷是否加密
if (content.indexOf('aes$') !== -1) {
let c = content.split('aes$')[1];
let encryptedContent = pm.environment.get(c); // 加密內(nèi)容
encryptedContent = encryptedContent ? encryptedContent : c;
pm.environment.set(content, aesEncrypt(encryptedContent));
localStore[content] = aesEncrypt(encryptedContent);
} else if (content.indexOf('rsa$') !== -1) {
let c = content.split('rsa$')[1];
let encryptedContent = pm.environment.get(c); // 加密內(nèi)容
encryptedContent = encryptedContent ? encryptedContent : c;
pm.environment.set(content, rsaEncrypt(encryptedContent));
localStore[content] = rsaEncrypt(encryptedContent);
}
}
});
pm.environment.set('localStore', JSON.stringify(localStore));
- Tests scripts
// 還原變量
if(!pm.environment.has('localStore')){
pm.environment.set('localStore', '{}');
}
let localStore = JSON.parse(pm.environment.get('localStore'));
Object.keys(localStore).map(key => {
pm.environment.unset(key)
});
pm.environment.unset('localStore')