@5minds/node-red-contrib-processcube 1.5.2-feature-4db23e-m33j5ixt → 1.5.2-feature-03d546-m361r6u4

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@5minds/node-red-contrib-processcube",
3
- "version": "1.5.2-feature-4db23e-m33j5ixt",
3
+ "version": "1.5.2-feature-03d546-m361r6u4",
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
  },
@@ -12,52 +12,8 @@ module.exports = function (RED) {
12
12
  this.url = RED.util.evaluateNodeProperty(n.url, n.urlType, node);
13
13
  this.identity = null;
14
14
 
15
- async function startUpdatingCredentialsCycle() {
16
- let retries = 5;
17
- const UPDATE_INTERVAL = 10000;
18
- let previousClientId, previousClientSecret;
19
-
20
- const updateCredentials = async () => {
21
- try {
22
- const newClientId = RED.util.evaluateNodeProperty(n.clientId, n.clientIdType, node);
23
- const newClientSecret = RED.util.evaluateNodeProperty(n.clientSecret, n.clientSecretType, node);
24
-
25
- if (newClientId !== previousClientId || newClientSecret !== previousClientSecret) {
26
- if (this.engineClient) this.engineClient.dispose();
27
-
28
- this.credentials.clientId = newClientId;
29
- this.credentials.clientSecret = newClientSecret;
30
-
31
- this.engineClient = new engine_client.EngineClient(this.url);
32
-
33
- if (this.credentials.clientId && this.credentials.clientSecret) {
34
- const authorityUrl = await this.engineClient.applicationInfo.getAuthorityAddress();
35
- startRefreshingIdentityCycle(
36
- this.credentials.clientId,
37
- this.credentials.clientSecret,
38
- authorityUrl,
39
- node
40
- ).catch(console.error);
41
- }
42
-
43
- previousClientId = newClientId;
44
- previousClientSecret = newClientSecret;
45
- }
46
-
47
- retries = 5;
48
- setTimeout(updateCredentials, UPDATE_INTERVAL);
49
- } catch (error) {
50
- if (retries === 0) return console.error('Credential update failed permanently:', error);
51
- console.error('Credential update error, retrying:', { error, retries });
52
- retries--;
53
- setTimeout(updateCredentials, 2000);
54
- }
55
- };
56
-
57
- await updateCredentials();
58
- }
59
-
60
- console.log('luis888', this.credentials.clientId);
15
+ this.credentials.clientId = RED.util.evaluateNodeProperty(n.clientId, n.clientIdType, node);
16
+ this.credentials.clientSecret = RED.util.evaluateNodeProperty(n.clientSecret, n.clientSecretType, node);
61
17
 
62
18
  this.registerOnIdentityChanged = function (callback) {
63
19
  identityChangedCallbacks.push(callback);
@@ -80,6 +36,51 @@ module.exports = function (RED) {
80
36
  }
81
37
  };
82
38
 
39
+ async function getFreshIdentity(url) {
40
+ try {
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
+ } catch (e) {
78
+ console.log('Could not get fresh identity', e);
79
+ node.error('Could not get fresh identity');
80
+ node.error(e);
81
+ }
82
+ }
83
+
83
84
  node.on('close', async () => {
84
85
  if (this.engineClient) {
85
86
  this.engineClient.dispose();
@@ -87,32 +88,29 @@ module.exports = function (RED) {
87
88
  }
88
89
  });
89
90
 
90
- startUpdatingCredentialsCycle();
91
-
92
- // if (this.credentials.clientId && this.credentials.clientSecret) {
93
- // console.log('luis999', this.credentials.clientId);
94
- // this.engineClient = new engine_client.EngineClient(this.url);
95
-
96
- // this.engineClient.applicationInfo
97
- // .getAuthorityAddress()
98
- // .then((authorityUrl) => {
99
- // startRefreshingIdentityCycle(
100
- // this.credentials.clientId,
101
- // this.credentials.clientSecret,
102
- // authorityUrl,
103
- // node
104
- // ).catch((reason) => {
105
- // console.error(reason);
106
- // node.error(reason);
107
- // });
108
- // })
109
- // .catch((reason) => {
110
- // console.error(reason);
111
- // node.error(reason);
112
- // });
113
- // } else {
114
- // this.engineClient = new engine_client.EngineClient(this.url);
115
- // }
91
+ if (this.credentials.clientId && this.credentials.clientSecret) {
92
+ this.engineClient = new engine_client.EngineClient(this.url, () => getFreshIdentity(this.url));
93
+
94
+ // this.engineClient.applicationInfo
95
+ // .getAuthorityAddress()
96
+ // .then((authorityUrl) => {
97
+ // startRefreshingIdentityCycle(
98
+ // this.credentials.clientId,
99
+ // this.credentials.clientSecret,
100
+ // authorityUrl,
101
+ // node
102
+ // ).catch((reason) => {
103
+ // console.error(reason);
104
+ // node.error(reason);
105
+ // });
106
+ // })
107
+ // .catch((reason) => {
108
+ // console.error(reason);
109
+ // node.error(reason);
110
+ // });
111
+ } else {
112
+ this.engineClient = new engine_client.EngineClient(this.url);
113
+ }
116
114
  }
117
115
  RED.nodes.registerType('processcube-engine-config', ProcessCubeEngineNode, {
118
116
  credentials: {
@@ -122,84 +120,84 @@ module.exports = function (RED) {
122
120
  });
123
121
  };
124
122
 
125
- async function getFreshTokenSet(clientId, clientSecret, authorityUrl) {
126
- const issuer = await oidc.Issuer.discover(authorityUrl);
127
-
128
- const client = new issuer.Client({
129
- client_id: clientId,
130
- client_secret: clientSecret,
131
- });
132
-
133
- const tokenSet = await client.grant({
134
- grant_type: 'client_credentials',
135
- scope: 'engine_etw engine_read engine_write',
136
- });
137
-
138
- return tokenSet;
139
- }
140
-
141
- function getIdentityForExternalTaskWorkers(tokenSet) {
142
- const accessToken = tokenSet.access_token;
143
- const decodedToken = jwt.jwtDecode(accessToken);
144
-
145
- return {
146
- token: tokenSet.access_token,
147
- userId: decodedToken.sub,
148
- };
149
- }
150
-
151
- async function getExpiresInForExternalTaskWorkers(tokenSet) {
152
- let expiresIn = tokenSet.expires_in;
153
-
154
- if (!expiresIn && tokenSet.expires_at) {
155
- expiresIn = Math.floor(tokenSet.expires_at - Date.now() / 1000);
156
- }
157
-
158
- if (expiresIn === undefined) {
159
- throw new Error('Could not determine the time until the access token for external task workers expires');
160
- }
161
-
162
- return expiresIn;
163
- }
164
-
165
- /**
166
- * Start refreshing the identity in regular intervals.
167
- * @param {TokenSet | null} tokenSet The token set to refresh the identity for
168
- * @returns {Promise<void>} A promise that resolves when the timer for refreshing the identity is initialized
169
- * */
170
- async function startRefreshingIdentityCycle(clientId, clientSecret, authorityUrl, configNode) {
171
- let retries = 5;
172
-
173
- const refresh = async () => {
174
- try {
175
- const newTokenSet = await getFreshTokenSet(clientId, clientSecret, authorityUrl);
176
- const expiresIn = await getExpiresInForExternalTaskWorkers(newTokenSet);
177
- const delay = expiresIn * DELAY_FACTOR * 1000;
178
-
179
- freshIdentity = getIdentityForExternalTaskWorkers(newTokenSet);
180
-
181
- configNode.setIdentity(freshIdentity);
182
-
183
- retries = 5;
184
- setTimeout(refresh, delay);
185
- } catch (error) {
186
- if (retries === 0) {
187
- console.error(
188
- 'Could not refresh identity for external task worker processes. Stopping all external task workers.',
189
- { error }
190
- );
191
- return;
192
- }
193
- console.error('Could not refresh identity for external task worker processes.', {
194
- error,
195
- retryCount: retries,
196
- });
197
- retries--;
198
-
199
- const delay = 2 * 1000;
200
- setTimeout(refresh, delay);
201
- }
202
- };
203
-
204
- await refresh();
205
- }
123
+ // async function getFreshTokenSet(clientId, clientSecret, authorityUrl) {
124
+ // const issuer = await oidc.Issuer.discover(authorityUrl);
125
+
126
+ // const client = new issuer.Client({
127
+ // client_id: clientId,
128
+ // client_secret: clientSecret,
129
+ // });
130
+
131
+ // const tokenSet = await client.grant({
132
+ // grant_type: 'client_credentials',
133
+ // scope: 'engine_etw engine_read engine_write',
134
+ // });
135
+
136
+ // return tokenSet;
137
+ // }
138
+
139
+ // function getIdentityForExternalTaskWorkers(tokenSet) {
140
+ // const accessToken = tokenSet.access_token;
141
+ // const decodedToken = jwt.jwtDecode(accessToken);
142
+
143
+ // return {
144
+ // token: tokenSet.access_token,
145
+ // userId: decodedToken.sub,
146
+ // };
147
+ // }
148
+
149
+ // async function getExpiresInForExternalTaskWorkers(tokenSet) {
150
+ // let expiresIn = tokenSet.expires_in;
151
+
152
+ // if (!expiresIn && tokenSet.expires_at) {
153
+ // expiresIn = Math.floor(tokenSet.expires_at - Date.now() / 1000);
154
+ // }
155
+
156
+ // if (expiresIn === undefined) {
157
+ // throw new Error('Could not determine the time until the access token for external task workers expires');
158
+ // }
159
+
160
+ // return expiresIn;
161
+ // }
162
+
163
+ // /**
164
+ // * Start refreshing the identity in regular intervals.
165
+ // * @param {TokenSet | null} tokenSet The token set to refresh the identity for
166
+ // * @returns {Promise<void>} A promise that resolves when the timer for refreshing the identity is initialized
167
+ // * */
168
+ // async function startRefreshingIdentityCycle(clientId, clientSecret, authorityUrl, configNode) {
169
+ // let retries = 5;
170
+
171
+ // const refresh = async () => {
172
+ // try {
173
+ // const newTokenSet = await getFreshTokenSet(clientId, clientSecret, authorityUrl);
174
+ // const expiresIn = await getExpiresInForExternalTaskWorkers(newTokenSet);
175
+ // const delay = expiresIn * DELAY_FACTOR * 1000;
176
+
177
+ // freshIdentity = getIdentityForExternalTaskWorkers(newTokenSet);
178
+
179
+ // configNode.setIdentity(freshIdentity);
180
+
181
+ // retries = 5;
182
+ // setTimeout(refresh, delay);
183
+ // } catch (error) {
184
+ // if (retries === 0) {
185
+ // console.error(
186
+ // 'Could not refresh identity for external task worker processes. Stopping all external task workers.',
187
+ // { error }
188
+ // );
189
+ // return;
190
+ // }
191
+ // console.error('Could not refresh identity for external task worker processes.', {
192
+ // error,
193
+ // retryCount: retries,
194
+ // });
195
+ // retries--;
196
+
197
+ // const delay = 2 * 1000;
198
+ // setTimeout(refresh, delay);
199
+ // }
200
+ // };
201
+
202
+ // await refresh();
203
+ // }