auth.js 5.71 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 DoubanStrategy = require('passport-douban').Strategy;
const RenrenStrategy = require('passport-renren').Strategy;
const AlipayStrategy = require('./models/passport-alipay').Strategy;
const request = require('request-promise');

const CaptchaServiceModel = require('./models/captcha-img-service');

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

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

class BaiduSDK {
    /*
     * @ description: 广告主回传转化数据接口, 上报百度
     * @ author: huzhiming
     * @ date: 2019-11-19 17:21:29
     * @ version: v1.0.0
     * 详情见文档:[http://ocpc.baidu.com/developer/d/guide/?iurl=api%2Fapi-doc%2Fapi-interface%2F]
    */
    static reportOcpcApi({
        token = '7GULUkX90QLZU6cHO9OEqUsRKttGNqpN@O62eFfb91OUVhmkakV1bQHIxF8xURBvP',
        conversionTypes = [{ logidUrl: '', newType: null }]
    }) {
        logger.info('--------------百度ocpc--------------');
        request({
            method: 'POST',
            uri: 'http://ocpc.baidu.com/ocpcapi/api/uploadConvertData',
            body: {
                token,
                conversionTypes
            },
            json: true
        }).then((result) => {
            logger.info(`[账号注册成功后上报 百度ocpc返回信息:${JSON.stringify(result)}`);
        }).catch((error) => {
            logger.info(`[账号注册成功后上报 百度ocpc失败 错误信息:${error}`);
        });
    }
}

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

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

    let area = req.body.areaCode || '86';

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

    let shoppingKey = cookie.getShoppingKey(req);
    let type = req.body.loginType;
    let close = _.get(req.app.locals.pc, 'login.closePasswordLogin', false);
    let id = req.session.id;
    let captcha = req.body.verifyCode;

    (async function() {
        const result = await req.ctx(LoginApi).signin(type, area, username, password, shoppingKey, close, id, captcha);

        if (_.get(result, 'data.nFlag', 'N') === 'Y') {
            const logidUrl = req.cookies.bd_vid_path;

            logger.info('-------logidUrl---auth-----', `${logidUrl}:${username}`);
            if (logidUrl) {
                BaiduSDK.reportOcpcApi({
                    conversionTypes: {
                        logidUrl,
                        newType: 3
                    }
                });
            }
        }

        if (result.code && (result.code === 200 || result.code === 510) && result.data.uid) {
            return done(null, Object.assign(result.data, {code: result.code}));
        }

        const captchaNeeded = await req.ctx(CaptchaServiceModel).try();

        if (result.code === 50004) {
            req.session.forceBind = {
                username,
                password
            };
        }

        return done({
            code: result.code,
            message: result.message,
            needCaptcha: captchaNeeded
        });
    }()).catch(e => {
        logger.error('call the signin service fail,', e);
        done({code: 500, message: '登录失败,请稍后重试'}, 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.appID,
    key: config.thirdLogin.alipay.appSecret,
    return_url: `${siteUrl}/passport/login/alipay/callback`
}, (profile, done) => {
    done(null, profile);
}));

// douban 登录
passport.use('douban', new DoubanStrategy({
    clientID: config.thirdLogin.douban.appID,
    clientSecret: config.thirdLogin.douban.appSecret,
    callbackURL: `${siteUrl}/passport/autosign/doubanback`
}, (accessToken, refreshToken, profile, done) => {
    done(null, profile);
}));

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