Authored by 毕凯

Merge branch 'feature/passport' of git.yoho.cn:fe/yohobuy-node into feature/passport

... ... @@ -39,7 +39,7 @@ passport.use(new LocalStrategy({
if (isNaN(parseInt(area, 0)) || _.isEmpty(username) || _.isEmpty(password)) {
logger.info(`【Passport Loginbad params, area:${area} account:${username} password:${password}`);
return done('登录参数错误', null);
return done({message: '登录参数错误'}, null);
}
let verifyEmail = helpers.verifyEmail(username);
... ... @@ -47,13 +47,19 @@ passport.use(new LocalStrategy({
if (!verifyEmail && !verifyMobile) {
logger.info(`【Passport Loginbad account, email:${verifyEmail} mobile:${verifyMobile}`);
return done('登录账号格式错误', null);
return done({message: '登录账号格式错误'}, null);
}
let expire = req.cookies['LE' + md5('_LOGIN_EXPIRE')];
if (_.isEmpty(expire) || expire < (new Date()).getTime() / 1000) {
return done('页面停留时间过长,请刷新页面', null);
return done({message: '页面停留时间过长,请刷新页面'}, null);
}
let verifyCode = req.body.captcha;
if (verifyCode && verifyCode !== req.session.captcha) {
return done({message: '验证码不正确或验证码过期', needCaptcha: true}, null);
}
let shoppingKey = cookie.getShoppingKey(req);
... ... @@ -72,15 +78,12 @@ passport.use(new LocalStrategy({
let accountTimes = parseInt(times[1], 0) || 0;
let ipTimes = parseInt(times[2], 0) || 0;
console.log(errLoginTimes);
if (accountTimes >= 10) {
done({ message: '您的账号已被暂时锁定,请稍后再试' }, null);
} else if (ipTimes >= 100) {
done({ message: '您尝试的次数过多,账号已被暂时锁定,请稍后再试' }, null);
} else {
return AuthHelper.signin(area, username, password, shoppingKey).then((result) => {
console.log(result);
if (result.code && result.code === 200 && result.data.uid) {
cache.del(errorLoginKey);
... ...
... ... @@ -48,7 +48,7 @@ const validateInputAPI = (req, res, next) => {
/**
* 校验用户输入信息,是否是已经注册的用户
*/
const validateUserPage = (req, res, next) => {
const validateInputPage = (req, res, next) => {
let userInput = req.body.phoneNum || '';
let areaCode = (req.body.area || '86').replace('+', '');
... ... @@ -78,6 +78,7 @@ const sendCodePage = (req, res, next) => {
service.sendCodeToUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area)
.then(result => {
if (!(result.code && result.code === 200)) {
console.log(result);
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
... ... @@ -98,6 +99,7 @@ const saveInSession = (req, res) => {
{
req.session.mobile = req.inputInfo.phone;
req.session.area = req.inputInfo.area;
req.session.verifyCode = req.session.captcha;
res.redirect(helpers.urlFormat('/passport/back/verification'));
break;
}
... ... @@ -138,9 +140,9 @@ const validateEmailInSession = (req, res, next) => {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
let isp = email.split('@')[1];
let isp = email.split('@')[1].toLowerCase();
const mapperEmailISP = {
'yoho.cn': 'http://smail.yoho.cn'
'yoho.cn': 'http://exmail.qq.com/login'
};
req.body.emailUrl = mapperEmailISP[isp] || `http://mail.${isp}`;
... ... @@ -254,9 +256,9 @@ const resetPwdSuccessPage = (req, res, next) => {
};
const verifyCodeByMobileAPI = (req, res) => {
let mobile = req.param('mobile', '');
let area = req.param('area', '86');
let mobileCode = req.param('code', '');
let mobile = req.body.mobile || '';
let area = req.body.area || '86';
let mobileCode = req.body.code || '';
const ERR = {
code: 400,
message: '验证码错误!',
... ... @@ -279,7 +281,6 @@ const verifyCodeByMobileAPI = (req, res) => {
};
const validateExistCodePage = (req, res, next) => {
let code = req.query.code || req.body.code;
if (!code) {
... ... @@ -343,7 +344,7 @@ const updatePwdAPI = (req, res, next) => {
const validateMobileInSession = (req, res, next) => {
req.body.mobile = req.session.mobile || '';
req.body.verifyCode = req.session.verifyCode || '';
req.body.verifyCode = req.session.captcha || '';
req.body.area = req.session.area || '';
if (!(req.body.mobile && req.body.verifyCode)) {
... ... @@ -371,7 +372,7 @@ module.exports = {
resetPwdSuccessPage, // 重设密码成功页面
validateInputAPI, // 验证用户输入的邮箱或者手机是否合法,返回是json
validateUserPage, // 验证用户输入的邮箱或者手机是否合法,跳转是页面
validateInputPage, // 验证用户输入的邮箱或者手机是否合法,跳转是页面
validateEmailInSession, // 验证邮箱是否在session中
validateMobileInSession, // 验证手机是否在session中
... ...
... ... @@ -4,13 +4,11 @@
'use strict';
const Captchapng = require('captchapng');
const _ = require('lodash');
const captchaService = require('../models/captcha-service');
const helpers = global.yoho.helpers;
const requiredAPI = (req, res, next) => {
let captchaToken = +(req.body.verifyCode || '').toLowerCase();
let captchaToken = req.body.verifyCode || '';
if (captchaToken === req.session.captcha) {
return next();
... ... @@ -23,7 +21,7 @@ const requiredAPI = (req, res, next) => {
};
const requiredPage = (req, res, next) => {
let captchaToken = +(req.body.verifyCode || '').toLowerCase();
let captchaToken = req.body.verifyCode || '';
if (captchaToken === req.session.captcha) {
return next();
... ... @@ -32,28 +30,11 @@ const requiredPage = (req, res, next) => {
}
};
const _generateCaptcha = (width, height, length) => {
let min = Math.pow(10, (length - 1 || 1));
let max = Math.pow(10, (length - 1 || 1)) * 9;
let token = '' + _.random(min, max);
let png = new Captchapng(width, height, token);//
png.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha)
png.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
return {
image: new Buffer(png.getBase64(), 'base64'),
text: token
};
};
const generate = (req, res) => {
let width = req.query.w || 150;
let height = req.query.h || 50;
let length = +(req.query.l || 4);
let captcha = _generateCaptcha(width, height, length);
let captcha = captchaService.generateCaptcha(width, height, length);
req.session.captcha = captcha.text;
res.writeHead(200, {
... ...
... ... @@ -91,7 +91,6 @@ const common = {
cache.get(errorLoginKey).then(errloginTimes => {
errloginTimes = parseInt(errloginTimes, 0) || 0;
console.log(errloginTimes);
if (!isNaN(errloginTimes) && errloginTimes >= 3) {
result.data = { needCaptcha: true };
}
... ...
/**
* Created by TaoHuang on 2016/7/1.
*/
'use strict';
const _ = require('lodash');
const Captchapng = require('captchapng');
exports.generateCaptcha = (width, height, length) => {
let min = Math.pow(10, (length - 1 || 1));
let max = Math.pow(10, (length - 1 || 1)) * 9;
let token = '' + _.random(min, max);
let png = new Captchapng(width, height, token);//
png.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha)
png.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
return {
image: new Buffer(png.getBase64(), 'base64'),
text: token
};
};
... ...
... ... @@ -84,14 +84,14 @@ router.post('/back/authcode',
// 提交按钮邮件API
router.post('/back/email',
captcha.requiredPage,
back.validateUserPage,
back.validateInputPage,
back.sendCodePage,
back.saveInSession);
// 提交按钮手机API
router.post('/back/mobile',
captcha.requiredPage,
back.validateUserPage,
back.validateInputPage,
back.sendCodePage,
back.saveInSession);
... ... @@ -108,7 +108,6 @@ router.get('/back/sendEmail',
*/
// 验证手机短信页面
router.get('/back/verification',
captcha.requiredPage,
back.validateMobileInSession,
back.verifyCodeByMobilePage);
... ...
... ... @@ -11,6 +11,7 @@ const isTest = process.env.NODE_ENV === 'test';
module.exports = {
app: 'web',
appVersion: '4.6.0', // 调用api的版本
port: 6002,
siteUrl: 'http://www.yohobuy.com',
domains: {
... ...