Skip to content

Commit

Permalink
fix(openapi): filter key with undefined-value in sig calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinzZH committed Jun 21, 2018
1 parent b9d6dd0 commit 69c44f4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
41 changes: 20 additions & 21 deletions bin/tsw/util/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,37 @@
*/
'use strict';


const crypto = require('crypto');

/**
* 计算openapi签名
* @param {[string]} method 请求方法 GET/POST
* @param {[string]} pathname 请求路径
* @param {[object]} data 请求数据
* @param {[string]} appkey 应用appkey
* @return {[string]} sig 签名结果
* @param {object} opt 签名数据
* @param {string} opt.method 请求方法 GET/POST
* @param {string} opt.pathname 请求路径
* @param {object} opt.data 请求数据
* @param {string} opt.appkey 应用appkey
* @return {string} sig 签名结果
*/
this.signature = function(opt) {
this.signature = opt => {
opt = opt || {};

const queryArray = [];

const busidataArr = [opt.method, encode(opt.pathname)]; // HTTP请求方式 & encode(uri) & encode(a=x&b=y&...)

let i;
for (i in opt.data) {
i !== 'sig' && queryArray.push(i + '=' + opt.data[i]);
for (const i in opt.data) {
// i !== 'sig' && queryArray.push(i + '=' + opt.data[i]); // 过滤掉undefined value的key,因为发送的时候data,会做JSON.stringify, 没有定义值的key会被过滤掉
if (typeof opt.data[i] !== 'undefined' && i !== 'sig') {
queryArray.push(i + '=' + opt.data[i]);
}
}

queryArray.sort(function(val1, val2) {
queryArray.sort((val1, val2) => {
if (val1 > val2) {
return 1;
}

if (val1 < val2) {
} else if (val1 < val2) {
return -1;
}

return 0;
});

Expand All @@ -49,14 +48,14 @@ this.signature = function(opt) {
};

// encode
function encode(str) {
const encode = (str = '') => {
let res = encodeURIComponent(str);

// 0~9 a~z A~Z !*()
res = res.replace(/[^0-9a-zA-Z\-_.%]/g, function($0) {
// 不用考虑一位数了
return '%' + $0.charCodeAt(0).toString(16).toUpperCase();
});
// 不用考虑一位数了
res = res.replace(/[^0-9a-zA-Z\-_.%]/g, ($0) =>
'%' + $0.charCodeAt(0).toString(16).toUpperCase()
);

return res;
}
};
30 changes: 30 additions & 0 deletions test/bin/tsw/util/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,35 @@ describe('test openapi', () => {
};
expect(openapi.signature(opt)).to.equal('RaeBv27oo3hOlDAYLXorp4zYhbs=');
});

it('#test undefined value key', () => {
let undefinedValue;
const opt = {
method: 'get',
pathname: '/api',
data: { a: 1, key: undefinedValue }
};
expect(openapi.signature(opt)).to.equal('grX3/m7QAZ19L5WXM6xDzmXQa8s=');
});

it('#test null value key', () => {
const undefinedValue = null;
const opt = {
method: 'get',
pathname: '/api',
data: { a: 1, key: undefinedValue }
};
expect(openapi.signature(opt)).to.equal('3hfVrbEv5VKMYu9QP4kB6rtBd4o=');
});

it('#test empty value key', () => {
const undefinedValue = '';
const opt = {
method: 'get',
pathname: '/api',
data: { a: 1, key: undefinedValue }
};
expect(openapi.signature(opt)).to.equal('AGMogi7bjCvtnnCecK+TnZGzR3M=');
});
});
});

0 comments on commit 69c44f4

Please sign in to comment.