fix: api请求验证
This commit is contained in:
@@ -144,7 +144,7 @@ async function confirm (job = mock, done = mock) {
|
|||||||
mylog.info(`用户${fund.userId} 共更新${acc}条`);
|
mylog.info(`用户${fund.userId} 共更新${acc}条`);
|
||||||
job.touch();
|
job.touch();
|
||||||
});
|
});
|
||||||
return done('矿晶确认完成');
|
return typeof done === 'function' && done('矿晶确认完成');
|
||||||
}
|
}
|
||||||
const jobs = [
|
const jobs = [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,11 +1,23 @@
|
|||||||
const { inject } = require('../common/Provider');
|
const { inject } = require('../common/Provider');
|
||||||
const { verifyToken, createToken } = require('../utils/jwt');
|
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'];
|
const ignoreToken = ['/sign/check', '/sign/in', '/sign/up', '/sign/upwi', '/sign/forget', '/sign/send', '/sign/changePwd', '/sign/code'];
|
||||||
module.exports = {
|
module.exports = {
|
||||||
auth: inject(function (SysConfig) {
|
auth: inject(function (SysConfig) {
|
||||||
const { JWT_SECRET } = SysConfig;
|
const { JWT_SECRET } = SysConfig;
|
||||||
|
if (!SysConfig.LIMITSALT) mylog.error('缺少限流密钥');
|
||||||
return async (req, res, next) => {
|
return async (req, res, next) => {
|
||||||
const token = req.headers.token;
|
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(!ignoreToken.includes(req.path)) {
|
||||||
if (token) {
|
if (token) {
|
||||||
const deToken = await verifyToken(token, JWT_SECRET);
|
const deToken = await verifyToken(token, JWT_SECRET);
|
||||||
|
|||||||
@@ -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 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(
|
let cipher = crypto.createCipher(
|
||||||
my.CIPHER_LIST.indexOf(option.cipher) >= 0 ? option.cipher : my.CIPHER,
|
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))
|
if (typeof (data) !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView))
|
||||||
data = JSON.stringify(data);
|
data = JSON.stringify(data);
|
||||||
let encrypted = cipher.update(data, inputEncoding, outputEncoding);
|
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 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(
|
let decipher = crypto.createDecipher(
|
||||||
my.CIPHER_LIST.indexOf(option.cipher) >= 0 ? option.cipher : my.CIPHER,
|
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);
|
let decrypted = decipher.update(data, inputEncoding, outputEncoding);
|
||||||
decrypted += decipher.final(outputEncoding); // 但是 Buffer + Buffer 还是会变成string
|
decrypted += decipher.final(outputEncoding); // 但是 Buffer + Buffer 还是会变成string
|
||||||
if (option.format === 'json') { // 如果用户输入错误密码,deciper也能返回结果。为了判断是否正确结果,对应当是 json 格式的原文做解析来验证。
|
|
||||||
try {
|
try {
|
||||||
JSON.parse(decrypted);
|
JSON.parse(decrypted);
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return decrypted;
|
return decrypted;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user