@erxes/plugin-sdk 0.1.0-v2.0 → 0.1.0-v2.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/dist/client.d.ts +195 -2
- package/dist/client.js +5 -4
- package/dist/index.d.ts +17 -3
- package/dist/index.js +2 -2
- package/dist/verify.d.ts +39 -24
- package/dist/verify.js +40 -26
- package/package.json +3 -3
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { PluginManifest, coreContract } from "@erxes/core-contract";
|
|
2
|
+
import { z } from "zod";
|
|
2
3
|
import { ContractRouterClient } from "@orpc/contract";
|
|
3
4
|
export type CoreClient = ContractRouterClient<typeof coreContract>;
|
|
4
5
|
export interface CoreClientOptions {
|
|
@@ -19,9 +20,201 @@ export declare const createCoreClient: (options: CoreClientOptions) => {
|
|
|
19
20
|
/** Declares the plugin's UI, API, scopes, permissions and events. Call at startup. */
|
|
20
21
|
registerManifest: (manifest: PluginManifest) => import("@orpc/client").ClientPromiseResult<{
|
|
21
22
|
ok: true;
|
|
22
|
-
},
|
|
23
|
+
}, import("@orpc/contract").ErrorFromErrorMap<import("@orpc/contract").MergedErrorMap<Record<never, never>, {
|
|
24
|
+
UNAUTHORIZED: {
|
|
25
|
+
status: number;
|
|
26
|
+
};
|
|
27
|
+
NOT_ALLOWED: {
|
|
28
|
+
status: number;
|
|
29
|
+
data: z.ZodOptional<z.ZodObject<{
|
|
30
|
+
permission: z.ZodString;
|
|
31
|
+
}, z.core.$strip>>;
|
|
32
|
+
};
|
|
33
|
+
PLUGIN_ONLY: {
|
|
34
|
+
status: number;
|
|
35
|
+
};
|
|
36
|
+
MEMBERS_ONLY: {
|
|
37
|
+
status: number;
|
|
38
|
+
};
|
|
39
|
+
ORG_REQUIRED: {
|
|
40
|
+
status: number;
|
|
41
|
+
};
|
|
42
|
+
SCOPE_REQUIRED: {
|
|
43
|
+
status: number;
|
|
44
|
+
};
|
|
45
|
+
SERVICE_TOKEN_REQUIRED: {
|
|
46
|
+
status: number;
|
|
47
|
+
};
|
|
48
|
+
ORGANIZATION_DISABLED: {
|
|
49
|
+
status: number;
|
|
50
|
+
data: z.ZodObject<{
|
|
51
|
+
manageUrl: z.ZodNullable<z.ZodString>;
|
|
52
|
+
}, z.core.$strip>;
|
|
53
|
+
};
|
|
54
|
+
NOT_IN_PLAN: {
|
|
55
|
+
status: number;
|
|
56
|
+
data: z.ZodOptional<z.ZodObject<{
|
|
57
|
+
manageUrl: z.ZodNullable<z.ZodString>;
|
|
58
|
+
}, z.core.$strip>>;
|
|
59
|
+
};
|
|
60
|
+
READ_ONLY: {
|
|
61
|
+
status: number;
|
|
62
|
+
};
|
|
63
|
+
TOO_MANY_REQUESTS: {
|
|
64
|
+
status: number;
|
|
65
|
+
};
|
|
66
|
+
INVALID_INPUT: {
|
|
67
|
+
status: number;
|
|
68
|
+
data: z.ZodObject<{
|
|
69
|
+
issues: z.ZodArray<z.ZodObject<{
|
|
70
|
+
path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodInt]>>;
|
|
71
|
+
message: z.ZodString;
|
|
72
|
+
}, z.core.$strip>>;
|
|
73
|
+
}, z.core.$strip>;
|
|
74
|
+
};
|
|
75
|
+
NOT_FOUND: {
|
|
76
|
+
status: number;
|
|
77
|
+
};
|
|
78
|
+
TAKEN: {
|
|
79
|
+
status: number;
|
|
80
|
+
data: z.ZodObject<{
|
|
81
|
+
field: z.ZodString;
|
|
82
|
+
id: z.ZodOptional<z.ZodString>;
|
|
83
|
+
values: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
84
|
+
}, z.core.$strip>;
|
|
85
|
+
};
|
|
86
|
+
INVALID_CURSOR: {
|
|
87
|
+
status: number;
|
|
88
|
+
};
|
|
89
|
+
INVALID_REFERENCE: {
|
|
90
|
+
status: number;
|
|
91
|
+
data: z.ZodObject<{
|
|
92
|
+
field: z.ZodString;
|
|
93
|
+
}, z.core.$strip>;
|
|
94
|
+
};
|
|
95
|
+
INVALID_FILTER: {
|
|
96
|
+
status: number;
|
|
97
|
+
};
|
|
98
|
+
FILTER_TOO_SLOW: {
|
|
99
|
+
status: number;
|
|
100
|
+
};
|
|
101
|
+
CROSS_SITE: {
|
|
102
|
+
status: number;
|
|
103
|
+
};
|
|
104
|
+
INVALID_CLIENT: {
|
|
105
|
+
status: number;
|
|
106
|
+
};
|
|
107
|
+
LIMIT_EXCEEDED: {
|
|
108
|
+
status: number;
|
|
109
|
+
};
|
|
110
|
+
NAMESPACE_REQUIRED: {
|
|
111
|
+
status: number;
|
|
112
|
+
};
|
|
113
|
+
NOT_INSTALLED: {
|
|
114
|
+
status: number;
|
|
115
|
+
};
|
|
116
|
+
PLUGIN_NOT_FOUND: {
|
|
117
|
+
status: number;
|
|
118
|
+
};
|
|
119
|
+
}>>>;
|
|
23
120
|
/** Tells the plugin's open UIs that topics changed (needs `live:write`). */
|
|
24
121
|
signal: (orgId: string, topics: string[], userIds?: string[]) => import("@orpc/client").ClientPromiseResult<{
|
|
25
122
|
ok: true;
|
|
26
|
-
},
|
|
123
|
+
}, import("@orpc/contract").ErrorFromErrorMap<import("@orpc/contract").MergedErrorMap<Record<never, never>, {
|
|
124
|
+
UNAUTHORIZED: {
|
|
125
|
+
status: number;
|
|
126
|
+
};
|
|
127
|
+
NOT_ALLOWED: {
|
|
128
|
+
status: number;
|
|
129
|
+
data: z.ZodOptional<z.ZodObject<{
|
|
130
|
+
permission: z.ZodString;
|
|
131
|
+
}, z.core.$strip>>;
|
|
132
|
+
};
|
|
133
|
+
PLUGIN_ONLY: {
|
|
134
|
+
status: number;
|
|
135
|
+
};
|
|
136
|
+
MEMBERS_ONLY: {
|
|
137
|
+
status: number;
|
|
138
|
+
};
|
|
139
|
+
ORG_REQUIRED: {
|
|
140
|
+
status: number;
|
|
141
|
+
};
|
|
142
|
+
SCOPE_REQUIRED: {
|
|
143
|
+
status: number;
|
|
144
|
+
};
|
|
145
|
+
SERVICE_TOKEN_REQUIRED: {
|
|
146
|
+
status: number;
|
|
147
|
+
};
|
|
148
|
+
ORGANIZATION_DISABLED: {
|
|
149
|
+
status: number;
|
|
150
|
+
data: z.ZodObject<{
|
|
151
|
+
manageUrl: z.ZodNullable<z.ZodString>;
|
|
152
|
+
}, z.core.$strip>;
|
|
153
|
+
};
|
|
154
|
+
NOT_IN_PLAN: {
|
|
155
|
+
status: number;
|
|
156
|
+
data: z.ZodOptional<z.ZodObject<{
|
|
157
|
+
manageUrl: z.ZodNullable<z.ZodString>;
|
|
158
|
+
}, z.core.$strip>>;
|
|
159
|
+
};
|
|
160
|
+
READ_ONLY: {
|
|
161
|
+
status: number;
|
|
162
|
+
};
|
|
163
|
+
TOO_MANY_REQUESTS: {
|
|
164
|
+
status: number;
|
|
165
|
+
};
|
|
166
|
+
INVALID_INPUT: {
|
|
167
|
+
status: number;
|
|
168
|
+
data: z.ZodObject<{
|
|
169
|
+
issues: z.ZodArray<z.ZodObject<{
|
|
170
|
+
path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodInt]>>;
|
|
171
|
+
message: z.ZodString;
|
|
172
|
+
}, z.core.$strip>>;
|
|
173
|
+
}, z.core.$strip>;
|
|
174
|
+
};
|
|
175
|
+
NOT_FOUND: {
|
|
176
|
+
status: number;
|
|
177
|
+
};
|
|
178
|
+
TAKEN: {
|
|
179
|
+
status: number;
|
|
180
|
+
data: z.ZodObject<{
|
|
181
|
+
field: z.ZodString;
|
|
182
|
+
id: z.ZodOptional<z.ZodString>;
|
|
183
|
+
values: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
184
|
+
}, z.core.$strip>;
|
|
185
|
+
};
|
|
186
|
+
INVALID_CURSOR: {
|
|
187
|
+
status: number;
|
|
188
|
+
};
|
|
189
|
+
INVALID_REFERENCE: {
|
|
190
|
+
status: number;
|
|
191
|
+
data: z.ZodObject<{
|
|
192
|
+
field: z.ZodString;
|
|
193
|
+
}, z.core.$strip>;
|
|
194
|
+
};
|
|
195
|
+
INVALID_FILTER: {
|
|
196
|
+
status: number;
|
|
197
|
+
};
|
|
198
|
+
FILTER_TOO_SLOW: {
|
|
199
|
+
status: number;
|
|
200
|
+
};
|
|
201
|
+
CROSS_SITE: {
|
|
202
|
+
status: number;
|
|
203
|
+
};
|
|
204
|
+
INVALID_CLIENT: {
|
|
205
|
+
status: number;
|
|
206
|
+
};
|
|
207
|
+
LIMIT_EXCEEDED: {
|
|
208
|
+
status: number;
|
|
209
|
+
};
|
|
210
|
+
NAMESPACE_REQUIRED: {
|
|
211
|
+
status: number;
|
|
212
|
+
};
|
|
213
|
+
NOT_INSTALLED: {
|
|
214
|
+
status: number;
|
|
215
|
+
};
|
|
216
|
+
PLUGIN_NOT_FOUND: {
|
|
217
|
+
status: number;
|
|
218
|
+
};
|
|
219
|
+
}>>>;
|
|
27
220
|
};
|
package/dist/client.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { CORE_API_PREFIX
|
|
1
|
+
import { CORE_API_PREFIX } from "@erxes/core-contract";
|
|
2
|
+
import { coreRoutes } from "@erxes/core-contract/routes";
|
|
2
3
|
import { createORPCClient } from "@orpc/client";
|
|
3
4
|
import { OpenAPILink } from "@orpc/openapi-client/fetch";
|
|
4
5
|
import { z } from "zod";
|
|
@@ -18,7 +19,7 @@ const TokenAnswer = z.object({
|
|
|
18
19
|
expires_in: z.number().optional(),
|
|
19
20
|
code: z.string().optional(),
|
|
20
21
|
message: z.string().optional()
|
|
21
|
-
})
|
|
22
|
+
});
|
|
22
23
|
const createCoreClient = (options) => {
|
|
23
24
|
const fetcher = options.fetch ?? globalThis.fetch;
|
|
24
25
|
const base = options.coreUrl.replace(/\/+$/, "");
|
|
@@ -34,7 +35,7 @@ const createCoreClient = (options) => {
|
|
|
34
35
|
org
|
|
35
36
|
})
|
|
36
37
|
});
|
|
37
|
-
const body = TokenAnswer.
|
|
38
|
+
const body = TokenAnswer.safeParse(await response.json().catch(() => null)).data ?? {};
|
|
38
39
|
if (!response.ok || !body.access_token) throw new CoreTokenError(response.status, body.code ?? "TOKEN_FAILED", body.message ?? "Could not get a core token");
|
|
39
40
|
return {
|
|
40
41
|
token: body.access_token,
|
|
@@ -54,7 +55,7 @@ const createCoreClient = (options) => {
|
|
|
54
55
|
request.catch(() => tokens.delete(key));
|
|
55
56
|
return (await request).token;
|
|
56
57
|
};
|
|
57
|
-
const clientFor = (org) => createORPCClient(new OpenAPILink(
|
|
58
|
+
const clientFor = (org) => createORPCClient(new OpenAPILink(coreRoutes, {
|
|
58
59
|
url: `${base}${CORE_API_PREFIX}`,
|
|
59
60
|
fetch: async (request, init) => {
|
|
60
61
|
const send = async (token) => {
|
package/dist/index.d.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* Payments (scopes `payments:write`, `payments:read`; subscribe to `core.payment.*`):
|
|
11
11
|
*
|
|
12
12
|
* const invoice = await core.forOrg(orgId).payments.createInvoice({
|
|
13
|
-
* amount: 150000, description: 'Order #12',
|
|
13
|
+
* amount: 150000, description: 'Order #12', referenceType: 'order', referenceId: order.id,
|
|
14
14
|
* metadata: { orderId: order.id }, dedupeKey: `order:${order.id}`,
|
|
15
15
|
* }); // show invoice.payment.qrImage / links to the payer
|
|
16
16
|
* app.post('/events', express.text({ type: '*\/*' }), async (req, res) => {
|
|
@@ -29,6 +29,20 @@
|
|
|
29
29
|
* // An invoice may list what it pays for; `plugin` names another plugin's line (it gets the events):
|
|
30
30
|
* // lines: [{ productId, quantity: 1, unitPrice: 150000 }, { name: 'Delivery', quantity: 1, unitPrice: 10000, plugin: 'tms' }]
|
|
31
31
|
*
|
|
32
|
+
* Customers (a shop, tracking, booking): core signs them in; the plugin never builds sign-up or
|
|
33
|
+
* sign-in. Declare `customerApp: { origins: ['https://{subdomain}.shop.example.com'] }` in the
|
|
34
|
+
* manifest. The app's own form makes two calls to core: `POST /core/v1/customer-auth/code`
|
|
35
|
+
* `{ clientId, kind, value }` sends a code, `POST /core/v1/customer-auth/sign-in` with the code
|
|
36
|
+
* returns `{ accessToken, refreshToken, expiresIn, customer }`; `/customer-auth/refresh` renews
|
|
37
|
+
* them. Serve customers under `/customer/*`, keyed by the customer id:
|
|
38
|
+
*
|
|
39
|
+
* app.use('/customer', async (req, res, next) => {
|
|
40
|
+
* req.customer = await verifyCustomerToken(req.headers.authorization, { coreUrl })
|
|
41
|
+
* .catch(() => null); // { orgId, customerId, clientId }
|
|
42
|
+
* return req.customer ? next() : res.sendStatus(401);
|
|
43
|
+
* });
|
|
44
|
+
* app.get('/customer/orders', (req) => orders.list(req.customer.orgId, req.customer.customerId));
|
|
45
|
+
*
|
|
32
46
|
* Tiers on SaaS (erxes.io: free / plus / pro; self-hosted: pro, full):
|
|
33
47
|
*
|
|
34
48
|
* const member = await verifyPluginSession(token, { coreUrl, plugin: 'hr' });
|
|
@@ -42,5 +56,5 @@
|
|
|
42
56
|
* `core_live_signal()` inside their own transactions.
|
|
43
57
|
*/
|
|
44
58
|
import { CoreClient, CoreClientOptions, CoreTokenError, createCoreClient } from "./client.js";
|
|
45
|
-
import { CoreCall, DeliveredEvent, ErxesSignIn, PluginMember, bearerOf, verifyCoreCall, verifyCoreEvent, verifyErxesAccessToken, verifyPluginSession } from "./verify.js";
|
|
46
|
-
export { CoreCall, CoreClient, CoreClientOptions, CoreTokenError, DeliveredEvent, ErxesSignIn, PluginMember, bearerOf, createCoreClient, verifyCoreCall, verifyCoreEvent, verifyErxesAccessToken, verifyPluginSession };
|
|
59
|
+
import { CoreCall, CustomerSignIn, DeliveredEvent, ErxesSignIn, PluginMember, bearerOf, verifyCoreCall, verifyCoreEvent, verifyCustomerToken, verifyErxesAccessToken, verifyPluginSession } from "./verify.js";
|
|
60
|
+
export { CoreCall, CoreClient, CoreClientOptions, CoreTokenError, CustomerSignIn, DeliveredEvent, ErxesSignIn, PluginMember, bearerOf, createCoreClient, verifyCoreCall, verifyCoreEvent, verifyCustomerToken, verifyErxesAccessToken, verifyPluginSession };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { CoreTokenError, createCoreClient } from "./client.js";
|
|
2
|
-
import { DeliveredEvent, bearerOf, verifyCoreCall, verifyCoreEvent, verifyErxesAccessToken, verifyPluginSession } from "./verify.js";
|
|
3
|
-
export { CoreTokenError, DeliveredEvent, bearerOf, createCoreClient, verifyCoreCall, verifyCoreEvent, verifyErxesAccessToken, verifyPluginSession };
|
|
2
|
+
import { DeliveredEvent, bearerOf, verifyCoreCall, verifyCoreEvent, verifyCustomerToken, verifyErxesAccessToken, verifyPluginSession } from "./verify.js";
|
|
3
|
+
export { CoreTokenError, DeliveredEvent, bearerOf, createCoreClient, verifyCoreCall, verifyCoreEvent, verifyCustomerToken, verifyErxesAccessToken, verifyPluginSession };
|
package/dist/verify.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PluginAccess, PluginTier } from "@erxes/core-contract";
|
|
1
2
|
import { z } from "zod";
|
|
2
3
|
/** The signed-in member a plugin UI calls the plugin's API for. */
|
|
3
4
|
export interface PluginMember {
|
|
@@ -13,17 +14,21 @@ export interface PluginMember {
|
|
|
13
14
|
* The plugin's tier in this organisation on erxes.io (`free`, `plus`,
|
|
14
15
|
* `pro`): gate paid features with it. Self-hosted: `pro`.
|
|
15
16
|
*/
|
|
16
|
-
tier:
|
|
17
|
+
tier: PluginTier | null;
|
|
17
18
|
/**
|
|
18
19
|
* `full`, `warning` (payment retrying / ending at period end: still
|
|
19
20
|
* works), `read_only` (left the plan: let them read and export, refuse
|
|
20
21
|
* changes). Self-hosted: `full`.
|
|
21
22
|
*/
|
|
22
|
-
access:
|
|
23
|
+
access: PluginAccess;
|
|
23
24
|
/** `member.canChange()`: false while read-only. */
|
|
24
25
|
canChange(): boolean;
|
|
25
26
|
}
|
|
26
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* A member signed into a plugin's own app with "Sign in with erxes". Access
|
|
29
|
+
* tokens carry no plan, so `tier` is null and `access` is `off`: ask core
|
|
30
|
+
* (`saas.pluginPlan`) before gating on them.
|
|
31
|
+
*/
|
|
27
32
|
export interface ErxesSignIn extends PluginMember {
|
|
28
33
|
scope: string[];
|
|
29
34
|
}
|
|
@@ -47,32 +52,25 @@ export declare const DeliveredEvent: z.ZodObject<{
|
|
|
47
52
|
source: z.ZodString;
|
|
48
53
|
type: z.ZodString;
|
|
49
54
|
version: z.ZodInt;
|
|
55
|
+
subjectType: z.ZodNullable<z.ZodEnum<{
|
|
56
|
+
company: "company";
|
|
57
|
+
customer: "customer";
|
|
58
|
+
}>>;
|
|
59
|
+
subjectId: z.ZodNullable<z.ZodString>;
|
|
60
|
+
entityType: z.ZodNullable<z.ZodString>;
|
|
61
|
+
entityId: z.ZodNullable<z.ZodString>;
|
|
62
|
+
actorType: z.ZodEnum<{
|
|
63
|
+
customer: "customer";
|
|
64
|
+
plugin: "plugin";
|
|
65
|
+
system: "system";
|
|
66
|
+
user: "user";
|
|
67
|
+
}>;
|
|
68
|
+
actorId: z.ZodNullable<z.ZodString>;
|
|
50
69
|
payload: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
51
70
|
summary: z.ZodNullable<z.ZodString>;
|
|
52
71
|
link: z.ZodNullable<z.ZodString>;
|
|
53
72
|
occurredAt: z.ZodISODateTime;
|
|
54
73
|
createdAt: z.ZodISODateTime;
|
|
55
|
-
subject: z.ZodNullable<z.ZodObject<{
|
|
56
|
-
type: z.ZodEnum<{
|
|
57
|
-
company: "company";
|
|
58
|
-
customer: "customer";
|
|
59
|
-
}>;
|
|
60
|
-
id: z.ZodString;
|
|
61
|
-
}, z.core.$strip>>;
|
|
62
|
-
entity: z.ZodNullable<z.ZodObject<{
|
|
63
|
-
type: z.ZodString;
|
|
64
|
-
id: z.ZodString;
|
|
65
|
-
}, z.core.$strip>>;
|
|
66
|
-
actor: z.ZodObject<{
|
|
67
|
-
type: z.ZodEnum<{
|
|
68
|
-
plugin: "plugin";
|
|
69
|
-
system: "system";
|
|
70
|
-
user: "user";
|
|
71
|
-
}>;
|
|
72
|
-
id: z.ZodNullable<z.ZodString>;
|
|
73
|
-
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
74
|
-
avatarUrl: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
75
|
-
}, z.core.$strip>;
|
|
76
74
|
}, {
|
|
77
75
|
out: {};
|
|
78
76
|
in: {};
|
|
@@ -104,6 +102,23 @@ export declare const verifyCoreCall: (token: string, options: VerifyOptions) =>
|
|
|
104
102
|
* Discovery: `<coreUrl>/core/v1/.well-known/openid-configuration`.
|
|
105
103
|
*/
|
|
106
104
|
export declare const verifyErxesAccessToken: (token: string, options: VerifyOptions) => Promise<ErxesSignIn>;
|
|
105
|
+
/** A customer signed in through core (`/customer-auth`), calling a plugin's `/customer/*` routes. */
|
|
106
|
+
export interface CustomerSignIn {
|
|
107
|
+
orgId: string;
|
|
108
|
+
customerId: string;
|
|
109
|
+
/** The plugin whose app the customer signed in to (any app of the organisation may call). */
|
|
110
|
+
clientId: string;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Verifies a customer's access token from the `Authorization` header with
|
|
114
|
+
* core's public keys: issued by core for customers, unexpired. Key the
|
|
115
|
+
* customer's data by `orgId` and `customerId`. Signing out reaches plugins
|
|
116
|
+
* when the token expires (15 minutes); ask core (`GET /customer/me` with the
|
|
117
|
+
* same token) where that matters. Throws if not valid.
|
|
118
|
+
*/
|
|
119
|
+
export declare const verifyCustomerToken: (authorization: string | undefined | null, options: {
|
|
120
|
+
coreUrl: string;
|
|
121
|
+
}) => Promise<CustomerSignIn>;
|
|
107
122
|
/**
|
|
108
123
|
* Verifies an event delivery and returns the event: the `Authorization`
|
|
109
124
|
* token is core's, for this plugin, unexpired, and signs this exact body
|
package/dist/verify.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CORE_API_PREFIX, CoreEvent } from "@erxes/core-contract";
|
|
1
|
+
import { CORE_API_PREFIX, CoreEvent, PluginAccess, PluginTier } from "@erxes/core-contract";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { createRemoteJWKSet, jwtVerify } from "jose";
|
|
4
4
|
// src/verify.ts
|
|
@@ -8,20 +8,24 @@ const DeliveredEvent = z.object({
|
|
|
8
8
|
type: z.string(),
|
|
9
9
|
organizationId: z.string(),
|
|
10
10
|
createdAt: z.string(),
|
|
11
|
-
data: CoreEvent
|
|
11
|
+
data: CoreEvent.extend({
|
|
12
|
+
occurredAt: z.iso.datetime(),
|
|
13
|
+
createdAt: z.iso.datetime()
|
|
14
|
+
})
|
|
12
15
|
});
|
|
13
16
|
const jwksByCore = /* @__PURE__ */ new Map();
|
|
14
|
-
/** A token core signed (its public keys), for this plugin, of this kind, unexpired; its claims parsed. */
|
|
15
|
-
const verifyCoreToken = async (token, options, typ, claims) => {
|
|
17
|
+
/** A token core signed (its public keys), for this plugin (or `audience`), of this kind, unexpired; its claims parsed. */
|
|
18
|
+
const verifyCoreToken = async (token, options, typ, claims, audience = options.plugin) => {
|
|
16
19
|
const base = options.coreUrl.replace(/\/+$/, "");
|
|
17
20
|
let jwks = jwksByCore.get(base);
|
|
18
21
|
if (!jwks) {
|
|
19
22
|
jwks = createRemoteJWKSet(new URL(`${base}${CORE_API_PREFIX}/.well-known/jwks.json`));
|
|
20
23
|
jwksByCore.set(base, jwks);
|
|
21
24
|
}
|
|
25
|
+
if (!audience) throw new Error("no audience to check the token for");
|
|
22
26
|
const { payload } = await jwtVerify(token, jwks, {
|
|
23
27
|
issuer: "erxes-core",
|
|
24
|
-
audience
|
|
28
|
+
audience,
|
|
25
29
|
typ,
|
|
26
30
|
algorithms: ["ES256"]
|
|
27
31
|
});
|
|
@@ -29,18 +33,7 @@ const verifyCoreToken = async (token, options, typ, claims) => {
|
|
|
29
33
|
if (!parsed.success) throw new Error(`not an erxes ${typ} token`);
|
|
30
34
|
return parsed.data;
|
|
31
35
|
};
|
|
32
|
-
const Tier =
|
|
33
|
-
"free",
|
|
34
|
-
"plus",
|
|
35
|
-
"pro"
|
|
36
|
-
]);
|
|
37
|
-
const Access = z.enum([
|
|
38
|
-
"full",
|
|
39
|
-
"warning",
|
|
40
|
-
"read_only",
|
|
41
|
-
"off"
|
|
42
|
-
]);
|
|
43
|
-
const Strings = z.array(z.unknown()).transform((items) => items.filter((item) => typeof item === "string"));
|
|
36
|
+
const Tier = PluginTier.nullable();
|
|
44
37
|
const member = (claims) => ({
|
|
45
38
|
userId: claims.sub,
|
|
46
39
|
orgId: claims.org,
|
|
@@ -55,9 +48,9 @@ const SessionClaims = z.object({
|
|
|
55
48
|
sub: z.string().min(1),
|
|
56
49
|
org: z.string(),
|
|
57
50
|
role: z.string(),
|
|
58
|
-
permissions:
|
|
59
|
-
tier:
|
|
60
|
-
access:
|
|
51
|
+
permissions: z.array(z.string()),
|
|
52
|
+
tier: Tier,
|
|
53
|
+
access: PluginAccess
|
|
61
54
|
});
|
|
62
55
|
/**
|
|
63
56
|
* Verifies a member's plugin session token (`Authorization: Bearer …` from
|
|
@@ -90,11 +83,11 @@ const verifyCoreCall = (token, options) => verifyCoreToken(token, options, "erxe
|
|
|
90
83
|
const AccessTokenClaims = z.object({
|
|
91
84
|
sub: z.string().min(1),
|
|
92
85
|
org: z.string(),
|
|
93
|
-
role: z.string()
|
|
94
|
-
permissions:
|
|
95
|
-
tier: Tier.
|
|
96
|
-
access:
|
|
97
|
-
scope: z.string().transform((scope) => scope.split(" "))
|
|
86
|
+
role: z.string(),
|
|
87
|
+
permissions: z.array(z.string()),
|
|
88
|
+
tier: Tier.default(null),
|
|
89
|
+
access: PluginAccess.default("off"),
|
|
90
|
+
scope: z.string().transform((scope) => scope.split(" "))
|
|
98
91
|
});
|
|
99
92
|
/**
|
|
100
93
|
* Verifies an access token from "Sign in with erxes" (OAuth 2.1, PKCE) for the
|
|
@@ -108,6 +101,27 @@ const verifyErxesAccessToken = async (token, options) => {
|
|
|
108
101
|
scope: claims.scope
|
|
109
102
|
};
|
|
110
103
|
};
|
|
104
|
+
const CustomerClaims = z.object({
|
|
105
|
+
sub: z.string().min(1),
|
|
106
|
+
org: z.string().min(1),
|
|
107
|
+
client_id: z.string()
|
|
108
|
+
}).transform((claims) => ({
|
|
109
|
+
orgId: claims.org,
|
|
110
|
+
customerId: claims.sub,
|
|
111
|
+
clientId: claims.client_id
|
|
112
|
+
}));
|
|
113
|
+
/**
|
|
114
|
+
* Verifies a customer's access token from the `Authorization` header with
|
|
115
|
+
* core's public keys: issued by core for customers, unexpired. Key the
|
|
116
|
+
* customer's data by `orgId` and `customerId`. Signing out reaches plugins
|
|
117
|
+
* when the token expires (15 minutes); ask core (`GET /customer/me` with the
|
|
118
|
+
* same token) where that matters. Throws if not valid.
|
|
119
|
+
*/
|
|
120
|
+
const verifyCustomerToken = async (authorization, options) => {
|
|
121
|
+
const token = bearerOf(authorization);
|
|
122
|
+
if (!token) throw new Error("no customer token");
|
|
123
|
+
return verifyCoreToken(token, options, "customer+jwt", CustomerClaims, "erxes-customer");
|
|
124
|
+
};
|
|
111
125
|
const sha256 = async (text) => {
|
|
112
126
|
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text)));
|
|
113
127
|
return btoa(String.fromCharCode(...digest)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
@@ -133,4 +147,4 @@ const verifyCoreEvent = async (authorization, rawBody, options) => {
|
|
|
133
147
|
if (event.id !== claims.event || event.organizationId !== claims.org) throw new Error("the event does not match the token");
|
|
134
148
|
return event;
|
|
135
149
|
};
|
|
136
|
-
export { DeliveredEvent, bearerOf, verifyCoreCall, verifyCoreEvent, verifyErxesAccessToken, verifyPluginSession };
|
|
150
|
+
export { DeliveredEvent, bearerOf, verifyCoreCall, verifyCoreEvent, verifyCustomerToken, verifyErxesAccessToken, verifyPluginSession };
|
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.2",
|
|
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",
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
"@orpc/client": "1.16.2",
|
|
30
30
|
"@orpc/contract": "1.16.2",
|
|
31
31
|
"@orpc/openapi-client": "1.16.2",
|
|
32
|
-
"jose": "^
|
|
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.2"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "tsdown"
|