@mimik/oauth-helper 1.10.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -3
- package/index.js +47 -12
- package/package.json +19 -19
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ const oauthHelper = require('@mimik/oauth-helper');
|
|
|
24
24
|
* [oauth-helper](#module_oauth-helper)
|
|
25
25
|
* _async_
|
|
26
26
|
* [~rpAuth(type, options)](#module_oauth-helper..rpAuth) ⇒ <code>Promise</code>
|
|
27
|
-
* [~authProfile(method, id, correlationId)](#module_oauth-helper..authProfile) ⇒ <code>Promise</code>
|
|
27
|
+
* [~authProfile(method, id, correlationId, options)](#module_oauth-helper..authProfile) ⇒ <code>Promise</code>
|
|
28
28
|
* _callback_
|
|
29
29
|
* [~apiKeySecurityHelper(request, definitions, apiKey, next)](#module_oauth-helper..apiKeySecurityHelper)
|
|
30
30
|
* [~apiTokenSecurityHelper(request, definitions, scopes, next)](#module_oauth-helper..apiTokenSecurityHelper)
|
|
@@ -42,7 +42,7 @@ Make an authorized request.
|
|
|
42
42
|
|
|
43
43
|
- <code>Promise</code> Will throw the same error than [request-promise](https://www.npmjs.com/package/request-promise).
|
|
44
44
|
|
|
45
|
-
The property `token` may be added to options
|
|
45
|
+
The property `token` may be added to `options`, in order to set up how the token is retrieved from the token manager. The structure is:
|
|
46
46
|
```
|
|
47
47
|
{
|
|
48
48
|
"retry": "object to specify how the retry to the token manager will be done. similar to rp-retry retry property",
|
|
@@ -50,6 +50,13 @@ The property `token` may be added to options, in order to set up how the token i
|
|
|
50
50
|
"cluster": "to set the token to be a cluster token"
|
|
51
51
|
}
|
|
52
52
|
````
|
|
53
|
+
The property `metrics` may be added to `options`, in order to setup metrics about calls made to other microservice. The structure is:
|
|
54
|
+
```
|
|
55
|
+
{
|
|
56
|
+
"HTTPrequestDuration": "prom-client function to label and record the elapsed time",
|
|
57
|
+
"url": "url to be displayed for the metrics. If not present the url of the options will be used"
|
|
58
|
+
}
|
|
59
|
+
```
|
|
53
60
|
|
|
54
61
|
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>
|
|
55
62
|
**Fulfil**: <code>object</code> - Response of the [request-promise](https://www.npmjs.com/package/request-promise) request.
|
|
@@ -61,7 +68,7 @@ The property `token` may be added to options, in order to set up how the token i
|
|
|
61
68
|
|
|
62
69
|
<a name="module_oauth-helper..authProfile"></a>
|
|
63
70
|
|
|
64
|
-
### oauth-helper~authProfile(method, id, correlationId) ⇒ <code>Promise</code>
|
|
71
|
+
### oauth-helper~authProfile(method, id, correlationId, options) ⇒ <code>Promise</code>
|
|
65
72
|
Make an authProfile request to mID.
|
|
66
73
|
|
|
67
74
|
**Kind**: inner method of [<code>oauth-helper</code>](#module_oauth-helper)
|
|
@@ -79,6 +86,7 @@ Make an authProfile request to mID.
|
|
|
79
86
|
| method | <code>string</code> | Method (`GET`, `DELETE`) of the request to be made. |
|
|
80
87
|
| id | <code>string</code> | `UserId` to associated witht the request. |
|
|
81
88
|
| correlationId | <code>UUID.<string></code> | CorrelationId to associated with the request. |
|
|
89
|
+
| options | <code>object</code> | Options to be added to the request. metrics can then be added to the options. |
|
|
82
90
|
|
|
83
91
|
<a name="module_oauth-helper..apiKeySecurityHelper"></a>
|
|
84
92
|
|
package/index.js
CHANGED
|
@@ -202,7 +202,7 @@ module.exports = (config) => {
|
|
|
202
202
|
* @fulfil {object} - Response of the [request-promise](https://www.npmjs.com/package/request-promise) request.
|
|
203
203
|
* @throws {Promise} Will throw the same error than [request-promise](https://www.npmjs.com/package/request-promise).
|
|
204
204
|
*
|
|
205
|
-
* The property `token` may be added to options
|
|
205
|
+
* The property `token` may be added to `options`, in order to set up how the token is retrieved from the token manager. The structure is:
|
|
206
206
|
* ```
|
|
207
207
|
* {
|
|
208
208
|
* "retry": "object to specify how the retry to the token manager will be done. similar to rp-retry retry property",
|
|
@@ -210,9 +210,28 @@ module.exports = (config) => {
|
|
|
210
210
|
* "cluster": "to set the token to be a cluster token"
|
|
211
211
|
* }
|
|
212
212
|
* ````
|
|
213
|
+
* The property `metrics` may be added to `options`, in order to setup metrics about calls made to other microservice. The structure is:
|
|
214
|
+
* ```
|
|
215
|
+
* {
|
|
216
|
+
* "HTTPrequestDuration": "prom-client function to label and record the elapsed time",
|
|
217
|
+
* "url": "url to be displayed for the metrics. If not present the url of the options will be used"
|
|
218
|
+
* }
|
|
219
|
+
* ```
|
|
213
220
|
*/
|
|
214
221
|
const rpAuth = (type, options) => {
|
|
222
|
+
const startHrTime = process.hrtime();
|
|
223
|
+
const { metrics } = options;
|
|
215
224
|
const enteredUrl = options.uri || options.url; // uri takes precedence over url in request-promise, conserving the order
|
|
225
|
+
const measure = (statusCode) => {
|
|
226
|
+
if (metrics && metrics.HTTPRequestDuration) {
|
|
227
|
+
const elapsedHrTime = process.hrtime(startHrTime);
|
|
228
|
+
const elapsedTimeInMs = elapsedHrTime[0] * 1000 + elapsedHrTime[1] / 1e6;
|
|
229
|
+
|
|
230
|
+
metrics.HTTPRequestDuration
|
|
231
|
+
.labels('rpAuth', options.method, metrics.url || enteredUrl, enteredUrl.includes('?'), statusCode)
|
|
232
|
+
.observe(elapsedTimeInMs);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
216
235
|
let url;
|
|
217
236
|
|
|
218
237
|
if (!type) return Promise.reject(new Error(`rpAuth type non existent for ${options.method || 'GET'} on ${enteredUrl}`));
|
|
@@ -229,6 +248,10 @@ module.exports = (config) => {
|
|
|
229
248
|
if (!opts.headers) opts.headers = {};
|
|
230
249
|
opts.headers.authorization = `Bearer ${token}`;
|
|
231
250
|
return rpRetry(opts)
|
|
251
|
+
.then((result) => {
|
|
252
|
+
measure(200);
|
|
253
|
+
return result;
|
|
254
|
+
})
|
|
232
255
|
.catch((err) => {
|
|
233
256
|
if (err.statusCode === 403) {
|
|
234
257
|
logger.warn('got a unauthorized request, retrying', { error: err.message, type }, correlationId);
|
|
@@ -236,11 +259,19 @@ module.exports = (config) => {
|
|
|
236
259
|
return getToken(type, options.uri || options.url, options.token, correlationId)
|
|
237
260
|
.then((newToken) => {
|
|
238
261
|
opts.headers.authorization = `Bearer ${newToken}`;
|
|
239
|
-
return rpRetry(opts)
|
|
262
|
+
return rpRetry(opts)
|
|
263
|
+
.then((result) => {
|
|
264
|
+
measure(200);
|
|
265
|
+
return result;
|
|
266
|
+
});
|
|
240
267
|
});
|
|
241
268
|
}
|
|
242
269
|
throw err;
|
|
243
270
|
});
|
|
271
|
+
})
|
|
272
|
+
.catch((err) => {
|
|
273
|
+
measure(err.statusCode);
|
|
274
|
+
throw err;
|
|
244
275
|
});
|
|
245
276
|
};
|
|
246
277
|
|
|
@@ -437,28 +468,32 @@ module.exports = (config) => {
|
|
|
437
468
|
* @param {string} method - Method (`GET`, `DELETE`) of the request to be made.
|
|
438
469
|
* @param {string} id - `UserId` to associated witht the request.
|
|
439
470
|
* @param {UUID<string>} correlationId - CorrelationId to associated with the request.
|
|
471
|
+
* @param {object} options - Options to be added to the request. metrics can then be added to the options.
|
|
440
472
|
* @return {Promise}.
|
|
441
473
|
* @fulfil {object} The response of the request made to `mID`.
|
|
442
474
|
* @throws {Promise} Will throw a rich error is the request fails or an error if the id is not identified
|
|
443
475
|
*/
|
|
444
|
-
const authProfile = (method, id, correlationId) => {
|
|
476
|
+
const authProfile = (method, id, correlationId, options) => {
|
|
445
477
|
if (!id) return Promise.reject(new Error('Id has to be defined'));
|
|
446
|
-
|
|
478
|
+
const opts = {
|
|
447
479
|
method,
|
|
448
480
|
uri: `${config.dependencies.mID.url}/users/${id}`,
|
|
449
481
|
headers: {
|
|
450
482
|
'x-correlation-id': correlationId,
|
|
451
483
|
},
|
|
452
484
|
json: true,
|
|
453
|
-
}
|
|
454
|
-
|
|
485
|
+
};
|
|
486
|
+
if (options && options.metrics) opts.metrics = options.metrics;
|
|
487
|
+
return rpAuth('mID', opts)
|
|
488
|
+
.catch((err) => {
|
|
489
|
+
const error = getRichError(err.statusCode, 'could perform operation on identity server', { method, id }, err);
|
|
455
490
|
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
491
|
+
if (error.statusCode === 400 && method === 'DELETE') {
|
|
492
|
+
logger.warn('profile without authprofile', { id, error }, correlationId);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
throw error;
|
|
496
|
+
});
|
|
462
497
|
};
|
|
463
498
|
|
|
464
499
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mimik/oauth-helper",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Oauth helper for mimik microservices",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -29,30 +29,30 @@
|
|
|
29
29
|
"url": "https://bitbucket.org/mimiktech/oauth-helper"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@mimik/request-retry": "^2.0.
|
|
33
|
-
"@mimik/response-helper": "^2.6.
|
|
34
|
-
"@mimik/sumologic-winston-logger": "^1.6.
|
|
35
|
-
"@mimik/swagger-helper": "^2.5.
|
|
32
|
+
"@mimik/request-retry": "^2.0.12",
|
|
33
|
+
"@mimik/response-helper": "^2.6.2",
|
|
34
|
+
"@mimik/sumologic-winston-logger": "^1.6.13",
|
|
35
|
+
"@mimik/swagger-helper": "^2.5.5",
|
|
36
36
|
"bluebird": "3.7.2",
|
|
37
|
-
"jsonwebtoken": "
|
|
37
|
+
"jsonwebtoken": "9.0.0",
|
|
38
38
|
"lodash": "4.17.21"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@mimik/eslint-plugin-dependencies": "^2.4.
|
|
42
|
-
"@mimik/eslint-plugin-document-env": "^1.0.
|
|
43
|
-
"@mimik/request-helper": "^1.7.
|
|
44
|
-
"body-parser": "1.20.
|
|
45
|
-
"chai": "4.3.
|
|
46
|
-
"eslint": "8.
|
|
41
|
+
"@mimik/eslint-plugin-dependencies": "^2.4.4",
|
|
42
|
+
"@mimik/eslint-plugin-document-env": "^1.0.4",
|
|
43
|
+
"@mimik/request-helper": "^1.7.7",
|
|
44
|
+
"body-parser": "1.20.1",
|
|
45
|
+
"chai": "4.3.7",
|
|
46
|
+
"eslint": "8.30.0",
|
|
47
47
|
"eslint-config-airbnb": "19.0.4",
|
|
48
48
|
"eslint-plugin-import": "2.26.0",
|
|
49
|
-
"eslint-plugin-jsx-a11y": "6.
|
|
50
|
-
"eslint-plugin-react": "7.
|
|
51
|
-
"eslint-plugin-react-hooks": "4.
|
|
52
|
-
"express": "4.18.
|
|
53
|
-
"husky": "8.0.
|
|
54
|
-
"jsdoc-to-markdown": "
|
|
55
|
-
"mocha": "10.
|
|
49
|
+
"eslint-plugin-jsx-a11y": "6.6.1",
|
|
50
|
+
"eslint-plugin-react": "7.31.11",
|
|
51
|
+
"eslint-plugin-react-hooks": "4.6.0",
|
|
52
|
+
"express": "4.18.2",
|
|
53
|
+
"husky": "8.0.2",
|
|
54
|
+
"jsdoc-to-markdown": "8.0.0",
|
|
55
|
+
"mocha": "10.2.0",
|
|
56
56
|
"mochawesome": "7.1.3",
|
|
57
57
|
"nyc": "15.1.0"
|
|
58
58
|
}
|