1
|
-/**
|
|
|
2
|
- * passport 验证策略注册
|
|
|
3
|
- *
|
|
|
4
|
- * @author: jiangfeng<jeff.jiang@yoho.cn>
|
|
|
5
|
- * @date: 2016/5/31
|
|
|
6
|
- */
|
|
|
7
|
-
|
|
|
8
|
-'use strict';
|
|
|
9
|
-const passport = require('passport');
|
|
|
10
|
-const WeixinStrategy = require('passport-weixin');
|
|
|
11
|
-const SinaStrategy = require('passport-sina').Strategy;
|
|
|
12
|
-const LocalStrategy = require('passport-local').Strategy;
|
|
|
13
|
-const QQStrategy = require('passport-qq').Strategy;
|
|
|
14
|
-const AlipayStrategy = require('./models/passport-alipay').Strategy;
|
|
|
15
|
-
|
|
|
16
|
-const _ = require('lodash');
|
|
|
17
|
-
|
|
|
18
|
-
|
|
|
19
|
-const config = global.yoho.config;
|
|
|
20
|
-const logger = global.yoho.logger;
|
|
|
21
|
-const helpers = global.yoho.helpers;
|
|
|
22
|
-const cookie = global.yoho.cookie;
|
|
|
23
|
-
|
|
|
24
|
-const AuthHelper = require('./models/auth-helper');
|
|
|
25
|
-
|
|
|
26
|
-let siteUrl = config.siteUrl.indexOf('//') === 0 ? 'http:' + config.siteUrl : config.siteUrl;
|
|
|
27
|
-
|
|
|
28
|
-
|
|
|
29
|
-// 本地登录
|
|
|
30
|
-passport.use(new LocalStrategy({
|
|
|
31
|
- usernameField: 'account',
|
|
|
32
|
- passwordField: 'password',
|
|
|
33
|
- passReqToCallback: true
|
|
|
34
|
-}, (req, username, password, done) => {
|
|
|
35
|
-
|
|
|
36
|
- let area = req.body.areaCode || '86';
|
|
|
37
|
- let clientIp = req.yoho.clientIp || '';
|
|
|
38
|
- let isSkip = req.body.isskip;
|
|
|
39
|
-
|
|
|
40
|
- if (isNaN(parseInt(area, 0)) || _.isEmpty(username) || _.isEmpty(password)) {
|
|
|
41
|
- logger.info(`【Passport Login】bad params, area:${area} account:${username} password:${password}`);
|
|
|
42
|
- return done('登录参数错误', null);
|
|
|
43
|
- }
|
|
|
44
|
-
|
|
|
45
|
-
|
|
|
46
|
- let verifyEmail = helpers.verifyEmail(username);
|
|
|
47
|
-
|
|
|
48
|
- // 国际号码验证取消
|
|
|
49
|
- let verifyMobile = area === '86' ? helpers.verifyAreaMobile(area + '-' + username) : true;
|
|
|
50
|
-
|
|
|
51
|
- // 999 号段为有货专用测试号段
|
|
|
52
|
- if (username.indexOf('999') === 0) {
|
|
|
53
|
- verifyMobile = true;
|
|
|
54
|
- }
|
|
|
55
|
-
|
|
|
56
|
- if (!verifyEmail && !verifyMobile) {
|
|
|
57
|
- logger.info(`【Passport Login】bad account, email:${verifyEmail} mobile:${verifyMobile}`);
|
|
|
58
|
- return done('登录账号格式错误', null);
|
|
|
59
|
- }
|
|
|
60
|
-
|
|
|
61
|
- // let expire = req.cookies['LE' + md5('_LOGIN_EXPIRE')];
|
|
|
62
|
-
|
|
|
63
|
- // if (_.isEmpty(expire) || expire < (new Date()).getTime() / 1000) {
|
|
|
64
|
- // return done('页面停留时间过长,请刷新页面', null);
|
|
|
65
|
- // }
|
|
|
66
|
-
|
|
|
67
|
- let shoppingKey = cookie.getShoppingKey(req);
|
|
|
68
|
-
|
|
|
69
|
- AuthHelper.signinAes(area, username, password, shoppingKey, clientIp, isSkip).then((result) => {
|
|
|
70
|
- if (result.code && result.code === 200 && result.data.uid) {
|
|
|
71
|
- done(null, result.data);
|
|
|
72
|
- } else if (result.code && result.code === 4189) {
|
|
|
73
|
- done({code: 4189}, null);
|
|
|
74
|
- } else if (result.code && result.code === 510) {
|
|
|
75
|
- done(null, Object.assign(result.data, {weakPassword: true}));
|
|
|
76
|
- } else {
|
|
|
77
|
- done('账号或密码不正确', null);
|
|
|
78
|
- }
|
|
|
79
|
- }).catch(e => {
|
|
|
80
|
- logger.error('call the signin service fail,', e);
|
|
|
81
|
- done('登录失败,请稍后重试', null);
|
|
|
82
|
- });
|
|
|
83
|
-}));
|
|
|
84
|
-
|
|
|
85
|
-// wechat 登录
|
|
|
86
|
-passport.use('weixin', new WeixinStrategy({
|
|
|
87
|
- clientID: config.thirdLogin.wechat.appID,
|
|
|
88
|
- clientSecret: config.thirdLogin.wechat.appSecret,
|
|
|
89
|
- callbackURL: `${siteUrl}/passport/login/wechat/callback`,
|
|
|
90
|
- requireState: true,
|
|
|
91
|
- authorizationURL: 'https://open.weixin.qq.com/connect/oauth2/authorize',
|
|
|
92
|
- scope: 'snsapi_userinfo'
|
|
|
93
|
-}, (accessToken, refreshToken, profile, done) => {
|
|
|
94
|
- done(null, profile);
|
|
|
95
|
-}));
|
|
|
96
|
-
|
|
|
97
|
-// sina 登录
|
|
|
98
|
-passport.use('sina', new SinaStrategy({
|
|
|
99
|
- clientID: '3739328910',
|
|
|
100
|
- clientSecret: '9d44cded26d048e23089e5e975c93df1',
|
|
|
101
|
- callbackURL: `${siteUrl}/passport/login/sina/callback`,
|
|
|
102
|
- requireState: false
|
|
|
103
|
-}, (accessToken, refreshToken, profile, done) => {
|
|
|
104
|
- done(null, profile);
|
|
|
105
|
-}));
|
|
|
106
|
-
|
|
|
107
|
-// qq 登录
|
|
|
108
|
-passport.use('qq', new QQStrategy({
|
|
|
109
|
- clientID: '100229394',
|
|
|
110
|
- clientSecret: 'c0af9c29e0900813028c2ccb42021792',
|
|
|
111
|
- callbackURL: `${siteUrl}/passport/login/qq/callback`,
|
|
|
112
|
- requireState: false
|
|
|
113
|
-}, (accessToken, refreshToken, profile, done) => {
|
|
|
114
|
- done(null, profile);
|
|
|
115
|
-}));
|
|
|
116
|
-
|
|
|
117
|
-// alipay 登录
|
|
|
118
|
-passport.use('alipay', new AlipayStrategy({
|
|
|
119
|
- partner: '2088701661478015',
|
|
|
120
|
- key: 'kcxawi9bb07mzh0aq2wcirsf9znusobw',
|
|
|
121
|
- return_url: `${siteUrl}/passport/login/alipay/callback`
|
|
|
122
|
-}), (profile, done) => {
|
|
|
123
|
- done(null, profile);
|
|
|
124
|
-}); |
|
|