MCSManager/route/token.js

46 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-11-13 12:26:31 +08:00
//基础的路由定义
const router = require('express')();
const response = require('../helper/Response');
const permssion = require('../helper/Permission');
const VarCenter = require('../model/VarCenter');
const counter = require('../core/counter');
2018-04-03 08:36:45 +08:00
const UUID = require('uuid');
2017-11-13 12:26:31 +08:00
//Token
router.get('/', function (req, res) {
let username = req.session['username'] || undefined;
2017-11-13 12:26:31 +08:00
//ajax 会受到浏览器跨域限制姑不能对其进行csrf攻击获取token尽管它可伪造。
if (req.xhr) {
if (!req.session['token']) {
MCSERVER.log('[ Token ]', '用户 ', username, ' 请求更新令牌');
2018-04-02 23:53:48 +08:00
//强化 token
2018-04-03 08:16:55 +08:00
req.session['token'] = permssion.randomString(6) + UUID.v4().replace(/-/igm, "");
2017-11-13 12:26:31 +08:00
}
if (username == undefined || username.trim() == '' || !req.session['login']) {
2018-04-03 08:36:45 +08:00
//用户未登录,返回一个随机的 token 给它,并且这个 token 与正常的 token 几乎一模一样
response.returnMsg(res, 'token', {
token: permssion.randomString(6) + UUID.v4().replace(/-/igm, ""),
username: username,
});
return;
}
2018-04-02 23:53:48 +08:00
VarCenter.get('user_token')[req.session['token']] = username;
req.session.save();
2017-11-13 12:26:31 +08:00
response.returnMsg(res, 'token', {
token: req.session['token'],
2018-04-02 23:53:48 +08:00
username: username,
2017-11-13 12:26:31 +08:00
});
} else {
counter.plus('csrfCounter');
res.send('<h1>CSRF 防御策略</h1><hr><p>您不能直接访问本页面,这是为了防御 CSRF 攻击,务必直接访问首页!</p>' +
'<p>具体信息我们将统计到非法 API 请求,这可能需要值得您注意.</p>');
}
res.end();
2017-11-13 12:26:31 +08:00
});
//模块导出
module.exports = router;
2018-04-02 23:53:48 +08:00
// res.header('X-Powered-By','Mcserver Manager HTT_P_SERVER');
//res.cookie('token_to',permssion.randomString(32));