-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
352 lines (292 loc) · 10.5 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
var _ = require('lodash');
var jwt = require('jsonwebtoken');
var request = require('request');
var nodeCache = require('node-cache');
var parseCacheControl = require('parse-cache-control');
var credential_cache = new nodeCache({ useClones: false });
var keyGenFunc = construct_cache_key;
var logger = console.log;
var REFRESH_TOKEN_CLIENT_ID = process.env.DEFAULT_TARGET_ID || 'QkxOvNz4fWRFT6vcq79ylcIuolFz2cwN';
var construct_cache_key = function (method, url, authToken) {
var decodedToken = jwt.decode(authToken);
if (decodedToken && decodedToken.sub) {
return "" + method + "-" + url + "-" + decodedToken.sub;
} else {
return "" + method + "-" + url;
}
};
var check_cache_for_response = function (method, url, authToken, callback) {
var cacheKey = keyGenFunc(method, url, authToken);
credential_cache.get(cacheKey, function (err, data) {
if (err) {
logger(err);
return callback(err);
} else {
if (data) {
return callback(err, JSON.parse(data));
}
return callback(err, data);
}
});
};
var parse_cache_control_header = function (headers) {
if (headers) {
if (headers["Cache-Control"]) {
return parseCacheControl(headers["Cache-Control"])['max-age'] || 0;
} else if (headers["cache-control"]) {
return parseCacheControl(headers["cache-control"])['max-age'] || 0;
} else {
return null;
}
} else {
return null;
}
};
var save_response_in_cache = function (method, url, authToken, res, body) {
var cacheKey = keyGenFunc(method, url, authToken);
var cacheControl = parse_cache_control_header(res.headers);
if (cacheControl) {
credential_cache.set(
cacheKey,
JSON.stringify({
res: res,
body: body,
}),
cacheControl)
}
return;
};
var retrieve_client_grant = function (config, cb) {
var audience = config.audience || 'https://api.cimpress.io/';
var client_grant_options = {
method: 'POST',
url: config.authorization_server || 'https://cimpress.auth0.com/oauth/token',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
audience: audience,
client_id: config.client_id,
client_secret: config.client_secret,
grant_type: 'client_credentials'
})
};
credential_cache.get(audience, function (error, jwt_obj) {
if (error) {
logger("Error when retrieving v2 access token from cache", error);
}
if (jwt_obj) {
logger("Found cached credential %s", audience);
return cb(null, jwt_obj);
}
request(client_grant_options, function (err, response, body) {
if (err) {
return cb(err);
}
try {
body = JSON.parse(body);
} catch (e) {
return cb('Invalid JSON response from Auth0: ' + body);
}
// Store the jwt keyed on the audience
var jwt_obj = jwt.decode(body.access_token);
credential_cache.set(audience, body.access_token, jwt_obj.exp - jwt_obj.iat);
return cb(null, body.access_token);
});
});
};
var retrieve_delegated_token = function (config, cb) {
var delegation_options = {
method: 'POST',
url: config.authorization_server || 'https://cimpress.auth0.com/delegation',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
client_id: REFRESH_TOKEN_CLIENT_ID,
target: config.target_id,
refresh_token: config.refresh_token || process.env.CIMPRESS_IO_REFRESH_TOKEN,
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
api_type: 'app',
scope: config.scope || 'openid'
})
};
// Check for target_id from cimpress.io portal
credential_cache.get(config.target_id, function (error, jwt_obj) {
if (error) {
logger("Retrieving v1 token from cache returned error:", error);
}
if (jwt_obj) {
logger("Found cached credential %s", config.target_id);
return cb(null, jwt_obj);
}
request(delegation_options, function (err, response, body) {
if (err) {
return cb(err);
}
try {
body = JSON.parse(body);
} catch (e) {
return cb('Invalid JSON response from Auth0: ' + body);
}
// Store the jwt keyed on the audience
var jwt_obj = jwt.decode(body.id_token);
credential_cache.set(config.target_id, body.id_token, jwt_obj.exp - jwt_obj.iat);
return cb(null, body.id_token);
});
});
};
/**
* Given an HTTP response, generate and return a configuration object suitable for passing as
* the first parameter of compute_bearer. If no suitable config can be generated due to the
* absence of Www-Authenticate headers, this method returns undefined.
*/
var parse_auth_headers = module.exports.parse_auth_headers = function (config, res) {
if (res.headers['www-authenticate']) {
var matches = res.headers['www-authenticate'].match(/client_id=([^\s]+)/);
if (matches.length > 0) {
return {
refresh_token: config.refresh_token,
target_id: matches[1]
};
}
}
};
/**
* Return a request.js instance that wraps a custom request builder responsible for authenticating
* all calls with an OAuth Bearer token from Auth0, whether retrieved via a client credentials grant
* flow (preferred) or via delegation.
*/
module.exports = (function () {
var request_builder = function (options, callback) {
if (!options.method) {
options.method = 'GET';
}
keyGenFunc = options.keyGen || construct_cache_key;
var retry_loop = function () {
// Look for a retry_count property in options
options.retry_count = options.retry_count + 1 || 1;
logger("Retrying in %sms", 200 << options.retry_count);
setTimeout(function () {
request_builder(options, callback);
}, 200 << options.retry_count);
};
var v1auth = function (res) {
// If we got an unauthorized since this API doesn't support client grant flows, we
// pass the response to parse_auth_headers to find a config that might work better.
var delegate_config = res ? parse_auth_headers(options.auth, res) : options.auth;
// Validate whether we have enough information to authenticate
if (!(delegate_config && delegate_config.refresh_token && delegate_config.target_id)) {
if (res) return callback(new Error('Not enough information for a delegation call'));
logger("No v1 auth possible. Attempting an unauthenticated request");
return request(_.omit(options, ['auth']), function (err, res, body) {
if (res.statusCode === 401) v1auth(res);
else callback(err, res, body);
});
}
retrieve_delegated_token(delegate_config, function (err, tok) {
if (err) {
logger(err);
return retry_loop();
}
options.auth.bearer = tok;
check_cache_for_response(options.method, options.url, options.auth.bearer, function (err, cachedResponse) {
if (err) {
logger(err);
return callback(err);
}
if (cachedResponse) {
return callback(err, cachedResponse.res, cachedResponse.body);
} else {
request(options, function (err, res, body) {
// If we get a 401 on even delegated auth, return to caller.
if (res && res.statusCode === 401) {
return callback(err, res, body);
}
// On a connection error, enter exponential backoff protocol
if (err) {
return retry_loop();
}
if (res.statusCode >= 200 && res.statusCode < 300) {
save_response_in_cache(options.method, options.url, options.auth.bearer, res, body);
}
return callback(err, res, body);
});
}
});
});
};
var v2auth = function () {
// Validate whether we have enough information to attempt authentication
if (!(options.auth && options.auth.client_id && options.auth.client_secret)) {
logger("No v2 auth possible. Falling back to v1.");
return v1auth();
}
retrieve_client_grant(options.auth, function (err, tok) {
if (err) {
logger(err);
return retry_loop();
}
options.auth.bearer = tok;
check_cache_for_response(options.method, options.url, options.auth.bearer, function (err, cachedResponse) {
if (err) {
logger(err);
return callback(err);
}
if (cachedResponse) {
return callback(err, cachedResponse.res, cachedResponse.body);
} else {
request(options, function (err, res, body) {
// If we got a 401, move on to v1auth
if (res && res.statusCode === 401) {
return v1auth(res);
}
// On a connection error, enter exponential backoff protocol
if (err) {
return retry_loop();
}
if (res.statusCode >= 200 && res.statusCode < 300) {
save_response_in_cache(options.method, options.url, options.auth.bearer, res, body);
}
return callback(err, res, body);
});
}
});
});
};
var passedInAuth = function () {
if (options.auth && options.auth.bearer) {
check_cache_for_response(options.method, options.url, options.auth.bearer, function (err, cachedResponse) {
if (cachedResponse) {
return callback(null, cachedResponse.res, cachedResponse.body);
} else {
request(options, function (err, res, body) {
// If we got a 401, move on to v2auth
if (res && res.statusCode === 401) {
return v2auth();
}
// On a connection error, enter exponential backoff protocol
if (err) {
return retry_loop();
}
if (res.statusCode >= 200 && res.statusCode < 300) {
save_response_in_cache(options.method, options.url, options.auth.bearer, res, body);
}
return callback(err, res, body);
});
}
});
}
else {
logger("No token passed in. Falling back to v2.");
return v2auth();
}
}
// Try pre-set auth token first
return passedInAuth();
};
return request.defaults(request_builder);
})();
module.exports.credential_cache = credential_cache;
module.exports.set_credential_cache = function (altCache) {
credential_cache = altCache;
};
module.exports.set_logger = function (l) {
logger = l;
};