@erxes/plugin-sdk 0.1.0-v2.3 → 0.1.0-v2.5
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/dist/client.d.ts +18 -0
- package/dist/client.js +54 -22
- package/dist/verify.js +7 -1
- package/package.json +2 -2
package/dist/client.d.ts
CHANGED
|
@@ -101,6 +101,9 @@ export declare const createCoreClient: (options: CoreClientOptions) => {
|
|
|
101
101
|
CROSS_SITE: {
|
|
102
102
|
status: number;
|
|
103
103
|
};
|
|
104
|
+
DUPLICATE_PERMISSION_SET: {
|
|
105
|
+
status: number;
|
|
106
|
+
};
|
|
104
107
|
INVALID_CLIENT: {
|
|
105
108
|
status: number;
|
|
106
109
|
};
|
|
@@ -116,6 +119,12 @@ export declare const createCoreClient: (options: CoreClientOptions) => {
|
|
|
116
119
|
PLUGIN_NOT_FOUND: {
|
|
117
120
|
status: number;
|
|
118
121
|
};
|
|
122
|
+
UNKNOWN_PERMISSION: {
|
|
123
|
+
status: number;
|
|
124
|
+
data: z.ZodObject<{
|
|
125
|
+
permissions: z.ZodArray<z.ZodString>;
|
|
126
|
+
}, z.core.$strip>;
|
|
127
|
+
};
|
|
119
128
|
}>>>;
|
|
120
129
|
/** Tells the plugin's open UIs that topics changed (needs `live:write`). */
|
|
121
130
|
signal: (orgId: string, topics: string[], userIds?: string[]) => import("@orpc/client").ClientPromiseResult<{
|
|
@@ -201,6 +210,9 @@ export declare const createCoreClient: (options: CoreClientOptions) => {
|
|
|
201
210
|
CROSS_SITE: {
|
|
202
211
|
status: number;
|
|
203
212
|
};
|
|
213
|
+
DUPLICATE_PERMISSION_SET: {
|
|
214
|
+
status: number;
|
|
215
|
+
};
|
|
204
216
|
INVALID_CLIENT: {
|
|
205
217
|
status: number;
|
|
206
218
|
};
|
|
@@ -216,5 +228,11 @@ export declare const createCoreClient: (options: CoreClientOptions) => {
|
|
|
216
228
|
PLUGIN_NOT_FOUND: {
|
|
217
229
|
status: number;
|
|
218
230
|
};
|
|
231
|
+
UNKNOWN_PERMISSION: {
|
|
232
|
+
status: number;
|
|
233
|
+
data: z.ZodObject<{
|
|
234
|
+
permissions: z.ZodArray<z.ZodString>;
|
|
235
|
+
}, z.core.$strip>;
|
|
236
|
+
};
|
|
219
237
|
}>>>;
|
|
220
238
|
};
|
package/dist/client.js
CHANGED
|
@@ -20,21 +20,35 @@ const TokenAnswer = z.object({
|
|
|
20
20
|
code: z.string().optional(),
|
|
21
21
|
message: z.string().optional()
|
|
22
22
|
});
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
/** A cached token is replaced this long before it ends, so no call carries an expiring one. */
|
|
24
|
+
const RENEW_BEFORE_MS = 6e4;
|
|
25
|
+
/** The pause before the one retry of a token request that failed on core's side. */
|
|
26
|
+
const TOKEN_RETRY_MS = 500;
|
|
27
|
+
/** The plugin's core tokens, one per organisation (and one for the deployment), kept fresh. */
|
|
28
|
+
const pluginTokens = (options, fetcher, base) => {
|
|
26
29
|
const tokens = /* @__PURE__ */ new Map();
|
|
30
|
+
const askOnce = (org) => fetcher(`${base}${CORE_API_PREFIX}/plugins/token`, {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: { "content-type": "application/json" },
|
|
33
|
+
body: JSON.stringify({
|
|
34
|
+
grant_type: "client_credentials",
|
|
35
|
+
client_id: options.plugin,
|
|
36
|
+
client_secret: options.clientSecret,
|
|
37
|
+
org
|
|
38
|
+
})
|
|
39
|
+
});
|
|
40
|
+
/** Core restarting or a network blip gets one more try; the second failure is the answer. */
|
|
41
|
+
const ask = async (org) => {
|
|
42
|
+
try {
|
|
43
|
+
const response = await askOnce(org);
|
|
44
|
+
if (response.status < 500) return response;
|
|
45
|
+
await response.body?.cancel();
|
|
46
|
+
} catch {}
|
|
47
|
+
await new Promise((resolve) => setTimeout(resolve, TOKEN_RETRY_MS));
|
|
48
|
+
return askOnce(org);
|
|
49
|
+
};
|
|
27
50
|
const requestToken = async (org) => {
|
|
28
|
-
const response = await
|
|
29
|
-
method: "POST",
|
|
30
|
-
headers: { "content-type": "application/json" },
|
|
31
|
-
body: JSON.stringify({
|
|
32
|
-
grant_type: "client_credentials",
|
|
33
|
-
client_id: options.plugin,
|
|
34
|
-
client_secret: options.clientSecret,
|
|
35
|
-
org
|
|
36
|
-
})
|
|
37
|
-
});
|
|
51
|
+
const response = await ask(org);
|
|
38
52
|
const body = TokenAnswer.safeParse(await response.json().catch(() => null)).data ?? {};
|
|
39
53
|
if (!response.ok || !body.access_token) throw new CoreTokenError(response.status, body.code ?? "TOKEN_FAILED", body.message ?? "Could not get a core token");
|
|
40
54
|
return {
|
|
@@ -42,29 +56,47 @@ const createCoreClient = (options) => {
|
|
|
42
56
|
expiresAt: Date.now() + (body.expires_in ?? 900) * 1e3
|
|
43
57
|
};
|
|
44
58
|
};
|
|
45
|
-
/**
|
|
46
|
-
|
|
59
|
+
/**
|
|
60
|
+
* The organisation's (or the deployment's) token, renewed a minute before it
|
|
61
|
+
* ends. `refused` is a token core just turned down: it is replaced once, for
|
|
62
|
+
* every call that failed with it at the same time.
|
|
63
|
+
*/
|
|
64
|
+
return async (org, refused) => {
|
|
47
65
|
const key = org ?? "";
|
|
48
66
|
const cached = tokens.get(key);
|
|
49
|
-
if (cached
|
|
67
|
+
if (cached) {
|
|
50
68
|
const value = await cached.catch(() => null);
|
|
51
|
-
if (value && value.expiresAt - Date.now() >
|
|
69
|
+
if (value && value.token !== refused && value.expiresAt - Date.now() > RENEW_BEFORE_MS) return value.token;
|
|
70
|
+
const renewing = tokens.get(key);
|
|
71
|
+
if (renewing && renewing !== cached) return (await renewing).token;
|
|
52
72
|
}
|
|
53
73
|
const request = requestToken(org);
|
|
54
74
|
tokens.set(key, request);
|
|
55
|
-
request.catch(() =>
|
|
75
|
+
request.catch(() => {
|
|
76
|
+
if (tokens.get(key) === request) tokens.delete(key);
|
|
77
|
+
});
|
|
56
78
|
return (await request).token;
|
|
57
79
|
};
|
|
80
|
+
};
|
|
81
|
+
const createCoreClient = (options) => {
|
|
82
|
+
const fetcher = options.fetch ?? globalThis.fetch;
|
|
83
|
+
const base = options.coreUrl.replace(/\/+$/, "");
|
|
84
|
+
const tokenFor = pluginTokens(options, fetcher, base);
|
|
58
85
|
const clientFor = (org) => createORPCClient(new OpenAPILink(coreRoutes, {
|
|
59
86
|
url: `${base}${CORE_API_PREFIX}`,
|
|
60
87
|
fetch: async (request, init) => {
|
|
61
|
-
const
|
|
88
|
+
const body = request.body ? await request.arrayBuffer() : void 0;
|
|
89
|
+
const send = (token) => {
|
|
62
90
|
const headers = new Headers(request.headers);
|
|
63
91
|
headers.set("authorization", `Bearer ${token}`);
|
|
64
|
-
return fetcher(new Request(request,
|
|
92
|
+
return fetcher(new Request(request, body ? {
|
|
93
|
+
headers,
|
|
94
|
+
body
|
|
95
|
+
} : { headers }), init);
|
|
65
96
|
};
|
|
66
|
-
const
|
|
67
|
-
|
|
97
|
+
const token = await tokenFor(org);
|
|
98
|
+
const response = await send(token);
|
|
99
|
+
return response.status === 401 ? send(await tokenFor(org, token)) : response;
|
|
68
100
|
}
|
|
69
101
|
}));
|
|
70
102
|
const orgClients = /* @__PURE__ */ new Map();
|
package/dist/verify.js
CHANGED
|
@@ -14,6 +14,11 @@ const DeliveredEvent = z.object({
|
|
|
14
14
|
})
|
|
15
15
|
});
|
|
16
16
|
const jwksByCore = /* @__PURE__ */ new Map();
|
|
17
|
+
/**
|
|
18
|
+
* The plugin's server clock may run a little ahead of core's: a token core
|
|
19
|
+
* just issued, or one a member's browser renews 30 s before it ends, still passes.
|
|
20
|
+
*/
|
|
21
|
+
const CLOCK_TOLERANCE_SECONDS = 60;
|
|
17
22
|
/** A token core signed (its public keys), for this plugin (or `audience`), of this kind, unexpired; its claims parsed. */
|
|
18
23
|
const verifyCoreToken = async (token, options, typ, claims, audience = options.plugin) => {
|
|
19
24
|
const base = options.coreUrl.replace(/\/+$/, "");
|
|
@@ -27,7 +32,8 @@ const verifyCoreToken = async (token, options, typ, claims, audience = options.p
|
|
|
27
32
|
issuer: "erxes-core",
|
|
28
33
|
audience,
|
|
29
34
|
typ,
|
|
30
|
-
algorithms: ["ES256"]
|
|
35
|
+
algorithms: ["ES256"],
|
|
36
|
+
clockTolerance: CLOCK_TOLERANCE_SECONDS
|
|
31
37
|
});
|
|
32
38
|
const parsed = claims.safeParse(payload);
|
|
33
39
|
if (!parsed.success) throw new Error(`not an erxes ${typ} token`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@erxes/plugin-sdk",
|
|
3
|
-
"version": "0.1.0-v2.
|
|
3
|
+
"version": "0.1.0-v2.5",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "For erxes plugin servers: a typed core client with automatic plugin tokens, and verification of members' plugin session tokens",
|
|
6
6
|
"license": "AGPL-3.0-only",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@orpc/openapi-client": "1.16.2",
|
|
32
32
|
"jose": "^6.2.17",
|
|
33
33
|
"zod": "^4.6.7",
|
|
34
|
-
"@erxes/core-contract": "0.1.0-v2.
|
|
34
|
+
"@erxes/core-contract": "0.1.0-v2.6"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "tsdown"
|