@5minds/node-red-contrib-processcube 1.5.2-develop-7add47-m2xezg12 → 1.5.2-feature-059ba7-m35qmr4f

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@5minds/node-red-contrib-processcube",
3
- "version": "1.5.2-develop-7add47-m2xezg12",
3
+ "version": "1.5.2-feature-059ba7-m35qmr4f",
4
4
  "license": "MIT",
5
5
  "description": "Node-RED nodes for ProcessCube",
6
6
  "scripts": {
@@ -57,7 +57,7 @@
57
57
  "examples": "examples"
58
58
  },
59
59
  "dependencies": {
60
- "@5minds/processcube_engine_client": "^5.1.0",
60
+ "@5minds/processcube_engine_client": "5.1.0-hotfix-a73235-m346kg1n",
61
61
  "jwt-decode": "^4.0.0",
62
62
  "openid-client": "^5.5.0"
63
63
  },
@@ -36,6 +36,46 @@ module.exports = function (RED) {
36
36
  }
37
37
  };
38
38
 
39
+ async function getFreshIdentity(url) {
40
+ console.log('luis777');
41
+ if (
42
+ !RED.util.evaluateNodeProperty(n.clientId, n.clientIdType, node) ||
43
+ !RED.util.evaluateNodeProperty(n.clientSecret, n.clientSecretType, node)
44
+ )
45
+ return null;
46
+ const res = await fetch(url + '/atlas_engine/api/v1/authority', {
47
+ method: 'GET',
48
+ headers: {
49
+ Authorization: `Bearer ZHVtbXlfdG9rZW4=`,
50
+ 'Content-Type': 'application/json',
51
+ },
52
+ });
53
+
54
+ const issuer = await oidc.Issuer.discover(await res.json());
55
+
56
+ const client = new issuer.Client({
57
+ client_id: RED.util.evaluateNodeProperty(n.clientId, n.clientIdType, node),
58
+ client_secret: RED.util.evaluateNodeProperty(n.clientSecret, n.clientSecretType, node),
59
+ });
60
+
61
+ const tokenSet = await client.grant({
62
+ grant_type: 'client_credentials',
63
+ scope: 'engine_etw engine_read engine_write',
64
+ });
65
+
66
+ const accessToken = tokenSet.access_token;
67
+ const decodedToken = jwt.jwtDecode(accessToken);
68
+
69
+ const freshIdentity = {
70
+ token: tokenSet.access_token,
71
+ userId: decodedToken.sub,
72
+ };
73
+
74
+ configNode.setIdentity(freshIdentity);
75
+
76
+ return freshIdentity;
77
+ }
78
+
39
79
  node.on('close', async () => {
40
80
  if (this.engineClient) {
41
81
  this.engineClient.dispose();
@@ -44,25 +84,25 @@ module.exports = function (RED) {
44
84
  });
45
85
 
46
86
  if (this.credentials.clientId && this.credentials.clientSecret) {
47
- this.engineClient = new engine_client.EngineClient(this.url);
48
-
49
- this.engineClient.applicationInfo
50
- .getAuthorityAddress()
51
- .then((authorityUrl) => {
52
- startRefreshingIdentityCycle(
53
- this.credentials.clientId,
54
- this.credentials.clientSecret,
55
- authorityUrl,
56
- node
57
- ).catch((reason) => {
58
- console.error(reason);
59
- node.error(reason);
60
- });
61
- })
62
- .catch((reason) => {
63
- console.error(reason);
64
- node.error(reason);
65
- });
87
+ this.engineClient = new engine_client.EngineClient(this.url, () => getFreshIdentity(this.url));
88
+
89
+ // this.engineClient.applicationInfo
90
+ // .getAuthorityAddress()
91
+ // .then((authorityUrl) => {
92
+ // startRefreshingIdentityCycle(
93
+ // this.credentials.clientId,
94
+ // this.credentials.clientSecret,
95
+ // authorityUrl,
96
+ // node
97
+ // ).catch((reason) => {
98
+ // console.error(reason);
99
+ // node.error(reason);
100
+ // });
101
+ // })
102
+ // .catch((reason) => {
103
+ // console.error(reason);
104
+ // node.error(reason);
105
+ // });
66
106
  } else {
67
107
  this.engineClient = new engine_client.EngineClient(this.url);
68
108
  }
@@ -75,84 +115,84 @@ module.exports = function (RED) {
75
115
  });
76
116
  };
77
117
 
78
- async function getFreshTokenSet(clientId, clientSecret, authorityUrl) {
79
- const issuer = await oidc.Issuer.discover(authorityUrl);
80
-
81
- const client = new issuer.Client({
82
- client_id: clientId,
83
- client_secret: clientSecret,
84
- });
85
-
86
- const tokenSet = await client.grant({
87
- grant_type: 'client_credentials',
88
- scope: 'engine_etw engine_read engine_write',
89
- });
90
-
91
- return tokenSet;
92
- }
93
-
94
- function getIdentityForExternalTaskWorkers(tokenSet) {
95
- const accessToken = tokenSet.access_token;
96
- const decodedToken = jwt.jwtDecode(accessToken);
97
-
98
- return {
99
- token: tokenSet.access_token,
100
- userId: decodedToken.sub,
101
- };
102
- }
103
-
104
- async function getExpiresInForExternalTaskWorkers(tokenSet) {
105
- let expiresIn = tokenSet.expires_in;
106
-
107
- if (!expiresIn && tokenSet.expires_at) {
108
- expiresIn = Math.floor(tokenSet.expires_at - Date.now() / 1000);
109
- }
110
-
111
- if (expiresIn === undefined) {
112
- throw new Error('Could not determine the time until the access token for external task workers expires');
113
- }
114
-
115
- return expiresIn;
116
- }
117
-
118
- /**
119
- * Start refreshing the identity in regular intervals.
120
- * @param {TokenSet | null} tokenSet The token set to refresh the identity for
121
- * @returns {Promise<void>} A promise that resolves when the timer for refreshing the identity is initialized
122
- * */
123
- async function startRefreshingIdentityCycle(clientId, clientSecret, authorityUrl, configNode) {
124
- let retries = 5;
125
-
126
- const refresh = async () => {
127
- try {
128
- const newTokenSet = await getFreshTokenSet(clientId, clientSecret, authorityUrl);
129
- const expiresIn = await getExpiresInForExternalTaskWorkers(newTokenSet);
130
- const delay = expiresIn * DELAY_FACTOR * 1000;
131
-
132
- freshIdentity = getIdentityForExternalTaskWorkers(newTokenSet);
133
-
134
- configNode.setIdentity(freshIdentity);
135
-
136
- retries = 5;
137
- setTimeout(refresh, delay);
138
- } catch (error) {
139
- if (retries === 0) {
140
- console.error(
141
- 'Could not refresh identity for external task worker processes. Stopping all external task workers.',
142
- { error }
143
- );
144
- return;
145
- }
146
- console.error('Could not refresh identity for external task worker processes.', {
147
- error,
148
- retryCount: retries,
149
- });
150
- retries--;
151
-
152
- const delay = 2 * 1000;
153
- setTimeout(refresh, delay);
154
- }
155
- };
156
-
157
- await refresh();
158
- }
118
+ // async function getFreshTokenSet(clientId, clientSecret, authorityUrl) {
119
+ // const issuer = await oidc.Issuer.discover(authorityUrl);
120
+
121
+ // const client = new issuer.Client({
122
+ // client_id: clientId,
123
+ // client_secret: clientSecret,
124
+ // });
125
+
126
+ // const tokenSet = await client.grant({
127
+ // grant_type: 'client_credentials',
128
+ // scope: 'engine_etw engine_read engine_write',
129
+ // });
130
+
131
+ // return tokenSet;
132
+ // }
133
+
134
+ // function getIdentityForExternalTaskWorkers(tokenSet) {
135
+ // const accessToken = tokenSet.access_token;
136
+ // const decodedToken = jwt.jwtDecode(accessToken);
137
+
138
+ // return {
139
+ // token: tokenSet.access_token,
140
+ // userId: decodedToken.sub,
141
+ // };
142
+ // }
143
+
144
+ // async function getExpiresInForExternalTaskWorkers(tokenSet) {
145
+ // let expiresIn = tokenSet.expires_in;
146
+
147
+ // if (!expiresIn && tokenSet.expires_at) {
148
+ // expiresIn = Math.floor(tokenSet.expires_at - Date.now() / 1000);
149
+ // }
150
+
151
+ // if (expiresIn === undefined) {
152
+ // throw new Error('Could not determine the time until the access token for external task workers expires');
153
+ // }
154
+
155
+ // return expiresIn;
156
+ // }
157
+
158
+ // /**
159
+ // * Start refreshing the identity in regular intervals.
160
+ // * @param {TokenSet | null} tokenSet The token set to refresh the identity for
161
+ // * @returns {Promise<void>} A promise that resolves when the timer for refreshing the identity is initialized
162
+ // * */
163
+ // async function startRefreshingIdentityCycle(clientId, clientSecret, authorityUrl, configNode) {
164
+ // let retries = 5;
165
+
166
+ // const refresh = async () => {
167
+ // try {
168
+ // const newTokenSet = await getFreshTokenSet(clientId, clientSecret, authorityUrl);
169
+ // const expiresIn = await getExpiresInForExternalTaskWorkers(newTokenSet);
170
+ // const delay = expiresIn * DELAY_FACTOR * 1000;
171
+
172
+ // freshIdentity = getIdentityForExternalTaskWorkers(newTokenSet);
173
+
174
+ // configNode.setIdentity(freshIdentity);
175
+
176
+ // retries = 5;
177
+ // setTimeout(refresh, delay);
178
+ // } catch (error) {
179
+ // if (retries === 0) {
180
+ // console.error(
181
+ // 'Could not refresh identity for external task worker processes. Stopping all external task workers.',
182
+ // { error }
183
+ // );
184
+ // return;
185
+ // }
186
+ // console.error('Could not refresh identity for external task worker processes.', {
187
+ // error,
188
+ // retryCount: retries,
189
+ // });
190
+ // retries--;
191
+
192
+ // const delay = 2 * 1000;
193
+ // setTimeout(refresh, delay);
194
+ // }
195
+ // };
196
+
197
+ // await refresh();
198
+ // }