Authored by htoooth

add captcha

@@ -77,8 +77,6 @@ const sendCodePage = (req, res, next) => { @@ -77,8 +77,6 @@ const sendCodePage = (req, res, next) => {
77 77
78 service.sendCodeToUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area) 78 service.sendCodeToUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area)
79 .then(result => { 79 .then(result => {
80 - console.log(result);  
81 -  
82 if (!(result.code && result.code === 200)) { 80 if (!(result.code && result.code === 200)) {
83 return res.redirect(helpers.urlFormat('/passport/back/index')); 81 return res.redirect(helpers.urlFormat('/passport/back/index'));
84 } 82 }
@@ -4,10 +4,13 @@ @@ -4,10 +4,13 @@
4 4
5 'use strict'; 5 'use strict';
6 6
  7 +const Captchapng = require('captchapng');
  8 +const _ = require('lodash');
  9 +
7 const helpers = global.yoho.helpers; 10 const helpers = global.yoho.helpers;
8 11
9 const requiredAPI = (req, res, next) => { 12 const requiredAPI = (req, res, next) => {
10 - let captchaToken = (req.body.verifyCode || '').toLowerCase(); 13 + let captchaToken = +(req.body.verifyCode || '').toLowerCase();
11 14
12 if (captchaToken === req.session.captcha) { 15 if (captchaToken === req.session.captcha) {
13 return next(); 16 return next();
@@ -20,16 +23,48 @@ const requiredAPI = (req, res, next) => { @@ -20,16 +23,48 @@ const requiredAPI = (req, res, next) => {
20 }; 23 };
21 24
22 const requiredPage = (req, res, next) => { 25 const requiredPage = (req, res, next) => {
23 - let captchaToken = (req.body.verifyCode || '').toLowerCase(); 26 + let captchaToken = +(req.body.verifyCode || '').toLowerCase();
24 27
25 if (captchaToken === req.session.captcha) { 28 if (captchaToken === req.session.captcha) {
26 return next(); 29 return next();
27 } else { 30 } else {
28 - return res.redirect(helpers.urlFormat('/passport/back/index.html')); 31 + return res.redirect(helpers.urlFormat('/passport/back/index'));
29 } 32 }
30 }; 33 };
31 34
  35 +
  36 +const _generateCaptcha = (width, height, length) => {
  37 + let min = Math.pow(10, (length - 1 || 1));
  38 + let max = Math.pow(10, (length - 1 || 1)) * 9;
  39 + let token = '' + _.random(min, max);
  40 +
  41 + let png = new Captchapng(width, height, token);//
  42 +
  43 + png.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha)
  44 + png.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
  45 +
  46 + return {
  47 + image: new Buffer(png.getBase64(), 'base64'),
  48 + text: token
  49 + };
  50 +};
  51 +
  52 +const generate = (req, res) => {
  53 + let width = req.query.w || 150;
  54 + let height = req.query.h || 50;
  55 + let length = +(req.query.l || 4);
  56 + let captcha = _generateCaptcha(width, height, length);
  57 +
  58 + req.session.captcha = captcha.text;
  59 + res.writeHead(200, {
  60 + 'Content-Type': 'image/png'
  61 + });
  62 +
  63 + res.end(captcha.image);
  64 +};
  65 +
32 module.exports = { 66 module.exports = {
33 requiredAPI, 67 requiredAPI,
34 - requiredPage 68 + requiredPage,
  69 + generate
35 }; 70 };
@@ -10,7 +10,7 @@ const express = require('express'); @@ -10,7 +10,7 @@ const express = require('express');
10 const cRoot = './controllers'; 10 const cRoot = './controllers';
11 const login = require(cRoot + '/login'); 11 const login = require(cRoot + '/login');
12 12
13 -// const captcha = require(cRoot + '/captcha'); 13 +const captcha = require(cRoot + '/captcha');
14 const back = require(cRoot + '/back'); 14 const back = require(cRoot + '/back');
15 const reg = require(cRoot + '/reg'); 15 const reg = require(cRoot + '/reg');
16 16
@@ -34,23 +34,20 @@ router.get('/back/index', back.index); @@ -34,23 +34,20 @@ router.get('/back/index', back.index);
34 34
35 // 实时验证输入是否正确 35 // 实时验证输入是否正确
36 router.post('/back/authcode', 36 router.post('/back/authcode',
37 -  
38 - // captcha.requiredAPI, 37 + captcha.requiredAPI,
39 back.validateInputAPI, 38 back.validateInputAPI,
40 back.getUserInfoAPI); 39 back.getUserInfoAPI);
41 40
42 // 提交按钮邮件API 41 // 提交按钮邮件API
43 router.post('/back/email', 42 router.post('/back/email',
44 -  
45 - // captcha.requiredPage, 43 + captcha.requiredPage,
46 back.validateUserPage, 44 back.validateUserPage,
47 back.sendCodePage, 45 back.sendCodePage,
48 back.saveInSession); 46 back.saveInSession);
49 47
50 // 提交按钮手机API 48 // 提交按钮手机API
51 router.post('/back/mobile', 49 router.post('/back/mobile',
52 -  
53 - // captcha.requiredPage, 50 + captcha.requiredPage,
54 back.validateUserPage, 51 back.validateUserPage,
55 back.sendCodePage, 52 back.sendCodePage,
56 back.saveInSession); 53 back.saveInSession);
@@ -68,29 +65,26 @@ router.get('/back/sendEmail', @@ -68,29 +65,26 @@ router.get('/back/sendEmail',
68 */ 65 */
69 // 验证手机短信页面 66 // 验证手机短信页面
70 router.get('/back/verification', 67 router.get('/back/verification',
  68 + captcha.requiredPage,
71 back.validateMobileInSession, 69 back.validateMobileInSession,
72 -  
73 - // captcha.requiredPage,  
74 back.verifyCodeByMobilePage); 70 back.verifyCodeByMobilePage);
75 71
76 // 重新发送短信接口 72 // 重新发送短信接口
77 router.post('/back/sendBackMobile', 73 router.post('/back/sendBackMobile',
78 -  
79 - // captcha.requiredAPI, 74 + captcha.requiredAPI,
80 back.validateMobileAPI, 75 back.validateMobileAPI,
81 back.sendBackMobileAPI); 76 back.sendBackMobileAPI);
82 77
83 // 验证手机验证码接口 78 // 验证手机验证码接口
84 router.post('/back/backMobile', 79 router.post('/back/backMobile',
85 -  
86 - // captcha.requiredAPI, 80 + captcha.requiredAPI,
87 back.verifyCodeByMobileAPI); 81 back.verifyCodeByMobileAPI);
88 82
89 /** 83 /**
90 * 重置密码 84 * 重置密码
91 */ 85 */
92 86
93 -// 重置密码页面 87 + // 重置密码页面
94 router.get('/back/backcode', 88 router.get('/back/backcode',
95 back.validateExistCodePage, 89 back.validateExistCodePage,
96 back.validateCodeByMobilePage, 90 back.validateCodeByMobilePage,
@@ -109,4 +103,6 @@ router.get('/back/resetSuccess', @@ -109,4 +103,6 @@ router.get('/back/resetSuccess',
109 back.validateSuccessStatusPage, 103 back.validateSuccessStatusPage,
110 back.resetPwdSuccessPage); 104 back.resetPwdSuccessPage);
111 105
  106 +router.get('/images', captcha.generate);
  107 +
112 module.exports = router; 108 module.exports = router;