Authored by 郭成尧

yoho-ip

@@ -10,10 +10,12 @@ const passport = require('passport'); @@ -10,10 +10,12 @@ const passport = require('passport');
10 10
11 // const md5 = require('yoho-md5'); 11 // const md5 = require('yoho-md5');
12 const uuid = require('uuid'); 12 const uuid = require('uuid');
  13 +const co = Promise.coroutine;
13 const cookie = global.yoho.cookie; 14 const cookie = global.yoho.cookie;
14 const helpers = global.yoho.helpers; 15 const helpers = global.yoho.helpers;
15 const log = global.yoho.logger; 16 const log = global.yoho.logger;
16 const config = global.yoho.config; 17 const config = global.yoho.config;
  18 +const cache = global.yoho.cache.master;
17 const utils = require(global.utils); 19 const utils = require(global.utils);
18 const RegService = require('../models/reg-service'); 20 const RegService = require('../models/reg-service');
19 const AuthHelper = require('../models/auth-helper'); 21 const AuthHelper = require('../models/auth-helper');
@@ -122,21 +124,10 @@ const local = { @@ -122,21 +124,10 @@ const local = {
122 req.session.captchaValidCount = 5; 124 req.session.captchaValidCount = 5;
123 } 125 }
124 126
125 - // 先清除cookie  
126 - // res.clearCookie('LE' + md5('_LOGIN_EXPIRE'), {  
127 - // domain: 'yohobuy.com'  
128 - // });  
129 -  
130 - // 设置登录有效时间30分钟, 防机器刷,cache不稳定,改为cookie  
131 - // res.cookie('LE' + md5('_LOGIN_EXPIRE'), (new Date()).getTime() / 1000 + 1800);  
132 -  
133 - // 170406 账户密码方式登录可以选择是否开启验证码,默认开关是关闭状态,此时开启验证码,开关开启,无需验证  
134 - let captchaShow = _.get(req.app.locals.wap, 'close.loginValidation', false);  
135 -  
136 res.render('login', { 127 res.render('login', {
137 width750: true, 128 width750: true,
138 loginIndex: true, // 模板中使用JS的标识 129 loginIndex: true, // 模板中使用JS的标识
139 - captchaShow: !captchaShow, // 170306 因为暴力破解密码问题,要求每次都展示验证码 130 + captchaShow: req.yoho.captchaShow,
140 backUrl: 'javascript:history.go(-1)', // eslint-disable-line 131 backUrl: 'javascript:history.go(-1)', // eslint-disable-line
141 showHeaderImg: true, // 控制显示头部图片 132 showHeaderImg: true, // 控制显示头部图片
142 isPassportPage: true, // 模板中模块标识 133 isPassportPage: true, // 模板中模块标识
@@ -166,22 +157,11 @@ const local = { @@ -166,22 +157,11 @@ const local = {
166 req.session.captchaValidCount = 5; 157 req.session.captchaValidCount = 5;
167 } 158 }
168 159
169 - // 先清除cookie  
170 - // res.clearCookie('LE' + md5('_LOGIN_EXPIRE'), {  
171 - // domain: 'yohobuy.com'  
172 - // });  
173 -  
174 - // 设置登录有效时间30分钟, 防机器刷,cache不稳定,改为cookie  
175 - // res.cookie('LE' + md5('_LOGIN_EXPIRE'), (new Date()).getTime() / 1000 + 1800);  
176 -  
177 - // 170406 账户密码方式登录可以选择是否开启验证码,默认开关是关闭状态,此时开启验证码,开关开启,无需验证  
178 - let captchaShow = _.get(req.app.locals.wap, 'close.loginValidation', false);  
179 -  
180 res.render('international', { 160 res.render('international', {
181 width750: true, 161 width750: true,
182 backUrl: 'javascript:history.go(-1)', // eslint-disable-line 162 backUrl: 'javascript:history.go(-1)', // eslint-disable-line
183 loginInternational: true, // 模板中使用JS的标识 163 loginInternational: true, // 模板中使用JS的标识
184 - captchaShow: !captchaShow, // 170306 因为暴力破解密码问题,要求每次都展示验证码 164 + captchaShow: req.yoho.captchaShow,
185 isPassportPage: true, // 模板中模块标识 165 isPassportPage: true, // 模板中模块标识
186 headerText: '登录', 166 headerText: '登录',
187 areaCode: '+86', // 默认区号 167 areaCode: '+86', // 默认区号
@@ -411,7 +391,27 @@ exports.user = function(req, res, next) { @@ -411,7 +391,27 @@ exports.user = function(req, res, next) {
411 * 根据用户登录是否成功决定是否展示验证码 391 * 根据用户登录是否成功决定是否展示验证码
412 */ 392 */
413 exports.loginShowCaptchaByIp = function(req, res, next) { 393 exports.loginShowCaptchaByIp = function(req, res, next) {
414 - return next(); 394 + // 总开关状态
  395 + req.yoho.captchaShow = !_.get(req.app.locals.wap, 'close.loginValidation', false);
  396 +
  397 + // 开关打开,不走任何验证逻辑
  398 + if (!req.yoho.captchaShow) {
  399 + return next();
  400 + }
  401 +
  402 + co(function*() {
  403 + let hasErrorLog = yield cache.getAsync(`loginErrorIp:${req.yoho.clientIp}`);
  404 +
  405 + log.info(`clientip ${req.yoho.clientIp} status is ` + cache.get(`loginErrorIp:${req.yoho.clientIp}`));
  406 +
  407 + if (hasErrorLog) {
  408 + req.yoho.captchaShow = true;
  409 + }
  410 + next();
  411 + })().catch(function(e) {
  412 + req.yoho.captchaShow = true;
  413 + next();
  414 + });
415 }; 415 };
416 416
417 exports.common = common; 417 exports.common = common;
@@ -7,6 +7,8 @@ @@ -7,6 +7,8 @@
7 'use strict'; 7 'use strict';
8 const _ = require('lodash'); 8 const _ = require('lodash');
9 const config = global.yoho.config; 9 const config = global.yoho.config;
  10 +const co = Promise.coroutine;
  11 +const cache = global.yoho.cache.master;
10 const geetest = require('./geetest'); 12 const geetest = require('./geetest');
11 const captcha = require('./captcha'); 13 const captcha = require('./captcha');
12 14
@@ -20,20 +22,41 @@ const check = (req, res, next) => { @@ -20,20 +22,41 @@ const check = (req, res, next) => {
20 return next(); 22 return next();
21 } 23 }
22 24
23 - // 170406 采用账号密码方式登录验证码可以配置关闭,默认开关是关闭状态,这时需要验证,开关开启,无需验证  
24 - if (_.get(req.app.locals.wap, 'close.loginValidation', false) && req.path === '/passport/login/auth') {  
25 - return next();  
26 - } 25 + // 默认取配置总开关来决定是否展示验证码
  26 + req.yoho.captchaShow = _.get(req.app.locals.wap, 'close.loginValidation', false);
27 27
28 - // 使用极验证  
29 - let useGeetest = !_.get(req.app.locals.wap, 'geetest.validation', false); 28 + co(function* () {
30 29
31 - // 某次请求极验证调用注册失败,强制使用自有图形验证码  
32 - if (req.session.useYohoCaptcha) {  
33 - useGeetest = false;  
34 - } 30 + // 如果是账号密码登录,那么需要检查是否登录失败过,登录失败过展示验证码
  31 + if (req.path === '/passport/login/auth') {
  32 + let hasErrorLog = yield cache.getAsync(`loginErrorIp:${req.yoho.clientIp}`);
  33 +
  34 + if (hasErrorLog) {
  35 + req.yoho.captchaShow = true;
  36 + }
  37 + }
  38 +
  39 + return req.yoho.captchaShow;
  40 + })().catch(function() {
  41 + // memcache 不可用,展示验证码
  42 + req.yoho.captchaShow = true;
  43 + return req.yoho.captchaShow;
  44 + }).then(function() {
  45 + // 不是账号密码登录,直接根据配置总开关决定是否需要展示验证码
  46 + if (!req.yoho.captchaShow) {
  47 + return next();
  48 + }
  49 +
  50 + // 使用极验证
  51 + let useGeetest = !_.get(req.app.locals.wap, 'geetest.validation', false);
  52 +
  53 + // 某次请求极验证调用注册失败,强制使用自有图形验证码
  54 + if (req.session.useYohoCaptcha) {
  55 + useGeetest = false;
  56 + }
35 57
36 - return (useGeetest ? geetest : captcha).validate(req, res, next); 58 + return (useGeetest ? geetest : captcha).validate(req, res, next);
  59 + });
37 }; 60 };
38 61
39 /** 62 /**