@5minds/node-red-contrib-processcube 1.5.2-feature-4db23e-m33j5ixt → 1.5.2-feature-059ba7-m35qmr4f

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-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
  },
@@ -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,46 @@ module.exports = function (RED) {
80
36
  }
81
37
  };
82
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
+
83
79
  node.on('close', async () => {
84
80
  if (this.engineClient) {
85
81
  this.engineClient.dispose();
@@ -87,32 +83,29 @@ module.exports = function (RED) {
87
83
  }
88
84
  });
89
85
 
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
- // }
86
+ if (this.credentials.clientId && this.credentials.clientSecret) {
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
+ // });
106
+ } else {
107
+ this.engineClient = new engine_client.EngineClient(this.url);
108
+ }
116
109
  }
117
110
  RED.nodes.registerType('processcube-engine-config', ProcessCubeEngineNode, {
118
111
  credentials: {
@@ -122,84 +115,84 @@ module.exports = function (RED) {
122
115
  });
123
116
  };
124
117
 
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
- }
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
+ // }