@aloma.io/integration-sdk 3.8.10 → 3.8.12
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/.prettierrc.cjs +8 -0
- package/build/builder/index.d.mts +7 -11
- package/build/builder/index.mjs +14 -15
- package/build/builder/runtime-context.d.mts +1 -1
- package/build/builder/runtime-context.mjs +11 -16
- package/build/cli.mjs +38 -38
- package/build/controller/index.d.mts +58 -2
- package/build/controller/index.mjs +68 -11
- package/build/index.d.mts +2 -2
- package/build/index.mjs +2 -2
- package/build/internal/connector/config.d.mts +2 -2
- package/build/internal/connector/config.mjs +10 -10
- package/build/internal/connector/index.d.mts +2 -2
- package/build/internal/connector/index.mjs +4 -4
- package/build/internal/connector/metrics.mjs +6 -6
- package/build/internal/connector/server/index.d.mts +3 -3
- package/build/internal/connector/server/index.mjs +7 -7
- package/build/internal/connector/server/on-connect/decrypt-config.mjs +3 -3
- package/build/internal/connector/server/on-connect/finish-oauth.d.mts +3 -3
- package/build/internal/connector/server/on-connect/finish-oauth.mjs +17 -23
- package/build/internal/connector/server/on-connect/index.d.mts +2 -2
- package/build/internal/connector/server/on-connect/index.mjs +13 -13
- package/build/internal/connector/server/on-connect/make-oauth.d.mts +3 -3
- package/build/internal/connector/server/on-connect/make-oauth.mjs +17 -25
- package/build/internal/connector/server/on-connect/start-oauth.mjs +6 -13
- package/build/internal/connector/server/on-message.mjs +1 -1
- package/build/internal/dispatcher/index.mjs +34 -45
- package/build/internal/fetcher/fetcher.d.mts +1 -1
- package/build/internal/fetcher/fetcher.mjs +11 -15
- package/build/internal/fetcher/oauth-fetcher.d.mts +1 -1
- package/build/internal/fetcher/oauth-fetcher.mjs +7 -7
- package/build/internal/index.d.mts +1 -1
- package/build/internal/index.mjs +1 -1
- package/build/internal/util/index.mjs +5 -5
- package/build/internal/util/jwe/cli.mjs +3 -3
- package/build/internal/util/jwe/index.d.mts +1 -1
- package/build/internal/util/jwe/index.mjs +10 -10
- package/build/internal/websocket/config.d.mts +1 -1
- package/build/internal/websocket/config.mjs +7 -7
- package/build/internal/websocket/connection/constants.mjs +3 -3
- package/build/internal/websocket/connection/index.mjs +8 -8
- package/build/internal/websocket/connection/registration.mjs +6 -6
- package/build/internal/websocket/index.d.mts +2 -2
- package/build/internal/websocket/index.mjs +3 -3
- package/build/internal/websocket/transport/durable.mjs +6 -6
- package/build/internal/websocket/transport/index.d.mts +2 -2
- package/build/internal/websocket/transport/index.mjs +19 -19
- package/build/internal/websocket/transport/packet.mjs +1 -1
- package/build/internal/websocket/transport/processor.mjs +5 -5
- package/build/transform/index.mjs +26 -26
- package/package.json +1 -1
- package/src/builder/index.mts +66 -66
- package/src/builder/runtime-context.mts +17 -43
- package/src/cli.mts +48 -53
- package/src/controller/index.mts +72 -12
- package/src/index.mts +2 -2
- package/src/internal/connector/config.mts +11 -19
- package/src/internal/connector/index.mts +8 -9
- package/src/internal/connector/metrics.mts +7 -7
- package/src/internal/connector/server/index.mts +10 -10
- package/src/internal/connector/server/on-connect/decrypt-config.mts +4 -4
- package/src/internal/connector/server/on-connect/finish-oauth.mts +22 -35
- package/src/internal/connector/server/on-connect/index.mts +27 -27
- package/src/internal/connector/server/on-connect/make-oauth.mts +22 -33
- package/src/internal/connector/server/on-connect/start-oauth.mts +7 -16
- package/src/internal/connector/server/on-message.mts +1 -1
- package/src/internal/dispatcher/index.mts +49 -70
- package/src/internal/fetcher/fetcher.mts +44 -49
- package/src/internal/fetcher/oauth-fetcher.mts +12 -13
- package/src/internal/index.mts +2 -2
- package/src/internal/util/index.mts +8 -11
- package/src/internal/util/jwe/cli.mts +3 -3
- package/src/internal/util/jwe/index.mts +17 -21
- package/src/internal/websocket/config.mjs +8 -12
- package/src/internal/websocket/connection/constants.mjs +3 -3
- package/src/internal/websocket/connection/index.mjs +14 -14
- package/src/internal/websocket/connection/registration.mjs +10 -10
- package/src/internal/websocket/index.mjs +6 -6
- package/src/internal/websocket/transport/durable.mjs +9 -9
- package/src/internal/websocket/transport/index.mjs +28 -32
- package/src/internal/websocket/transport/packet.mjs +4 -4
- package/src/internal/websocket/transport/processor.mjs +9 -12
- package/src/transform/index.mts +28 -41
package/src/controller/index.mts
CHANGED
@@ -1,31 +1,71 @@
|
|
1
|
-
import Fetcher from
|
1
|
+
import Fetcher from '../internal/fetcher/fetcher.mjs';
|
2
|
+
import { OAuth } from '../internal/fetcher/oauth-fetcher.mjs';
|
2
3
|
|
3
4
|
export abstract class AbstractController {
|
5
|
+
/**
|
6
|
+
* connector configuration
|
7
|
+
*/
|
4
8
|
protected config: {[key: string]: any} = {};
|
9
|
+
|
10
|
+
/**
|
11
|
+
* @deprecated
|
12
|
+
*/
|
5
13
|
protected client: any;
|
6
14
|
|
15
|
+
/**
|
16
|
+
* oauth data, if the connector is using oauth
|
17
|
+
*/
|
18
|
+
protected oauth?: OAuth;
|
19
|
+
|
20
|
+
/**
|
21
|
+
* invoked, when the controller is started
|
22
|
+
*/
|
7
23
|
protected async start(): Promise<void> {}
|
8
24
|
|
25
|
+
/**
|
26
|
+
* invoked, when the controller is stopped
|
27
|
+
* @param isShutdown true if the controller is stopped due to shutdown
|
28
|
+
*/
|
9
29
|
protected async stop(isShutdown: boolean = false): Promise<void> {}
|
10
30
|
|
11
31
|
protected configQuery(arg: any): Promise<any> {
|
12
32
|
return Promise.resolve({});
|
13
33
|
}
|
14
34
|
|
35
|
+
/**
|
36
|
+
* autocomplete configuration options
|
37
|
+
*
|
38
|
+
* e.g. if this connector is connected to a system which has multiple entities with dynamic attributes, like deals, ...
|
39
|
+
* then this method should return the possible attributes of the entity when invoked like
|
40
|
+
* ```javascript
|
41
|
+
* autocomplete({entity: 'deal'})
|
42
|
+
* ```
|
43
|
+
* @param arg
|
44
|
+
* @returns
|
45
|
+
*/
|
15
46
|
protected autocomplete(arg: any): Promise<any> {
|
16
47
|
return Promise.resolve({});
|
17
48
|
}
|
18
49
|
|
50
|
+
/**
|
51
|
+
* called, when the remote public method is not found on the controller
|
52
|
+
* @param arg
|
53
|
+
*/
|
19
54
|
protected fallback(arg: any): Promise<any> {
|
20
|
-
throw new Error(
|
55
|
+
throw new Error('method not found');
|
21
56
|
}
|
22
57
|
|
23
58
|
protected async endpoint(arg: any): Promise<any> {
|
24
|
-
throw new Error(
|
59
|
+
throw new Error('method not found');
|
25
60
|
}
|
26
61
|
|
62
|
+
/**
|
63
|
+
* create a new task
|
64
|
+
* @param name name of the task
|
65
|
+
* @param data data of the task
|
66
|
+
*/
|
27
67
|
protected async newTask(name: string, data: any): Promise<string> {
|
28
|
-
throw new Error(
|
68
|
+
throw new Error('not implemented');
|
29
69
|
}
|
30
70
|
|
31
71
|
protected getClient({
|
@@ -37,11 +77,17 @@ export abstract class AbstractController {
|
|
37
77
|
onResponse?: (response: any) => void;
|
38
78
|
customize?: (request: any) => void;
|
39
79
|
}): Fetcher {
|
40
|
-
throw new Error(
|
80
|
+
throw new Error('not implemented');
|
41
81
|
}
|
42
82
|
|
83
|
+
/**
|
84
|
+
* update a task
|
85
|
+
* @param name name of the task
|
86
|
+
* @param data partial data of the task to update
|
87
|
+
* @returns taskId
|
88
|
+
*/
|
43
89
|
protected async updateTask(name: string, data: any): Promise<string> {
|
44
|
-
throw new Error(
|
90
|
+
throw new Error('not implemented');
|
45
91
|
}
|
46
92
|
|
47
93
|
protected async createBlob({
|
@@ -59,9 +105,13 @@ export abstract class AbstractController {
|
|
59
105
|
meta?: any;
|
60
106
|
taskId?: string;
|
61
107
|
}): Promise<string> {
|
62
|
-
throw new Error(
|
108
|
+
throw new Error('not implemented');
|
63
109
|
}
|
64
110
|
|
111
|
+
/**
|
112
|
+
* get the metadata of a blob by id
|
113
|
+
* @param id blob id
|
114
|
+
*/
|
65
115
|
protected async getBlob(id: string): Promise<{
|
66
116
|
name?: string;
|
67
117
|
id;
|
@@ -69,13 +119,21 @@ export abstract class AbstractController {
|
|
69
119
|
mimetype?: string;
|
70
120
|
meta?: any;
|
71
121
|
}> {
|
72
|
-
throw new Error(
|
122
|
+
throw new Error('not implemented');
|
73
123
|
}
|
74
124
|
|
125
|
+
/**
|
126
|
+
* get the content of a blob by id as base64
|
127
|
+
* @param id blob id
|
128
|
+
*/
|
75
129
|
protected async getBlobContent(id: string): Promise<string> {
|
76
|
-
throw new Error(
|
130
|
+
throw new Error('not implemented');
|
77
131
|
}
|
78
132
|
|
133
|
+
/**
|
134
|
+
* health check, will be called periodically
|
135
|
+
* throw an error if unhealthy with a message describing what needs to be fixed
|
136
|
+
*/
|
79
137
|
protected async healthCheck(): Promise<any> {
|
80
138
|
// blank, throw an error if unhealthy
|
81
139
|
}
|
@@ -102,16 +160,18 @@ export abstract class AbstractController {
|
|
102
160
|
|
103
161
|
async _doStart(
|
104
162
|
config: any,
|
105
|
-
|
163
|
+
oauth: any,
|
106
164
|
newTask: any,
|
107
165
|
updateTask: any,
|
108
166
|
getClient: any,
|
109
167
|
getBlob: any,
|
110
168
|
getBlobContent: any,
|
111
|
-
createBlob: any
|
169
|
+
createBlob: any
|
112
170
|
): Promise<void> {
|
171
|
+
console.dir(config, {depth: null});
|
113
172
|
this.config = config;
|
114
|
-
this.client =
|
173
|
+
this.client = oauth;
|
174
|
+
this.oauth = oauth;
|
115
175
|
this.newTask = newTask;
|
116
176
|
this.updateTask = updateTask;
|
117
177
|
this.getClient = getClient;
|
package/src/index.mts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
export * from
|
2
|
-
export * from
|
1
|
+
export * from './builder/index.mjs';
|
2
|
+
export * from './controller/index.mjs';
|
@@ -1,22 +1,14 @@
|
|
1
|
-
import JWE from
|
2
|
-
import {
|
1
|
+
import JWE from '../util/jwe/index.mjs';
|
2
|
+
import {Config} from '../websocket/config.mjs';
|
3
3
|
|
4
|
-
export const makeConfig = async ({
|
5
|
-
id,
|
6
|
-
version,
|
7
|
-
name,
|
8
|
-
introspect,
|
9
|
-
configSchema,
|
10
|
-
icon,
|
11
|
-
}): Promise<Config> => {
|
4
|
+
export const makeConfig = async ({id, version, name, introspect, configSchema, icon}): Promise<Config> => {
|
12
5
|
const config = new Config({
|
13
6
|
id: id,
|
14
7
|
version: version,
|
15
8
|
name: process.env.HOSTNAME || name,
|
16
9
|
registrationToken: process.env.REGISTRATION_TOKEN,
|
17
|
-
endpoint: process.env.DEVICE_ENDPOINT ||
|
18
|
-
wsEndpoint:
|
19
|
-
process.env.WEBSOCKET_ENDPOINT || "wss://connect.aloma.io/transport/",
|
10
|
+
endpoint: process.env.DEVICE_ENDPOINT || 'https://connect.aloma.io/',
|
11
|
+
wsEndpoint: process.env.WEBSOCKET_ENDPOINT || 'wss://connect.aloma.io/transport/',
|
20
12
|
privateKey: process.env.PRIVATE_KEY,
|
21
13
|
publicKey: process.env.PUBLIC_KEY,
|
22
14
|
introspect,
|
@@ -30,16 +22,16 @@ export const makeConfig = async ({
|
|
30
22
|
} catch (e) {
|
31
23
|
const haveKey = !!process.env.PRIVATE_KEY;
|
32
24
|
const jwe = new JWE({});
|
33
|
-
var text =
|
25
|
+
var text = 'Please double check the env variables';
|
34
26
|
|
35
27
|
if (!haveKey) {
|
36
28
|
await jwe.newPair();
|
37
29
|
text =
|
38
|
-
|
30
|
+
'fresh keys generated, set environment variables: \n\nPRIVATE_KEY: ' +
|
39
31
|
(await jwe.exportPrivateAsBase64()) +
|
40
|
-
|
32
|
+
'\n\nPUBLIC_KEY: ' +
|
41
33
|
(await jwe.exportPublicAsBase64()) +
|
42
|
-
|
34
|
+
'\n';
|
43
35
|
}
|
44
36
|
|
45
37
|
console.log(`
|
@@ -55,11 +47,11 @@ ${text}
|
|
55
47
|
() => {
|
56
48
|
resolve(null);
|
57
49
|
},
|
58
|
-
2 * 60 * 1000
|
50
|
+
2 * 60 * 1000
|
59
51
|
);
|
60
52
|
});
|
61
53
|
|
62
|
-
throw new Error(
|
54
|
+
throw new Error('could not start');
|
63
55
|
}
|
64
56
|
}
|
65
57
|
|
@@ -1,15 +1,15 @@
|
|
1
|
-
import { Dispatcher } from
|
2
|
-
import { makeConfig } from
|
3
|
-
import { makeMetrics } from
|
4
|
-
import { makeServer } from
|
1
|
+
import { Dispatcher } from '../dispatcher/index.mjs';
|
2
|
+
import { makeConfig } from './config.mjs';
|
3
|
+
import { makeMetrics } from './metrics.mjs';
|
4
|
+
import { makeServer } from './server/index.mjs';
|
5
5
|
|
6
6
|
export class Connector {
|
7
7
|
id: any;
|
8
8
|
version: any;
|
9
9
|
name: any;
|
10
10
|
icon: any;
|
11
|
-
dispatcher
|
12
|
-
constructor({
|
11
|
+
dispatcher!: Dispatcher;
|
12
|
+
constructor({version, id, name, icon}) {
|
13
13
|
this.id = id;
|
14
14
|
this.version = version;
|
15
15
|
this.name = name;
|
@@ -23,8 +23,7 @@ export class Connector {
|
|
23
23
|
async run() {
|
24
24
|
console.log(`Running ${this.name}`);
|
25
25
|
|
26
|
-
const {
|
27
|
-
this.dispatcher!.build();
|
26
|
+
const {processPacket, start, introspect, configSchema} = this.dispatcher.build();
|
28
27
|
|
29
28
|
const config = await makeConfig({
|
30
29
|
id: this.id,
|
@@ -46,7 +45,7 @@ export class Connector {
|
|
46
45
|
configSchema,
|
47
46
|
start,
|
48
47
|
processPacket,
|
49
|
-
dispatcher: this.dispatcher
|
48
|
+
dispatcher: this.dispatcher,
|
50
49
|
});
|
51
50
|
|
52
51
|
await server.start();
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import express from
|
2
|
-
import PromClient from
|
1
|
+
import express from 'express';
|
2
|
+
import PromClient from 'prom-client';
|
3
3
|
|
4
|
-
export const makeMetrics = async ({
|
4
|
+
export const makeMetrics = async ({id, name, version}): Promise<void> => {
|
5
5
|
const newMetrics = () => {
|
6
6
|
const metrics = PromClient;
|
7
7
|
|
@@ -9,7 +9,7 @@ export const makeMetrics = async ({ id, name, version }): Promise<void> => {
|
|
9
9
|
service: name,
|
10
10
|
connectorId: id,
|
11
11
|
connectorVersion: version,
|
12
|
-
node: process.env.HOSTNAME ||
|
12
|
+
node: process.env.HOSTNAME || 'test',
|
13
13
|
};
|
14
14
|
metrics.register.setDefaultLabels(defaultLabels);
|
15
15
|
metrics.collectDefaultMetrics();
|
@@ -20,9 +20,9 @@ export const makeMetrics = async ({ id, name, version }): Promise<void> => {
|
|
20
20
|
const makeMetricsServer = (metrics) => {
|
21
21
|
const app = express();
|
22
22
|
|
23
|
-
app.get(
|
23
|
+
app.get('/metrics', async (request, response, next) => {
|
24
24
|
response.status(200);
|
25
|
-
response.set(
|
25
|
+
response.set('Content-type', metrics.contentType);
|
26
26
|
response.send(await metrics.register.metrics());
|
27
27
|
response.end();
|
28
28
|
});
|
@@ -30,5 +30,5 @@ export const makeMetrics = async ({ id, name, version }): Promise<void> => {
|
|
30
30
|
return app;
|
31
31
|
};
|
32
32
|
|
33
|
-
makeMetricsServer(newMetrics()).listen(4050,
|
33
|
+
makeMetricsServer(newMetrics()).listen(4050, '0.0.0.0');
|
34
34
|
};
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import Dispatcher from
|
2
|
-
import {
|
3
|
-
import {
|
4
|
-
import {
|
5
|
-
import {
|
1
|
+
import Dispatcher from '../../dispatcher/index.mjs';
|
2
|
+
import {Config} from '../../websocket/config.mjs';
|
3
|
+
import {WebsocketConnector} from '../../websocket/index.mjs';
|
4
|
+
import {onConnect} from './on-connect/index.mjs';
|
5
|
+
import {onMessage} from './on-message.mjs';
|
6
6
|
|
7
7
|
export const makeServer = async ({
|
8
8
|
config,
|
@@ -19,7 +19,7 @@ export const makeServer = async ({
|
|
19
19
|
}): Promise<WebsocketConnector> => {
|
20
20
|
const server = new WebsocketConnector({
|
21
21
|
config,
|
22
|
-
onConnect: onConnect({
|
22
|
+
onConnect: onConnect({config, configSchema, dispatcher, start}),
|
23
23
|
onMessage: onMessage(processPacket),
|
24
24
|
});
|
25
25
|
|
@@ -36,16 +36,16 @@ export const makeServer = async ({
|
|
36
36
|
process.exit(0);
|
37
37
|
};
|
38
38
|
|
39
|
-
process.on(
|
39
|
+
process.on('uncaughtException', (e) => {
|
40
40
|
console.log(e);
|
41
41
|
});
|
42
42
|
|
43
|
-
process.on(
|
43
|
+
process.on('unhandledRejection', (e) => {
|
44
44
|
console.log(e);
|
45
45
|
});
|
46
46
|
|
47
|
-
process.on(
|
48
|
-
process.on(
|
47
|
+
process.on('SIGTERM', term);
|
48
|
+
process.on('SIGINT', term);
|
49
49
|
|
50
50
|
return server;
|
51
51
|
};
|
@@ -1,23 +1,23 @@
|
|
1
|
-
export const decryptConfig = async ({
|
1
|
+
export const decryptConfig = async ({configSchema, config, secrets}) => {
|
2
2
|
const decrypted: any = {};
|
3
3
|
|
4
4
|
const fields = configSchema().fields;
|
5
5
|
|
6
6
|
const keys = Object.keys(secrets);
|
7
|
-
const jwe = await config.validateKeys(
|
7
|
+
const jwe = await config.validateKeys('RSA-OAEP-256');
|
8
8
|
|
9
9
|
for (var i = 0; i < keys.length; ++i) {
|
10
10
|
const key = keys[i];
|
11
11
|
const value = secrets[key];
|
12
12
|
if (!value) continue;
|
13
13
|
|
14
|
-
if (fields[key]?.plain || [
|
14
|
+
if (fields[key]?.plain || ['endpointUrl'].includes(key)) {
|
15
15
|
decrypted[key] = value;
|
16
16
|
} else {
|
17
17
|
try {
|
18
18
|
decrypted[key] = await jwe.decrypt(value, config.id());
|
19
19
|
} catch (e) {
|
20
|
-
console.log(
|
20
|
+
console.log('failed to decrypt key', key, config.id(), e);
|
21
21
|
}
|
22
22
|
}
|
23
23
|
}
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import Dispatcher from
|
2
|
-
import {
|
3
|
-
import {
|
1
|
+
import Dispatcher from '../../../dispatcher/index.mjs';
|
2
|
+
import {Config} from '../../../websocket/config.mjs';
|
3
|
+
import {WebsocketConnector} from '../../../websocket/index.mjs';
|
4
4
|
|
5
5
|
export const patchFinishOAuth = async ({
|
6
6
|
dispatcher,
|
@@ -17,41 +17,30 @@ export const patchFinishOAuth = async ({
|
|
17
17
|
code: string;
|
18
18
|
redirectURI: string;
|
19
19
|
codeVerifier?: string;
|
20
|
-
}): Promise<{
|
21
|
-
const tokenURL =
|
22
|
-
process.env.OAUTH_TOKEN_URL ||
|
23
|
-
decrypted.tokenURL ||
|
24
|
-
dispatcher._oauth.tokenURL;
|
20
|
+
}): Promise<{value: string}> {
|
21
|
+
const tokenURL = process.env.OAUTH_TOKEN_URL || decrypted.tokenURL || dispatcher._oauth.tokenURL;
|
25
22
|
|
26
|
-
if (!dispatcher._oauth) throw new Error(
|
27
|
-
if (!tokenURL && !dispatcher._oauth.finishOAuth)
|
28
|
-
throw new Error("need tokenURL or finishOAuth(arg)");
|
23
|
+
if (!dispatcher._oauth) throw new Error('oauth not configured');
|
24
|
+
if (!tokenURL && !dispatcher._oauth.finishOAuth) throw new Error('need tokenURL or finishOAuth(arg)');
|
29
25
|
|
30
26
|
var data = null;
|
31
27
|
|
32
28
|
const doFinish = async () => {
|
33
|
-
if (!arg.code || !arg.redirectURI)
|
34
|
-
throw new Error("need code and redirectUri");
|
29
|
+
if (!arg.code || !arg.redirectURI) throw new Error('need code and redirectUri');
|
35
30
|
|
36
|
-
const clientId =
|
37
|
-
decrypted.clientId ||
|
38
|
-
process.env.OAUTH_CLIENT_ID ||
|
39
|
-
dispatcher._oauth.clientId;
|
31
|
+
const clientId = decrypted.clientId || process.env.OAUTH_CLIENT_ID || dispatcher._oauth.clientId;
|
40
32
|
|
41
|
-
if (!clientId) throw new Error(
|
33
|
+
if (!clientId) throw new Error('clientId not configured');
|
42
34
|
|
43
|
-
const clientSecret =
|
44
|
-
|
45
|
-
process.env.OAUTH_CLIENT_SECRET ||
|
46
|
-
dispatcher._oauth.clientSecret;
|
47
|
-
if (!clientSecret) throw new Error("clientSecret not configured");
|
35
|
+
const clientSecret = decrypted.clientSecret || process.env.OAUTH_CLIENT_SECRET || dispatcher._oauth.clientSecret;
|
36
|
+
if (!clientSecret) throw new Error('clientSecret not configured');
|
48
37
|
|
49
38
|
const additionalTokenArgs = dispatcher._oauth.additionalTokenArgs || {};
|
50
39
|
const useAuthHeader = !!dispatcher._oauth.useAuthHeader;
|
51
40
|
const useCodeChallenge = !!dispatcher._oauth.useCodeChallenge;
|
52
41
|
|
53
42
|
let body = {
|
54
|
-
grant_type:
|
43
|
+
grant_type: 'authorization_code',
|
55
44
|
...additionalTokenArgs,
|
56
45
|
code: arg.code,
|
57
46
|
redirect_uri: arg.redirectURI,
|
@@ -62,8 +51,8 @@ export const patchFinishOAuth = async ({
|
|
62
51
|
}
|
63
52
|
|
64
53
|
let headers: any = {
|
65
|
-
|
66
|
-
Accept:
|
54
|
+
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
|
55
|
+
Accept: 'application/json',
|
67
56
|
};
|
68
57
|
|
69
58
|
if (useAuthHeader) {
|
@@ -80,7 +69,7 @@ export const patchFinishOAuth = async ({
|
|
80
69
|
}
|
81
70
|
|
82
71
|
const response = await fetch(tokenURL, {
|
83
|
-
method:
|
72
|
+
method: 'POST',
|
84
73
|
body: new URLSearchParams(body),
|
85
74
|
headers,
|
86
75
|
signal: AbortSignal.timeout(60 * 1000),
|
@@ -92,16 +81,14 @@ export const patchFinishOAuth = async ({
|
|
92
81
|
if (status === 200) {
|
93
82
|
const ret = JSON.parse(text);
|
94
83
|
if (ret.error) {
|
95
|
-
throw new Error(
|
96
|
-
`${status} ${ret.error} ${ret.error_description || ""}`,
|
97
|
-
);
|
84
|
+
throw new Error(`${status} ${ret.error} ${ret.error_description || ''}`);
|
98
85
|
} else if (ret.access_token) {
|
99
|
-
return {
|
86
|
+
return {...ret};
|
100
87
|
} else {
|
101
|
-
throw new Error(status +
|
88
|
+
throw new Error(status + ' response has no access_token - ' + text);
|
102
89
|
}
|
103
90
|
} else {
|
104
|
-
throw new Error(status +
|
91
|
+
throw new Error(status + ' ' + text);
|
105
92
|
}
|
106
93
|
};
|
107
94
|
|
@@ -115,8 +102,8 @@ export const patchFinishOAuth = async ({
|
|
115
102
|
data = await doFinish();
|
116
103
|
}
|
117
104
|
|
118
|
-
const jwe = await config.validateKeys(
|
105
|
+
const jwe = await config.validateKeys('RSA-OAEP-256');
|
119
106
|
|
120
|
-
return {
|
107
|
+
return {value: await jwe.encrypt(data, 'none', config.id())};
|
121
108
|
};
|
122
109
|
};
|
@@ -1,13 +1,14 @@
|
|
1
|
-
import { init } from
|
2
|
-
import
|
3
|
-
import
|
4
|
-
import
|
5
|
-
import {
|
6
|
-
import {
|
7
|
-
import {
|
8
|
-
import {
|
9
|
-
|
10
|
-
|
1
|
+
import { init } from '@paralleldrive/cuid2';
|
2
|
+
import { AbstractController } from '../../../../index.mjs';
|
3
|
+
import Dispatcher from '../../../dispatcher/index.mjs';
|
4
|
+
import Fetcher from '../../../fetcher/fetcher.mjs';
|
5
|
+
import { Config } from '../../../websocket/config.mjs';
|
6
|
+
import { decryptConfig } from './decrypt-config.mjs';
|
7
|
+
import { patchFinishOAuth } from './finish-oauth.mjs';
|
8
|
+
import { makeOAuth } from './make-oauth.mjs';
|
9
|
+
import { patchStartOAuth } from './start-oauth.mjs';
|
10
|
+
|
11
|
+
const cuid = init({length: 32});
|
11
12
|
|
12
13
|
export const onConnect = ({
|
13
14
|
dispatcher,
|
@@ -28,8 +29,8 @@ export const onConnect = ({
|
|
28
29
|
config,
|
29
30
|
});
|
30
31
|
|
31
|
-
await patchStartOAuth({
|
32
|
-
await patchFinishOAuth({
|
32
|
+
await patchStartOAuth({dispatcher, decrypted});
|
33
|
+
await patchFinishOAuth({dispatcher, decrypted, config, transport});
|
33
34
|
|
34
35
|
const theOAuth = await makeOAuth({
|
35
36
|
config,
|
@@ -43,10 +44,10 @@ export const onConnect = ({
|
|
43
44
|
const packet = transport.newPacket(
|
44
45
|
{},
|
45
46
|
(ret) => (ret?.error ? reject(ret.error) : resolve(ret)),
|
46
|
-
`_req-${cuid()}
|
47
|
+
`_req-${cuid()}`
|
47
48
|
);
|
48
49
|
|
49
|
-
packet.method(
|
50
|
+
packet.method('connector.blob.get');
|
50
51
|
packet.args({
|
51
52
|
id,
|
52
53
|
});
|
@@ -60,10 +61,10 @@ export const onConnect = ({
|
|
60
61
|
const packet = transport.newPacket(
|
61
62
|
{},
|
62
63
|
(ret) => (ret?.error ? reject(ret.error) : resolve(ret?.content)),
|
63
|
-
`_req-${cuid()}
|
64
|
+
`_req-${cuid()}`
|
64
65
|
);
|
65
66
|
|
66
|
-
packet.method(
|
67
|
+
packet.method('connector.blob.get-content');
|
67
68
|
packet.args({
|
68
69
|
id,
|
69
70
|
});
|
@@ -77,10 +78,10 @@ export const onConnect = ({
|
|
77
78
|
const packet = transport.newPacket(
|
78
79
|
{},
|
79
80
|
(ret) => (ret?.error ? reject(ret.error) : resolve(ret?.id)),
|
80
|
-
`_req-${cuid()}
|
81
|
+
`_req-${cuid()}`
|
81
82
|
);
|
82
83
|
|
83
|
-
packet.method(
|
84
|
+
packet.method('connector.blob.create');
|
84
85
|
packet.args(args);
|
85
86
|
|
86
87
|
transport.send(packet);
|
@@ -95,8 +96,8 @@ export const onConnect = ({
|
|
95
96
|
getBlob,
|
96
97
|
getBlobContent,
|
97
98
|
createBlob,
|
98
|
-
healthCheck: async (controller) => {
|
99
|
-
let result: any = {
|
99
|
+
healthCheck: async (controller: AbstractController) => {
|
100
|
+
let result: any = {ok: true, error: null};
|
100
101
|
|
101
102
|
try {
|
102
103
|
if (oauthClient) {
|
@@ -111,22 +112,21 @@ export const onConnect = ({
|
|
111
112
|
|
112
113
|
const packet = transport.newPacket({});
|
113
114
|
|
114
|
-
packet.method(
|
115
|
+
packet.method('connector.health.check');
|
115
116
|
packet.args(result);
|
116
117
|
|
117
118
|
transport.send(packet);
|
118
119
|
},
|
119
|
-
getClient: (arg) =>
|
120
|
-
theOAuth ? (oauthClient = theOAuth.getClient(arg)) : new Fetcher(arg),
|
120
|
+
getClient: (arg) => (theOAuth ? (oauthClient = theOAuth.getClient(arg)) : new Fetcher(arg)),
|
121
121
|
newTask: (name, data) => {
|
122
122
|
return new Promise((resolve, reject) => {
|
123
123
|
const packet = transport.newPacket(
|
124
124
|
{},
|
125
125
|
(ret) => (ret?.error ? reject(ret.error) : resolve(ret)),
|
126
|
-
`_req-${cuid()}
|
126
|
+
`_req-${cuid()}`
|
127
127
|
);
|
128
128
|
|
129
|
-
packet.method(
|
129
|
+
packet.method('connector.task.new');
|
130
130
|
packet.args({
|
131
131
|
name,
|
132
132
|
a: data,
|
@@ -140,10 +140,10 @@ export const onConnect = ({
|
|
140
140
|
const packet = transport.newPacket(
|
141
141
|
{},
|
142
142
|
(ret) => (ret?.error ? reject(ret.error) : resolve(ret)),
|
143
|
-
`_req-${cuid()}
|
143
|
+
`_req-${cuid()}`
|
144
144
|
);
|
145
145
|
|
146
|
-
packet.method(
|
146
|
+
packet.method('connector.task.update');
|
147
147
|
packet.args({
|
148
148
|
id,
|
149
149
|
a: data,
|