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

'use strict';
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('./models/passport-alipay').Strategy;

const _ = require('lodash');


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

const AuthHelperModel = require('./models/auth-helper');

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


// 本地登录
passport.use(new LocalStrategy({
    usernameField: 'account',
    passwordField: 'password',
    passReqToCallback: true
}, (req, username, password, done) => {

    let area = req.body.areaCode || '86';
    let clientIp = req.yoho.clientIp || '';
    let isSkip = req.body.isskip;
    let from = req.cookies.from || 'yohobuy';
    let udid = req.cookies.udid;
    let captcha = req.body.captcha;
    let superCapture = '';

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


    let verifyEmail = helpers.verifyEmail(username);

    // 国际号码验证取消
    let verifyMobile = area === '86' ? helpers.verifyAreaMobile(area + '-' + username) : true;

    // 999 号段为有货专用测试号段
    if (username.indexOf('999') === 0) {
        verifyMobile = true;
    }

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

    let shoppingKey = cookie.getShoppingKey(req);

    if (req.body.geetest_challenge) {
        captcha = '';
        superCapture = config.superCapture;
    }

    req.ctx(AuthHelperModel).signinAes({
        area,
        profile: username,
        password,
        shoppingKey,
        ip: clientIp,
        isSkip,
        from,
        udid,
        degrees: captcha,
        superCapture
    }).then((result) => {
        logger.info(`login result: ${JSON.stringify(result)}`);

        if (result.code && result.code === 200 && result.data.uid) {
            done(null, result.data);
        } else if (result.code && result.code === 4189) {
            done({code: 4189}, null);
        } else if (result.code && result.code === 510) {
            done(null, Object.assign(result.data, {weakPassword: true}));
        } else if (result.code && result.code === 50004) {
            done(result, null);
        } else if (result.code) {
            done(result.message, null);
        } else {
            done('账号或密码不正确', null);
        }
    }).catch(e => {
        logger.error('call the signin service fail,', e);
        done('登录失败,请稍后重试', null);
    });
}));

// wechat 登录
passport.use('weixin', new WeixinStrategy({
    clientID: config.thirdLogin.wechat.appID,
    clientSecret: config.thirdLogin.wechat.appSecret,
    callbackURL: `${siteUrl}/passport/login/wechat/callback`,
    requireState: true,
    authorizationURL: 'https://open.weixin.qq.com/connect/oauth2/authorize',
    scope: 'snsapi_userinfo'
}, (accessToken, refreshToken, profile, done) => {
    done(null, profile);
}));

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

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

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