Blame view

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

'use strict';
const passport = require('passport');
ccbikai authored
10
const WeixinStrategy = require('passport-weixin');
11 12 13 14 15 16
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');
郭成尧 authored
17
姜枫 authored
18
姜枫 authored
19 20 21 22
const config = global.yoho.config;
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
const cookie = global.yoho.cookie;
23
郭成尧 authored
24
const AuthHelperModel = require('./models/auth-helper');
姜枫 authored
25
姜枫 authored
26 27
let siteUrl = config.siteUrl.indexOf('//') === 0 ? 'http:' + config.siteUrl : config.siteUrl;
姜枫 authored
28 29

// 本地登录
30 31 32 33 34 35
passport.use(new LocalStrategy({
    usernameField: 'account',
    passwordField: 'password',
    passReqToCallback: true
}, (req, username, password, done) => {
王水玲 authored
36
    let area = req.body.areaCode || '86';
郭成尧 authored
37
    let clientIp = req.yoho.clientIp || '';
郭成尧 authored
38
    let isSkip = req.body.isskip;
郭成尧 authored
39
    let from = req.cookies.from || 'yohobuy';
毕凯 authored
40
    let udid = req.cookies.udid;
郭成尧 authored
41
    let captcha = req.body.captcha;
郭成尧 authored
42
    let superCapture = '';
郭成尧 authored
43
姜枫 authored
44
    if (isNaN(parseInt(area, 0)) || _.isEmpty(username) || _.isEmpty(password)) {
姜枫 authored
45
        logger.info(`【Passport Loginbad params, area:${area} account:${username} password:${password}`);
46 47 48
        return done('登录参数错误', null);
    }
王水玲 authored
49
50
    let verifyEmail = helpers.verifyEmail(username);
王水玲 authored
51 52

    // 国际号码验证取消
王水玲 authored
53
    let verifyMobile = area === '86' ? helpers.verifyAreaMobile(area + '-' + username) : true;
54
55 56 57 58 59
    // 999 号段为有货专用测试号段
    if (username.indexOf('999') === 0) {
        verifyMobile = true;
    }
60
    if (!verifyEmail && !verifyMobile) {
姜枫 authored
61
        logger.info(`【Passport Loginbad account, email:${verifyEmail} mobile:${verifyMobile}`);
62 63 64 65 66
        return done('登录账号格式错误', null);
    }

    let shoppingKey = cookie.getShoppingKey(req);
郭成尧 authored
67
    if (req.body.geetest_challenge) {
郭成尧 authored
68 69
        captcha = '';
        superCapture = config.superCapture;
郭成尧 authored
70 71 72 73 74 75 76 77 78 79 80
    }

    req.ctx(AuthHelperModel).signinAes({
        area,
        profile: username,
        password,
        shoppingKey,
        ip: clientIp,
        isSkip,
        from,
        udid,
郭成尧 authored
81 82
        degrees: captcha,
        superCapture
郭成尧 authored
83
    }).then((result) => {
毕凯 authored
84 85
        logger.info(`login result: ${JSON.stringify(result)}`);
姜枫 authored
86
        if (result.code && result.code === 200 && result.data.uid) {
87
            done(null, result.data);
lijing authored
88
        } else if (result.code && result.code === 4189) {
lijing authored
89
            done({code: 4189}, null);
郭成尧 authored
90
        } else if (result.code && result.code === 510) {
郭成尧 authored
91
            done(null, Object.assign(result.data, {weakPassword: true}));
92 93
        } else if (result.code && result.code === 50004) {
            done(result, null);
毕凯 authored
94 95
        } else if (result.code) {
            done(result.message, null);
96 97 98 99
        } else {
            done('账号或密码不正确', null);
        }
    }).catch(e => {
姜枫 authored
100 101
        logger.error('call the signin service fail,', e);
        done('登录失败,请稍后重试', null);
102 103 104
    });
}));
姜枫 authored
105
// wechat 登录
ccbikai authored
106
passport.use('weixin', new WeixinStrategy({
毕凯 authored
107 108
    clientID: 'wx75e5a7c0c88e45c2',
    clientSecret: 'ce21ae4a3f93852279175a167e54509b',
姜枫 authored
109
    callbackURL: `${siteUrl}/passport/login/wechat/callback`,
ccbikai authored
110 111
    requireState: true,
    authorizationURL: 'https://open.weixin.qq.com/connect/oauth2/authorize',
姜枫 authored
112
    scope: 'snsapi_userinfo'
113 114 115 116 117 118 119 120 121 122 123
}, (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) => {
姜枫 authored
124
    done(null, profile);
ccbikai authored
125
}));
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

// 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',
王水玲 authored
141 142 143 144
    return_url: `${siteUrl}/passport/login/alipay/callback`
}), (profile, done) => {
    done(null, profile);
});