You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.2 KiB
43 lines
1.2 KiB
const crypto = require('crypto')
|
|
const exec = require('child_process').exec
|
|
const CFG = require('../config')
|
|
|
|
const tools = {}
|
|
|
|
tools.createHash = function (pData, pHash, pSalt) {
|
|
let hash = pHash || 'md5'
|
|
let data = typeof pData === 'object' ? JSON.stringify(pData) : pData
|
|
if (pSalt) {
|
|
data = `${data}$-|-$${pSalt}`
|
|
}
|
|
return crypto.createHash(hash).update(data).digest('hex')
|
|
}
|
|
|
|
tools.buildClientInfo = function (req) {
|
|
return {
|
|
uaHash: tools.createHash(req.headers['user-agent']),
|
|
ip: req.clientIp
|
|
}
|
|
}
|
|
|
|
tools.jwt = {}
|
|
tools.jwt.checkPayload = function (req, pPayload) {
|
|
let clientInfo = tools.buildClientInfo(req)
|
|
if (!pPayload.user) return 'user'
|
|
if (!pPayload.user.id) return 'user.id'
|
|
if (!pPayload.user.pass) return 'user.pass'
|
|
if (!pPayload.clientInfo) return 'clientInfo'
|
|
if (pPayload.clientInfo.ip !== clientInfo.ip) return 'clientInfo.ip'
|
|
if (pPayload.clientInfo.uaHash !== clientInfo.uaHash) return 'clientInfo.uaHash'
|
|
if (pPayload.sid !== CFG.jwt.sid) return 'clientInfo.sid'
|
|
return null
|
|
}
|
|
|
|
tools.git = {}
|
|
tools.git.currentRevision = '---'
|
|
|
|
exec('git rev-parse --short HEAD | xargs echo -n', function (pErr, pStdout) {
|
|
if (pStdout) tools.git.currentRevision = pStdout
|
|
})
|
|
|
|
module.exports = tools
|