@ptkl/toolkit 1.4.0 → 1.4.1
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.
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
const calls = {};
|
|
3
|
+
const client = {
|
|
4
|
+
get: vi.fn(), post: vi.fn(), put: vi.fn(),
|
|
5
|
+
};
|
|
6
|
+
const appApi = {
|
|
7
|
+
client,
|
|
8
|
+
download: vi.fn(async (ref, version) => { calls.download = [ref, version]; return {}; }),
|
|
9
|
+
updateSettings: vi.fn(async (v, ref) => ({ v, ref })),
|
|
10
|
+
};
|
|
11
|
+
vi.mock('@ptkl/sdk', () => ({ Platform: class {
|
|
12
|
+
app() { return appApi; }
|
|
13
|
+
} }));
|
|
14
|
+
vi.mock('../../lib/util.js', () => ({
|
|
15
|
+
default: {
|
|
16
|
+
getCurrentProfile: () => ({ token: 't', host: 'h', name: 'p' }),
|
|
17
|
+
getClientForProfile: () => ({ app: () => appApi }),
|
|
18
|
+
},
|
|
19
|
+
}));
|
|
20
|
+
vi.mock('vite', () => ({ build: vi.fn(), createServer: vi.fn() }));
|
|
21
|
+
vi.mock('../../playground.js', () => ({ default: class {
|
|
22
|
+
} }));
|
|
23
|
+
import apps from '../apps.js';
|
|
24
|
+
describe('apps', () => {
|
|
25
|
+
beforeEach(() => { vi.clearAllMocks(); });
|
|
26
|
+
it('download passes (ref, version) in SDK order', async () => {
|
|
27
|
+
await apps.download({ apptype: 'app', version: '1.2.3', ref: 'my-app', output: '' });
|
|
28
|
+
expect(appApi.download).toHaveBeenCalledWith('my-app', '1.2.3');
|
|
29
|
+
});
|
|
30
|
+
it('remove-version GETs settings then PUTs the filtered list', async () => {
|
|
31
|
+
client.get.mockResolvedValue({ data: { public_version: '2', settings: [{ version: '1' }, { version: '2' }, { version: '3' }] } });
|
|
32
|
+
await apps.removeVersion({ version: '3', ref: 'r' });
|
|
33
|
+
expect(client.get).toHaveBeenCalledWith('/v3/system/gateway/app-service/app/r/settings');
|
|
34
|
+
expect(appApi.updateSettings).toHaveBeenCalledWith({ settings: [{ version: '1' }, { version: '2' }] }, 'r');
|
|
35
|
+
});
|
|
36
|
+
it('remove-version refuses the public version', async () => {
|
|
37
|
+
client.get.mockResolvedValue({ data: { public_version: '2', settings: [] } });
|
|
38
|
+
await expect(apps.removeVersion({ version: '2', ref: 'r' })).rejects.toThrow(/public version/);
|
|
39
|
+
expect(appApi.updateSettings).not.toHaveBeenCalled();
|
|
40
|
+
});
|
|
41
|
+
it('bundle upload posts to the platform app upload route', async () => {
|
|
42
|
+
const { default: Cmd } = await import('../apps.js');
|
|
43
|
+
client.post.mockResolvedValue({});
|
|
44
|
+
await Cmd.constructor.postArchive(appApi, 'app', Buffer.from('x'));
|
|
45
|
+
expect(client.post.mock.calls[0][0]).toBe('/v3/system/gateway/app-service/app/upload');
|
|
46
|
+
});
|
|
47
|
+
it('public-app is refused under an OAuth profile', () => {
|
|
48
|
+
const cls = apps.constructor;
|
|
49
|
+
expect(() => cls.assertAllowed('public-app', { auth: 'oauth', client_id: 'x', refresh_token: 'r' })).toThrow(/OAuth/);
|
|
50
|
+
expect(() => cls.assertAllowed('app', { auth: 'oauth' })).not.toThrow();
|
|
51
|
+
});
|
|
52
|
+
});
|
package/dist/commands/apps.js
CHANGED
|
@@ -7,7 +7,8 @@ import { Writable } from "stream";
|
|
|
7
7
|
import { writeFileSync } from "fs";
|
|
8
8
|
import Util from "../lib/util.js";
|
|
9
9
|
import Playground from "../playground.js";
|
|
10
|
-
import { join } from 'path';
|
|
10
|
+
import { join, resolve } from 'path';
|
|
11
|
+
import OAuthSession from "../lib/oauth/OAuthSession.js";
|
|
11
12
|
/**
|
|
12
13
|
* Deprecation notice: Apps as such will be deprecated in favor for Protokol Forge and this will be removed in the future.
|
|
13
14
|
*/
|
|
@@ -71,11 +72,36 @@ class AppsCommand {
|
|
|
71
72
|
const { apptype, version, ref, output } = options;
|
|
72
73
|
const profile = util.getCurrentProfile();
|
|
73
74
|
const client = new Api({ token: profile.token, host: profile.host }).app(apptype);
|
|
74
|
-
|
|
75
|
+
AppsCommand.assertAllowed(apptype, profile);
|
|
76
|
+
const res = await client.download(ref, version);
|
|
77
|
+
if (output && res?.data) {
|
|
78
|
+
const file = resolve(output, `${ref}-${version}.tar.gz`);
|
|
79
|
+
writeFileSync(file, Buffer.from(res.data));
|
|
80
|
+
console.log(`Downloaded to ${file}`);
|
|
81
|
+
}
|
|
82
|
+
return res;
|
|
83
|
+
}
|
|
84
|
+
/** `public-app` routes are deprecated and denied to OAuth tokens. */
|
|
85
|
+
static assertAllowed(apptype, profile) {
|
|
86
|
+
if (apptype === "public-app" && OAuthSession.isOAuth(profile)) {
|
|
87
|
+
throw new Error("public-app routes are deprecated and not available to OAuth profiles. Use an API-user profile (`ptkl profile new --legacy`).");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** POST a tar.gz to the app-service upload route (shared by `upload` and `bundle -u`). */
|
|
91
|
+
static async postArchive(api, apptype, buffer, build) {
|
|
92
|
+
const formData = new FormData();
|
|
93
|
+
formData.append('app', new Blob([buffer], { type: "application/gzip" }), "app.tar.gz");
|
|
94
|
+
if (build)
|
|
95
|
+
formData.append('build', 'true');
|
|
96
|
+
return await api.client.post(`/v3/system/gateway/app-service/${apptype}/upload`, formData, {
|
|
97
|
+
timeout: 60000,
|
|
98
|
+
headers: { 'Content-Type': 'multipart/form-data' }
|
|
99
|
+
});
|
|
75
100
|
}
|
|
76
101
|
async upload(options) {
|
|
77
102
|
const { apptype, directory, build } = options;
|
|
78
103
|
const profile = util.getCurrentProfile();
|
|
104
|
+
AppsCommand.assertAllowed(apptype, profile);
|
|
79
105
|
const api = new Api({ token: profile.token, host: profile.host }).app(apptype);
|
|
80
106
|
try {
|
|
81
107
|
let buffer = Buffer.alloc(0);
|
|
@@ -93,19 +119,7 @@ class AppsCommand {
|
|
|
93
119
|
.on('error', reject);
|
|
94
120
|
});
|
|
95
121
|
console.log('Archive created successfully');
|
|
96
|
-
|
|
97
|
-
const formData = new FormData();
|
|
98
|
-
const blob = new Blob([buffer], { type: 'application/gzip' });
|
|
99
|
-
formData.append('app', blob, 'app.tar.gz');
|
|
100
|
-
if (build) {
|
|
101
|
-
formData.append('build', 'true');
|
|
102
|
-
}
|
|
103
|
-
return await api.client.post(`/v3/system/gateway/app-service/${apptype}/upload`, formData, {
|
|
104
|
-
timeout: 60000,
|
|
105
|
-
headers: {
|
|
106
|
-
'Content-Type': 'multipart/form-data'
|
|
107
|
-
}
|
|
108
|
-
});
|
|
122
|
+
return await AppsCommand.postArchive(api, apptype, buffer, build);
|
|
109
123
|
}
|
|
110
124
|
catch (error) {
|
|
111
125
|
throw error;
|
|
@@ -200,17 +214,20 @@ class AppsCommand {
|
|
|
200
214
|
callback();
|
|
201
215
|
},
|
|
202
216
|
});
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
217
|
+
await new Promise((done) => {
|
|
218
|
+
c({ gzip: true, cwd: distPath }, ['.']).pipe(bufferStream).on('finish', () => {
|
|
219
|
+
AppsCommand.postArchive(client.app("app"), "app", Buffer.concat(chunks)).then(() => {
|
|
220
|
+
console.log('Bundle uploaded successfully');
|
|
221
|
+
})
|
|
222
|
+
.catch((error) => {
|
|
223
|
+
console.error('Bundle error:', error.response?.data?.message ?? error.message);
|
|
224
|
+
})
|
|
225
|
+
.finally(done);
|
|
207
226
|
})
|
|
208
|
-
.
|
|
209
|
-
console.error('
|
|
227
|
+
.on('error', (error) => {
|
|
228
|
+
console.error('Error creating the archive:', error.message);
|
|
229
|
+
done();
|
|
210
230
|
});
|
|
211
|
-
})
|
|
212
|
-
.on('error', (error) => {
|
|
213
|
-
console.error('Error creating the archive:', error.message);
|
|
214
231
|
});
|
|
215
232
|
}
|
|
216
233
|
else {
|
|
@@ -233,16 +250,15 @@ class AppsCommand {
|
|
|
233
250
|
}
|
|
234
251
|
async removeVersion(options) {
|
|
235
252
|
const { version, ref } = options;
|
|
236
|
-
const
|
|
237
|
-
const
|
|
238
|
-
const
|
|
253
|
+
const api = util.getClientForProfile().app("app");
|
|
254
|
+
const url = `/v3/system/gateway/app-service/app/${ref}/settings`;
|
|
255
|
+
const res = await api.client.get(url);
|
|
256
|
+
const data = res.data?.data ?? res.data;
|
|
239
257
|
if (data.public_version === version) {
|
|
240
258
|
throw new Error("Cannot remove public version");
|
|
241
259
|
}
|
|
242
|
-
const settings = data.settings.filter((s) => s.version !== version);
|
|
243
|
-
return await
|
|
244
|
-
'settings': settings
|
|
245
|
-
}, ref);
|
|
260
|
+
const settings = (data.settings ?? []).filter((s) => s.version !== version);
|
|
261
|
+
return await api.updateSettings({ settings }, ref);
|
|
246
262
|
}
|
|
247
263
|
}
|
|
248
264
|
export default new AppsCommand();
|
|
@@ -90,7 +90,10 @@ export default class OAuthSession {
|
|
|
90
90
|
return undefined;
|
|
91
91
|
const challenge = String(res.headers?.["www-authenticate"] ?? "");
|
|
92
92
|
if (res.status === 403 && (/insufficient_scope/.test(challenge) || OAuthSession.isOAuth(profile))) {
|
|
93
|
-
|
|
93
|
+
const named = /scope="([^"]+)"/.exec(challenge)?.[1] ?? res.data?.required_scope ?? res.data?.scope;
|
|
94
|
+
const missing = typeof named === "string" && named.trim() ? named.trim() : undefined;
|
|
95
|
+
return `This profile's token lacks ${missing ? `the permission "${missing}"` : "a permission this command needs"}. `
|
|
96
|
+
+ `Log in again with \`ptkl profile new --add-scope "${missing ?? "<permission>"}"\`, or use an API-user profile (\`--legacy\`) for CI.`;
|
|
94
97
|
}
|
|
95
98
|
if (res.status === 401 && OAuthSession.isOAuth(profile)) {
|
|
96
99
|
return `The OAuth token was rejected. Run \`ptkl profile oauth\` again to log in${profile ? ` (profile '${profile.name}')` : ""}.`;
|
|
@@ -9,6 +9,26 @@ export const SCOPE_PRESET = [
|
|
|
9
9
|
{ permission: "manage base_components", commands: "component, generate-types, ratchet test connection" },
|
|
10
10
|
{ permission: "manage base_templates", commands: "template" },
|
|
11
11
|
{ permission: "manage base_thunder", commands: "thunder" },
|
|
12
|
+
{ permission: "list forge", commands: "forge list" },
|
|
13
|
+
{ permission: "read thunder", commands: "thunder (data read)" },
|
|
14
|
+
{ permission: "write thunder", commands: "thunder (data write)" },
|
|
15
|
+
{ permission: "manage base_workflows", commands: "workflow" },
|
|
16
|
+
{ permission: "create base_users", commands: "project invite, invites" },
|
|
17
|
+
{ permission: "manage integrations", commands: "integration list/install" },
|
|
18
|
+
{ permission: "view_transactions protokol-payments", commands: "integration payments (transactions)" },
|
|
19
|
+
{ permission: "refund_transaction protokol-payments", commands: "integration payments refund" },
|
|
20
|
+
{ permission: "void_transaction protokol-payments", commands: "integration payments void/deactivate" },
|
|
21
|
+
{ permission: "read protokol-mail", commands: "integration mail (list/get/attachments)" },
|
|
22
|
+
{ permission: "send protokol-mail", commands: "integration mail (send/resend)" },
|
|
23
|
+
{ permission: "manage timber", commands: "integration timber query/usage, logs" },
|
|
24
|
+
{ permission: "write timber", commands: "integration timber write" },
|
|
25
|
+
{ permission: "use protokol-media", commands: "integration dms (dirs, backup, policies, reminders, download)" },
|
|
26
|
+
{ permission: "utilities protokol-media", commands: "integration dms (libraries, convert, ocr)" },
|
|
27
|
+
{ permission: "upload protokol-media", commands: "integration dms upload" },
|
|
28
|
+
{ permission: "read_documents protokol-media", commands: "integration dms document (read/list/revisions/generate)" },
|
|
29
|
+
{ permission: "create_document protokol-media", commands: "integration dms document create" },
|
|
30
|
+
{ permission: "edit_document protokol-media", commands: "integration dms document (update/comment)" },
|
|
31
|
+
{ permission: "delete_document protokol-media", commands: "integration dms document (delete/restore)" },
|
|
12
32
|
{ permission: "read base_ratchet", commands: "ratchet (list/get)" },
|
|
13
33
|
{ permission: "inspect base_ratchet", commands: "ratchet (query inspect)" },
|
|
14
34
|
{ permission: "read base_users", commands: "users (list)" },
|
|
@@ -48,6 +48,19 @@ describe("ScopePreset", () => {
|
|
|
48
48
|
expect(ScopePreset.resolve({ scope: "x", addScope: "y,x" })).toEqual(["x", "y"]);
|
|
49
49
|
expect(ScopePreset.resolve({ addScope: "edit base_users" })).toContain("edit base_users");
|
|
50
50
|
});
|
|
51
|
+
it("covers thunder data, workflows, invites, forge list and integrations", () => {
|
|
52
|
+
expect(ScopePreset.permissions).toEqual(expect.arrayContaining([
|
|
53
|
+
"read thunder", "write thunder", "manage base_workflows", "create base_users", "list forge", "manage integrations",
|
|
54
|
+
"view_transactions protokol-payments", "refund_transaction protokol-payments", "void_transaction protokol-payments",
|
|
55
|
+
"read protokol-mail", "send protokol-mail", "manage timber", "write timber",
|
|
56
|
+
"use protokol-media", "utilities protokol-media", "upload protokol-media",
|
|
57
|
+
"read_documents protokol-media", "create_document protokol-media", "edit_document protokol-media", "delete_document protokol-media"
|
|
58
|
+
]));
|
|
59
|
+
for (const bad of ["admin", "manage forge"])
|
|
60
|
+
expect(ScopePreset.permissions).not.toContain(bad);
|
|
61
|
+
const wire = ScopePreset.toWire(ScopePreset.permissions);
|
|
62
|
+
expect(wire).toContain(`perm64:${Buffer.from("write thunder").toString("base64url")}`);
|
|
63
|
+
});
|
|
51
64
|
it("asks for API-user management and secrets by default", () => {
|
|
52
65
|
expect(ScopePreset.permissions).toEqual(expect.arrayContaining(["edit base_users", "manage_secrets base_users", "dev env_access"]));
|
|
53
66
|
});
|
|
@@ -189,7 +202,11 @@ describe("OAuthSession", () => {
|
|
|
189
202
|
});
|
|
190
203
|
it("maps 403 insufficient_scope to an unavailable-to-OAuth message", () => {
|
|
191
204
|
const err = { response: { status: 403, headers: { "www-authenticate": 'Bearer error="insufficient_scope"' } } };
|
|
192
|
-
expect(OAuthSession.describeApiError(err)).toMatch(/
|
|
205
|
+
expect(OAuthSession.describeApiError(err)).toMatch(/lacks a permission this command needs/);
|
|
206
|
+
expect(OAuthSession.describeApiError(err)).toMatch(/--add-scope "<permission>"/);
|
|
207
|
+
expect(OAuthSession.describeApiError(err)).toMatch(/--legacy/);
|
|
208
|
+
const named = { response: { status: 403, headers: { "www-authenticate": 'Bearer error="insufficient_scope", scope="read thunder"' } } };
|
|
209
|
+
expect(OAuthSession.describeApiError(named)).toMatch(/lacks the permission "read thunder"[\s\S]*--add-scope "read thunder"/);
|
|
193
210
|
expect(OAuthSession.describeApiError({ response: { status: 403, headers: {} } })).toBeUndefined();
|
|
194
211
|
});
|
|
195
212
|
});
|