login-auth-service.js 4.84 KB
/**
 * passport 验证策略注册
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 * @date: 2016/5/31
 */

'use strict';
const _ = require('lodash');
const passport = require('passport');
const WeixinStrategy = require('passport-weixin');
const SinaStrategy = require('passport-sina').Strategy;
const LocalStrategy = require('passport-local').Strategy;
const QQStrategy = require('passport-qq').Strategy;
const AlipayStrategy = require('./passport-alipay').Strategy;

const md5 = require('md5');

const LoginService = require('./login-service');

const config = global.yoho.config;
const helpers = global.yoho.helpers;
const cookie = global.yoho.cookie;
const logger = global.yoho.logger;
const cache = global.yoho.cache;

let siteUrl = config.siteUrl.indexOf('//') === 0 ? 'http:' + config.siteUrl : config.siteUrl;


function getLoginStat(ip) {
    let errorLoginKey = 'loginErrorIp:' + ip;

    return cache.get(errorLoginKey);
}

// 本地登录
passport.use('local', new LocalStrategy({
    usernameField: 'account',
    passwordField: 'password',
    passReqToCallback: true
}, (req, username, password, done) => {
    let area = req.body.areaCode || '86';
    let type = req.body.loginType;
    let clientIp = req.yoho.clientIp;
    let errorLoginKey = 'loginErrorIp:' + clientIp;

    return getLoginStat(clientIp).then((times) => {
        let errLoginTimes = _.parseInt(times) || 0;
        let verifyCode = req.body.captcha;

        if (errLoginTimes > 0 && type !== 'SMSLogin') {
            if (!verifyCode || verifyCode !== req.session.captcha) {
                return done({message: '验证码不正确或验证码过期', needCaptcha: true}, null);
            }
        }


        if (isNaN(_.parseInt(area)) || _.isEmpty(username) || _.isEmpty(password)) {
            logger.info(`【Passport Loginbad params, area:${area} account:${username} password:${password}`);
            return done({message: '登录参数错误'}, null);
        }

        let verifyEmail = helpers.verifyEmail(username);
        let verifyMobile = area === '86' ? helpers.verifyAreaMobile(area + '-' + username) : true;

        if (!verifyEmail && !verifyMobile) {
            logger.info(`【Passport Loginbad account, email:${verifyEmail} mobile:${verifyMobile}`);
            return done({message: '登录账号格式错误'}, null);
        }

        let expire = req.cookies['LE' + md5('_LOGIN_EXPIRE')] || '';

        if (expire && expire < (new Date()).getTime() / 1000) {
            return done({message: '页面停留时间过长,请刷新页面'}, null);
        }


        if (type !== 'SMSLogin' && verifyCode && verifyCode !== req.session.captcha) {
            return done({message: '验证码不正确或验证码过期', needCaptcha: true}, null);
        }

        let shoppingKey = cookie.getShoppingKey(req);

        return LoginService.signin(type, area, username, password, shoppingKey, req).then((result) => {
            if (result.code && result.code === 200 && result.data.uid) {
                cache.del(errorLoginKey).catch(() => {});
                done(null, result.data);
            } else {
                cache.set(errorLoginKey, errLoginTimes + 1, 3600).catch(() => {});
                done({
                    message: '请输入正确的账号或密码',
                    needCaptcha: true
                });
            }
        }).catch(e => {
            logger.error('call the signin service fail,', e);
            done('登录失败,请稍后重试', null);
        });
    }).catch(e => {
        logger.error('call the signin service fail,', e);
        return done('登录失败,请稍后重试', null);
    });

    
}));

/**
 * wechat登录
 */

passport.use('wechat', new WeixinStrategy({
    clientID: config.thirdLogin.wechat.appID,
    clientSecret: config.thirdLogin.wechat.appSecret,
    callbackURL: `${siteUrl}/passport/login/wechat/callback`,
    requireState: true,
    scope: 'snsapi_login'
}, (accessToken, refreshToken, profile, done) => {
    done(null, profile);
}));

// sina 登录
passport.use('sina', new SinaStrategy({
    clientID: config.thirdLogin.sina.appID,
    clientSecret: config.thirdLogin.sina.appSecret,
    callbackURL: `${siteUrl}/passport/login/sina/callback`,
    requireState: false
}, (accessToken, refreshToken, profile, done) => {
    done(null, profile);
}));

// qq 登录
passport.use('qq', new QQStrategy({
    clientID: config.thirdLogin.qq.appID,
    clientSecret: config.thirdLogin.qq.appSecret,
    callbackURL: `${siteUrl}/passport/login/qq/callback`,
    requireState: false
}, (accessToken, refreshToken, profile, done) => {
    done(null, profile);
}));

// alipay 登录
passport.use('alipay', new AlipayStrategy({
    partner: config.thirdLogin.alipay.partnerID,
    key: config.thirdLogin.alipay.appSecret,
    return_url: `${siteUrl}/passport/login/alipay/callback`
}, (profile, done) => {
    done(null, profile);
}));