@aloma.io/integration-sdk 3.8.48 → 3.8.50
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/build/cli.mjs +0 -0
- package/build/internal/connector/server/on-connect/decrypt-config.mjs +2 -1
- package/build/internal/fetcher/fetcher.mjs +0 -1
- package/package.json +1 -1
- package/src/internal/connector/server/on-connect/decrypt-config.mts +2 -1
- package/src/internal/fetcher/fetcher.mts +0 -2
- package/build/internal/connector/server/on-connect.d.mts +0 -8
- package/build/internal/connector/server/on-connect.mjs +0 -297
package/build/cli.mjs
CHANGED
File without changes
|
@@ -16,7 +16,8 @@ export const decryptConfig = async ({ configSchema, config, secrets }) => {
|
|
16
16
|
decrypted[key] = await jwe.decrypt(value, config.id());
|
17
17
|
}
|
18
18
|
catch (e) {
|
19
|
-
|
19
|
+
const errorMsg = String(e).replaceAll(value, '***');
|
20
|
+
console.log('failed to decrypt key', key, config.id(), errorMsg);
|
20
21
|
}
|
21
22
|
}
|
22
23
|
}
|
@@ -73,7 +73,6 @@ export default class Fetcher {
|
|
73
73
|
options0?.headers?.['Content-type'] === 'application/json') {
|
74
74
|
options0.body = JSON.stringify(options0.body);
|
75
75
|
}
|
76
|
-
console.dir(options0, { depth: null });
|
77
76
|
const timeout = Math.min(options0?.timeout || 30 * 60 * 1000, 30 * 60 * 1000);
|
78
77
|
const ret = await fetch(theURL, {
|
79
78
|
...options0,
|
package/package.json
CHANGED
@@ -17,7 +17,8 @@ export const decryptConfig = async ({configSchema, config, secrets}) => {
|
|
17
17
|
try {
|
18
18
|
decrypted[key] = await jwe.decrypt(value, config.id());
|
19
19
|
} catch (e) {
|
20
|
-
|
20
|
+
const errorMsg = String(e).replaceAll(value, '***');
|
21
|
+
console.log('failed to decrypt key', key, config.id(), errorMsg);
|
21
22
|
}
|
22
23
|
}
|
23
24
|
}
|
@@ -119,8 +119,6 @@ export default class Fetcher {
|
|
119
119
|
options0.body = JSON.stringify(options0.body);
|
120
120
|
}
|
121
121
|
|
122
|
-
console.dir(options0, {depth: null})
|
123
|
-
|
124
122
|
const timeout = Math.min(options0?.timeout || 30 * 60 * 1000, 30 * 60 * 1000);
|
125
123
|
const ret = await fetch(theURL, {
|
126
124
|
...options0,
|
@@ -1,8 +0,0 @@
|
|
1
|
-
import Dispatcher from "../../dispatcher/index.mjs";
|
2
|
-
import { Config } from "../../websocket/config.mjs";
|
3
|
-
export declare const onConnect: ({ dispatcher, configSchema, config, start }: {
|
4
|
-
config: Config;
|
5
|
-
configSchema: any;
|
6
|
-
start: any;
|
7
|
-
dispatcher: Dispatcher;
|
8
|
-
}) => (transport: any) => void;
|
@@ -1,297 +0,0 @@
|
|
1
|
-
import { init } from "@paralleldrive/cuid2";
|
2
|
-
import Fetcher from "../../fetcher/fetcher.mjs";
|
3
|
-
import { OAuth } from "../../fetcher/oauth-fetcher.mjs";
|
4
|
-
const cuid = init({ length: 32 });
|
5
|
-
export const onConnect = ({ dispatcher, configSchema, config, start }) => {
|
6
|
-
return (transport) => {
|
7
|
-
dispatcher.onConfig = async function (secrets) {
|
8
|
-
const decrypted = {};
|
9
|
-
const fields = configSchema().fields;
|
10
|
-
const keys = Object.keys(secrets);
|
11
|
-
const jwe = await config.validateKeys("RSA-OAEP-256");
|
12
|
-
for (var i = 0; i < keys.length; ++i) {
|
13
|
-
const key = keys[i];
|
14
|
-
const value = secrets[key];
|
15
|
-
if (!value)
|
16
|
-
continue;
|
17
|
-
if (fields[key]?.plain || ["endpointUrl"].includes(key)) {
|
18
|
-
decrypted[key] = value;
|
19
|
-
}
|
20
|
-
else {
|
21
|
-
try {
|
22
|
-
decrypted[key] = await jwe.decrypt(value, config.id());
|
23
|
-
}
|
24
|
-
catch (e) {
|
25
|
-
console.log("failed to decrypt key", key, config.id(), e);
|
26
|
-
}
|
27
|
-
}
|
28
|
-
}
|
29
|
-
dispatcher.startOAuth = async function () {
|
30
|
-
if (!dispatcher._oauth)
|
31
|
-
throw new Error("oauth not configured");
|
32
|
-
const authorizationURL = process.env.OAUTH_AUTHORIZATION_URL ||
|
33
|
-
decrypted.authorizationURL ||
|
34
|
-
that._oauth.authorizationURL;
|
35
|
-
if (!authorizationURL)
|
36
|
-
throw new Error("authorizationURL not configured");
|
37
|
-
const clientId = decrypted.clientId ||
|
38
|
-
process.env.OAUTH_CLIENT_ID ||
|
39
|
-
dispatcher._oauth.clientId;
|
40
|
-
if (!clientId)
|
41
|
-
throw new Error("clientId not configured");
|
42
|
-
const scopes = process.env.OAUTH_SCOPE ||
|
43
|
-
decrypted.scope ||
|
44
|
-
dispatcher._oauth.scope ||
|
45
|
-
"";
|
46
|
-
const useCodeChallenge = !!that._oauth.useCodeChallenge;
|
47
|
-
return {
|
48
|
-
url: authorizationURL
|
49
|
-
.replace(/\{\{clientId\}\}/gi, encodeURIComponent(clientId))
|
50
|
-
.replace(/\{\{scope\}\}/gi, encodeURIComponent(scopes)),
|
51
|
-
useCodeChallenge,
|
52
|
-
};
|
53
|
-
};
|
54
|
-
dispatcher.finishOAuth = async function (arg) {
|
55
|
-
var that = this;
|
56
|
-
const tokenURL = process.env.OAUTH_TOKEN_URL ||
|
57
|
-
decrypted.tokenURL ||
|
58
|
-
that._oauth.tokenURL;
|
59
|
-
if (!dispatcher._oauth)
|
60
|
-
throw new Error("oauth not configured");
|
61
|
-
if (!tokenURL && !dispatcher._oauth.finishOAuth)
|
62
|
-
throw new Error("need tokenURL or finishOAuth(arg)");
|
63
|
-
var data = null;
|
64
|
-
const doFinish = async () => {
|
65
|
-
if (!arg.code || !arg.redirectURI)
|
66
|
-
throw new Error("need code and redirectUri");
|
67
|
-
const clientId = decrypted.clientId ||
|
68
|
-
process.env.OAUTH_CLIENT_ID ||
|
69
|
-
that._oauth.clientId;
|
70
|
-
if (!clientId)
|
71
|
-
throw new Error("clientId not configured");
|
72
|
-
const clientSecret = decrypted.clientSecret ||
|
73
|
-
process.env.OAUTH_CLIENT_SECRET ||
|
74
|
-
that._oauth.clientSecret;
|
75
|
-
if (!clientSecret)
|
76
|
-
throw new Error("clientSecret not configured");
|
77
|
-
const additionalTokenArgs = that._oauth.additionalTokenArgs || {};
|
78
|
-
const useAuthHeader = !!that._oauth.useAuthHeader;
|
79
|
-
const useCodeChallenge = !!that._oauth.useCodeChallenge;
|
80
|
-
let body = {
|
81
|
-
grant_type: "authorization_code",
|
82
|
-
...additionalTokenArgs,
|
83
|
-
code: arg.code,
|
84
|
-
redirect_uri: arg.redirectURI,
|
85
|
-
};
|
86
|
-
if (useCodeChallenge) {
|
87
|
-
body.code_verifier = arg.codeVerifier;
|
88
|
-
}
|
89
|
-
let headers = {
|
90
|
-
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
91
|
-
Accept: "application/json",
|
92
|
-
};
|
93
|
-
if (useAuthHeader) {
|
94
|
-
headers = {
|
95
|
-
...headers,
|
96
|
-
Authorization: `Basic ${btoa(`${clientId}:${clientSecret}`)}`,
|
97
|
-
};
|
98
|
-
}
|
99
|
-
else {
|
100
|
-
body = {
|
101
|
-
...body,
|
102
|
-
client_id: clientId,
|
103
|
-
client_secret: clientSecret,
|
104
|
-
};
|
105
|
-
}
|
106
|
-
const response = await fetch(tokenURL, {
|
107
|
-
method: "POST",
|
108
|
-
body: new URLSearchParams(body),
|
109
|
-
headers,
|
110
|
-
signal: AbortSignal.timeout(60 * 1000),
|
111
|
-
});
|
112
|
-
const status = await response.status;
|
113
|
-
const text = await response.text();
|
114
|
-
if (status === 200) {
|
115
|
-
const ret = JSON.parse(text);
|
116
|
-
if (ret.error) {
|
117
|
-
throw new Error(`${status} ${ret.error} ${ret.error_description || ""}`);
|
118
|
-
}
|
119
|
-
else if (ret.access_token) {
|
120
|
-
return { ...ret };
|
121
|
-
}
|
122
|
-
else {
|
123
|
-
throw new Error(status + " response has no access_token - " + text);
|
124
|
-
}
|
125
|
-
}
|
126
|
-
else {
|
127
|
-
throw new Error(status + " " + text);
|
128
|
-
}
|
129
|
-
};
|
130
|
-
if (dispatcher._oauth.finishOAuth) {
|
131
|
-
data = await dispatcher._oauth.finishOAuth({
|
132
|
-
arg,
|
133
|
-
doFinish,
|
134
|
-
transport,
|
135
|
-
});
|
136
|
-
}
|
137
|
-
else {
|
138
|
-
data = await doFinish();
|
139
|
-
}
|
140
|
-
const jwe = await config.validateKeys("RSA-OAEP-256");
|
141
|
-
return { value: await jwe.encrypt(data, "none", config.id()) };
|
142
|
-
};
|
143
|
-
const saveOAuthResult = async (what) => {
|
144
|
-
const jwe = await config.validateKeys("RSA-OAEP-256");
|
145
|
-
const value = await jwe.encrypt(what, "none", config.id());
|
146
|
-
const packet = transport.newPacket({});
|
147
|
-
packet.method("connector.config-update");
|
148
|
-
packet.args({
|
149
|
-
value,
|
150
|
-
});
|
151
|
-
transport.send(packet);
|
152
|
-
};
|
153
|
-
const that = this;
|
154
|
-
const getRefreshToken = async (refreshToken) => {
|
155
|
-
const tokenURL = process.env.OAUTH_TOKEN_URL ||
|
156
|
-
decrypted.tokenURL ||
|
157
|
-
that._oauth.tokenURL;
|
158
|
-
const clientId = decrypted.clientId ||
|
159
|
-
process.env.OAUTH_CLIENT_ID ||
|
160
|
-
that._oauth.clientId;
|
161
|
-
if (!clientId)
|
162
|
-
throw new Error("clientId not configured");
|
163
|
-
const clientSecret = decrypted.clientSecret ||
|
164
|
-
process.env.OAUTH_CLIENT_SECRET ||
|
165
|
-
that._oauth.clientSecret;
|
166
|
-
if (!clientSecret)
|
167
|
-
throw new Error("clientSecret not configured");
|
168
|
-
const useAuthHeader = !!that._oauth.useAuthHeader;
|
169
|
-
let headers = {};
|
170
|
-
if (useAuthHeader) {
|
171
|
-
headers = {
|
172
|
-
...headers,
|
173
|
-
Authorization: `Basic ${btoa(`${clientId}:${clientSecret}`)}`,
|
174
|
-
};
|
175
|
-
}
|
176
|
-
const response = await fetch(tokenURL, {
|
177
|
-
method: "POST",
|
178
|
-
body: new URLSearchParams({
|
179
|
-
grant_type: "refresh_token",
|
180
|
-
refresh_token: refreshToken,
|
181
|
-
client_id: clientId,
|
182
|
-
client_secret: clientSecret,
|
183
|
-
}),
|
184
|
-
headers: {
|
185
|
-
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
186
|
-
Accept: "application/json",
|
187
|
-
...headers,
|
188
|
-
},
|
189
|
-
signal: AbortSignal.timeout(60 * 1000),
|
190
|
-
});
|
191
|
-
const status = await response.status;
|
192
|
-
const text = await response.text();
|
193
|
-
if (status === 200) {
|
194
|
-
return JSON.parse(text);
|
195
|
-
}
|
196
|
-
else {
|
197
|
-
throw new Error("could not get refresh token " + status + " " + text);
|
198
|
-
}
|
199
|
-
};
|
200
|
-
const theOAuth = that._oauth
|
201
|
-
? new OAuth(decrypted.oauthResult, saveOAuthResult, getRefreshToken)
|
202
|
-
: null;
|
203
|
-
if (theOAuth) {
|
204
|
-
clearInterval(dispatcher._refreshOAuthToken);
|
205
|
-
if (!(dispatcher._oauth.noPeriodicTokenRefresh === false)) {
|
206
|
-
dispatcher._refreshOAuthToken = setInterval(async () => {
|
207
|
-
try {
|
208
|
-
console.log("refreshing oauth token");
|
209
|
-
await theOAuth.periodicRefresh();
|
210
|
-
}
|
211
|
-
catch (e) {
|
212
|
-
console.log("periodic refresh", e);
|
213
|
-
}
|
214
|
-
}, dispatcher._oauth.tokenRefreshPeriod || 4 * 60 * 60 * 15000);
|
215
|
-
}
|
216
|
-
}
|
217
|
-
const getBlob = (id) => {
|
218
|
-
return new Promise((resolve, reject) => {
|
219
|
-
const packet = transport.newPacket({}, (ret) => (ret?.error ? reject(ret.error) : resolve(ret)), `_req-${cuid()}`);
|
220
|
-
packet.method("connector.blob.get");
|
221
|
-
packet.args({
|
222
|
-
id,
|
223
|
-
});
|
224
|
-
transport.send(packet);
|
225
|
-
});
|
226
|
-
};
|
227
|
-
const getBlobContent = (id) => {
|
228
|
-
return new Promise((resolve, reject) => {
|
229
|
-
const packet = transport.newPacket({}, (ret) => ret?.error ? reject(ret.error) : resolve(ret?.content), `_req-${cuid()}`);
|
230
|
-
packet.method("connector.blob.get-content");
|
231
|
-
packet.args({
|
232
|
-
id,
|
233
|
-
});
|
234
|
-
transport.send(packet);
|
235
|
-
});
|
236
|
-
};
|
237
|
-
const createBlob = (args = {}) => {
|
238
|
-
return new Promise((resolve, reject) => {
|
239
|
-
const packet = transport.newPacket({}, (ret) => (ret?.error ? reject(ret.error) : resolve(ret?.id)), `_req-${cuid()}`);
|
240
|
-
packet.method("connector.blob.create");
|
241
|
-
packet.args(args);
|
242
|
-
transport.send(packet);
|
243
|
-
});
|
244
|
-
};
|
245
|
-
let oauthClient;
|
246
|
-
start({
|
247
|
-
config: decrypted,
|
248
|
-
oauth: theOAuth,
|
249
|
-
getBlob,
|
250
|
-
getBlobContent,
|
251
|
-
createBlob,
|
252
|
-
healthCheck: async (controller) => {
|
253
|
-
let result = { ok: true, error: null };
|
254
|
-
try {
|
255
|
-
if (oauthClient) {
|
256
|
-
await oauthClient.__healthCheck();
|
257
|
-
}
|
258
|
-
await controller.__healthCheck();
|
259
|
-
}
|
260
|
-
catch (e) {
|
261
|
-
result.ok = false;
|
262
|
-
result.error = e.message;
|
263
|
-
}
|
264
|
-
const packet = transport.newPacket({});
|
265
|
-
packet.method("connector.health.check");
|
266
|
-
packet.args(result);
|
267
|
-
transport.send(packet);
|
268
|
-
},
|
269
|
-
getClient: (arg) => theOAuth
|
270
|
-
? (oauthClient = theOAuth.getClient(arg))
|
271
|
-
: new Fetcher(arg),
|
272
|
-
newTask: (name, data) => {
|
273
|
-
return new Promise((resolve, reject) => {
|
274
|
-
const packet = transport.newPacket({}, (ret) => (ret?.error ? reject(ret.error) : resolve(ret)), `_req-${cuid()}`);
|
275
|
-
packet.method("connector.task.new");
|
276
|
-
packet.args({
|
277
|
-
name,
|
278
|
-
a: data,
|
279
|
-
});
|
280
|
-
transport.send(packet);
|
281
|
-
});
|
282
|
-
},
|
283
|
-
updateTask: (id, data) => {
|
284
|
-
return new Promise((resolve, reject) => {
|
285
|
-
const packet = transport.newPacket({}, (ret) => (ret?.error ? reject(ret.error) : resolve(ret)), `_req-${cuid()}`);
|
286
|
-
packet.method("connector.task.update");
|
287
|
-
packet.args({
|
288
|
-
id,
|
289
|
-
a: data,
|
290
|
-
});
|
291
|
-
transport.send(packet);
|
292
|
-
});
|
293
|
-
},
|
294
|
-
});
|
295
|
-
};
|
296
|
-
};
|
297
|
-
};
|