swallowkit 1.0.0-beta.8 → 1.0.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/README.ja.md +87 -181
- package/README.md +87 -238
- package/dist/__tests__/fixtures.d.ts +12 -0
- package/dist/__tests__/fixtures.d.ts.map +1 -1
- package/dist/__tests__/fixtures.js +101 -0
- package/dist/__tests__/fixtures.js.map +1 -1
- package/dist/cli/commands/add-auth.d.ts +15 -0
- package/dist/cli/commands/add-auth.d.ts.map +1 -0
- package/dist/cli/commands/add-auth.js +790 -0
- package/dist/cli/commands/add-auth.js.map +1 -0
- package/dist/cli/commands/add-connector.d.ts +20 -0
- package/dist/cli/commands/add-connector.d.ts.map +1 -0
- package/dist/cli/commands/add-connector.js +164 -0
- package/dist/cli/commands/add-connector.js.map +1 -0
- package/dist/cli/commands/cosmos-worktree-isolation.d.ts +25 -0
- package/dist/cli/commands/cosmos-worktree-isolation.d.ts.map +1 -0
- package/dist/cli/commands/cosmos-worktree-isolation.js +124 -0
- package/dist/cli/commands/cosmos-worktree-isolation.js.map +1 -0
- package/dist/cli/commands/create-model.d.ts +1 -4
- package/dist/cli/commands/create-model.d.ts.map +1 -1
- package/dist/cli/commands/create-model.js +20 -82
- package/dist/cli/commands/create-model.js.map +1 -1
- package/dist/cli/commands/dev-seeds.d.ts +27 -0
- package/dist/cli/commands/dev-seeds.d.ts.map +1 -1
- package/dist/cli/commands/dev-seeds.js +188 -6
- package/dist/cli/commands/dev-seeds.js.map +1 -1
- package/dist/cli/commands/dev.d.ts +30 -0
- package/dist/cli/commands/dev.d.ts.map +1 -1
- package/dist/cli/commands/dev.js +495 -129
- package/dist/cli/commands/dev.js.map +1 -1
- package/dist/cli/commands/init.d.ts +70 -0
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/init.js +797 -768
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/cli/commands/scaffold.d.ts +24 -3
- package/dist/cli/commands/scaffold.d.ts.map +1 -1
- package/dist/cli/commands/scaffold.js +408 -189
- package/dist/cli/commands/scaffold.js.map +1 -1
- package/dist/cli/index.d.ts +5 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +335 -58
- package/dist/cli/index.js.map +1 -1
- package/dist/core/config.d.ts +13 -3
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +151 -8
- package/dist/core/config.js.map +1 -1
- package/dist/core/mock/connector-mock-server.d.ts +101 -0
- package/dist/core/mock/connector-mock-server.d.ts.map +1 -0
- package/dist/core/mock/connector-mock-server.js +480 -0
- package/dist/core/mock/connector-mock-server.js.map +1 -0
- package/dist/core/mock/zod-mock-generator.d.ts +14 -0
- package/dist/core/mock/zod-mock-generator.d.ts.map +1 -0
- package/dist/core/mock/zod-mock-generator.js +163 -0
- package/dist/core/mock/zod-mock-generator.js.map +1 -0
- package/dist/core/operations/auth-operations.d.ts +61 -0
- package/dist/core/operations/auth-operations.d.ts.map +1 -0
- package/dist/core/operations/auth-operations.js +272 -0
- package/dist/core/operations/auth-operations.js.map +1 -0
- package/dist/core/operations/create-model.d.ts +15 -0
- package/dist/core/operations/create-model.d.ts.map +1 -0
- package/dist/core/operations/create-model.js +185 -0
- package/dist/core/operations/create-model.js.map +1 -0
- package/dist/core/operations/file-session.d.ts +56 -0
- package/dist/core/operations/file-session.d.ts.map +1 -0
- package/dist/core/operations/file-session.js +203 -0
- package/dist/core/operations/file-session.js.map +1 -0
- package/dist/core/operations/provision-operations.d.ts +52 -0
- package/dist/core/operations/provision-operations.d.ts.map +1 -0
- package/dist/core/operations/provision-operations.js +211 -0
- package/dist/core/operations/provision-operations.js.map +1 -0
- package/dist/core/operations/runtime.d.ts +37 -0
- package/dist/core/operations/runtime.d.ts.map +1 -0
- package/dist/core/operations/runtime.js +248 -0
- package/dist/core/operations/runtime.js.map +1 -0
- package/dist/core/operations/scaffold-apply.d.ts +21 -0
- package/dist/core/operations/scaffold-apply.d.ts.map +1 -0
- package/dist/core/operations/scaffold-apply.js +57 -0
- package/dist/core/operations/scaffold-apply.js.map +1 -0
- package/dist/core/operations/scaffold-machine.d.ts +16 -0
- package/dist/core/operations/scaffold-machine.d.ts.map +1 -0
- package/dist/core/operations/scaffold-machine.js +51 -0
- package/dist/core/operations/scaffold-machine.js.map +1 -0
- package/dist/core/operations/scaffold-plan.d.ts +43 -0
- package/dist/core/operations/scaffold-plan.d.ts.map +1 -0
- package/dist/core/operations/scaffold-plan.js +187 -0
- package/dist/core/operations/scaffold-plan.js.map +1 -0
- package/dist/core/project/agent-instructions.d.ts +29 -0
- package/dist/core/project/agent-instructions.d.ts.map +1 -0
- package/dist/core/project/agent-instructions.js +160 -0
- package/dist/core/project/agent-instructions.js.map +1 -0
- package/dist/core/project/artifacts.d.ts +58 -0
- package/dist/core/project/artifacts.d.ts.map +1 -0
- package/dist/core/project/artifacts.js +179 -0
- package/dist/core/project/artifacts.js.map +1 -0
- package/dist/core/project/boundaries.d.ts +50 -0
- package/dist/core/project/boundaries.d.ts.map +1 -0
- package/dist/core/project/boundaries.js +117 -0
- package/dist/core/project/boundaries.js.map +1 -0
- package/dist/core/project/capabilities.d.ts +58 -0
- package/dist/core/project/capabilities.d.ts.map +1 -0
- package/dist/core/project/capabilities.js +135 -0
- package/dist/core/project/capabilities.js.map +1 -0
- package/dist/core/project/drift.d.ts +29 -0
- package/dist/core/project/drift.d.ts.map +1 -0
- package/dist/core/project/drift.js +188 -0
- package/dist/core/project/drift.js.map +1 -0
- package/dist/core/project/infra.d.ts +49 -0
- package/dist/core/project/infra.d.ts.map +1 -0
- package/dist/core/project/infra.js +167 -0
- package/dist/core/project/infra.js.map +1 -0
- package/dist/core/project/manifest.d.ts +92 -0
- package/dist/core/project/manifest.d.ts.map +1 -0
- package/dist/core/project/manifest.js +321 -0
- package/dist/core/project/manifest.js.map +1 -0
- package/dist/core/project/state.d.ts +15 -0
- package/dist/core/project/state.d.ts.map +1 -0
- package/dist/core/project/state.js +106 -0
- package/dist/core/project/state.js.map +1 -0
- package/dist/core/project/validation.d.ts +20 -0
- package/dist/core/project/validation.d.ts.map +1 -0
- package/dist/core/project/validation.js +274 -0
- package/dist/core/project/validation.js.map +1 -0
- package/dist/core/project/workflows.d.ts +43 -0
- package/dist/core/project/workflows.d.ts.map +1 -0
- package/dist/core/project/workflows.js +299 -0
- package/dist/core/project/workflows.js.map +1 -0
- package/dist/core/scaffold/auth-generator.d.ts +63 -0
- package/dist/core/scaffold/auth-generator.d.ts.map +1 -0
- package/dist/core/scaffold/auth-generator.js +1774 -0
- package/dist/core/scaffold/auth-generator.js.map +1 -0
- package/dist/core/scaffold/connector-functions-generator.d.ts +41 -0
- package/dist/core/scaffold/connector-functions-generator.d.ts.map +1 -0
- package/dist/core/scaffold/connector-functions-generator.js +1029 -0
- package/dist/core/scaffold/connector-functions-generator.js.map +1 -0
- package/dist/core/scaffold/functions-generator.d.ts +4 -3
- package/dist/core/scaffold/functions-generator.d.ts.map +1 -1
- package/dist/core/scaffold/functions-generator.js +405 -134
- package/dist/core/scaffold/functions-generator.js.map +1 -1
- package/dist/core/scaffold/model-parser.d.ts +20 -0
- package/dist/core/scaffold/model-parser.d.ts.map +1 -1
- package/dist/core/scaffold/model-parser.js +229 -78
- package/dist/core/scaffold/model-parser.js.map +1 -1
- package/dist/core/scaffold/native-schema-generator.d.ts +19 -0
- package/dist/core/scaffold/native-schema-generator.d.ts.map +1 -0
- package/dist/core/scaffold/native-schema-generator.js +753 -0
- package/dist/core/scaffold/native-schema-generator.js.map +1 -0
- package/dist/core/scaffold/nextjs-generator.d.ts +8 -0
- package/dist/core/scaffold/nextjs-generator.d.ts.map +1 -1
- package/dist/core/scaffold/nextjs-generator.js +152 -8
- package/dist/core/scaffold/nextjs-generator.js.map +1 -1
- package/dist/core/scaffold/ui-generator.d.ts +17 -4
- package/dist/core/scaffold/ui-generator.d.ts.map +1 -1
- package/dist/core/scaffold/ui-generator.js +174 -34
- package/dist/core/scaffold/ui-generator.js.map +1 -1
- package/dist/core/verify/index.d.ts +69 -0
- package/dist/core/verify/index.d.ts.map +1 -0
- package/dist/core/verify/index.js +412 -0
- package/dist/core/verify/index.js.map +1 -0
- package/dist/machine/contracts.d.ts +24 -0
- package/dist/machine/contracts.d.ts.map +1 -0
- package/dist/machine/contracts.js +3 -0
- package/dist/machine/contracts.js.map +1 -0
- package/dist/machine/errors.d.ts +14 -0
- package/dist/machine/errors.d.ts.map +1 -0
- package/dist/machine/errors.js +42 -0
- package/dist/machine/errors.js.map +1 -0
- package/dist/machine/index.d.ts +3 -0
- package/dist/machine/index.d.ts.map +1 -0
- package/dist/machine/index.js +566 -0
- package/dist/machine/index.js.map +1 -0
- package/dist/mcp/index.d.ts +25 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +425 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/types/index.d.ts +105 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils/package-manager.d.ts +7 -11
- package/dist/utils/package-manager.d.ts.map +1 -1
- package/dist/utils/package-manager.js +42 -17
- package/dist/utils/package-manager.js.map +1 -1
- package/dist/utils/python-uv.d.ts +21 -0
- package/dist/utils/python-uv.d.ts.map +1 -0
- package/dist/utils/python-uv.js +111 -0
- package/dist/utils/python-uv.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +49 -0
- package/dist/version.js.map +1 -0
- package/package.json +15 -17
- package/src/__tests__/__snapshots__/functions-generator.test.ts.snap +716 -22
- package/src/__tests__/__snapshots__/nextjs-generator.test.ts.snap +19 -7
- package/src/__tests__/__snapshots__/ui-generator.test.ts.snap +20 -12
- package/src/__tests__/agent-instructions.test.ts +237 -0
- package/src/__tests__/agent-loop-phase34.test.ts +513 -0
- package/src/__tests__/auth.test.ts +770 -0
- package/src/__tests__/bff-swa-auth.test.ts +275 -0
- package/src/__tests__/config.test.ts +176 -3
- package/src/__tests__/connector-functions-generator.test.ts +288 -0
- package/src/__tests__/connector-mock-server.test.ts +459 -0
- package/src/__tests__/connector-model-bff.test.ts +162 -0
- package/src/__tests__/cosmos-worktree-isolation.test.ts +149 -0
- package/src/__tests__/dev-seeds.test.ts +124 -0
- package/src/__tests__/dev.test.ts +254 -0
- package/src/__tests__/file-session.test.ts +98 -0
- package/src/__tests__/fixtures.ts +101 -0
- package/src/__tests__/functions-generator.test.ts +137 -1
- package/src/__tests__/init.test.ts +386 -1
- package/src/__tests__/jest-globals.d.ts +8 -0
- package/src/__tests__/machine.test.ts +371 -0
- package/src/__tests__/mcp.test.ts +253 -0
- package/src/__tests__/model-parser.test.ts +142 -0
- package/src/__tests__/multi-auth.test.ts +160 -0
- package/src/__tests__/nextjs-generator.test.ts +8 -0
- package/src/__tests__/package-manager.test.ts +59 -4
- package/src/__tests__/plan-apply.test.ts +499 -0
- package/src/__tests__/python-uv.test.ts +48 -0
- package/src/__tests__/scaffold.test.ts +287 -24
- package/src/__tests__/tsconfig.json +15 -0
- package/src/__tests__/ui-generator.test.ts +104 -0
- package/src/__tests__/zod-mock-generator.test.ts +132 -0
- package/src/cli/commands/add-auth.ts +853 -0
- package/src/cli/commands/add-connector.ts +159 -0
- package/src/cli/commands/cosmos-worktree-isolation.ts +130 -0
- package/src/cli/commands/create-model.ts +21 -100
- package/src/cli/commands/dev-seeds.ts +278 -7
- package/src/cli/commands/dev.ts +607 -160
- package/src/cli/commands/init.ts +880 -808
- package/src/cli/commands/scaffold.ts +531 -236
- package/src/cli/index.ts +338 -60
- package/src/core/config.ts +145 -9
- package/src/core/mock/connector-mock-server.ts +555 -0
- package/src/core/mock/zod-mock-generator.ts +205 -0
- package/src/core/operations/auth-operations.ts +360 -0
- package/src/core/operations/create-model.ts +191 -0
- package/src/core/operations/file-session.ts +222 -0
- package/src/core/operations/provision-operations.ts +297 -0
- package/src/core/operations/runtime.ts +258 -0
- package/src/core/operations/scaffold-apply.ts +106 -0
- package/src/core/operations/scaffold-machine.ts +77 -0
- package/src/core/operations/scaffold-plan.ts +215 -0
- package/src/core/project/agent-instructions.ts +167 -0
- package/src/core/project/agent-templates/AGENTS.md +504 -0
- package/src/core/project/agent-templates/CLAUDE.md +31 -0
- package/src/core/project/agent-templates/azure-functions.instructions.md +22 -0
- package/src/core/project/agent-templates/bff-routes.instructions.md +18 -0
- package/src/core/project/agent-templates/copilot-instructions.md +27 -0
- package/src/core/project/agent-templates/shared-models.instructions.md +72 -0
- package/src/core/project/artifacts.ts +205 -0
- package/src/core/project/boundaries.ts +170 -0
- package/src/core/project/capabilities.ts +193 -0
- package/src/core/project/drift.ts +202 -0
- package/src/core/project/infra.ts +196 -0
- package/src/core/project/manifest.ts +402 -0
- package/src/core/project/state.ts +77 -0
- package/src/core/project/validation.ts +295 -0
- package/src/core/project/workflows.ts +295 -0
- package/src/core/scaffold/auth-generator.ts +1824 -0
- package/src/core/scaffold/connector-functions-generator.ts +1133 -0
- package/src/core/scaffold/functions-generator.ts +432 -134
- package/src/core/scaffold/model-parser.ts +244 -90
- package/src/core/scaffold/native-schema-generator.ts +904 -0
- package/src/core/scaffold/nextjs-generator.ts +174 -9
- package/src/core/scaffold/ui-generator.ts +191 -36
- package/src/core/verify/index.ts +468 -0
- package/src/machine/contracts.ts +33 -0
- package/src/machine/errors.ts +45 -0
- package/src/machine/index.ts +690 -0
- package/src/mcp/index.ts +423 -0
- package/src/types/index.ts +137 -0
- package/src/utils/package-manager.ts +41 -19
- package/src/utils/python-uv.ts +96 -0
- package/src/version.ts +13 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import {
|
|
3
|
+
generateBFFCallFunctionWithMultipleAuth,
|
|
4
|
+
generateBFFCallFunctionWithSwaAuth,
|
|
5
|
+
} from "../core/scaffold/auth-generator";
|
|
6
|
+
|
|
7
|
+
type GeneratedModule = {
|
|
8
|
+
callFunction(config: { method: "GET"; path: string }): Promise<unknown>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type FetchCall = {
|
|
12
|
+
url: string;
|
|
13
|
+
init: RequestInit | undefined;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const originalFetch = global.fetch;
|
|
17
|
+
const originalBaseUrl = process.env.BACKEND_FUNCTIONS_BASE_URL;
|
|
18
|
+
const originalFunctionsKey = process.env.BACKEND_FUNCTIONS_KEY;
|
|
19
|
+
|
|
20
|
+
function encodePrincipal(value: unknown): string {
|
|
21
|
+
return Buffer.from(JSON.stringify(value), "utf8").toString("base64");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function decodeForwardedPrincipal(call: FetchCall): Record<string, unknown> | undefined {
|
|
25
|
+
const value = (call.init?.headers as Record<string, string>)["x-ms-client-principal"];
|
|
26
|
+
return value
|
|
27
|
+
? JSON.parse(Buffer.from(value, "base64").toString("utf8"))
|
|
28
|
+
: undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function loadGeneratedModule(
|
|
32
|
+
code: string,
|
|
33
|
+
requestHeaders: Record<string, string>,
|
|
34
|
+
): GeneratedModule {
|
|
35
|
+
const output = ts.transpileModule(code, {
|
|
36
|
+
compilerOptions: {
|
|
37
|
+
esModuleInterop: true,
|
|
38
|
+
module: ts.ModuleKind.CommonJS,
|
|
39
|
+
target: ts.ScriptTarget.ES2020,
|
|
40
|
+
},
|
|
41
|
+
reportDiagnostics: true,
|
|
42
|
+
});
|
|
43
|
+
expect(output.diagnostics).toEqual([]);
|
|
44
|
+
|
|
45
|
+
class MockNextResponse {
|
|
46
|
+
static json(body: unknown, init?: { status?: number }) {
|
|
47
|
+
return { body, status: init?.status ?? 200 };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
constructor(
|
|
51
|
+
public body: unknown,
|
|
52
|
+
public init?: { status?: number },
|
|
53
|
+
) {}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const generatedModule = { exports: {} as GeneratedModule };
|
|
57
|
+
const mockRequire = (id: string): unknown => {
|
|
58
|
+
if (id === "next/server") return { NextResponse: MockNextResponse };
|
|
59
|
+
if (id === "next/headers") {
|
|
60
|
+
return { headers: async () => new Headers(requestHeaders) };
|
|
61
|
+
}
|
|
62
|
+
if (id === "zod/v4") return require("zod/v4");
|
|
63
|
+
throw new Error(`Unexpected generated import: ${id}`);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
new Function("require", "module", "exports", output.outputText)(
|
|
67
|
+
mockRequire,
|
|
68
|
+
generatedModule,
|
|
69
|
+
generatedModule.exports,
|
|
70
|
+
);
|
|
71
|
+
return generatedModule.exports;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function runGeneratedCall(options: {
|
|
75
|
+
code: string;
|
|
76
|
+
requestHeaders: Record<string, string>;
|
|
77
|
+
mePrincipal?: unknown;
|
|
78
|
+
functionsBaseUrl?: string;
|
|
79
|
+
}): Promise<FetchCall[]> {
|
|
80
|
+
process.env.BACKEND_FUNCTIONS_BASE_URL =
|
|
81
|
+
options.functionsBaseUrl ?? "https://functions.example.test";
|
|
82
|
+
process.env.BACKEND_FUNCTIONS_KEY = "functions-key-secret";
|
|
83
|
+
|
|
84
|
+
const calls: FetchCall[] = [];
|
|
85
|
+
global.fetch = jest.fn(async (input: string | URL | Request, init?: RequestInit) => {
|
|
86
|
+
const url = input.toString();
|
|
87
|
+
calls.push({ url, init });
|
|
88
|
+
if (url.endsWith("/.auth/me")) {
|
|
89
|
+
return {
|
|
90
|
+
ok: true,
|
|
91
|
+
status: 200,
|
|
92
|
+
json: async () => ({ clientPrincipal: options.mePrincipal }),
|
|
93
|
+
} as Response;
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
ok: true,
|
|
97
|
+
status: 200,
|
|
98
|
+
json: async () => ({ ok: true }),
|
|
99
|
+
text: async () => "",
|
|
100
|
+
} as Response;
|
|
101
|
+
}) as typeof fetch;
|
|
102
|
+
|
|
103
|
+
const generated = loadGeneratedModule(options.code, options.requestHeaders);
|
|
104
|
+
await generated.callFunction({ method: "GET", path: "/api/resource" });
|
|
105
|
+
return calls;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function functionsCall(calls: FetchCall[]): FetchCall {
|
|
109
|
+
const call = calls.find(({ url }) => url.endsWith("/api/resource"));
|
|
110
|
+
expect(call).toBeDefined();
|
|
111
|
+
return call!;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
afterEach(() => {
|
|
115
|
+
global.fetch = originalFetch;
|
|
116
|
+
if (originalBaseUrl === undefined) delete process.env.BACKEND_FUNCTIONS_BASE_URL;
|
|
117
|
+
else process.env.BACKEND_FUNCTIONS_BASE_URL = originalBaseUrl;
|
|
118
|
+
if (originalFunctionsKey === undefined) delete process.env.BACKEND_FUNCTIONS_KEY;
|
|
119
|
+
else process.env.BACKEND_FUNCTIONS_KEY = originalFunctionsKey;
|
|
120
|
+
jest.restoreAllMocks();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("generated BFF SWA credential forwarding", () => {
|
|
124
|
+
const validPrincipal = {
|
|
125
|
+
identityProvider: "github",
|
|
126
|
+
userId: "user-123",
|
|
127
|
+
userDetails: "octocat",
|
|
128
|
+
userRoles: ["anonymous", "authenticated", 123],
|
|
129
|
+
untrustedExtra: "must-not-be-forwarded",
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
it("normalizes an incoming principal and does not call /.auth/me", async () => {
|
|
133
|
+
const calls = await runGeneratedCall({
|
|
134
|
+
code: generateBFFCallFunctionWithSwaAuth(),
|
|
135
|
+
requestHeaders: {
|
|
136
|
+
cookie: "session=cookie-secret",
|
|
137
|
+
host: "app.example.test",
|
|
138
|
+
"x-ms-client-principal": encodePrincipal(validPrincipal),
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
expect(calls.map(({ url }) => url)).not.toContain("https://app.example.test/.auth/me");
|
|
143
|
+
expect(decodeForwardedPrincipal(functionsCall(calls))).toEqual({
|
|
144
|
+
identityProvider: "github",
|
|
145
|
+
userId: "user-123",
|
|
146
|
+
userDetails: "octocat",
|
|
147
|
+
userRoles: ["anonymous", "authenticated"],
|
|
148
|
+
});
|
|
149
|
+
expect(
|
|
150
|
+
(functionsCall(calls).init?.headers as Record<string, string>)[
|
|
151
|
+
"x-functions-key"
|
|
152
|
+
],
|
|
153
|
+
).toBe("functions-key-secret");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("uses x-forwarded-host for the /.auth/me fallback", async () => {
|
|
157
|
+
const calls = await runGeneratedCall({
|
|
158
|
+
code: generateBFFCallFunctionWithSwaAuth(),
|
|
159
|
+
requestHeaders: {
|
|
160
|
+
cookie: "session=valid",
|
|
161
|
+
host: "internal.example.test",
|
|
162
|
+
"x-forwarded-host": "public.example.test",
|
|
163
|
+
},
|
|
164
|
+
mePrincipal: validPrincipal,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
expect(calls[0].url).toBe("https://public.example.test/.auth/me");
|
|
168
|
+
expect(decodeForwardedPrincipal(functionsCall(calls))).toEqual({
|
|
169
|
+
identityProvider: "github",
|
|
170
|
+
userId: "user-123",
|
|
171
|
+
userDetails: "octocat",
|
|
172
|
+
userRoles: ["anonymous", "authenticated"],
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("falls back to host when x-forwarded-host is absent", async () => {
|
|
177
|
+
const calls = await runGeneratedCall({
|
|
178
|
+
code: generateBFFCallFunctionWithSwaAuth(),
|
|
179
|
+
requestHeaders: { cookie: "session=valid", host: "app.example.test" },
|
|
180
|
+
mePrincipal: validPrincipal,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
expect(calls[0].url).toBe("https://app.example.test/.auth/me");
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("uses the first comma-separated forwarded host and protocol", async () => {
|
|
187
|
+
const calls = await runGeneratedCall({
|
|
188
|
+
code: generateBFFCallFunctionWithSwaAuth(),
|
|
189
|
+
requestHeaders: {
|
|
190
|
+
cookie: "session=valid",
|
|
191
|
+
host: "internal.example.test",
|
|
192
|
+
"x-forwarded-host": "public.example.test, proxy.example.test",
|
|
193
|
+
"x-forwarded-proto": "http, https",
|
|
194
|
+
},
|
|
195
|
+
mePrincipal: validPrincipal,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
expect(calls[0].url).toBe("http://public.example.test/.auth/me");
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it.each([
|
|
202
|
+
["a missing userId", { ...validPrincipal, userId: undefined }],
|
|
203
|
+
["a missing authenticated role", { ...validPrincipal, userRoles: ["anonymous"] }],
|
|
204
|
+
["a missing identityProvider", { ...validPrincipal, identityProvider: "" }],
|
|
205
|
+
])("does not forward a principal with %s", async (_case, principal) => {
|
|
206
|
+
const calls = await runGeneratedCall({
|
|
207
|
+
code: generateBFFCallFunctionWithSwaAuth(),
|
|
208
|
+
requestHeaders: { "x-ms-client-principal": encodePrincipal(principal) },
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
expect(decodeForwardedPrincipal(functionsCall(calls))).toBeUndefined();
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it.each([
|
|
215
|
+
"not-base64!",
|
|
216
|
+
`${encodePrincipal(validPrincipal)}!`,
|
|
217
|
+
Buffer.from("not-json", "utf8").toString("base64"),
|
|
218
|
+
])(
|
|
219
|
+
"fails closed without logging credentials for malformed principal %s",
|
|
220
|
+
async (principal) => {
|
|
221
|
+
const error = jest.spyOn(console, "error").mockImplementation(() => undefined);
|
|
222
|
+
const cookie = "session=highly-sensitive-cookie";
|
|
223
|
+
const calls = await runGeneratedCall({
|
|
224
|
+
code: generateBFFCallFunctionWithSwaAuth(),
|
|
225
|
+
requestHeaders: { cookie, "x-ms-client-principal": principal },
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
expect(decodeForwardedPrincipal(functionsCall(calls))).toBeUndefined();
|
|
229
|
+
const logged = JSON.stringify(error.mock.calls);
|
|
230
|
+
expect(logged).not.toContain(cookie);
|
|
231
|
+
expect(logged).not.toContain(principal);
|
|
232
|
+
},
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
it("preserves Bearer and applies the same SWA rules for multiple auth", async () => {
|
|
236
|
+
const calls = await runGeneratedCall({
|
|
237
|
+
code: generateBFFCallFunctionWithMultipleAuth(),
|
|
238
|
+
requestHeaders: {
|
|
239
|
+
authorization: "Bearer line-token-secret",
|
|
240
|
+
"x-ms-client-principal": encodePrincipal(validPrincipal),
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
const headers = functionsCall(calls).init?.headers as Record<string, string>;
|
|
244
|
+
|
|
245
|
+
expect(headers.Authorization).toBe("Bearer line-token-secret");
|
|
246
|
+
expect(decodeForwardedPrincipal(functionsCall(calls))?.userId).toBe("user-123");
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("does not add Bearer forwarding to the SWA-only variant", async () => {
|
|
250
|
+
const code = generateBFFCallFunctionWithSwaAuth();
|
|
251
|
+
const calls = await runGeneratedCall({
|
|
252
|
+
code,
|
|
253
|
+
requestHeaders: {
|
|
254
|
+
authorization: "Bearer must-not-be-forwarded",
|
|
255
|
+
"x-ms-client-principal": encodePrincipal(validPrincipal),
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
const headers = functionsCall(calls).init?.headers as Record<string, string>;
|
|
259
|
+
|
|
260
|
+
expect(headers.Authorization).toBeUndefined();
|
|
261
|
+
expect(code).not.toContain("reqHeaders.get('authorization')");
|
|
262
|
+
expect(decodeForwardedPrincipal(functionsCall(calls))?.userId).toBe("user-123");
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("defaults /.auth/me to http for local Functions development", async () => {
|
|
266
|
+
const calls = await runGeneratedCall({
|
|
267
|
+
code: generateBFFCallFunctionWithSwaAuth(),
|
|
268
|
+
requestHeaders: { cookie: "session=valid", host: "localhost:3000" },
|
|
269
|
+
mePrincipal: validPrincipal,
|
|
270
|
+
functionsBaseUrl: "http://localhost:7071",
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
expect(calls[0].url).toBe("http://localhost:3000/.auth/me");
|
|
274
|
+
});
|
|
275
|
+
});
|
|
@@ -1,7 +1,28 @@
|
|
|
1
|
-
import { validateConfig, loadConfigFromEnv } from "../core/config";
|
|
1
|
+
import { validateConfig, loadConfigFromEnv, normalizeAuthConfig } from "../core/config";
|
|
2
2
|
import { SwallowKitConfig } from "../types";
|
|
3
3
|
|
|
4
4
|
describe("validateConfig", () => {
|
|
5
|
+
it("normalizes a legacy provider as the default scheme", () => {
|
|
6
|
+
const auth = normalizeAuthConfig({ provider: "external-token", authorization: { defaultPolicy: "authenticated" } });
|
|
7
|
+
expect(auth?.schemes.default.provider).toBe("external-token");
|
|
8
|
+
expect(auth?.authorization.defaultPolicy).toBe("authenticated");
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("accepts SWA and external token named schemes", () => {
|
|
12
|
+
const result = validateConfig({ auth: { schemes: { admin: { provider: "swa" }, lineUser: { provider: "external-token" } }, authorization: { defaultPolicy: "anonymous", policies: { adminOnly: { schemes: ["admin"] }, lineUserOnly: { schemes: ["lineUser"] } } } } });
|
|
13
|
+
expect(result).toEqual({ valid: true, errors: [] });
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("rejects undefined scheme references and ambiguous bearer schemes", () => {
|
|
17
|
+
const missing = validateConfig({ auth: { schemes: { admin: { provider: "swa" } }, authorization: { defaultPolicy: "anonymous", policies: { bad: { schemes: ["missing"] } } } } });
|
|
18
|
+
expect(missing.errors.join("\n")).toContain("auth.authorization.policies.bad.schemes");
|
|
19
|
+
const ambiguous = validateConfig({ auth: { schemes: { a: { provider: "external-token" }, b: { provider: "external-token" } }, authorization: { defaultPolicy: "anonymous", policies: { bad: { schemes: ["a", "b"] } } } } });
|
|
20
|
+
expect(ambiguous.errors.join("\n")).toContain("deterministic");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("normalizes public to anonymous", () => {
|
|
24
|
+
expect(normalizeAuthConfig({ provider: "none", authorization: { defaultPolicy: "public" } })?.authorization.defaultPolicy).toBe("anonymous");
|
|
25
|
+
});
|
|
5
26
|
it("returns valid for a complete config", () => {
|
|
6
27
|
const config: SwallowKitConfig = {
|
|
7
28
|
database: { connectionString: "AccountEndpoint=https://..." },
|
|
@@ -13,13 +34,24 @@ describe("validateConfig", () => {
|
|
|
13
34
|
expect(result.errors).toHaveLength(0);
|
|
14
35
|
});
|
|
15
36
|
|
|
16
|
-
it("
|
|
37
|
+
it("accepts an empty database object", () => {
|
|
17
38
|
const config: SwallowKitConfig = {
|
|
18
39
|
database: {},
|
|
19
40
|
backend: { language: "typescript" },
|
|
20
41
|
api: { endpoint: "/api/_swallowkit" },
|
|
21
42
|
};
|
|
22
43
|
const result = validateConfig(config);
|
|
44
|
+
expect(result.valid).toBe(true);
|
|
45
|
+
expect(result.errors).not.toContain("Cosmos DB connection string is required");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("returns error when database settings are incomplete", () => {
|
|
49
|
+
const config: SwallowKitConfig = {
|
|
50
|
+
database: { databaseName: "SwallowKitDB" },
|
|
51
|
+
backend: { language: "typescript" },
|
|
52
|
+
api: { endpoint: "/api/_swallowkit" },
|
|
53
|
+
};
|
|
54
|
+
const result = validateConfig(config);
|
|
23
55
|
expect(result.valid).toBe(false);
|
|
24
56
|
expect(result.errors).toContain("Cosmos DB connection string is required");
|
|
25
57
|
});
|
|
@@ -37,7 +69,7 @@ describe("validateConfig", () => {
|
|
|
37
69
|
|
|
38
70
|
it("returns multiple errors for multiple issues", () => {
|
|
39
71
|
const config: SwallowKitConfig = {
|
|
40
|
-
database: {},
|
|
72
|
+
database: { databaseName: "SwallowKitDB" },
|
|
41
73
|
backend: { language: "typescript" },
|
|
42
74
|
api: { endpoint: "bad-endpoint" },
|
|
43
75
|
};
|
|
@@ -61,6 +93,147 @@ describe("validateConfig", () => {
|
|
|
61
93
|
// database is undefined → no connectionString check triggered
|
|
62
94
|
expect(result.errors.filter((e) => e.includes("endpoint"))).toHaveLength(0);
|
|
63
95
|
});
|
|
96
|
+
|
|
97
|
+
// ─── Connector Validation ───────────────────────────────────
|
|
98
|
+
|
|
99
|
+
it("validates valid RDB connector config", () => {
|
|
100
|
+
const config: SwallowKitConfig = {
|
|
101
|
+
connectors: {
|
|
102
|
+
mysql: {
|
|
103
|
+
type: "rdb",
|
|
104
|
+
provider: "mysql",
|
|
105
|
+
connectionEnvVar: "MYSQL_CONNECTION_STRING",
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
const result = validateConfig(config);
|
|
110
|
+
expect(result.valid).toBe(true);
|
|
111
|
+
expect(result.errors).toHaveLength(0);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("validates valid API connector config", () => {
|
|
115
|
+
const config: SwallowKitConfig = {
|
|
116
|
+
connectors: {
|
|
117
|
+
backlog: {
|
|
118
|
+
type: "api",
|
|
119
|
+
baseUrlEnvVar: "BACKLOG_API_BASE_URL",
|
|
120
|
+
auth: {
|
|
121
|
+
type: "apiKey",
|
|
122
|
+
envVar: "BACKLOG_API_KEY",
|
|
123
|
+
placement: "query",
|
|
124
|
+
paramName: "apiKey",
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
const result = validateConfig(config);
|
|
130
|
+
expect(result.valid).toBe(true);
|
|
131
|
+
expect(result.errors).toHaveLength(0);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("returns error for invalid connector type", () => {
|
|
135
|
+
const config = {
|
|
136
|
+
connectors: {
|
|
137
|
+
bad: {
|
|
138
|
+
type: "graphql",
|
|
139
|
+
baseUrlEnvVar: "GRAPHQL_URL",
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
} as unknown as SwallowKitConfig;
|
|
143
|
+
const result = validateConfig(config);
|
|
144
|
+
expect(result.valid).toBe(false);
|
|
145
|
+
expect(result.errors[0]).toContain("Connector 'bad'");
|
|
146
|
+
expect(result.errors[0]).toContain("type must be one of");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("returns error for RDB connector without provider", () => {
|
|
150
|
+
const config: SwallowKitConfig = {
|
|
151
|
+
connectors: {
|
|
152
|
+
mydb: {
|
|
153
|
+
type: "rdb",
|
|
154
|
+
provider: "" as any,
|
|
155
|
+
connectionEnvVar: "CONN",
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
const result = validateConfig(config);
|
|
160
|
+
expect(result.valid).toBe(false);
|
|
161
|
+
expect(result.errors).toEqual(
|
|
162
|
+
expect.arrayContaining([expect.stringContaining("provider must be one of")])
|
|
163
|
+
);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("returns error for RDB connector without connectionEnvVar", () => {
|
|
167
|
+
const config: SwallowKitConfig = {
|
|
168
|
+
connectors: {
|
|
169
|
+
mydb: {
|
|
170
|
+
type: "rdb",
|
|
171
|
+
provider: "mysql",
|
|
172
|
+
connectionEnvVar: "",
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
const result = validateConfig(config);
|
|
177
|
+
expect(result.valid).toBe(false);
|
|
178
|
+
expect(result.errors).toEqual(
|
|
179
|
+
expect.arrayContaining([expect.stringContaining("connectionEnvVar is required")])
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("returns error for API connector without baseUrlEnvVar", () => {
|
|
184
|
+
const config: SwallowKitConfig = {
|
|
185
|
+
connectors: {
|
|
186
|
+
ext: {
|
|
187
|
+
type: "api",
|
|
188
|
+
baseUrlEnvVar: "",
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
const result = validateConfig(config);
|
|
193
|
+
expect(result.valid).toBe(false);
|
|
194
|
+
expect(result.errors).toEqual(
|
|
195
|
+
expect.arrayContaining([expect.stringContaining("baseUrlEnvVar is required")])
|
|
196
|
+
);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("returns error for API connector with invalid auth type", () => {
|
|
200
|
+
const config: SwallowKitConfig = {
|
|
201
|
+
connectors: {
|
|
202
|
+
ext: {
|
|
203
|
+
type: "api",
|
|
204
|
+
baseUrlEnvVar: "EXT_URL",
|
|
205
|
+
auth: {
|
|
206
|
+
type: "saml" as any,
|
|
207
|
+
envVar: "KEY",
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
const result = validateConfig(config);
|
|
213
|
+
expect(result.valid).toBe(false);
|
|
214
|
+
expect(result.errors).toEqual(
|
|
215
|
+
expect.arrayContaining([expect.stringContaining("auth.type must be one of")])
|
|
216
|
+
);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("validates multiple connectors at once", () => {
|
|
220
|
+
const config: SwallowKitConfig = {
|
|
221
|
+
connectors: {
|
|
222
|
+
mysql: {
|
|
223
|
+
type: "rdb",
|
|
224
|
+
provider: "mysql",
|
|
225
|
+
connectionEnvVar: "MYSQL_CONN",
|
|
226
|
+
},
|
|
227
|
+
backlog: {
|
|
228
|
+
type: "api",
|
|
229
|
+
baseUrlEnvVar: "BACKLOG_URL",
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
const result = validateConfig(config);
|
|
234
|
+
expect(result.valid).toBe(true);
|
|
235
|
+
expect(result.errors).toHaveLength(0);
|
|
236
|
+
});
|
|
64
237
|
});
|
|
65
238
|
|
|
66
239
|
describe("loadConfigFromEnv", () => {
|