@codelitdev/platform-cli 0.2.0 → 0.3.0
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/preset.json +8 -8
- package/package.json +1 -1
- package/template/apps/api/src/app.test.ts +35 -1
- package/template/apps/api/src/auth/authenticate.ts +1 -0
- package/template/apps/api/src/auth/options.ts +22 -3
- package/template/apps/api/src/permissions.ts +20 -0
- package/template/apps/api/src/seed.ts +3 -2
- package/template/apps/api/src/tenancy.ts +28 -3
package/dist/preset.json
CHANGED
|
@@ -5,42 +5,42 @@
|
|
|
5
5
|
},
|
|
6
6
|
"packages": {
|
|
7
7
|
"@codelitdev/platform": {
|
|
8
|
-
"recommended": "0.
|
|
8
|
+
"recommended": "0.3.0",
|
|
9
9
|
"supported": ">=0.1.0-alpha.0 <2",
|
|
10
10
|
"minimumSecure": "0.1.0-alpha.0"
|
|
11
11
|
},
|
|
12
12
|
"@codelitdev/observability": {
|
|
13
|
-
"recommended": "0.
|
|
13
|
+
"recommended": "0.3.0",
|
|
14
14
|
"supported": ">=0.1.0-alpha.0 <2",
|
|
15
15
|
"minimumSecure": "0.1.0-alpha.0"
|
|
16
16
|
},
|
|
17
17
|
"@codelitdev/mcp-server-kit": {
|
|
18
|
-
"recommended": "0.
|
|
18
|
+
"recommended": "0.3.0",
|
|
19
19
|
"supported": ">=0.1.0-alpha.0 <2",
|
|
20
20
|
"minimumSecure": "0.1.0-alpha.0"
|
|
21
21
|
},
|
|
22
22
|
"@codelitdev/platform-conformance": {
|
|
23
|
-
"recommended": "0.
|
|
23
|
+
"recommended": "0.3.0",
|
|
24
24
|
"supported": ">=0.1.0-alpha.0 <2",
|
|
25
25
|
"minimumSecure": "0.1.0-alpha.0"
|
|
26
26
|
},
|
|
27
27
|
"@codelitdev/platform-cli": {
|
|
28
|
-
"recommended": "0.
|
|
28
|
+
"recommended": "0.3.0",
|
|
29
29
|
"supported": ">=0.1.0-alpha.0 <2",
|
|
30
30
|
"minimumSecure": "0.1.0-alpha.0"
|
|
31
31
|
},
|
|
32
32
|
"@codelitdev/billing": {
|
|
33
|
-
"recommended": "0.
|
|
33
|
+
"recommended": "0.3.0",
|
|
34
34
|
"supported": ">=0.1.0-alpha.4 <1",
|
|
35
35
|
"minimumSecure": "0.1.0-alpha.4"
|
|
36
36
|
},
|
|
37
37
|
"@codelitdev/oauth-server-kit": {
|
|
38
|
-
"recommended": "0.
|
|
38
|
+
"recommended": "0.3.0",
|
|
39
39
|
"supported": ">=0.1.0-alpha.2 <1",
|
|
40
40
|
"minimumSecure": "0.1.0-alpha.2"
|
|
41
41
|
},
|
|
42
42
|
"@codelitdev/design-system": {
|
|
43
|
-
"recommended": "0.
|
|
43
|
+
"recommended": "0.3.0",
|
|
44
44
|
"supported": ">=0.1.0-alpha.7 <1",
|
|
45
45
|
"minimumSecure": "0.1.0-alpha.7"
|
|
46
46
|
}
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@ import * as schema from "./db/schema/index.js";
|
|
|
7
7
|
import { dispatch } from "./dispatch.js";
|
|
8
8
|
import { createExpressApp } from "./express-app.js";
|
|
9
9
|
import { createPgliteRuntime, freezeRuntimeClock } from "./runtime.js";
|
|
10
|
-
import { seedWorld } from "./seed.js";
|
|
10
|
+
import { mintOAuthToken, seedWorld } from "./seed.js";
|
|
11
11
|
|
|
12
12
|
describe.serial("reference API adapters", () => {
|
|
13
13
|
it("does not run DDL when oauth-server-kit options and schema modules are imported", async () => {
|
|
@@ -253,6 +253,40 @@ describe.serial("reference API adapters", () => {
|
|
|
253
253
|
await runtime.close();
|
|
254
254
|
});
|
|
255
255
|
|
|
256
|
+
it("limits an OAuth token to the data scopes it was granted", async () => {
|
|
257
|
+
const clock = freezeRuntimeClock(new Date("2026-03-01T00:00:00.000Z"));
|
|
258
|
+
const runtime = await createPgliteRuntime({ clock });
|
|
259
|
+
const world = await seedWorld(runtime, clock);
|
|
260
|
+
const readOnly = await mintOAuthToken(
|
|
261
|
+
runtime,
|
|
262
|
+
world.owner.sessionCookie,
|
|
263
|
+
"openid profile email data:read",
|
|
264
|
+
);
|
|
265
|
+
const headers = {
|
|
266
|
+
authorization: `Bearer ${readOnly}`,
|
|
267
|
+
"x-tenant-id": world.tenantA.publicId,
|
|
268
|
+
};
|
|
269
|
+
const list = await dispatch(runtime, { method: "GET", path: "/v1/notes", headers });
|
|
270
|
+
expect(list.status).toBe(200);
|
|
271
|
+
// The owner may write, but this token was only granted data:read.
|
|
272
|
+
const update = await dispatch(runtime, {
|
|
273
|
+
method: "PATCH",
|
|
274
|
+
path: `/v1/notes/${world.noteA.publicId}`,
|
|
275
|
+
headers,
|
|
276
|
+
body: { title: "read-only token" },
|
|
277
|
+
});
|
|
278
|
+
expect(update.status).toBe(403);
|
|
279
|
+
expect(update.body).toMatchObject({ code: "forbidden" });
|
|
280
|
+
const fullAccess = await dispatch(runtime, {
|
|
281
|
+
method: "PATCH",
|
|
282
|
+
path: `/v1/notes/${world.noteA.publicId}`,
|
|
283
|
+
headers: { ...headers, authorization: `Bearer ${world.owner.oauthToken}` },
|
|
284
|
+
body: { title: "read-write token" },
|
|
285
|
+
});
|
|
286
|
+
expect(fullAccess.status).toBe(200);
|
|
287
|
+
await runtime.close();
|
|
288
|
+
});
|
|
289
|
+
|
|
256
290
|
it("serves the same notes service over MCP with parity, isolation, and confirmation", async () => {
|
|
257
291
|
const clock = freezeRuntimeClock(new Date("2026-03-01T00:00:00.000Z"));
|
|
258
292
|
const runtime = await createPgliteRuntime({ clock });
|
|
@@ -7,7 +7,15 @@ import { jwt } from "better-auth/plugins/jwt";
|
|
|
7
7
|
export const AUTH_BASE_PATH = "/api/auth";
|
|
8
8
|
export const AUTH_SECRET_MIN_LENGTH = 32;
|
|
9
9
|
|
|
10
|
-
export const
|
|
10
|
+
export const DATA_READ_SCOPE = "data:read";
|
|
11
|
+
export const DATA_WRITE_SCOPE = "data:write";
|
|
12
|
+
// MCP clients request exactly these. `offline_access` gets them a refresh
|
|
13
|
+
// token instead of a new consent every time the access token expires.
|
|
14
|
+
export const MCP_SCOPES_SUPPORTED = [
|
|
15
|
+
DATA_READ_SCOPE,
|
|
16
|
+
DATA_WRITE_SCOPE,
|
|
17
|
+
"offline_access",
|
|
18
|
+
] as const;
|
|
11
19
|
|
|
12
20
|
export type ReferenceAuthUrls = {
|
|
13
21
|
publicApiUrl: string;
|
|
@@ -36,12 +44,23 @@ export function oauthProviderInput(urls: ReturnType<typeof authUrls>) {
|
|
|
36
44
|
return createOAuthProviderOptions({
|
|
37
45
|
loginPage: `${urls.publicApiUrl}/oauth/login`,
|
|
38
46
|
consentPage: `${urls.publicApiUrl}/oauth/consent`,
|
|
39
|
-
scopes: [
|
|
47
|
+
scopes: [
|
|
48
|
+
"openid",
|
|
49
|
+
"profile",
|
|
50
|
+
"email",
|
|
51
|
+
"offline_access",
|
|
52
|
+
DATA_READ_SCOPE,
|
|
53
|
+
DATA_WRITE_SCOPE,
|
|
54
|
+
],
|
|
40
55
|
validAudiences: [urls.restResource, urls.mcpResource],
|
|
41
56
|
allowDynamicClientRegistration: true,
|
|
42
57
|
allowUnauthenticatedDynamicClientRegistration: true,
|
|
43
58
|
clientRegistrationDefaultScopes: ["openid", "profile", "email"],
|
|
44
|
-
clientRegistrationAllowedScopes: [
|
|
59
|
+
clientRegistrationAllowedScopes: [
|
|
60
|
+
"offline_access",
|
|
61
|
+
DATA_READ_SCOPE,
|
|
62
|
+
DATA_WRITE_SCOPE,
|
|
63
|
+
],
|
|
45
64
|
});
|
|
46
65
|
}
|
|
47
66
|
|
|
@@ -18,6 +18,26 @@ export const MEMBER_PERMISSIONS: readonly ReferencePermission[] = [
|
|
|
18
18
|
"billing:read",
|
|
19
19
|
];
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* What each OAuth data scope allows. An OAuth request keeps only the
|
|
23
|
+
* member's permissions that its granted scopes cover, so a token approved
|
|
24
|
+
* as read-only cannot write even when the member could.
|
|
25
|
+
*/
|
|
26
|
+
export const SCOPE_PERMISSIONS: Readonly<
|
|
27
|
+
Record<string, readonly ReferencePermission[]>
|
|
28
|
+
> = {
|
|
29
|
+
"data:read": ["notes:read", "billing:read"],
|
|
30
|
+
"data:write": ["notes:write", "notes:delete", "tenant:admin"],
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export function narrowToScopes(
|
|
34
|
+
permissions: ReadonlySet<ReferencePermission>,
|
|
35
|
+
scopes: readonly string[],
|
|
36
|
+
): Set<ReferencePermission> {
|
|
37
|
+
const allowed = new Set(scopes.flatMap((scope) => SCOPE_PERMISSIONS[scope] ?? []));
|
|
38
|
+
return new Set([...permissions].filter((permission) => allowed.has(permission)));
|
|
39
|
+
}
|
|
40
|
+
|
|
21
41
|
export function parsePermissions(value: string): Set<ReferencePermission> {
|
|
22
42
|
const allowed = new Set<string>(REFERENCE_PERMISSIONS);
|
|
23
43
|
const parsed = new Set<ReferencePermission>();
|
|
@@ -91,9 +91,10 @@ function pkce() {
|
|
|
91
91
|
};
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
async function mintOAuthToken(
|
|
94
|
+
export async function mintOAuthToken(
|
|
95
95
|
runtime: Runtime,
|
|
96
96
|
sessionCookie: string,
|
|
97
|
+
scope = "openid profile email offline_access data:read data:write",
|
|
97
98
|
): Promise<string> {
|
|
98
99
|
const redirectUri = "com.example.__PRODUCT_SLUG__:/oauth/callback";
|
|
99
100
|
const client = await runtime.auth.auth.api.createOAuthClient({
|
|
@@ -123,7 +124,7 @@ async function mintOAuthToken(
|
|
|
123
124
|
client_id: client.client_id,
|
|
124
125
|
redirect_uri: redirectUri,
|
|
125
126
|
response_type: "code",
|
|
126
|
-
scope
|
|
127
|
+
scope,
|
|
127
128
|
state: "state-123",
|
|
128
129
|
code_challenge: proof.challenge,
|
|
129
130
|
code_challenge_method: "S256",
|
|
@@ -5,7 +5,11 @@ import {
|
|
|
5
5
|
} from "@codelitdev/platform";
|
|
6
6
|
import { and, eq } from "drizzle-orm";
|
|
7
7
|
import * as schema from "./db/schema/index.js";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
narrowToScopes,
|
|
10
|
+
parsePermissions,
|
|
11
|
+
type ReferencePermission,
|
|
12
|
+
} from "./permissions.js";
|
|
9
13
|
import type { AppDb } from "./types.js";
|
|
10
14
|
|
|
11
15
|
export type ResolvedTenant = {
|
|
@@ -14,12 +18,33 @@ export type ResolvedTenant = {
|
|
|
14
18
|
permissions: ReadonlySet<ReferencePermission>;
|
|
15
19
|
};
|
|
16
20
|
|
|
17
|
-
|
|
21
|
+
type TenantInput = {
|
|
18
22
|
db: AppDb;
|
|
19
23
|
principalId: string;
|
|
20
24
|
credential: PlatformCredential;
|
|
21
25
|
requestedPublicTenantId: string | null;
|
|
22
|
-
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export async function resolveTenantContext(
|
|
29
|
+
input: TenantInput,
|
|
30
|
+
): Promise<{ ok: true; value: ResolvedTenant } | { ok: false; error: PlatformError }> {
|
|
31
|
+
const resolved = await resolveTenant(input);
|
|
32
|
+
if (!resolved.ok || input.credential.kind !== "oauth") return resolved;
|
|
33
|
+
return {
|
|
34
|
+
ok: true,
|
|
35
|
+
value: {
|
|
36
|
+
...resolved.value,
|
|
37
|
+
permissions: narrowToScopes(
|
|
38
|
+
resolved.value.permissions,
|
|
39
|
+
input.credential.scopes ?? [],
|
|
40
|
+
),
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function resolveTenant(
|
|
46
|
+
input: TenantInput,
|
|
47
|
+
): Promise<{ ok: true; value: ResolvedTenant } | { ok: false; error: PlatformError }> {
|
|
23
48
|
if (input.credential.kind === "api_key") {
|
|
24
49
|
if (!input.credential.credentialId) {
|
|
25
50
|
return { ok: false, error: createPlatformError("unauthenticated") };
|