Blame view

apps/passport/auth.js 4.21 KB
毕凯 authored
1 2 3 4 5 6 7
/**
 * passport 验证策略注册
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 * @date: 2016/5/31
 */

'use strict';
姜枫 authored
8
const _ = require('lodash');
毕凯 authored
9 10
const passport = require('passport');
const WeixinStrategy = require('passport-weixin');
姜枫 authored
11
const SinaStrategy = require('passport-sina').Strategy;
姜枫 authored
12
const LocalStrategy = require('passport-local').Strategy;
姜枫 authored
13
const QQStrategy = require('passport-qq').Strategy;
姜枫 authored
14 15
const DoubanStrategy = require('passport-douban').Strategy;
const RenrenStrategy = require('passport-renren').Strategy;
姜枫 authored
16
const AlipayStrategy = require('./models/passport-alipay').Strategy;
毕凯 authored
17
htoooth authored
18 19
const CaptchaServiceModel = require('./models/captcha-img-service');
htoooth authored
20
const LoginApi = require('./models/login-service');
姜枫 authored
21 22 23 24

const config = global.yoho.config;
const cookie = global.yoho.cookie;
const logger = global.yoho.logger;
毕凯 authored
25 26 27

let siteUrl = config.siteUrl.indexOf('//') === 0 ? 'http:' + config.siteUrl : config.siteUrl;
姜枫 authored
28
// 本地登录
王水玲 authored
29
passport.use('local', new LocalStrategy({
姜枫 authored
30 31 32 33 34
    usernameField: 'account',
    passwordField: 'password',
    passReqToCallback: true
}, (req, username, password, done) => {
王水玲 authored
35
    let area = req.body.areaCode || '86';
姜枫 authored
36
htoooth authored
37
    if (_.isEmpty(password)) {
姜枫 authored
38
        logger.info(`【Passport Loginbad params, area:${area} account:${username} password:${password}`);
姜枫 authored
39
        return done({message: '登录参数错误'}, null);
姜枫 authored
40 41 42
    }

    let shoppingKey = cookie.getShoppingKey(req);
htoooth authored
43
    let type = req.body.loginType;
htoooth authored
44
    let close = _.get(req.app.locals.pc, 'login.closePasswordLogin', false);
htoooth authored
45 46
    let id = req.session.id;
    let captcha = req.body.verifyCode;
姜枫 authored
47
htoooth authored
48 49 50 51 52 53
    (async function() {
        const result = await req.ctx(LoginApi).signin(type, area, username, password, shoppingKey, close, id, captcha);

        if (result.code && (result.code === 200 || result.code === 510) && result.data.uid) {
            return done(null, Object.assign(result.data, {code: result.code}));
        }
姜枫 authored
54
htoooth authored
55 56
        const captchaNeeded = await req.ctx(CaptchaServiceModel).try();
htoooth authored
57
        if (result.code === 50004) {
htoooth authored
58 59 60 61
            req.session.forceBind = {
                username,
                password
            };
htoooth authored
62 63
        }
htoooth authored
64
        return done({
htoooth authored
65
            code: result.code,
htoooth authored
66 67 68 69 70
            message: result.message,
            needCaptcha: captchaNeeded
        });
    }()).catch(e => {
        logger.error('call the signin service fail,', e);
htoooth authored
71
        done({code: 500, message: '登录失败,请稍后重试'}, null);
htoooth authored
72
    });
姜枫 authored
73 74
}));
毕凯 authored
75 76 77 78 79 80 81 82 83 84
/**
 * 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'
姜枫 authored
85 86 87 88 89 90
}, (accessToken, refreshToken, profile, done) => {
    done(null, profile);
}));

// sina 登录
passport.use('sina', new SinaStrategy({
htoooth authored
91 92
    clientID: config.thirdLogin.sina.appID,
    clientSecret: config.thirdLogin.sina.appSecret,
姜枫 authored
93 94 95
    callbackURL: `${siteUrl}/passport/login/sina/callback`,
    requireState: false
}, (accessToken, refreshToken, profile, done) => {
毕凯 authored
96 97
    done(null, profile);
}));
姜枫 authored
98 99 100

// qq 登录
passport.use('qq', new QQStrategy({
htoooth authored
101 102
    clientID: config.thirdLogin.qq.appID,
    clientSecret: config.thirdLogin.qq.appSecret,
姜枫 authored
103 104 105 106 107 108 109 110
    callbackURL: `${siteUrl}/passport/login/qq/callback`,
    requireState: false
}, (accessToken, refreshToken, profile, done) => {
    done(null, profile);
}));

// alipay 登录
passport.use('alipay', new AlipayStrategy({
htoooth authored
111 112
    partner: config.thirdLogin.alipay.appID,
    key: config.thirdLogin.alipay.appSecret,
姜枫 authored
113
    return_url: `${siteUrl}/passport/login/alipay/callback`
姜枫 authored
114
}, (profile, done) => {
姜枫 authored
115
    done(null, profile);
姜枫 authored
116 117 118 119
}));

// douban 登录
passport.use('douban', new DoubanStrategy({
htoooth authored
120 121
    clientID: config.thirdLogin.douban.appID,
    clientSecret: config.thirdLogin.douban.appSecret,
姜枫 authored
122 123 124 125 126 127 128
    callbackURL: `${siteUrl}/passport/autosign/doubanback`
}, (accessToken, refreshToken, profile, done) => {
    done(null, profile);
}));

// renren 登录
passport.use('renren', new RenrenStrategy({
htoooth authored
129 130
    clientID: config.thirdLogin.renren.appID,
    clientSecret: config.thirdLogin.renren.appSecret,
姜枫 authored
131 132 133 134
    callbackURL: `${siteUrl}/passport/login/renren/callback`
}, (accessToken, refreshToken, profile, done) => {
    done(null, profile);
}));