Commit 43dc2eca by zCaesar

check token and role

parent 8b4f9441
var jwt = require('jsonwebtoken')
var config = require('config')
function checkRealDB(token, cb) {
const verifyOptions = {
algorithms: ['RS256']
};
jwt.verify(token, config.get('pubca'), verifyOptions, (err, decoded) => {
cb(err, decoded)
})
}
module.exports.checkRealDB = checkRealDB
function getRole(token, cb) {
var secret = 'nexpie'
jwt.verify(token, secret, (err, decoded) => {
if (err) {
// console.log(err)
cb(false)
}
else {
// console.log(decoded)
cb(decoded)
}
})
}
module.exports.getRole = getRole
function signRole(req, res) {
var secret = 'nexpie'
if (req.body.scope && req.body.exp) {
const signOptions = {
expiresIn: getExp(req.body.exp)
}
var payload = req.body
delete payload['exp']
res.send(jwt.sign(payload, secret, signOptions))
}
else {
res.send('role not complete')
}
}
module.exports.signRole = signRole
function getExp(expires) {
var exp
if (expires.endsWith('y')) {
exp = getYears(expires.split('y')[0])
}
else if (expires.endsWith('m')) {
exp = getMonths(expires.split('m')[0])
}
else if (expires.endsWith('d')) {
exp = expires.split('d')[0] + 'd'
}
else if (expires.endsWith('h')) {
exp = expires.split('h')[0] + 'h'
}
else if (expires.endsWith('mi')) {
exp = getMinutes(expires.split('mi')[0])
}
else if (expires.endsWith('s')) {
exp = getSeconds(expires.split('s')[0])
}
else exp = getYears(10)
return exp
}
function getMonths(d) {
return (d * 30) + 'd'
}
function getYears(m) {
return (m * 30 * 12) + 'd'
}
function getMinutes(mi) {
return (mi * 1000 * 60) + 'ms'
}
function getSeconds(ms) {
return (ms * 1000) + 'ms'
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment