@aloma.io/integration-sdk 3.8.0 → 3.8.2
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/builder/index.d.mts +54 -4
- package/build/builder/index.mjs +1 -1
- package/build/internal/connector/config.d.mts +1 -1
- package/build/internal/connector/config.mjs +2 -2
- package/build/internal/connector/index.mjs +7 -1
- package/build/internal/connector/server/index.d.mts +1 -1
- package/build/internal/connector/server/index.mjs +2 -2
- package/build/internal/connector/server/on-connect/finish-oauth.d.mts +1 -1
- package/build/internal/connector/server/on-connect/finish-oauth.mjs +1 -1
- package/build/internal/connector/server/on-connect/index.d.mts +1 -1
- package/build/internal/connector/server/on-connect/index.mjs +14 -7
- package/build/internal/connector/server/on-connect/make-oauth.d.mts +1 -1
- package/build/internal/connector/server/on-connect/make-oauth.mjs +1 -1
- package/build/internal/index.d.mts +1 -1
- package/build/internal/index.mjs +1 -1
- package/package.json +1 -1
- package/src/builder/index.mts +51 -4
- package/src/internal/connector/config.mts +16 -6
- package/src/internal/connector/index.mts +9 -3
- package/src/internal/connector/metrics.mts +2 -3
- package/src/internal/connector/server/index.mts +16 -4
- package/src/internal/connector/server/on-connect/decrypt-config.mts +2 -3
- package/src/internal/connector/server/on-connect/finish-oauth.mts +19 -8
- package/src/internal/connector/server/on-connect/index.mts +28 -12
- package/src/internal/connector/server/on-connect/make-oauth.mts +32 -25
- package/src/internal/connector/server/on-connect/start-oauth.mts +3 -5
- package/src/internal/connector/server/on-message.mts +11 -9
- package/src/internal/dispatcher/index.mts +7 -3
- package/src/internal/index.mts +1 -1
@@ -1,12 +1,62 @@
|
|
1
|
-
import RuntimeContext from "./runtime-context.mjs";
|
2
1
|
import "dotenv/config";
|
2
|
+
import RuntimeContext from "./runtime-context.mjs";
|
3
3
|
export declare const TARGET_DIR: string;
|
4
|
+
export type ConfigField = {
|
5
|
+
name: string;
|
6
|
+
description?: string;
|
7
|
+
placeholder?: string;
|
8
|
+
type: "multiline" | "text" | "number" | "boolean";
|
9
|
+
optional?: boolean;
|
10
|
+
plain?: boolean;
|
11
|
+
} | undefined;
|
12
|
+
declare type Config = {
|
13
|
+
summary?: string;
|
14
|
+
description?: string;
|
15
|
+
fields?: {
|
16
|
+
authorizationURL?: ConfigField;
|
17
|
+
tokenURL?: ConfigField;
|
18
|
+
scope?: ConfigField;
|
19
|
+
clientId?: ConfigField;
|
20
|
+
clientSecret?: ConfigField;
|
21
|
+
[key: string]: ConfigField;
|
22
|
+
};
|
23
|
+
};
|
24
|
+
declare type Options = {
|
25
|
+
endpoint?: {
|
26
|
+
enabled: boolean;
|
27
|
+
required?: boolean;
|
28
|
+
};
|
29
|
+
};
|
30
|
+
declare type OAuth = {
|
31
|
+
/**
|
32
|
+
* preferred via process.env.OAUTH_CLIENT_ID
|
33
|
+
*/
|
34
|
+
clientId?: string;
|
35
|
+
/**
|
36
|
+
* preferred via process.env.OAUTH_CLIENT_SECRET
|
37
|
+
*/
|
38
|
+
clientSecret?: string;
|
39
|
+
/**
|
40
|
+
* @example https://example.com/oauth2/v2/auth?client_id={{clientId}}&redirect_uri={{redirectURI}}&scope={{scope}}&response_type=code
|
41
|
+
*/
|
42
|
+
authorizationURL?: string;
|
43
|
+
tokenURL?: string;
|
44
|
+
scope?: string;
|
45
|
+
tokenRefreshPeriod?: number;
|
46
|
+
useAuthHeader?: boolean;
|
47
|
+
additionalTokenArgs?: {
|
48
|
+
grant_type?: string;
|
49
|
+
};
|
50
|
+
};
|
4
51
|
export declare class Builder {
|
5
52
|
private data;
|
6
|
-
config(arg:
|
7
|
-
options(arg:
|
8
|
-
auth(arg:
|
53
|
+
config(arg: Config): Builder;
|
54
|
+
options(arg: Options): Builder;
|
55
|
+
auth(arg: {
|
56
|
+
oauth?: OAuth;
|
57
|
+
}): Builder;
|
9
58
|
build(): Promise<RuntimeContext>;
|
10
59
|
private checkIcon;
|
11
60
|
private loadDescriptor;
|
12
61
|
}
|
62
|
+
export {};
|
package/build/builder/index.mjs
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
+
import "dotenv/config";
|
1
2
|
import fs from "node:fs";
|
2
3
|
import path from "node:path";
|
3
4
|
import { fileURLToPath } from "node:url";
|
4
5
|
import { notEmpty } from "../internal/util/index.mjs";
|
5
6
|
import RuntimeContext from "./runtime-context.mjs";
|
6
|
-
import "dotenv/config";
|
7
7
|
const DIR_OFFSET = "/../../../../../";
|
8
8
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
9
9
|
export const TARGET_DIR = `${__dirname}${DIR_OFFSET}`;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { Config } from "../websocket/config.mjs";
|
2
|
-
export declare const makeConfig: ({ id, version, name, introspect, configSchema, icon }: {
|
2
|
+
export declare const makeConfig: ({ id, version, name, introspect, configSchema, icon, }: {
|
3
3
|
id: any;
|
4
4
|
version: any;
|
5
5
|
name: any;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import JWE from "../util/jwe/index.mjs";
|
2
2
|
import { Config } from "../websocket/config.mjs";
|
3
|
-
export const makeConfig = async ({ id, version, name, introspect, configSchema, icon }) => {
|
3
|
+
export const makeConfig = async ({ id, version, name, introspect, configSchema, icon, }) => {
|
4
4
|
const config = new Config({
|
5
5
|
id: id,
|
6
6
|
version: version,
|
@@ -43,7 +43,7 @@ ${text}
|
|
43
43
|
resolve(null);
|
44
44
|
}, 2 * 60 * 1000);
|
45
45
|
});
|
46
|
-
throw new Error(
|
46
|
+
throw new Error("could not start");
|
47
47
|
}
|
48
48
|
}
|
49
49
|
return config;
|
@@ -33,7 +33,13 @@ export class Connector {
|
|
33
33
|
name: this.name,
|
34
34
|
version: this.version,
|
35
35
|
});
|
36
|
-
const server = await makeServer({
|
36
|
+
const server = await makeServer({
|
37
|
+
config,
|
38
|
+
configSchema,
|
39
|
+
start,
|
40
|
+
processPacket,
|
41
|
+
dispatcher: this.dispatcher,
|
42
|
+
});
|
37
43
|
await server.start();
|
38
44
|
}
|
39
45
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import Dispatcher from "../../dispatcher/index.mjs";
|
2
2
|
import { Config } from "../../websocket/config.mjs";
|
3
3
|
import { WebsocketConnector } from "../../websocket/index.mjs";
|
4
|
-
export declare const makeServer: ({ config, configSchema, start, processPacket, dispatcher }: {
|
4
|
+
export declare const makeServer: ({ config, configSchema, start, processPacket, dispatcher, }: {
|
5
5
|
config: Config;
|
6
6
|
configSchema: any;
|
7
7
|
start: any;
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import { WebsocketConnector } from "../../websocket/index.mjs";
|
2
2
|
import { onConnect } from "./on-connect/index.mjs";
|
3
3
|
import { onMessage } from "./on-message.mjs";
|
4
|
-
export const makeServer = async ({ config, configSchema, start, processPacket, dispatcher }) => {
|
4
|
+
export const makeServer = async ({ config, configSchema, start, processPacket, dispatcher, }) => {
|
5
5
|
const server = new WebsocketConnector({
|
6
6
|
config,
|
7
7
|
onConnect: onConnect({ config, configSchema, dispatcher, start }),
|
8
|
-
onMessage: onMessage(processPacket)
|
8
|
+
onMessage: onMessage(processPacket),
|
9
9
|
});
|
10
10
|
const term = async () => {
|
11
11
|
await server.leaving();
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import Dispatcher from "../../../dispatcher/index.mjs";
|
2
2
|
import { Config } from "../../../websocket/config.mjs";
|
3
3
|
import { WebsocketConnector } from "../../../websocket/index.mjs";
|
4
|
-
export declare const patchFinishOAuth: ({ dispatcher, decrypted, config, transport }: {
|
4
|
+
export declare const patchFinishOAuth: ({ dispatcher, decrypted, config, transport, }: {
|
5
5
|
dispatcher: Dispatcher;
|
6
6
|
decrypted: any;
|
7
7
|
config: Config;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
export const patchFinishOAuth = async ({ dispatcher, decrypted, config, transport }) => {
|
1
|
+
export const patchFinishOAuth = async ({ dispatcher, decrypted, config, transport, }) => {
|
2
2
|
dispatcher.finishOAuth = async function (arg) {
|
3
3
|
const tokenURL = process.env.OAUTH_TOKEN_URL ||
|
4
4
|
decrypted.tokenURL ||
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import Dispatcher from "../../../dispatcher/index.mjs";
|
2
2
|
import { Config } from "../../../websocket/config.mjs";
|
3
|
-
export declare const onConnect: ({ dispatcher, configSchema, config, start }: {
|
3
|
+
export declare const onConnect: ({ dispatcher, configSchema, config, start, }: {
|
4
4
|
config: Config;
|
5
5
|
configSchema: any;
|
6
6
|
start: any;
|
@@ -5,13 +5,22 @@ import { patchFinishOAuth } from "./finish-oauth.mjs";
|
|
5
5
|
import { makeOAuth } from "./make-oauth.mjs";
|
6
6
|
import { patchStartOAuth } from "./start-oauth.mjs";
|
7
7
|
const cuid = init({ length: 32 });
|
8
|
-
export const onConnect = ({ dispatcher, configSchema, config, start }) => {
|
8
|
+
export const onConnect = ({ dispatcher, configSchema, config, start, }) => {
|
9
9
|
return async (transport) => {
|
10
10
|
dispatcher.onConfig = async function (secrets) {
|
11
|
-
const decrypted = await decryptConfig({
|
11
|
+
const decrypted = await decryptConfig({
|
12
|
+
configSchema,
|
13
|
+
secrets,
|
14
|
+
config,
|
15
|
+
});
|
12
16
|
await patchStartOAuth({ dispatcher, decrypted });
|
13
17
|
await patchFinishOAuth({ dispatcher, decrypted, config, transport });
|
14
|
-
const theOAuth = await makeOAuth({
|
18
|
+
const theOAuth = await makeOAuth({
|
19
|
+
config,
|
20
|
+
transport,
|
21
|
+
decrypted,
|
22
|
+
dispatcher,
|
23
|
+
});
|
15
24
|
const getBlob = (id) => {
|
16
25
|
return new Promise((resolve, reject) => {
|
17
26
|
const packet = transport.newPacket({}, (ret) => (ret?.error ? reject(ret.error) : resolve(ret)), `_req-${cuid()}`);
|
@@ -24,7 +33,7 @@ export const onConnect = ({ dispatcher, configSchema, config, start }) => {
|
|
24
33
|
};
|
25
34
|
const getBlobContent = (id) => {
|
26
35
|
return new Promise((resolve, reject) => {
|
27
|
-
const packet = transport.newPacket({}, (ret) => ret?.error ? reject(ret.error) : resolve(ret?.content), `_req-${cuid()}`);
|
36
|
+
const packet = transport.newPacket({}, (ret) => (ret?.error ? reject(ret.error) : resolve(ret?.content)), `_req-${cuid()}`);
|
28
37
|
packet.method("connector.blob.get-content");
|
29
38
|
packet.args({
|
30
39
|
id,
|
@@ -64,9 +73,7 @@ export const onConnect = ({ dispatcher, configSchema, config, start }) => {
|
|
64
73
|
packet.args(result);
|
65
74
|
transport.send(packet);
|
66
75
|
},
|
67
|
-
getClient: (arg) => theOAuth
|
68
|
-
? (oauthClient = theOAuth.getClient(arg))
|
69
|
-
: new Fetcher(arg),
|
76
|
+
getClient: (arg) => theOAuth ? (oauthClient = theOAuth.getClient(arg)) : new Fetcher(arg),
|
70
77
|
newTask: (name, data) => {
|
71
78
|
return new Promise((resolve, reject) => {
|
72
79
|
const packet = transport.newPacket({}, (ret) => (ret?.error ? reject(ret.error) : resolve(ret)), `_req-${cuid()}`);
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import Dispatcher from "../../../dispatcher/index.mjs";
|
2
2
|
import { OAuth } from "../../../fetcher/oauth-fetcher.mjs";
|
3
3
|
import { Config } from "../../../websocket/config.mjs";
|
4
|
-
export declare const makeOAuth: ({ config, transport, decrypted, dispatcher }: {
|
4
|
+
export declare const makeOAuth: ({ config, transport, decrypted, dispatcher, }: {
|
5
5
|
config: Config;
|
6
6
|
transport: any;
|
7
7
|
decrypted: any;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { OAuth } from "../../../fetcher/oauth-fetcher.mjs";
|
2
|
-
export const makeOAuth = async ({ config, transport, decrypted, dispatcher }) => {
|
2
|
+
export const makeOAuth = async ({ config, transport, decrypted, dispatcher, }) => {
|
3
3
|
const saveOAuthResult = async (what) => {
|
4
4
|
const jwe = await config.validateKeys("RSA-OAEP-256");
|
5
5
|
const value = await jwe.encrypt(what, "none", config.id());
|
@@ -1,2 +1,2 @@
|
|
1
|
-
import { Connector } from
|
1
|
+
import { Connector } from "./connector/index.mjs";
|
2
2
|
export { Connector };
|
package/build/internal/index.mjs
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import { Connector } from
|
1
|
+
import { Connector } from "./connector/index.mjs";
|
2
2
|
export { Connector };
|
package/package.json
CHANGED
package/src/builder/index.mts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
+
import "dotenv/config";
|
1
2
|
import fs from "node:fs";
|
2
3
|
import path from "node:path";
|
3
4
|
import { fileURLToPath } from "node:url";
|
4
5
|
import { notEmpty } from "../internal/util/index.mjs";
|
5
6
|
import RuntimeContext from "./runtime-context.mjs";
|
6
|
-
import "dotenv/config";
|
7
7
|
|
8
8
|
const DIR_OFFSET = "/../../../../../";
|
9
9
|
|
@@ -11,24 +11,71 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
11
11
|
|
12
12
|
export const TARGET_DIR = `${__dirname}${DIR_OFFSET}`;
|
13
13
|
|
14
|
+
export type ConfigField = {
|
15
|
+
name: string;
|
16
|
+
description?: string;
|
17
|
+
placeholder?: string;
|
18
|
+
type: "multiline" | "text" | "number" | "boolean";
|
19
|
+
optional?: boolean;
|
20
|
+
plain?: boolean;
|
21
|
+
} | undefined;
|
22
|
+
|
23
|
+
declare type Config = {
|
24
|
+
summary?: string;
|
25
|
+
description?: string;
|
26
|
+
fields?: {
|
27
|
+
authorizationURL?: ConfigField;
|
28
|
+
tokenURL?: ConfigField;
|
29
|
+
scope?: ConfigField;
|
30
|
+
clientId?: ConfigField;
|
31
|
+
clientSecret?: ConfigField;
|
32
|
+
[key: string]: ConfigField
|
33
|
+
};
|
34
|
+
};
|
35
|
+
|
36
|
+
declare type Options = {
|
37
|
+
endpoint?: {enabled: boolean, required?: boolean};
|
38
|
+
};
|
39
|
+
|
40
|
+
declare type OAuth = {
|
41
|
+
/**
|
42
|
+
* preferred via process.env.OAUTH_CLIENT_ID
|
43
|
+
*/
|
44
|
+
clientId?: string;
|
45
|
+
/**
|
46
|
+
* preferred via process.env.OAUTH_CLIENT_SECRET
|
47
|
+
*/
|
48
|
+
clientSecret?: string;
|
49
|
+
|
50
|
+
/**
|
51
|
+
* @example https://example.com/oauth2/v2/auth?client_id={{clientId}}&redirect_uri={{redirectURI}}&scope={{scope}}&response_type=code
|
52
|
+
*/
|
53
|
+
authorizationURL?: string;
|
54
|
+
tokenURL?: string;
|
55
|
+
scope?: string;
|
56
|
+
tokenRefreshPeriod?: number;
|
57
|
+
useAuthHeader?: boolean;
|
58
|
+
additionalTokenArgs?: {grant_type?: string}
|
59
|
+
};
|
60
|
+
|
14
61
|
export class Builder {
|
15
62
|
private data: any = {
|
16
63
|
controller: "./build/.controller.json",
|
17
64
|
};
|
18
65
|
|
19
|
-
config(arg:
|
66
|
+
config(arg: Config): Builder {
|
20
67
|
this.data.config = arg;
|
21
68
|
|
22
69
|
return this;
|
23
70
|
}
|
24
71
|
|
25
|
-
options(arg:
|
72
|
+
options(arg: Options): Builder {
|
26
73
|
this.data.options = arg;
|
27
74
|
|
28
75
|
return this;
|
29
76
|
}
|
30
77
|
|
31
|
-
auth(arg:
|
78
|
+
auth(arg: {oauth?: OAuth}): Builder {
|
32
79
|
this.data.auth = arg;
|
33
80
|
return this;
|
34
81
|
}
|
@@ -1,7 +1,14 @@
|
|
1
1
|
import JWE from "../util/jwe/index.mjs";
|
2
2
|
import { Config } from "../websocket/config.mjs";
|
3
3
|
|
4
|
-
export const makeConfig = async ({
|
4
|
+
export const makeConfig = async ({
|
5
|
+
id,
|
6
|
+
version,
|
7
|
+
name,
|
8
|
+
introspect,
|
9
|
+
configSchema,
|
10
|
+
icon,
|
11
|
+
}): Promise<Config> => {
|
5
12
|
const config = new Config({
|
6
13
|
id: id,
|
7
14
|
version: version,
|
@@ -44,14 +51,17 @@ ${text}
|
|
44
51
|
`);
|
45
52
|
|
46
53
|
await new Promise((resolve) => {
|
47
|
-
setTimeout(
|
48
|
-
|
49
|
-
|
54
|
+
setTimeout(
|
55
|
+
() => {
|
56
|
+
resolve(null);
|
57
|
+
},
|
58
|
+
2 * 60 * 1000,
|
59
|
+
);
|
50
60
|
});
|
51
61
|
|
52
|
-
throw new Error(
|
62
|
+
throw new Error("could not start");
|
53
63
|
}
|
54
64
|
}
|
55
65
|
|
56
66
|
return config;
|
57
|
-
}
|
67
|
+
};
|
@@ -23,7 +23,8 @@ export class Connector {
|
|
23
23
|
async run() {
|
24
24
|
console.log(`Running ${this.name}`);
|
25
25
|
|
26
|
-
const { processPacket, start, introspect, configSchema } =
|
26
|
+
const { processPacket, start, introspect, configSchema } =
|
27
|
+
this.dispatcher!.build();
|
27
28
|
|
28
29
|
const config = await makeConfig({
|
29
30
|
id: this.id,
|
@@ -40,9 +41,14 @@ export class Connector {
|
|
40
41
|
version: this.version,
|
41
42
|
});
|
42
43
|
|
43
|
-
const server = await makeServer({
|
44
|
+
const server = await makeServer({
|
45
|
+
config,
|
46
|
+
configSchema,
|
47
|
+
start,
|
48
|
+
processPacket,
|
49
|
+
dispatcher: this.dispatcher!,
|
50
|
+
});
|
44
51
|
|
45
52
|
await server.start();
|
46
53
|
}
|
47
54
|
}
|
48
|
-
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import express from "express";
|
2
2
|
import PromClient from "prom-client";
|
3
3
|
|
4
|
-
export const makeMetrics = async ({id, name, version}): Promise<void> => {
|
4
|
+
export const makeMetrics = async ({ id, name, version }): Promise<void> => {
|
5
5
|
const newMetrics = () => {
|
6
6
|
const metrics = PromClient;
|
7
7
|
|
@@ -31,5 +31,4 @@ export const makeMetrics = async ({id, name, version}): Promise<void> => {
|
|
31
31
|
};
|
32
32
|
|
33
33
|
makeMetricsServer(newMetrics()).listen(4050, "0.0.0.0");
|
34
|
-
|
35
|
-
}
|
34
|
+
};
|
@@ -4,11 +4,23 @@ import { WebsocketConnector } from "../../websocket/index.mjs";
|
|
4
4
|
import { onConnect } from "./on-connect/index.mjs";
|
5
5
|
import { onMessage } from "./on-message.mjs";
|
6
6
|
|
7
|
-
export const makeServer = async ({
|
7
|
+
export const makeServer = async ({
|
8
|
+
config,
|
9
|
+
configSchema,
|
10
|
+
start,
|
11
|
+
processPacket,
|
12
|
+
dispatcher,
|
13
|
+
}: {
|
14
|
+
config: Config;
|
15
|
+
configSchema: any;
|
16
|
+
start: any;
|
17
|
+
processPacket: any;
|
18
|
+
dispatcher: Dispatcher;
|
19
|
+
}): Promise<WebsocketConnector> => {
|
8
20
|
const server = new WebsocketConnector({
|
9
21
|
config,
|
10
|
-
onConnect: onConnect({config, configSchema, dispatcher, start}),
|
11
|
-
onMessage: onMessage(processPacket)
|
22
|
+
onConnect: onConnect({ config, configSchema, dispatcher, start }),
|
23
|
+
onMessage: onMessage(processPacket),
|
12
24
|
});
|
13
25
|
|
14
26
|
const term = async () => {
|
@@ -36,4 +48,4 @@ export const makeServer = async ({config, configSchema, start, processPacket, di
|
|
36
48
|
process.on("SIGINT", term);
|
37
49
|
|
38
50
|
return server;
|
39
|
-
}
|
51
|
+
};
|
@@ -1,5 +1,4 @@
|
|
1
|
-
|
2
|
-
export const decryptConfig = async ({configSchema, config, secrets}) => {
|
1
|
+
export const decryptConfig = async ({ configSchema, config, secrets }) => {
|
3
2
|
const decrypted: any = {};
|
4
3
|
|
5
4
|
const fields = configSchema().fields;
|
@@ -24,4 +23,4 @@ export const decryptConfig = async ({configSchema, config, secrets}) => {
|
|
24
23
|
}
|
25
24
|
|
26
25
|
return decrypted;
|
27
|
-
}
|
26
|
+
};
|
@@ -2,8 +2,22 @@ import Dispatcher from "../../../dispatcher/index.mjs";
|
|
2
2
|
import { Config } from "../../../websocket/config.mjs";
|
3
3
|
import { WebsocketConnector } from "../../../websocket/index.mjs";
|
4
4
|
|
5
|
-
export const patchFinishOAuth = async ({
|
6
|
-
dispatcher
|
5
|
+
export const patchFinishOAuth = async ({
|
6
|
+
dispatcher,
|
7
|
+
decrypted,
|
8
|
+
config,
|
9
|
+
transport,
|
10
|
+
}: {
|
11
|
+
dispatcher: Dispatcher;
|
12
|
+
decrypted: any;
|
13
|
+
config: Config;
|
14
|
+
transport: WebsocketConnector;
|
15
|
+
}) => {
|
16
|
+
dispatcher.finishOAuth = async function (arg: {
|
17
|
+
code: string;
|
18
|
+
redirectURI: string;
|
19
|
+
codeVerifier?: string;
|
20
|
+
}): Promise<{ value: string }> {
|
7
21
|
const tokenURL =
|
8
22
|
process.env.OAUTH_TOKEN_URL ||
|
9
23
|
decrypted.tokenURL ||
|
@@ -48,8 +62,7 @@ export const patchFinishOAuth = async ({dispatcher, decrypted, config, transport
|
|
48
62
|
}
|
49
63
|
|
50
64
|
let headers: any = {
|
51
|
-
"Content-Type":
|
52
|
-
"application/x-www-form-urlencoded;charset=UTF-8",
|
65
|
+
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
53
66
|
Accept: "application/json",
|
54
67
|
};
|
55
68
|
|
@@ -85,9 +98,7 @@ export const patchFinishOAuth = async ({dispatcher, decrypted, config, transport
|
|
85
98
|
} else if (ret.access_token) {
|
86
99
|
return { ...ret };
|
87
100
|
} else {
|
88
|
-
throw new Error(
|
89
|
-
status + " response has no access_token - " + text,
|
90
|
-
);
|
101
|
+
throw new Error(status + " response has no access_token - " + text);
|
91
102
|
}
|
92
103
|
} else {
|
93
104
|
throw new Error(status + " " + text);
|
@@ -108,4 +119,4 @@ export const patchFinishOAuth = async ({dispatcher, decrypted, config, transport
|
|
108
119
|
|
109
120
|
return { value: await jwe.encrypt(data, "none", config.id()) };
|
110
121
|
};
|
111
|
-
}
|
122
|
+
};
|
@@ -9,15 +9,34 @@ import { patchStartOAuth } from "./start-oauth.mjs";
|
|
9
9
|
|
10
10
|
const cuid = init({ length: 32 });
|
11
11
|
|
12
|
-
export const onConnect = ({
|
12
|
+
export const onConnect = ({
|
13
|
+
dispatcher,
|
14
|
+
configSchema,
|
15
|
+
config,
|
16
|
+
start,
|
17
|
+
}: {
|
18
|
+
config: Config;
|
19
|
+
configSchema: any;
|
20
|
+
start: any;
|
21
|
+
dispatcher: Dispatcher;
|
22
|
+
}) => {
|
13
23
|
return async (transport) => {
|
14
24
|
dispatcher.onConfig = async function (secrets) {
|
15
|
-
const decrypted: any = await decryptConfig({
|
25
|
+
const decrypted: any = await decryptConfig({
|
26
|
+
configSchema,
|
27
|
+
secrets,
|
28
|
+
config,
|
29
|
+
});
|
16
30
|
|
17
|
-
await patchStartOAuth({dispatcher, decrypted});
|
18
|
-
await patchFinishOAuth({dispatcher, decrypted, config, transport});
|
31
|
+
await patchStartOAuth({ dispatcher, decrypted });
|
32
|
+
await patchFinishOAuth({ dispatcher, decrypted, config, transport });
|
19
33
|
|
20
|
-
const theOAuth = await makeOAuth({
|
34
|
+
const theOAuth = await makeOAuth({
|
35
|
+
config,
|
36
|
+
transport,
|
37
|
+
decrypted,
|
38
|
+
dispatcher,
|
39
|
+
});
|
21
40
|
|
22
41
|
const getBlob = (id) => {
|
23
42
|
return new Promise((resolve, reject) => {
|
@@ -40,8 +59,7 @@ export const onConnect = ({dispatcher, configSchema, config, start}: {config: C
|
|
40
59
|
return new Promise((resolve, reject) => {
|
41
60
|
const packet = transport.newPacket(
|
42
61
|
{},
|
43
|
-
(ret) =>
|
44
|
-
ret?.error ? reject(ret.error) : resolve(ret?.content),
|
62
|
+
(ret) => (ret?.error ? reject(ret.error) : resolve(ret?.content)),
|
45
63
|
`_req-${cuid()}`,
|
46
64
|
);
|
47
65
|
|
@@ -99,9 +117,7 @@ export const onConnect = ({dispatcher, configSchema, config, start}: {config: C
|
|
99
117
|
transport.send(packet);
|
100
118
|
},
|
101
119
|
getClient: (arg) =>
|
102
|
-
theOAuth
|
103
|
-
? (oauthClient = theOAuth.getClient(arg))
|
104
|
-
: new Fetcher(arg),
|
120
|
+
theOAuth ? (oauthClient = theOAuth.getClient(arg)) : new Fetcher(arg),
|
105
121
|
newTask: (name, data) => {
|
106
122
|
return new Promise((resolve, reject) => {
|
107
123
|
const packet = transport.newPacket(
|
@@ -138,5 +154,5 @@ export const onConnect = ({dispatcher, configSchema, config, start}: {config: C
|
|
138
154
|
},
|
139
155
|
});
|
140
156
|
};
|
141
|
-
}
|
142
|
-
};
|
157
|
+
};
|
158
|
+
};
|
@@ -2,7 +2,17 @@ import Dispatcher from "../../../dispatcher/index.mjs";
|
|
2
2
|
import { OAuth } from "../../../fetcher/oauth-fetcher.mjs";
|
3
3
|
import { Config } from "../../../websocket/config.mjs";
|
4
4
|
|
5
|
-
export const makeOAuth = async ({
|
5
|
+
export const makeOAuth = async ({
|
6
|
+
config,
|
7
|
+
transport,
|
8
|
+
decrypted,
|
9
|
+
dispatcher,
|
10
|
+
}: {
|
11
|
+
config: Config;
|
12
|
+
transport: any;
|
13
|
+
decrypted: any;
|
14
|
+
dispatcher: Dispatcher;
|
15
|
+
}) => {
|
6
16
|
const saveOAuthResult = async (what) => {
|
7
17
|
const jwe = await config.validateKeys("RSA-OAEP-256");
|
8
18
|
const value = await jwe.encrypt(what, "none", config.id());
|
@@ -55,8 +65,7 @@ export const makeOAuth = async ({config, transport, decrypted, dispatcher}: {con
|
|
55
65
|
client_secret: clientSecret,
|
56
66
|
}),
|
57
67
|
headers: {
|
58
|
-
"Content-Type":
|
59
|
-
"application/x-www-form-urlencoded;charset=UTF-8",
|
68
|
+
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
60
69
|
Accept: "application/json",
|
61
70
|
...headers,
|
62
71
|
},
|
@@ -69,33 +78,31 @@ export const makeOAuth = async ({config, transport, decrypted, dispatcher}: {con
|
|
69
78
|
if (status === 200) {
|
70
79
|
return JSON.parse(text);
|
71
80
|
} else {
|
72
|
-
throw new Error(
|
73
|
-
"could not get refresh token " + status + " " + text,
|
74
|
-
);
|
81
|
+
throw new Error("could not get refresh token " + status + " " + text);
|
75
82
|
}
|
76
83
|
};
|
77
84
|
|
78
|
-
const theOAuth =
|
85
|
+
const theOAuth = dispatcher._oauth
|
79
86
|
? new OAuth(decrypted.oauthResult, saveOAuthResult, getRefreshToken)
|
80
87
|
: null;
|
81
88
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
}
|
89
|
+
if (theOAuth) {
|
90
|
+
clearInterval(dispatcher._refreshOAuthToken);
|
91
|
+
|
92
|
+
if (!(dispatcher._oauth.noPeriodicTokenRefresh === false)) {
|
93
|
+
dispatcher._refreshOAuthToken = setInterval(
|
94
|
+
async () => {
|
95
|
+
try {
|
96
|
+
console.log("refreshing oauth token");
|
97
|
+
await theOAuth.periodicRefresh();
|
98
|
+
} catch (e) {
|
99
|
+
console.log("periodic refresh", e);
|
100
|
+
}
|
101
|
+
},
|
102
|
+
dispatcher._oauth.tokenRefreshPeriod || 4 * 60 * 60 * 15000,
|
103
|
+
);
|
98
104
|
}
|
105
|
+
}
|
99
106
|
|
100
|
-
|
101
|
-
}
|
107
|
+
return theOAuth;
|
108
|
+
};
|
@@ -1,5 +1,4 @@
|
|
1
|
-
|
2
|
-
export const patchStartOAuth = async ({dispatcher, decrypted}) => {
|
1
|
+
export const patchStartOAuth = async ({ dispatcher, decrypted }) => {
|
3
2
|
dispatcher.startOAuth = async function () {
|
4
3
|
if (!dispatcher._oauth) throw new Error("oauth not configured");
|
5
4
|
|
@@ -8,8 +7,7 @@ export const patchStartOAuth = async ({dispatcher, decrypted}) => {
|
|
8
7
|
decrypted.authorizationURL ||
|
9
8
|
dispatcher._oauth.authorizationURL;
|
10
9
|
|
11
|
-
if (!authorizationURL)
|
12
|
-
throw new Error("authorizationURL not configured");
|
10
|
+
if (!authorizationURL) throw new Error("authorizationURL not configured");
|
13
11
|
|
14
12
|
const clientId =
|
15
13
|
decrypted.clientId ||
|
@@ -32,4 +30,4 @@ export const patchStartOAuth = async ({dispatcher, decrypted}) => {
|
|
32
30
|
useCodeChallenge,
|
33
31
|
};
|
34
32
|
};
|
35
|
-
}
|
33
|
+
};
|
@@ -1,11 +1,13 @@
|
|
1
1
|
import { handlePacketError, reply } from "../../util/index.mjs";
|
2
2
|
|
3
|
-
export const onMessage = (processPacket: any) => {
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
}
|
3
|
+
export const onMessage = (processPacket: any) => {
|
4
|
+
return async (packet, transport) => {
|
5
|
+
try {
|
6
|
+
const ret = await processPacket(packet);
|
7
|
+
if (ret) reply(ret, packet, transport);
|
8
|
+
} catch (e) {
|
9
|
+
console.log(e);
|
10
|
+
handlePacketError(packet, e, transport);
|
11
|
+
}
|
12
|
+
};
|
13
|
+
};
|
@@ -131,13 +131,17 @@ ${arg.configurableClientScope}
|
|
131
131
|
}
|
132
132
|
|
133
133
|
async startOAuth(): Promise<{
|
134
|
-
url: string
|
135
|
-
useCodeChallenge: boolean
|
134
|
+
url: string;
|
135
|
+
useCodeChallenge: boolean;
|
136
136
|
}> {
|
137
137
|
throw new Error("oauth not configured");
|
138
138
|
}
|
139
139
|
|
140
|
-
async finishOAuth(arg: {
|
140
|
+
async finishOAuth(arg: {
|
141
|
+
code: string;
|
142
|
+
redirectURI: string;
|
143
|
+
codeVerifier?: string;
|
144
|
+
}): Promise<{ value: string }> {
|
141
145
|
throw new Error("oauth not configured");
|
142
146
|
}
|
143
147
|
|
package/src/internal/index.mts
CHANGED