auth-helper.js
5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
const _ = require('lodash');
const aes = require('./aes-pwd');
const authcode = require('../../../utils/authcode');
const logger = global.yoho.logger;
const sign = global.yoho.sign;
const api = global.yoho.API;
const uuid = require('uuid');
const url = require('url');
const md5 = require('yoho-md5');
const moment = require('moment');
const querystring = require('querystring');
const thirdAccount = require('../data/third-account.json');
const FROM = require('../../../config/from');
class Auth {
static signin(area, profile, password, shoppingKey) {
let param = {
method: 'app.passport.signin',
area: area,
profile: profile,
password: password
};
if (shoppingKey) {
param.shopping_key = shoppingKey;
}
return api.post('', param);
}
static signinAes(area, profile, password, shoppingKey, ip, isSkip, from) {
let param = {
method: 'sesame.flowering.higher',
area: area,
profile: profile,
password: aes.aesPwd(password),
isSkip: isSkip ? isSkip : 'N',
business_line: FROM[from].business_line
};
if (shoppingKey) {
param.shopping_key = shoppingKey;
}
logger.info(`${profile}, login from ${ip}`);
return api.post('', param, {
headers: {
'user-agent': 'yoho/nodejs',
'X-YOHO-IP': ip,
'X-Forwarded-For': ip
}
});
}
static signinByOpenID(nickname, openId, sourceType, shoppingKey) {
let param = {
nickname: nickname,
openId: openId,
source_type: sourceType, // esline-disable-line
method: 'app.passport.signinByOpenID',
shoppingKey: shoppingKey
};
if (shoppingKey) {
param.shopping_key = shoppingKey;
}
return api.get('', param);
}
static profile(uid) {
let param = {
uid: uid,
method: 'app.passport.profile'
};
return api.get('', param);
}
static thirdSign(params, clientSecret) {
let secretParams = {},
secretStr = '';
for (const k of Object.keys(params).sort()) {
if (k === 'yh_sign') {
continue;
}
secretParams[k] = params[k];
}
secretStr = _.join(_.map(secretParams, (v, k) => {
return `${k}=${v}`;
}), '&');
return md5(secretStr + clientSecret);
}
static thirdLogin(req, res, uid) {
if (req.cookies.third_type && req.cookies.third_backurl && req.cookies.third_refer) {
let backurl = url.parse(req.cookies.third_backurl),
account = thirdAccount[req.cookies.third_type];
let params = Object.assign({
yh_uid: uid,
yh_type: req.cookies.third_type,
yh_time: moment().format('YYYY-MM-DD HH:mm:ss')
}, querystring.parse(backurl.query));
let signStr = this.thirdSign(params, account.clientSecret);
params.yh_sign = signStr;
let queryStr = _.join(_.map(params, (v, k) => {
return `${k}=${encodeURIComponent(v)}`;
}), '&');
return {
refer: `${backurl.protocol}//${backurl.host}${backurl.pathname}?${queryStr}`
};
}
res.clearCookie('third_type', {
domain: 'yohobuy.com'
});
res.clearCookie('third_backurl', {
domain: 'yohobuy.com'
});
res.clearCookie('third_refer', {
domain: 'yohobuy.com'
});
}
static syncUserSession(uid, req, res, sessionKey) {
let userId = {
toString: () => {
return uid;
}
};
if (sessionKey) {
// 弃用
// global.yoho.cache.set(`java_session_key:${uid}`, sessionKey).catch(() => {
// global.yoho.logger.error('write session key fail');
// });
req.session.SESSION_KEY = sessionKey;
res.cookie('_SESSION_KEY', authcode(sessionKey, '_SESSION_KEY', 2592000000, 'encode'), {
domain: 'yohobuy.com',
expires: new Date(Date.now() + 2592000000) // 有效期一年
});
userId.sessionKey = sessionKey;
}
res.cookie('_LOGIN_IS_REPORT', false, {
domain: 'm.yohobuy.com',
path: '/'
});
return Auth.profile(userId).then((userInfo) => {
let salt = uuid.v4().substr(0, 8);
let saltedUid = uid + salt;
let saltedToken = sign.makeToken(saltedUid);
let publicToken = saltedToken + salt;
let data = userInfo.data;
let encryptionUid = aes.encryptionUid(uid);
if (data) {
data.profile_name = (data.profile_name || '').replace(/::/g, '');
let uidCookie =
`${data.profile_name}::${encryptionUid}::${data.vip_info && data.vip_info.title}::${saltedToken}`;
res.cookie('_UID', uidCookie, {
domain: 'yohobuy.com',
expires: new Date(Date.now() + 2592000000) // 有效期一年
});
req.session.AVATAR = data.head_ico;
_.set(req.session, 'USER.AVATAR', data.head_ico);
_.set(req.session, 'USER.NAME', data.profile_name);
}
req.session.TOKEN = publicToken;
req.session.LOGIN_UID = uid;
_.set(req.session, 'USER.ENCRYPTION_UID', encryptionUid);
res.cookie('_TOKEN', publicToken, {
httpOnly: true,
domain: 'yohobuy.com',
expires: new Date(Date.now() + 2592000000) // 有效期一年
});
// 第三方登录逻辑
return this.thirdLogin(req, res, uid);
});
}
}
module.exports = Auth;