fix: api请求验证

This commit is contained in:
chaosBreaking
2019-09-29 23:10:13 +08:00
parent 624f34e1ed
commit f0115b3c26
3 changed files with 19 additions and 9 deletions

View File

@@ -144,7 +144,7 @@ async function confirm (job = mock, done = mock) {
mylog.info(`用户${fund.userId} 共更新${acc}`);
job.touch();
});
return done('矿晶确认完成');
return typeof done === 'function' && done('矿晶确认完成');
}
const jobs = [
{

View File

@@ -1,11 +1,23 @@
const { inject } = require('../common/Provider');
const { verifyToken, createToken } = require('../utils/jwt');
const crypto = require('../utils/crypto');
const ignoreToken = ['/sign/check', '/sign/in', '/sign/up', '/sign/upwi', '/sign/forget', '/sign/send', '/sign/changePwd', '/sign/code'];
module.exports = {
auth: inject(function (SysConfig) {
const { JWT_SECRET } = SysConfig;
if (!SysConfig.LIMITSALT) mylog.error('缺少限流密钥');
return async (req, res, next) => {
const token = req.headers.token;
if (ignoreToken.includes(req.path)) {
const { sig, ts } = req.headers;
const salt = SysConfig.LIMITSALT;
const my = crypto.hash(ts + req.path + salt, { hasher: 'md5' });
const invalid = sig !== my || Date.now() - ts > 60000;
if (invalid) {
return res.status(403).send('unauthorized');
}
}
if(!ignoreToken.includes(req.path)) {
if (token) {
const deToken = await verifyToken(token, JWT_SECRET);

View File

@@ -66,7 +66,7 @@ module.exports = {
let outputEncoding = (option.output === 'buf') ? undefined : (my.OUTPUT_LIST.indexOf(option.output) >= 0 ? option.output : my.OUTPUT); // 'latin1', 'base64', 'hex' by default or 'buf' to Buffer explicitly
let cipher = crypto.createCipher(
my.CIPHER_LIST.indexOf(option.cipher) >= 0 ? option.cipher : my.CIPHER,
this.hash(pwd));
this.hash(pwd, option)); //哈希加盐
if (typeof (data) !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView))
data = JSON.stringify(data);
let encrypted = cipher.update(data, inputEncoding, outputEncoding);
@@ -83,16 +83,14 @@ module.exports = {
let outputEncoding = (option.output === 'buf') ? undefined : (my.INPUT_LIST.indexOf(option.output) >= 0 ? option.output : my.INPUT); // output (=input of encrypt) could be 'latin1', 'ascii', 'utf8' by default or 'buf' to Buffer explicitly
let decipher = crypto.createDecipher(
my.CIPHER_LIST.indexOf(option.cipher) >= 0 ? option.cipher : my.CIPHER,
this.hash(pwd));
this.hash(pwd, option)); // 哈希加盐
let decrypted = decipher.update(data, inputEncoding, outputEncoding);
decrypted += decipher.final(outputEncoding); // 但是 Buffer + Buffer 还是会变成string
if (option.format === 'json') { // 如果用户输入错误密码deciper也能返回结果。为了判断是否正确结果对应当是 json 格式的原文做解析来验证。
try {
JSON.parse(decrypted);
} catch (exception) {
return null;
}
}
return decrypted;
}
return null;