@researchdatabox/sails-hook-redbox-servicenow-catalog 0.0.11 → 0.0.13

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/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ v20.11.1
@@ -0,0 +1,14 @@
1
+ import { AppPage } from './app.po';
2
+
3
+ describe('catalog App', () => {
4
+ let page: AppPage;
5
+
6
+ beforeEach(() => {
7
+ page = new AppPage();
8
+ });
9
+
10
+ it('should display welcome message', () => {
11
+ page.navigateTo();
12
+ expect(page.getParagraphText()).toEqual('Welcome to app!');
13
+ });
14
+ });
@@ -0,0 +1,11 @@
1
+ import { browser, by, element } from 'protractor';
2
+
3
+ export class AppPage {
4
+ navigateTo() {
5
+ return browser.get('/');
6
+ }
7
+
8
+ getParagraphText() {
9
+ return element(by.css('app-root h1')).getText();
10
+ }
11
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../out-tsc/e2e",
5
+ "baseUrl": "./",
6
+ "module": "commonjs",
7
+ "target": "es5",
8
+ "types": [
9
+ "jasmine",
10
+ "jasminewd2",
11
+ "node"
12
+ ]
13
+ }
14
+ }
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
26
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
27
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,13 +31,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
32
  });
10
33
  };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
11
37
  Object.defineProperty(exports, "__esModule", { value: true });
12
38
  exports.Services = void 0;
13
- const numeral = require("numeral");
39
+ const numeral = __importStar(require("numeral"));
14
40
  const moment = require("moment");
15
- const axios_1 = require("axios");
41
+ const axios_1 = __importDefault(require("axios"));
16
42
  const util = require('util');
17
43
  const redbox_core_types_1 = require("@researchdatabox/redbox-core-types");
44
+ const axios_oauth2_1 = require("@scienta/axios-oauth2");
18
45
  var Services;
19
46
  (function (Services) {
20
47
  class ServicenowCatalogService extends redbox_core_types_1.Services.Core.Service {
@@ -63,6 +90,64 @@ var Services;
63
90
  sails.log.verbose(JSON.stringify(axiosOptions));
64
91
  let snResponse = null;
65
92
  try {
93
+ const oauthOptions = this.getConfig('catalog.oauth', options);
94
+ sails.log.verbose(JSON.stringify(oauthOptions));
95
+ let oauthEnabled = _.get(oauthOptions, 'enabled', 'false');
96
+ sails.log.verbose('OAuth enabled ' + oauthEnabled);
97
+ if (oauthEnabled == 'true') {
98
+ let oauthUrl = _.get(oauthOptions, 'url');
99
+ let oauthClientId = _.get(oauthOptions, 'clientId');
100
+ let oauthClientSecret = _.get(oauthOptions, 'clientSecret');
101
+ let oauthScope = _.get(oauthOptions, 'scope');
102
+ let oauthGrantType = _.get(oauthOptions, 'grantType');
103
+ sails.log.verbose('================================================');
104
+ sails.log.verbose('oauthUrl ' + oauthUrl);
105
+ sails.log.verbose('oauthClientId ' + oauthClientId);
106
+ sails.log.verbose('oauthClientSecret ' + oauthClientSecret);
107
+ sails.log.verbose('oauthScope ' + oauthScope);
108
+ sails.log.verbose('oauthGrantType ' + oauthGrantType);
109
+ let oauthConfig = {};
110
+ if (oauthGrantType == 'password') {
111
+ let oauthUsername = _.get(oauthOptions, 'username');
112
+ let oauthPassword = _.get(oauthOptions, 'password');
113
+ sails.log.verbose('oauthUsername ' + oauthUsername);
114
+ sails.log.verbose('oauthPassword ' + oauthPassword);
115
+ oauthConfig = {
116
+ url: oauthUrl,
117
+ grant_type: oauthGrantType,
118
+ client_id: oauthClientId,
119
+ client_secret: oauthClientSecret,
120
+ scope: oauthScope,
121
+ username: oauthUsername,
122
+ password: oauthPassword
123
+ };
124
+ }
125
+ else if (oauthGrantType == 'refresh_token') {
126
+ let oauthRefreshToken = _.get(oauthOptions, 'refreshToken');
127
+ oauthConfig = {
128
+ url: oauthUrl,
129
+ grant_type: oauthGrantType,
130
+ client_id: oauthClientId,
131
+ client_secret: oauthClientSecret,
132
+ scope: oauthScope,
133
+ 'refresh_token': oauthRefreshToken
134
+ };
135
+ }
136
+ else {
137
+ oauthConfig = {
138
+ url: oauthUrl,
139
+ grant_type: 'client_credentials',
140
+ client_id: oauthClientId,
141
+ client_secret: oauthClientSecret,
142
+ scope: oauthScope
143
+ };
144
+ }
145
+ const client = (0, axios_oauth2_1.clientFactory)(axios_1.default.create(), oauthConfig);
146
+ const auth1 = yield client();
147
+ sails.log.verbose(JSON.stringify(auth1));
148
+ sails.log.verbose('================================================');
149
+ _.set(axiosOptions, 'headers.Authorization', 'Bearer ' + _.get(auth1, 'access_token'));
150
+ }
66
151
  snResponse = yield (0, axios_1.default)(axiosOptions);
67
152
  }
68
153
  catch (err) {
@@ -97,9 +182,10 @@ var Services;
97
182
  }
98
183
  runDataRemap(source, target, fields) {
99
184
  for (const fieldDef of fields) {
100
- const source_field = fieldDef.source_field;
101
- const dest_field = fieldDef.dest_field;
102
- const dest_template = fieldDef.dest_template;
185
+ const source_field = _.get(fieldDef, 'source_field');
186
+ const dest_field = _.get(fieldDef, 'dest_field');
187
+ const dest_template = _.get(fieldDef, 'dest_template');
188
+ let parseObject = _.get(fieldDef, 'parseObject', false);
103
189
  let src_data = null;
104
190
  if (!_.isEmpty(source_field)) {
105
191
  src_data = _.get(source, source_field);
@@ -111,7 +197,13 @@ var Services;
111
197
  src_data = template();
112
198
  }
113
199
  if (!_.isEmpty(dest_field)) {
114
- _.set(target, dest_field, src_data);
200
+ if (parseObject) {
201
+ let obj = JSON.parse(src_data);
202
+ _.set(target, dest_field, obj);
203
+ }
204
+ else {
205
+ _.set(target, dest_field, src_data);
206
+ }
115
207
  }
116
208
  }
117
209
  return target;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@researchdatabox/sails-hook-redbox-servicenow-catalog",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "description": "ReDBox Workspace provisioner plugin for creating ServiceNow Catalog items.",
5
5
  "main": "index.js",
6
6
  "sails": {
@@ -42,7 +42,8 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@researchdatabox/redbox-core-types": "^1.0.5-beta",
45
- "axios": "^0.20.0",
45
+ "@scienta/axios-oauth2": "^1.2.1",
46
+ "axios": "^1.6.7",
46
47
  "fs-extra": "^8.1.0",
47
48
  "lodash": "4.17.14",
48
49
  "lodash-es": "4.17.14",
package/tsconfig.json CHANGED
@@ -16,7 +16,8 @@
16
16
  "suppressImplicitAnyIndexErrors": true,
17
17
  "sourceMap": false,
18
18
  "outDir": "./",
19
- "rootDir": "./typescript"
19
+ "rootDir": "./typescript",
20
+ "esModuleInterop": true
20
21
  },
21
22
  "exclude": [
22
23
  "node_modules/**",
@@ -1,11 +1,11 @@
1
1
  import {Sails, Model} from 'sails';
2
2
  import {Observable, from, of, throwError} from 'rxjs';
3
3
  import * as numeral from 'numeral';
4
- // import * as got from 'got';
5
4
  import moment = require('moment');
6
5
  import axios from 'axios';
7
6
  const util = require('util');
8
7
  import { RecordsService, Services as services} from '@researchdatabox/redbox-core-types';
8
+ import {clientFactory, interceptorFactory} from '@scienta/axios-oauth2';
9
9
 
10
10
  declare var sails: Sails;
11
11
  declare var WorkspaceService, _;
@@ -70,9 +70,72 @@ export module Services {
70
70
  const axiosOptions = this.getConfig('catalog.axios', options);
71
71
  axiosOptions.data = body_template;
72
72
  sails.log.verbose(JSON.stringify(axiosOptions));
73
+
73
74
  let snResponse = null;
74
75
  try {
76
+ const oauthOptions = this.getConfig('catalog.oauth', options);
77
+ sails.log.verbose(JSON.stringify(oauthOptions));
78
+ let oauthEnabled = _.get(oauthOptions, 'enabled', 'false');
79
+ sails.log.verbose('OAuth enabled '+oauthEnabled);
80
+ if(oauthEnabled == 'true') {
81
+ let oauthUrl = _.get(oauthOptions, 'url');
82
+ let oauthClientId = _.get(oauthOptions, 'clientId');
83
+ let oauthClientSecret = _.get(oauthOptions, 'clientSecret');
84
+ let oauthScope = _.get(oauthOptions, 'scope');
85
+ let oauthGrantType = _.get(oauthOptions, 'grantType');
86
+ sails.log.verbose('================================================');
87
+ sails.log.verbose('oauthUrl '+oauthUrl);
88
+ sails.log.verbose('oauthClientId '+oauthClientId);
89
+ sails.log.verbose('oauthClientSecret '+oauthClientSecret);
90
+ sails.log.verbose('oauthScope '+oauthScope);
91
+ sails.log.verbose('oauthGrantType '+oauthGrantType);
92
+
93
+ let oauthConfig = {};
94
+ if(oauthGrantType == 'password') {
95
+ let oauthUsername = _.get(oauthOptions, 'username');
96
+ let oauthPassword = _.get(oauthOptions, 'password');
97
+ sails.log.verbose('oauthUsername '+oauthUsername);
98
+ sails.log.verbose('oauthPassword '+oauthPassword);
99
+ oauthConfig = {
100
+ url: oauthUrl,
101
+ grant_type: oauthGrantType,
102
+ client_id: oauthClientId,
103
+ client_secret: oauthClientSecret,
104
+ scope: oauthScope,
105
+ username: oauthUsername,
106
+ password: oauthPassword
107
+ };
108
+ } else if(oauthGrantType == 'refresh_token') {
109
+ let oauthRefreshToken = _.get(oauthOptions, 'refreshToken');
110
+ oauthConfig = {
111
+ url: oauthUrl,
112
+ grant_type: oauthGrantType,
113
+ client_id: oauthClientId,
114
+ client_secret: oauthClientSecret,
115
+ scope: oauthScope,
116
+ 'refresh_token': oauthRefreshToken
117
+ };
118
+ } else {
119
+ oauthConfig = {
120
+ url: oauthUrl,
121
+ grant_type: 'client_credentials',
122
+ client_id: oauthClientId,
123
+ client_secret: oauthClientSecret,
124
+ scope: oauthScope
125
+ };
126
+ }
127
+
128
+ //@ts-ignore
129
+ const client = clientFactory(axios.create(), oauthConfig);
130
+ const auth1 = await client();
131
+ sails.log.verbose(JSON.stringify(auth1));
132
+ sails.log.verbose('================================================');
133
+
134
+ _.set(axiosOptions, 'headers.Authorization', 'Bearer '+ _.get(auth1, 'access_token'));
135
+ }
136
+
75
137
  snResponse = await axios(axiosOptions);
138
+
76
139
  } catch (err) {
77
140
  sails.log.error(`ServiceNowCatalog failed to submit to request for workspace OID: ${oid}, error is:`);
78
141
  sails.log.error(JSON.stringify(err));
@@ -107,9 +170,10 @@ export module Services {
107
170
 
108
171
  protected runDataRemap(source: any, target: any, fields: any[]) {
109
172
  for (const fieldDef of fields) {
110
- const source_field = fieldDef.source_field;
111
- const dest_field = fieldDef.dest_field;
112
- const dest_template = fieldDef.dest_template;
173
+ const source_field = _.get(fieldDef,'source_field');
174
+ const dest_field = _.get(fieldDef,'dest_field');
175
+ const dest_template = _.get(fieldDef,'dest_template');
176
+ let parseObject = _.get(fieldDef, 'parseObject', false);
113
177
  let src_data = null;
114
178
  if (!_.isEmpty(source_field)) {
115
179
  src_data = _.get(source, source_field);
@@ -121,7 +185,12 @@ export module Services {
121
185
  src_data = template();
122
186
  }
123
187
  if (!_.isEmpty(dest_field)) {
124
- _.set(target, dest_field, src_data);
188
+ if(parseObject) {
189
+ let obj = JSON.parse(src_data);
190
+ _.set(target, dest_field, obj);
191
+ } else {
192
+ _.set(target, dest_field, src_data);
193
+ }
125
194
  }
126
195
  }
127
196
  return target;