@itentialopensource/adapter-utils 5.3.10 → 5.5.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/CHANGELOG.md CHANGED
@@ -1,4 +1,24 @@
1
1
 
2
+ ## 5.5.0 [05-30-2024]
3
+
4
+ * Add support for gzip responsedatatype
5
+
6
+ Closes ADAPT-3270
7
+
8
+ See merge request itentialopensource/adapter-utils!293
9
+
10
+ ---
11
+
12
+ ## 5.4.0 [04-26-2024]
13
+
14
+ * changes to support changing scope and resource
15
+
16
+ Closes ADAPT-3342
17
+
18
+ See merge request itentialopensource/adapter-utils!292
19
+
20
+ ---
21
+
2
22
  ## 5.3.10 [03-06-2024]
3
23
 
4
24
  * Fix some security vulnerabilities
@@ -58,6 +58,8 @@ let username = null;
58
58
  let password = null;
59
59
  let clientId = null;
60
60
  let clientSecret = null;
61
+ let scope = null;
62
+ let resource = null;
61
63
  let grantType = null;
62
64
  let staticToken = null;
63
65
  let tokenUserField = 'username';
@@ -2083,6 +2085,12 @@ async function buildTokenRequest(reqPath, reqBody, callProperties, callback) {
2083
2085
  if (clientSecret) {
2084
2086
  creds.client_secret = clientSecret;
2085
2087
  }
2088
+ if (scope) {
2089
+ creds.scope = scope;
2090
+ }
2091
+ if (resource) {
2092
+ creds.resource = resource;
2093
+ }
2086
2094
  if (grantType) {
2087
2095
  creds.grant_type = grantType;
2088
2096
  }
@@ -2092,6 +2100,12 @@ async function buildTokenRequest(reqPath, reqBody, callProperties, callback) {
2092
2100
  if (callProperties && callProperties.authentication && callProperties.authentication.client_secret) {
2093
2101
  creds.client_secret = callProperties.authentication.client_secret;
2094
2102
  }
2103
+ if (callProperties && callProperties.authentication && callProperties.authentication.scope) {
2104
+ creds.scope = callProperties.authentication.scope;
2105
+ }
2106
+ if (callProperties && callProperties.authentication && callProperties.authentication.resource) {
2107
+ creds.resource = callProperties.authentication.resource;
2108
+ }
2095
2109
  if (callProperties && callProperties.authentication && callProperties.authentication.grant_type) {
2096
2110
  creds.grant_type = callProperties.authentication.grant_type;
2097
2111
  }
@@ -3986,6 +4000,16 @@ class ConnectorRest {
3986
4000
  clientSecret = props.authentication.client_secret;
3987
4001
  }
3988
4002
 
4003
+ // set the client secret (optional - default is null)
4004
+ if (typeof props.authentication.scope === 'string') {
4005
+ scope = props.authentication.scope;
4006
+ }
4007
+
4008
+ // set the client secret (optional - default is null)
4009
+ if (typeof props.authentication.resource === 'string') {
4010
+ resource = props.authentication.resource;
4011
+ }
4012
+
3989
4013
  // set the grant type (optional - default is null)
3990
4014
  if (typeof props.authentication.grant_type === 'string') {
3991
4015
  grantType = props.authentication.grant_type;
@@ -8,6 +8,7 @@ const querystring = require('querystring');
8
8
  const jsonQuery = require('json-query');
9
9
  const jsonxml = require('jsontoxml');
10
10
  const xml2js = require('xml2js');
11
+ const pako = require('pako');
11
12
 
12
13
  const globalSchema = JSON.parse(require('fs').readFileSync(require('path').join(__dirname, '/../schemas/globalSchema.json')));
13
14
 
@@ -52,6 +53,19 @@ function matchResponse(uriPath, method, type, mockresponses) {
52
53
  return null;
53
54
  }
54
55
 
56
+ /*
57
+ * INTERNAL FUNCTION: Parse XML
58
+ */
59
+ function parseXML(xmlString, callback) {
60
+ const parser = new xml2js.Parser({ explicitArray: false, attrkey: '_attr' });
61
+ parser.parseString(xmlString, (error, result) => {
62
+ if (error) {
63
+ return callback(error);
64
+ }
65
+ return callback(null, result);
66
+ });
67
+ }
68
+
55
69
  /*
56
70
  * INTERNAL FUNCTION: recursively inspect body data if heirarchical
57
71
  */
@@ -477,6 +491,53 @@ function handleRestRequest(request, entityId, entitySchema, callProperties, filt
477
491
  if (entitySchema && entitySchema.responseDatatype && entitySchema.responseDatatype.toUpperCase() === 'URLENCODE') {
478
492
  // return the response
479
493
  retResponse = querystring.parse(resObj.response.trim());
494
+ } else if (entitySchema && entitySchema.responseDatatype && (entitySchema.responseDatatype.toUpperCase() === 'GZIP2PLAIN'
495
+ || entitySchema.responseDatatype.toUpperCase() === 'GZIP2JSON' || entitySchema.responseDatatype.toUpperCase() === 'GZIP2XML'
496
+ || entitySchema.responseDatatype.toUpperCase() === 'GZIP2XML2JSON')) {
497
+ // If response is GZIP
498
+ const responseDatatype = entitySchema.responseDatatype.toUpperCase();
499
+ const responseBody = pako.inflate(Buffer.from(resObj.response, 'binary'), { to: 'string' });
500
+ // Handle conversion based on responseDatatype
501
+ if (responseDatatype === 'GZIP2PLAIN') {
502
+ retObject.response = responseBody;
503
+ } else if (responseDatatype === 'GZIP2JSON') {
504
+ // Parse responseBody as JSON
505
+ retObject.response = JSON.parse(responseBody);
506
+ } else if (responseDatatype === 'GZIP2XML' || responseDatatype === 'GZIP2XML2JSON') {
507
+ parseXML(responseBody, (error, xmlResult) => {
508
+ if (error) {
509
+ log.warn(`${origin}: Error parsing XML response: ${error}`);
510
+ return callback(retObject);
511
+ }
512
+
513
+ retObject.response = xmlResult;
514
+
515
+ if (responseDatatype === 'GZIP2XML2JSON') {
516
+ // Optionally convert XML to JSON
517
+ try {
518
+ const parser = new xml2js.Parser({ explicitArray: false, attrkey: '_attr' });
519
+ return parser.parseString(xmlResult, (err, result) => {
520
+ if (err) {
521
+ log.warn(`${origin}: Unable to parse xml to json ${err}`);
522
+ return callback(retObject);
523
+ }
524
+ // Logic to convert keys specified to array if object
525
+ if (xmlArrayKeys) {
526
+ setArrays(result, xmlArrayKeys);
527
+ }
528
+ retObject.response = result;
529
+ return callback(retObject);
530
+ });
531
+ } catch (ex) {
532
+ log.warn(`${origin}: Unable to get json from xml ${ex}`);
533
+ return callback(retObject);
534
+ }
535
+ }
536
+
537
+ log.debug(`${origin}: RESPONSE: ${retObject.response}`);
538
+ return callback(retObject);
539
+ });
540
+ }
480
541
  } else {
481
542
  // process the response - parse it
482
543
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itentialopensource/adapter-utils",
3
- "version": "5.3.10",
3
+ "version": "5.5.0",
4
4
  "description": "Itential Adapter Utility Libraries",
5
5
  "scripts": {
6
6
  "postinstall": "node utils/setup.js",
@@ -31,7 +31,7 @@
31
31
  "aws4": "^1.9.1",
32
32
  "cookie": "^0.5.0",
33
33
  "crypto-js": "^4.1.1",
34
- "ejs": "^3.1.9",
34
+ "ejs": "^3.1.10",
35
35
  "form-data": "^4.0.0",
36
36
  "fs-extra": "^11.1.1",
37
37
  "https-proxy-agent": "^7.0.0",
@@ -39,6 +39,7 @@
39
39
  "jsontoxml": "^1.0.1",
40
40
  "jsonwebtoken": "^9.0.1",
41
41
  "mongodb": "^3.7.4",
42
+ "pako": "^2.1.0",
42
43
  "querystring": "^0.2.0",
43
44
  "readline-sync": "^1.4.10",
44
45
  "socks-proxy-agent": "^8.0.1",
@@ -59,6 +60,5 @@
59
60
  "testdouble": "^3.18.0",
60
61
  "winston": "^3.9.0"
61
62
  },
62
- "peerDependencies": {},
63
63
  "private": false
64
64
  }
Binary file