@nebula.js/cli-serve 3.0.4 → 3.1.0-alpha.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/lib/serve.js CHANGED
@@ -153,6 +153,7 @@ module.exports = async (argv) => {
153
153
  port,
154
154
  disableHostCheck: serveConfig.disableHostCheck,
155
155
  enigmaConfig,
156
+ clientId: serveConfig.clientId,
156
157
  webIntegrationId: serveConfig.webIntegrationId,
157
158
  snName: serveConfig.type || snName,
158
159
  snUrl,
@@ -4,6 +4,7 @@ const chalk = require('chalk');
4
4
  const express = require('express');
5
5
  const fs = require('fs');
6
6
  const homedir = require('os').homedir();
7
+ const { Auth, AuthType } = require('@qlik/sdk');
7
8
 
8
9
  const webpack = require('webpack');
9
10
  const WebpackDevServer = require('webpack-dev-server');
@@ -14,11 +15,25 @@ const snapshotRouter = require('./snapshot-router');
14
15
  const httpsKeyPath = path.join(homedir, '.certs/key.pem');
15
16
  const httpsCertPath = path.join(homedir, '.certs/cert.pem');
16
17
 
18
+ let authInstance = null;
19
+ const getAuthInstance = (returnToOrigin, host, clientId) => {
20
+ if (authInstance) return authInstance;
21
+
22
+ authInstance = new Auth({
23
+ authType: AuthType.OAuth2,
24
+ host,
25
+ clientId,
26
+ redirectUri: `${returnToOrigin}/login/callback`,
27
+ });
28
+ return authInstance;
29
+ };
30
+
17
31
  module.exports = async ({
18
32
  host,
19
33
  port,
20
34
  disableHostCheck,
21
35
  enigmaConfig,
36
+ clientId,
22
37
  webIntegrationId,
23
38
  snName,
24
39
  snUrl,
@@ -142,7 +157,10 @@ module.exports = async ({
142
157
  res.set(devServer.options.headers);
143
158
  res.json({
144
159
  enigma: enigmaConfig,
160
+ clientId,
145
161
  webIntegrationId,
162
+ isClientIdProvided: Boolean(clientId),
163
+ isWebIntegrationIdProvided: Boolean(webIntegrationId),
146
164
  supernova: {
147
165
  name: snName,
148
166
  url: snUrl,
@@ -157,6 +175,95 @@ module.exports = async ({
157
175
  });
158
176
  });
159
177
 
178
+ let cachedHost = null;
179
+ let cachedClientId = null;
180
+
181
+ app.get('/oauth', async (req, res) => {
182
+ const { host: qHost, clientId: qClientId } = req.query;
183
+ if (!cachedHost && !cachedClientId) {
184
+ cachedHost = qHost;
185
+ cachedClientId = qClientId;
186
+ }
187
+
188
+ const returnTo = `${req.protocol}://${req.get('host')}`;
189
+ const instacne = getAuthInstance(returnTo, qHost, qClientId);
190
+ const isAuthorized = await instacne.isAuthorized();
191
+ if (!isAuthorized) {
192
+ const { url: redirectUrl } = await instacne.generateAuthorizationUrl();
193
+ res.status(200).json({ redirectUrl });
194
+ } else {
195
+ const redirectUrl = `${req.protocol}://${req.get(
196
+ 'host'
197
+ )}/?engine_url=wss://${cachedHost}&qlik-client-id=${cachedClientId}&shouldFetchAppList=true`;
198
+ cachedHost = null;
199
+ cachedClientId = null;
200
+ res.redirect(redirectUrl);
201
+ }
202
+ });
203
+
204
+ app.get('/login/callback', async (req, res) => {
205
+ const authLink = new URL(req.url, `http://${req.headers.host}`).href;
206
+ try {
207
+ // TODO:
208
+ // this is a temp fix in front end side
209
+ // (temp workaround of not presisting origin while backend tries to authorize user)
210
+ // they need to handle this in qlik-sdk-typescript repo
211
+ // and will notify us about when they got fixed it,
212
+ // but until then, we need to take care of it here!
213
+ authInstance.rest.interceptors.request.use((_req) => {
214
+ // eslint-disable-next-line no-param-reassign, dot-notation
215
+ _req[1]['headers'] = { origin: 'http://localhost:8000' };
216
+ return _req;
217
+ });
218
+ await authInstance.authorize(authLink);
219
+ res.redirect(301, '/oauth/');
220
+ } catch (err) {
221
+ console.log({ err });
222
+ res.status(401).send(JSON.stringify(err, null, 2));
223
+ }
224
+ });
225
+
226
+ app.get('/getSocketUrl/:appId', async (req, res) => {
227
+ const { appId } = req.params;
228
+ const webSocketUrl = await authInstance.generateWebsocketUrl(appId, true);
229
+ res.status(200).json({ webSocketUrl });
230
+ });
231
+
232
+ app.get('/deauthorize', async (req, res) => {
233
+ try {
234
+ await authInstance.deauthorize();
235
+ res.status(200).json({
236
+ deauthorize: true,
237
+ });
238
+ } catch (error) {
239
+ console.log({ error });
240
+ }
241
+ });
242
+
243
+ app.get('/isAuthorized', async (req, res) => {
244
+ if (!authInstance) {
245
+ res.status(200).json({
246
+ isAuthorized: false,
247
+ });
248
+ } else {
249
+ const isAuthorized = await authInstance.isAuthorized();
250
+ res.status(200).json({
251
+ isAuthorized,
252
+ });
253
+ }
254
+ });
255
+
256
+ app.get('/apps', async (req, res) => {
257
+ const appsListUrl = `/items?resourceType=app&limit=30&sort=-updatedAt`;
258
+ const { data = [] } = await (await authInstance.rest(appsListUrl)).json();
259
+ res.status(200).json(
260
+ data.map((d) => ({
261
+ qDocId: d.resourceId,
262
+ qTitle: d.name,
263
+ }))
264
+ );
265
+ });
266
+
160
267
  if (serveConfig.resources) {
161
268
  app.use('/resources', express.static(serveConfig.resources));
162
269
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nebula.js/cli-serve",
3
- "version": "3.0.4",
3
+ "version": "3.1.0-alpha.0",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "author": "QlikTech International AB",
@@ -27,7 +27,7 @@
27
27
  "prepublishOnly": "rm -rf dist && yarn run build"
28
28
  },
29
29
  "dependencies": {
30
- "@nebula.js/cli-build": "^3.0.4",
30
+ "@nebula.js/cli-build": "^3.1.0-alpha.0",
31
31
  "body-parser": "1.20.0",
32
32
  "chalk": "4.1.2",
33
33
  "chokidar": "3.5.3",
@@ -49,12 +49,13 @@
49
49
  "@babel/preset-env": "7.19.3",
50
50
  "@babel/preset-react": "7.18.6",
51
51
  "@mui/material": "^5.10.8",
52
- "@nebula.js/enigma-mocker": "^3.0.4",
53
- "@nebula.js/nucleus": "^3.0.4",
54
- "@nebula.js/snapshooter": "^3.0.4",
55
- "@nebula.js/stardust": "^3.0.4",
56
- "@nebula.js/ui": "^3.0.4",
57
- "@qlik/sdk": "^0.10.2",
52
+ "@mui/system": "^5.10.8",
53
+ "@nebula.js/enigma-mocker": "^3.1.0-alpha.0",
54
+ "@nebula.js/nucleus": "^3.1.0-alpha.0",
55
+ "@nebula.js/snapshooter": "^3.1.0-alpha.0",
56
+ "@nebula.js/stardust": "^3.1.0-alpha.0",
57
+ "@nebula.js/ui": "^3.1.0-alpha.0",
58
+ "@qlik/sdk": "^0.12.0",
58
59
  "autosuggest-highlight": "3.3.4",
59
60
  "babel-loader": "8.2.5",
60
61
  "d3-require": "1.3.0",
@@ -68,5 +69,5 @@
68
69
  "react-dom": "18.2.0",
69
70
  "webpack-cli": "4.10.0"
70
71
  },
71
- "gitHead": "419f994f8f5e15235fa20057bc9085b03a9a904b"
72
+ "gitHead": "06eb9b2d1876ac3abce9a1c749c0b428d3251481"
72
73
  }