passport-taobao.js
3.74 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
/**
* Module dependencies.
*/
const util = require('util');
const md5 = require('yoho-md5');
const TopClient = require('topsdk');
const { OAuth2Strategy, InternalOAuthError } = require('passport-oauth');
let client;
/**
* @param {Object} params
* @api public
*/
function signMD5(params, clientSecret) {
let _signStr = '';
params.sign_method = 'md5';
for (const k of Object.keys(params).sort()) {
_signStr += `${k}${params[k]}`;
}
let signd = md5(clientSecret + _signStr + clientSecret);
params.sign = new Buffer(signd, 'base64').toString('hex');
return params;
}
/**
* @param {Object} options
* @param {Function} verify
* @api public
*/
function Strategy(options, verify) {
options = options || {};
options.authorizationURL = options.authorizationURL || 'https://oauth.taobao.com/authorize';
options.tokenURL = options.tokenURL || 'https://oauth.taobao.com/token';
options.scopeSeparator = options.scopeSeparator || ',';
options.customHeaders = options.customHeaders || {};
client = new TopClient(options.clientID, options.clientSecret, {
endpoint: 'https://eco.taobao.com/router/rest',
useValidators: true,
rawResponse: false
});
if (!options.customHeaders['User-Agent']) {
options.customHeaders['User-Agent'] = options.userAgent || 'passport-taobao';
}
OAuth2Strategy.call(this, options, verify);
this.name = 'taobao';
this._userProfileURL = options.userProfileURL || 'https://eco.taobao.com/router/rest';
}
/**
* Inherit from `OAuth2Strategy`.
*/
util.inherits(Strategy, OAuth2Strategy);
/**
* @param {String} accessToken
* @param {Function} done
* @api protected
*/
Strategy.prototype.userProfile = function (accessToken, done) {
console.log(client)
client.execute('taobao.user.buyer.get', {
session: accessToken,
fields: 'nick,sex'
}).then(res => {
console.log(res);
});
return;
console.log(arguments)
let oauth2 = this._oauth2;
let url = 'https://eco.taobao.com/router/rest';
let params = {
method: 'taobao.user.buyer.get',
app_key: oauth2._clientId,
session: accessToken,
format: 'json',
v: '2.0',
fields: 'uid,nick,avatar'
};
// _clientSecret
// ?format=json&v=2.0&fields=uid,nick,avatar&method=taobao.user.buyer.get';
// url = url + '&app_key=' + oauth2._clientId;
// url = url + '×tamp=' + Date.parse(new Date());
// url = url + '&access_token=' + accessToken;
if (!accessToken) {
return done(new Error('accessToken is empty'));
}
Object.keys(signMD5(params, oauth2._clientSecret)).forEach((k, i) => {
url += `${i ? '&' : '?'}${k}=${params[k]}`;
});
console.log(url)
oauth2.get(url, accessToken, function (err, result, res) {
if (err) {
return done(new InternalOAuthError('failed to fetch user profile', err));
}
try {
if (result) {
console.log(result)
let json = JSON.parse(result);
if (json.error_response)
return done(new InternalOAuthError(json.error_response.code + '-' + json.error_response.msg, new Error(json.error_response.msg)));
else {
let json = JSON.parse(result);
let profile = { provider: 'taobao' };
profile.id = json.uid;
profile.nickname = json.nick;
profile.avatar = json.avatar;
profile._raw = result;
profile._json = json;
done(null, profile);
}
}
} catch (e) {
done('ERROR:' + e + result);
}
});
}
module.exports = Strategy;