forked from paypal/paypal-retail-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
288 lines (274 loc) · 13.3 KB
/
index.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
var wreck = require('wreck'),
util = require('util'),
crypto = require('crypto');
var configs = {};
module.exports = {
/**
* Create a new environment with the given configuration
* @param environment The name of the PayPal environment
* @param options An object with clientId, secret, [returnUrl], [refreshUrl], [scopes] keys (braced values are optional)
*/
configure: function configure(environment, options) {
if (!options.clientId) {
throw new Error('Missing clientId for PayPal environment "'+environment+'"');
}
if (!options.secret) {
throw new Error('Missing secret for PayPal environment "'+environment+'"');
}
if (!options.returnUrl) {
throw new Error('Missing returnUrl for PayPal environment "'+environment+'"');
}
if (!options.refreshUrl) {
throw new Error('Missing refreshUrl for PayPal environment "'+environment+'"');
}
if (!options.scopes) {
options.scopes = 'openid email profile address https://uri.paypal.com/services/paypalhere https://uri.paypal.com/services/paypalattributes/business';
}
configs[environment] = options;
},
/**
* Retrieve the scopes enabled for this application from the PayPal servers.
* @param env The name of the previously-configured PayPal environment
* @param callback (error, scopeArray)
*/
queryAvailableScopes: function (env, callback) {
var cfg = configs[env];
if (!cfg) {
throw new Error('Invalid environment ' + encodeURIComponent(env));
}
wreck.post(oauthUrl(env), {
payload: 'grant_type=client_credentials&return_client_metadata=true',
json: 'force',
headers: {
'Authorization': 'Basic ' + new Buffer(cfg.clientId + ':').toString('base64'),
'Content-Type': 'application/x-www-form-urlencoded',
'X-IDENTITY-ROUTE-TO': 'APS'
}
}, function (err, rz, payload) {
if (!err && payload && payload.client_metadata) {
payload = payload.client_metadata.scopes;
// As a bonus, we'll return an error if one of the scopes you are configured to request is
// not available.
if (!payload) {
err = new Error('PayPal services did not return scopes for your application.');
}
var requiredScopes = cfg.scopes.split(' '), missing = [];
for (var si = 0; si < requiredScopes.length; si++) {
if (payload.indexOf(requiredScopes[si]) < 0) {
missing.push(requiredScopes[si]);
}
}
if (missing.length) {
err = new Error('Your application is missing the following required scopes: ' + missing.join(' '));
err.missing = missing;
err.required = requiredScopes;
}
} else if (!err) {
err = new Error('Invalid response received from PayPal services.');
}
if (err) {
err.response = payload;
}
callback(err, payload);
});
},
/**
* Build the URL to which the customer browser should be sent to login to PayPal and provide consent to your
* application.
* @param env The name of the previously-configured PayPal environment
* @param finalUrl The URL the customer browser should be sent to when auth/consent is complete
* @param returnTokenOnQueryString true if you want raw token information on the query string rather than an "SDK token"
* @returns url to send the browser
*/
redirect: function (env, finalUrl, returnTokenOnQueryString) {
var cfg = configs[env];
if (!cfg) {
throw new Error('Invalid environment ' + encodeURIComponent(env));
}
if (env == module.exports.SANDBOX) {
return util.format('https://www.sandbox.paypal.com/signin/authorize?response_type=code&client_id=%s&scope=%s&redirect_uri=%s&state=%s',
encodeURIComponent(cfg.clientId), encodeURIComponent(cfg.scopes), encodeURIComponent(cfg.returnUrl),
encodeURIComponent(JSON.stringify([env,finalUrl,!!returnTokenOnQueryString])));
} else if (env.indexOf('stage2') === 0) {
return util.format('https://www.%s.stage.paypal.com/signin/authorize?response_type=code&client_id=%s&scope=%s&redirect_uri=%s&state=%s',
env, encodeURIComponent(cfg.clientId), encodeURIComponent(cfg.scopes), encodeURIComponent(cfg.returnUrl),
encodeURIComponent(JSON.stringify([env,finalUrl,!!returnTokenOnQueryString])));
} else {
return util.format('https://www.paypal.com/signin/authorize?response_type=code&client_id=%s&scope=%s&redirect_uri=%s&state=%s',
encodeURIComponent(cfg.clientId), encodeURIComponent(cfg.scopes), encodeURIComponent(cfg.returnUrl),
encodeURIComponent(JSON.stringify([env,finalUrl,!!returnTokenOnQueryString])));
}
},
/**
* Retrieve a new access token for the customer that was originally given a 'refresh url' that had an
* encrypted version of their refresh token on it (encrypted using the app_secure_identifier)
* @param query The query string arguments (usually straight from Express.JS)
* @param app_secure_identifier Your server encryption secret
* @param callback (error,newToken) called when refresh completes
*/
refresh: function (query, app_secure_identifier, callback) {
if (!query.token) {
throw new Error('Refresh token is missing from request.');
}
decrypt(query.token, app_secure_identifier, function (e, plain) {
if (e) {
console.error(e.message, e.stack);
return callback(new Error('Invalid refresh token presented.'));
}
try {
// [0] = environment, [1] = raw refresh token
var info = JSON.parse(plain);
var url = tsUrl(info[0]);
var cfg = configs[info[0]];
if (!cfg) {
return callback(new Error('Invalid environment ' + encodeURIComponent(info[0])));
}
wreck.post(url, {
payload: util.format('grant_type=refresh_token&refresh_token=%s', encodeURIComponent(info[1])),
json: 'force',
headers: {
'Authorization': 'Basic ' + new Buffer(cfg.clientId + ':' + cfg.secret).toString('base64'),
'Content-Type': 'application/x-www-form-urlencoded'
}
}, function (err, rz, payload) {
callback(err, payload);
});
} catch (x) {
return callback(new Error('Invalid refresh token presented.'));
}
});
},
/**
* After the customer returns from the URL specified in redirect(), this method will complete the process
* and generate an access token and refresh URL
* @param query The query string arguments (usually straight from Express.JS)
* @param app_secure_identifier Your server encryption secret
* @param callback (error, tokenInfo) Called with the access token and refresh url in the tokenInfo object
*/
completeAuthentication: function (query, app_secure_identifier, callback) {
if (!app_secure_identifier) {
throw new Error('app_secure_identifier parameter is required to complete authentication.');
}
if (!callback || typeof(callback) !== 'function') {
throw new Error('completeAuthentication requires a callback parameter that is a function.');
}
if (query.error) {
return callback(new Error(util.format('Login with PayPal Error! %s: %s', query.error, query.error_description)));
}
var state = JSON.parse(query.state);
if (!state || state.length < 2) {
throw new Error('The "state" parameter is invalid when trying to complete PayPal authentication.');
}
var env = state[0];
var returnTokenOnQueryString = state.length > 2 ? (!!state[2]) : false;
var url = tsUrl(env);
var cfg = configs[env];
wreck.post(url, {
payload: util.format('grant_type=authorization_code&code=%s&redirect_uri=%s', encodeURIComponent(query.code), encodeURIComponent(cfg.returnUrl)),
json: 'force',
headers: {
'Authorization': 'Basic ' + new Buffer(cfg.clientId + ':' + cfg.secret).toString('base64'),
'Content-Type': 'application/x-www-form-urlencoded'
}
}, function (err, rz, payload) {
if (err) {
err.env = env;
return callback(err);
}
if (payload.error) {
var appError = new Error(payload.error + ' ' + payload.error_description);
appError.env = env;
return callback(appError);
}
var returnUrl = state[1] + (state[1].indexOf('?')>=0 ? '&':'?');
if (!returnTokenOnQueryString) {
returnUrl += "sdk_token=";
}
var refreshUrl = cfg.refreshUrl + (cfg.refreshUrl.indexOf('?')>=0 ? '&':'?');
encrypt(JSON.stringify([env, payload.refresh_token]), app_secure_identifier, function encryptionDone (e,v) {
if (e) {
e.env = env;
return callback(e);
}
var tokenInformation = [
payload.access_token,
payload.expires_in,
refreshUrl + '&token=' + encodeURIComponent(v)
];
if (returnTokenOnQueryString) {
returnUrl += 'access_token=' + encodeURIComponent(tokenInformation[0]) +
'&expires_in=' + encodeURIComponent(tokenInformation[1]) +
'&refresh_url=' + encodeURIComponent(tokenInformation[2]) +
'&env=' + encodeURIComponent(env);
} else {
returnUrl += env + ':' + encodeURIComponent(new Buffer(JSON.stringify(tokenInformation)).toString('base64'));
}
callback(null, returnUrl);
});
});
},
/* Predefined environments. You can also pass values given to you by PayPal folks for our stage servers. */
LIVE: 'live',
SANDBOX: 'sandbox'
};
function tsUrl(env) {
var url = 'https://api.paypal.com/v1/identity/openidconnect/tokenservice';
if (env == module.exports.SANDBOX) {
url = 'https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice';
} else if (env.indexOf('stage2') === 0) {
url = util.format('https://www.%s.stage.paypal.com:12714/v1/identity/openidconnect/tokenservice', env);
}
return url;
}
function oauthUrl(env) {
var url = 'https://api.paypal.com/v1/oauth2/token';
if (env == module.exports.SANDBOX) {
url = 'https://api.sandbox.paypal.com/v1/oauth2/token';
} else if (env.indexOf('stage2') === 0) {
url = util.format('https://www.%s.stage.paypal.com:11888/v1/oauth2/token', env);
}
return url;
}
function encrypt(plainText, password, cb) {
var salt = new Buffer(crypto.randomBytes(16), 'binary');
var iv = new Buffer(crypto.randomBytes(16), 'binary');
crypto.pbkdf2(password, salt, 1000, 32, function (err, key) {
if (err) {
logger.error('Failed to generate key.', err);
cb(err, null);
return;
}
var cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
var buffer = new Buffer(cipher.update(plainText, 'utf8', 'binary'), 'binary');
buffer = Buffer.concat([buffer, new Buffer(cipher.final('binary'), 'binary')]);
var hashKey = crypto.createHash('sha1').update(key).digest('binary');
var hmac = new Buffer(crypto.createHmac('sha1', hashKey).update(buffer).digest('binary'), 'binary');
buffer = Buffer.concat([salt, iv, hmac, buffer]);
cb(null, buffer.toString('base64'));
});
}
function decrypt(cipherText, password, cb) {
var cipher = new Buffer(cipherText, 'base64');
var salt = cipher.slice(0, 16);
var iv = cipher.slice(16, 32);
var hmac = cipher.slice(32, 52);
cipherText = cipher.slice(52);
crypto.pbkdf2(password, salt, 1000, 32, function (err, key) {
if (err) {
logger.error('Failed to generate key.', err);
cb(err, null);
return;
}
var cipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
// Verify the HMAC first
var hashKey = crypto.createHash('sha1').update(key).digest('binary');
var hmacgen = new Buffer(crypto.createHmac('sha1', hashKey).update(cipherText).digest('binary'), 'binary');
if (hmacgen.toString('base64') !== hmac.toString('base64')) {
cb(new Error('HMAC Mismatch!'), null);
return;
}
var buffer = new Buffer(cipher.update(cipherText), 'binary');
buffer = Buffer.concat([buffer, new Buffer(cipher.final('binary'))]);
cb(null, buffer.toString('utf8'), key);
});
}