@duct-sdk/sdk 0.3.12 → 0.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.
- package/CHANGELOG.md +42 -0
- package/dist/adapters/next/receiver.d.ts +2 -6
- package/dist/adapters/next/receiver.js +20 -66
- package/dist/adapters/remix/receiver.d.ts +2 -6
- package/dist/adapters/remix/receiver.js +16 -33
- package/dist/adapters/sveltekit/receiver.d.ts +2 -6
- package/dist/adapters/sveltekit/receiver.js +13 -40
- package/dist/adapters/vite/receiver.d.ts +2 -4
- package/dist/adapters/vite/receiver.js +13 -30
- package/dist/cli/agents/api-agent.d.ts +3 -1
- package/dist/cli/agents/api-agent.js +22 -11
- package/dist/cli/agents/auth-agent.js +4 -4
- package/dist/cli/agents/pipeline.js +65 -7
- package/dist/cli/agents/route-agent.d.ts +3 -1
- package/dist/cli/agents/route-agent.js +17 -6
- package/dist/cli/detectors/topology.d.ts +1 -1
- package/dist/cli/detectors/topology.js +1 -1
- package/dist/cli/generators/config.js +77 -44
- package/dist/cli/generators/config.test.d.ts +1 -0
- package/dist/cli/generators/config.test.js +219 -0
- package/dist/cli/generators/normalize.d.ts +10 -0
- package/dist/cli/generators/normalize.js +49 -0
- package/dist/cli/generators/receiver.d.ts +2 -1
- package/dist/cli/generators/receiver.js +20 -201
- package/dist/cli/generators/token-route.d.ts +8 -0
- package/dist/cli/generators/token-route.js +235 -0
- package/dist/cli/generators/token-route.test.d.ts +1 -0
- package/dist/cli/generators/token-route.test.js +53 -0
- package/dist/cli/generators/wiring-snippets.d.ts +23 -0
- package/dist/cli/generators/wiring-snippets.js +233 -0
- package/dist/cli/generators/wiring-snippets.test.d.ts +1 -0
- package/dist/cli/generators/wiring-snippets.test.js +66 -0
- package/dist/cli/index.js +41 -1
- package/dist/cli/init.d.ts +1 -7
- package/dist/cli/init.js +100 -53
- package/dist/cli/output-paths.d.ts +10 -0
- package/dist/cli/output-paths.js +60 -0
- package/dist/cli/processors/curl-parser.test.d.ts +1 -0
- package/dist/cli/processors/curl-parser.test.js +24 -0
- package/dist/cli/processors/integration-audit.d.ts +23 -0
- package/dist/cli/processors/integration-audit.js +208 -0
- package/dist/cli/processors/integration-audit.test.d.ts +1 -0
- package/dist/cli/processors/integration-audit.test.js +154 -0
- package/dist/cli/processors/prisma-parser.js +1 -1
- package/dist/cli/processors/verify-discovery.d.ts +33 -0
- package/dist/cli/processors/verify-discovery.js +193 -0
- package/dist/cli/processors/verify-discovery.test.d.ts +1 -0
- package/dist/cli/processors/verify-discovery.test.js +153 -0
- package/dist/cli/processors/zod-extractor.js +1 -1
- package/dist/core/config.d.ts +26 -0
- package/dist/core/config.js +65 -28
- package/dist/core/endpoints.d.ts +2 -0
- package/dist/core/endpoints.js +10 -0
- package/dist/core/login-handoff.d.ts +41 -0
- package/dist/core/login-handoff.js +133 -0
- package/dist/core/login-handoff.test.d.ts +1 -0
- package/dist/core/login-handoff.test.js +31 -0
- package/dist/core/receiver.d.ts +33 -0
- package/dist/core/receiver.js +77 -0
- package/dist/core/types.d.ts +10 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.js +2 -0
- package/package.json +2 -1
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { describe, it } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { validateDuctConfigStrict } from "../../core/config.js";
|
|
4
|
+
import { generateConfigFile } from "./config.js";
|
|
5
|
+
import { generateReceiver, generateTokenRoute } from "./receiver.js";
|
|
6
|
+
import { normalizeAuthSchema, normalizeDuctConfigForCodegen, resolveTokenEndpointUrl, } from "./normalize.js";
|
|
7
|
+
const BASE_URL = "https://app.acme.com";
|
|
8
|
+
function sampleConfig(overrides = {}) {
|
|
9
|
+
return {
|
|
10
|
+
shellId: "shell_acme_prod",
|
|
11
|
+
product: {
|
|
12
|
+
name: "Acme",
|
|
13
|
+
description: "Acme test application",
|
|
14
|
+
baseUrl: BASE_URL,
|
|
15
|
+
apiBaseUrl: "https://api.acme.com",
|
|
16
|
+
},
|
|
17
|
+
auth: {
|
|
18
|
+
strategy: "nextauth",
|
|
19
|
+
sessionFields: ["id", "email"],
|
|
20
|
+
guestAllowed: false,
|
|
21
|
+
tokenExpirySeconds: 300,
|
|
22
|
+
tokenEndpoint: "/api/duct/token",
|
|
23
|
+
},
|
|
24
|
+
routes: [
|
|
25
|
+
{
|
|
26
|
+
path: "/orders/:orderId",
|
|
27
|
+
name: "Order Detail",
|
|
28
|
+
description: "Shows a single order",
|
|
29
|
+
params: [
|
|
30
|
+
{ name: "orderId", type: "string", required: true },
|
|
31
|
+
],
|
|
32
|
+
queryParams: [],
|
|
33
|
+
uiStateHint: "detail",
|
|
34
|
+
requiresAuth: true,
|
|
35
|
+
deeplinkable: true,
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
actions: [
|
|
39
|
+
{
|
|
40
|
+
id: "get_order",
|
|
41
|
+
name: "Get Order",
|
|
42
|
+
description: "Fetch a single order by ID",
|
|
43
|
+
method: "GET",
|
|
44
|
+
path: "/api/orders/:orderId",
|
|
45
|
+
agentAccessible: true,
|
|
46
|
+
sideEffects: false,
|
|
47
|
+
sensitivityLevel: "low",
|
|
48
|
+
params: [],
|
|
49
|
+
requiresAuth: true,
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
deeplinks: [],
|
|
53
|
+
agentPermissions: {
|
|
54
|
+
defaultLevel: "none",
|
|
55
|
+
requireUserConsent: true,
|
|
56
|
+
},
|
|
57
|
+
widget: {
|
|
58
|
+
position: "bottom-right",
|
|
59
|
+
logoUrl: "https://app.acme.com/logo.png",
|
|
60
|
+
},
|
|
61
|
+
intershell_enabled: true,
|
|
62
|
+
registry_visibility: "public",
|
|
63
|
+
...overrides,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
describe("resolveTokenEndpointUrl", () => {
|
|
67
|
+
it("converts a relative path to a full URL", () => {
|
|
68
|
+
assert.equal(resolveTokenEndpointUrl("/api/duct/token", BASE_URL), "https://app.acme.com/api/duct/token");
|
|
69
|
+
});
|
|
70
|
+
it("preserves an already-absolute URL", () => {
|
|
71
|
+
const url = "https://app.acme.com/api/duct/token";
|
|
72
|
+
assert.equal(resolveTokenEndpointUrl(url, BASE_URL), url);
|
|
73
|
+
});
|
|
74
|
+
it("returns undefined for invalid base URLs", () => {
|
|
75
|
+
assert.equal(resolveTokenEndpointUrl("/api/duct/token", "not-a-url"), undefined);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
describe("normalizeDuctConfigForCodegen", () => {
|
|
79
|
+
it("normalizes relative auth.tokenEndpoint before validation", () => {
|
|
80
|
+
const normalized = normalizeDuctConfigForCodegen(sampleConfig());
|
|
81
|
+
assert.equal(normalized.auth.tokenEndpoint, "https://app.acme.com/api/duct/token");
|
|
82
|
+
const validation = validateDuctConfigStrict(normalized);
|
|
83
|
+
assert.equal(validation.ok, true);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
describe("validateDuctConfigStrict", () => {
|
|
87
|
+
it("rejects configs with relative tokenEndpoint before normalization", () => {
|
|
88
|
+
const validation = validateDuctConfigStrict(sampleConfig());
|
|
89
|
+
assert.equal(validation.ok, false);
|
|
90
|
+
if (!validation.ok) {
|
|
91
|
+
assert.ok(validation.issues.some((issue) => issue.area === "auth.tokenEndpoint"));
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
it("accepts normalized configs", () => {
|
|
95
|
+
const validation = validateDuctConfigStrict(normalizeDuctConfigForCodegen(sampleConfig()));
|
|
96
|
+
assert.equal(validation.ok, true);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
describe("generateConfigFile", () => {
|
|
100
|
+
it("emits apiBaseUrl, normalized tokenEndpoint, intershell flags, and widget logoUrl", () => {
|
|
101
|
+
const output = generateConfigFile(sampleConfig());
|
|
102
|
+
assert.match(output, /apiBaseUrl: "https:\/\/api\.acme\.com"/);
|
|
103
|
+
assert.match(output, /tokenEndpoint: "https:\/\/app\.acme\.com\/api\/duct\/token"/);
|
|
104
|
+
assert.match(output, /intershell_enabled: true/);
|
|
105
|
+
assert.match(output, /registry_visibility: "public"/);
|
|
106
|
+
assert.match(output, /logoUrl: "https:\/\/app\.acme\.com\/logo\.png"/);
|
|
107
|
+
});
|
|
108
|
+
it("emits discovery evidence comments and schemas", () => {
|
|
109
|
+
const output = generateConfigFile(sampleConfig({
|
|
110
|
+
routes: [
|
|
111
|
+
{
|
|
112
|
+
path: "/orders/:orderId",
|
|
113
|
+
name: "Order Detail",
|
|
114
|
+
description: "Shows a single order",
|
|
115
|
+
params: [{ name: "orderId", type: "string", required: true }],
|
|
116
|
+
queryParams: [],
|
|
117
|
+
uiStateHint: "detail",
|
|
118
|
+
requiresAuth: true,
|
|
119
|
+
deeplinkable: true,
|
|
120
|
+
confidence: 0.91,
|
|
121
|
+
evidence: ["app/orders/[orderId]/page.tsx:4"],
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
actions: [
|
|
125
|
+
{
|
|
126
|
+
id: "get_order",
|
|
127
|
+
name: "Get Order",
|
|
128
|
+
description: "Fetch a single order by ID",
|
|
129
|
+
method: "GET",
|
|
130
|
+
path: "/api/orders/:orderId",
|
|
131
|
+
agentAccessible: true,
|
|
132
|
+
sideEffects: false,
|
|
133
|
+
sensitivityLevel: "low",
|
|
134
|
+
params: [],
|
|
135
|
+
requiresAuth: true,
|
|
136
|
+
confidence: 0.88,
|
|
137
|
+
evidence: ["app/api/orders/[orderId]/route.ts:12"],
|
|
138
|
+
inputSchema: {
|
|
139
|
+
type: "object",
|
|
140
|
+
properties: { orderId: { type: "string" } },
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
id: "legacy_health",
|
|
145
|
+
name: "Legacy Health",
|
|
146
|
+
description: "Non /api backend route",
|
|
147
|
+
method: "GET",
|
|
148
|
+
path: "/v1/health",
|
|
149
|
+
agentAccessible: true,
|
|
150
|
+
sideEffects: false,
|
|
151
|
+
sensitivityLevel: "none",
|
|
152
|
+
params: [],
|
|
153
|
+
requiresAuth: false,
|
|
154
|
+
confidence: 0.65,
|
|
155
|
+
evidence: ["warning: path does not start with /api/ — verify action proxy target"],
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
}));
|
|
159
|
+
assert.match(output, /confidence 91%/);
|
|
160
|
+
assert.match(output, /app\/orders\/\[orderId\]\/page\.tsx:4/);
|
|
161
|
+
assert.match(output, /inputSchema:/);
|
|
162
|
+
assert.match(output, /path: "\/v1\/health"/);
|
|
163
|
+
assert.match(output, /verify action proxy target/);
|
|
164
|
+
});
|
|
165
|
+
it("uses the Duct docs URL instead of a fabricated company docs domain", () => {
|
|
166
|
+
const output = generateConfigFile(sampleConfig());
|
|
167
|
+
assert.match(output, /Docs: http:\/\/localhost:3005\/docs\/config/);
|
|
168
|
+
assert.doesNotMatch(output, /docs\.acme\.com/);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
describe("generateReceiver", () => {
|
|
172
|
+
it("uses the Duct docs URL instead of a fabricated company docs domain, for every framework", () => {
|
|
173
|
+
const config = normalizeDuctConfigForCodegen(sampleConfig());
|
|
174
|
+
const frameworks = [
|
|
175
|
+
{ name: "next-app", hasAppRouter: true, hasPagesRouter: false },
|
|
176
|
+
{ name: "next-pages", hasAppRouter: false, hasPagesRouter: false },
|
|
177
|
+
{ name: "remix", hasAppRouter: false, hasPagesRouter: false },
|
|
178
|
+
{ name: "vite-react", hasAppRouter: false, hasPagesRouter: false },
|
|
179
|
+
{ name: "sveltekit", hasAppRouter: false, hasPagesRouter: false },
|
|
180
|
+
];
|
|
181
|
+
for (const fw of frameworks) {
|
|
182
|
+
const output = generateReceiver(config, {
|
|
183
|
+
name: fw.name,
|
|
184
|
+
version: "1.0",
|
|
185
|
+
hasAppRouter: fw.hasAppRouter,
|
|
186
|
+
hasPagesRouter: fw.hasPagesRouter,
|
|
187
|
+
packageManager: "npm",
|
|
188
|
+
srcDir: ".",
|
|
189
|
+
});
|
|
190
|
+
assert.doesNotMatch(output, /docs\.your-domain\.com/, `${fw.name} receiver should not fabricate a docs domain`);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
describe("generateTokenRoute", () => {
|
|
195
|
+
it("imports generateDuctToken from @duct-sdk/sdk/server", () => {
|
|
196
|
+
const output = generateTokenRoute(normalizeDuctConfigForCodegen(sampleConfig()), {
|
|
197
|
+
name: "next-app",
|
|
198
|
+
version: "14",
|
|
199
|
+
hasAppRouter: true,
|
|
200
|
+
hasPagesRouter: false,
|
|
201
|
+
packageManager: "npm",
|
|
202
|
+
srcDir: ".",
|
|
203
|
+
});
|
|
204
|
+
assert.match(output, /from "@duct-sdk\/sdk\/server"/);
|
|
205
|
+
assert.doesNotMatch(output, /from "@duct-sdk\/sdk";/);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
describe("normalizeAuthSchema", () => {
|
|
209
|
+
it("omits tokenEndpoint when it cannot be resolved", () => {
|
|
210
|
+
const auth = normalizeAuthSchema({
|
|
211
|
+
strategy: "none",
|
|
212
|
+
sessionFields: [],
|
|
213
|
+
guestAllowed: true,
|
|
214
|
+
tokenExpirySeconds: 300,
|
|
215
|
+
tokenEndpoint: "/api/duct/token",
|
|
216
|
+
}, "not-a-url");
|
|
217
|
+
assert.equal(auth.tokenEndpoint, undefined);
|
|
218
|
+
});
|
|
219
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AuthSchema, DuctConfig } from "../../core/types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Resolve a token endpoint path to a full URL using the product baseUrl.
|
|
4
|
+
* Returns undefined when the value cannot be normalized to a valid URL.
|
|
5
|
+
*/
|
|
6
|
+
export declare function resolveTokenEndpointUrl(tokenEndpoint: string | undefined, baseUrl: string): string | undefined;
|
|
7
|
+
/** Normalize auth fields so generated config passes Zod validation. */
|
|
8
|
+
export declare function normalizeAuthSchema(auth: AuthSchema, baseUrl: string): AuthSchema;
|
|
9
|
+
/** Apply all pre-codegen normalizations to a DuctConfig object. */
|
|
10
|
+
export declare function normalizeDuctConfigForCodegen(config: DuctConfig): DuctConfig;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// ============================================================
|
|
2
|
+
// @duct/sdk — Config normalization for codegen
|
|
3
|
+
// ============================================================
|
|
4
|
+
/**
|
|
5
|
+
* Resolve a token endpoint path to a full URL using the product baseUrl.
|
|
6
|
+
* Returns undefined when the value cannot be normalized to a valid URL.
|
|
7
|
+
*/
|
|
8
|
+
export function resolveTokenEndpointUrl(tokenEndpoint, baseUrl) {
|
|
9
|
+
if (!tokenEndpoint?.trim())
|
|
10
|
+
return undefined;
|
|
11
|
+
const trimmed = tokenEndpoint.trim();
|
|
12
|
+
if (/^https?:\/\//i.test(trimmed)) {
|
|
13
|
+
try {
|
|
14
|
+
return new URL(trimmed).toString();
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
const origin = new URL(baseUrl).origin;
|
|
22
|
+
const pathname = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
23
|
+
return new URL(pathname, `${origin}/`).toString();
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Normalize auth fields so generated config passes Zod validation. */
|
|
30
|
+
export function normalizeAuthSchema(auth, baseUrl) {
|
|
31
|
+
const tokenEndpoint = resolveTokenEndpointUrl(auth.tokenEndpoint, baseUrl);
|
|
32
|
+
const normalized = {
|
|
33
|
+
...auth,
|
|
34
|
+
};
|
|
35
|
+
if (tokenEndpoint) {
|
|
36
|
+
normalized.tokenEndpoint = tokenEndpoint;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
delete normalized.tokenEndpoint;
|
|
40
|
+
}
|
|
41
|
+
return normalized;
|
|
42
|
+
}
|
|
43
|
+
/** Apply all pre-codegen normalizations to a DuctConfig object. */
|
|
44
|
+
export function normalizeDuctConfigForCodegen(config) {
|
|
45
|
+
return {
|
|
46
|
+
...config,
|
|
47
|
+
auth: normalizeAuthSchema(config.auth, config.product.baseUrl),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { DuctConfig } from "../../core/types.js";
|
|
2
2
|
import type { FrameworkInfo } from "../detectors/framework.js";
|
|
3
|
+
import { type TokenRouteOptions } from "./token-route.js";
|
|
3
4
|
/**
|
|
4
5
|
* Generate a framework-specific receiver file.
|
|
5
6
|
*
|
|
@@ -9,4 +10,4 @@ export declare function generateReceiver(config: DuctConfig, framework: Framewor
|
|
|
9
10
|
/**
|
|
10
11
|
* Generate the token API route file (separate from the receiver).
|
|
11
12
|
*/
|
|
12
|
-
export declare function generateTokenRoute(config: DuctConfig, framework: FrameworkInfo): string;
|
|
13
|
+
export declare function generateTokenRoute(config: DuctConfig, framework: FrameworkInfo, tokenOptions?: Partial<TokenRouteOptions>): string;
|
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
// @duct/sdk — Receiver File Generator
|
|
3
3
|
// Generates framework-specific deeplink receiver routes
|
|
4
4
|
// ============================================================
|
|
5
|
+
import { getDocsUrl } from "../../core/endpoints.js";
|
|
6
|
+
import { generateStrategyTokenRoute, } from "./token-route.js";
|
|
5
7
|
// ----------------------------------------------------------------
|
|
6
8
|
// Next.js App Router receiver
|
|
7
9
|
// ----------------------------------------------------------------
|
|
8
10
|
function generateNextAppRouterReceiver(_config) {
|
|
9
|
-
const docsBase =
|
|
11
|
+
const docsBase = getDocsUrl();
|
|
10
12
|
return `/**
|
|
11
13
|
* Duct Deeplink Receiver — auto-generated by \`npx duct init\`
|
|
12
14
|
*
|
|
@@ -20,7 +22,7 @@ function generateNextAppRouterReceiver(_config) {
|
|
|
20
22
|
*/
|
|
21
23
|
|
|
22
24
|
import { NextRequest, NextResponse } from "next/server";
|
|
23
|
-
import { verifyDeeplinkToken } from "@duct-sdk/sdk";
|
|
25
|
+
import { verifyDeeplinkToken } from "@duct-sdk/sdk/server";
|
|
24
26
|
import ductConfig from "../../../../duct.config";
|
|
25
27
|
|
|
26
28
|
// ── JTI replay protection ─────────────────────────────────────
|
|
@@ -180,7 +182,7 @@ function buildRedirectPath(
|
|
|
180
182
|
// Next.js Pages Router receiver
|
|
181
183
|
// ----------------------------------------------------------------
|
|
182
184
|
function generateNextPagesRouterReceiver(_config) {
|
|
183
|
-
const docsBase =
|
|
185
|
+
const docsBase = getDocsUrl();
|
|
184
186
|
return `/**
|
|
185
187
|
* Duct Deeplink Receiver — auto-generated by \`npx duct init\`
|
|
186
188
|
*
|
|
@@ -194,7 +196,7 @@ function generateNextPagesRouterReceiver(_config) {
|
|
|
194
196
|
*/
|
|
195
197
|
|
|
196
198
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
197
|
-
import { verifyDeeplinkToken } from "@duct-sdk/sdk";
|
|
199
|
+
import { verifyDeeplinkToken } from "@duct-sdk/sdk/server";
|
|
198
200
|
import ductConfig from "../../../duct.config";
|
|
199
201
|
|
|
200
202
|
type ReceiveResponse = {
|
|
@@ -333,195 +335,12 @@ function buildRedirectPath(
|
|
|
333
335
|
// ----------------------------------------------------------------
|
|
334
336
|
// Token API route generator
|
|
335
337
|
// ----------------------------------------------------------------
|
|
336
|
-
function generateTokenApiRoute(
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
import type { RequestHandler } from "@sveltejs/kit";
|
|
343
|
-
import { json } from "@sveltejs/kit";
|
|
344
|
-
import { generateDuctToken } from "@duct-sdk/sdk";
|
|
345
|
-
|
|
346
|
-
export const POST: RequestHandler = async ({ locals }) => {
|
|
347
|
-
const secretKey = process.env.DUCT_SECRET_KEY;
|
|
348
|
-
const shellId = process.env.PUBLIC_DUCT_SHELL_ID || process.env.NEXT_PUBLIC_DUCT_SHELL_ID;
|
|
349
|
-
|
|
350
|
-
if (!secretKey || !shellId) {
|
|
351
|
-
return json({ error: "Duct not configured" }, { status: 500 });
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// Replace with your auth integration (locals.user/session from your hooks).
|
|
355
|
-
const userSession = ((locals as Record<string, unknown>).user ?? {}) as Record<string, unknown>;
|
|
356
|
-
|
|
357
|
-
const { token, expiresAt } = await generateDuctToken({
|
|
358
|
-
secretKey,
|
|
359
|
-
shellId,
|
|
360
|
-
userSession,
|
|
361
|
-
expirySeconds: 300,
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
return json({ token, expiresAt: expiresAt.toISOString() });
|
|
365
|
-
};
|
|
366
|
-
`;
|
|
367
|
-
}
|
|
368
|
-
if (framework.name === "remix") {
|
|
369
|
-
return `// app/routes/api.duct.token.ts
|
|
370
|
-
// Generated by @duct/sdk
|
|
371
|
-
// This endpoint generates short-lived Duct tokens for authenticated users.
|
|
372
|
-
|
|
373
|
-
import type { ActionFunctionArgs } from "@remix-run/node";
|
|
374
|
-
import { json } from "@remix-run/node";
|
|
375
|
-
import { generateDuctToken } from "@duct-sdk/sdk";
|
|
376
|
-
|
|
377
|
-
export async function action({ request }: ActionFunctionArgs) {
|
|
378
|
-
if (request.method !== "POST") {
|
|
379
|
-
return json({ error: "Method not allowed" }, { status: 405 });
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
const secretKey = process.env.DUCT_SECRET_KEY;
|
|
383
|
-
const shellId = process.env.VITE_DUCT_SHELL_ID ?? process.env.DUCT_SHELL_ID;
|
|
384
|
-
|
|
385
|
-
if (!secretKey || !shellId) {
|
|
386
|
-
return json({ error: "Duct not configured" }, { status: 500 });
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// Replace with your auth integration.
|
|
390
|
-
// Example: const session = await getSession(request.headers.get("Cookie"));
|
|
391
|
-
// If your app requires auth for shell actions, return 401 when no session.
|
|
392
|
-
const userSession: Record<string, unknown> = {};
|
|
393
|
-
|
|
394
|
-
const { token, expiresAt } = await generateDuctToken({
|
|
395
|
-
secretKey,
|
|
396
|
-
shellId,
|
|
397
|
-
userSession,
|
|
398
|
-
expirySeconds: 300,
|
|
399
|
-
});
|
|
400
|
-
|
|
401
|
-
return json({ token, expiresAt: expiresAt.toISOString() });
|
|
402
|
-
}
|
|
403
|
-
`;
|
|
404
|
-
}
|
|
405
|
-
if (framework.name === "vite-react") {
|
|
406
|
-
return `// server/routes/duct-token.ts
|
|
407
|
-
// Generated by @duct/sdk
|
|
408
|
-
// This endpoint generates short-lived Duct tokens for authenticated users.
|
|
409
|
-
|
|
410
|
-
import { Router, Request, Response } from "express";
|
|
411
|
-
import { generateDuctToken } from "@duct-sdk/sdk";
|
|
412
|
-
|
|
413
|
-
const router = Router();
|
|
414
|
-
|
|
415
|
-
router.post("/api/duct/token", async (req: Request, res: Response) => {
|
|
416
|
-
const secretKey = process.env.DUCT_SECRET_KEY;
|
|
417
|
-
const shellId = process.env.VITE_DUCT_SHELL_ID ?? process.env.DUCT_SHELL_ID;
|
|
418
|
-
|
|
419
|
-
if (!secretKey || !shellId) {
|
|
420
|
-
return res.status(500).json({ error: "Duct not configured" });
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
// Replace with your existing auth/session lookup.
|
|
424
|
-
// Example: const user = req.user; or decode req.cookies/Authorization.
|
|
425
|
-
// If your app requires auth for shell actions, return 401 when no user.
|
|
426
|
-
const userSession = (req as Request & { user?: Record<string, unknown> }).user ?? {};
|
|
427
|
-
|
|
428
|
-
const { token, expiresAt } = await generateDuctToken({
|
|
429
|
-
secretKey,
|
|
430
|
-
shellId,
|
|
431
|
-
userSession,
|
|
432
|
-
expirySeconds: 300,
|
|
433
|
-
});
|
|
434
|
-
|
|
435
|
-
return res.status(200).json({ token, expiresAt: expiresAt.toISOString() });
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
export default router;
|
|
439
|
-
`;
|
|
440
|
-
}
|
|
441
|
-
if (framework.hasAppRouter || framework.name === "next-app") {
|
|
442
|
-
return `// app/api/duct/token/route.ts
|
|
443
|
-
// Generated by @duct/sdk
|
|
444
|
-
// This endpoint generates short-lived Duct tokens for authenticated users.
|
|
445
|
-
// The Duct shell calls this when it needs to identify the current user.
|
|
446
|
-
|
|
447
|
-
import { NextRequest, NextResponse } from "next/server";
|
|
448
|
-
import { generateDuctToken } from "@duct-sdk/sdk";
|
|
449
|
-
import { getServerSession } from "next-auth"; // or your auth library
|
|
450
|
-
// import { auth } from "@/auth"; // NextAuth v5
|
|
451
|
-
// import { currentUser } from "@clerk/nextjs/server"; // Clerk
|
|
452
|
-
|
|
453
|
-
export async function POST(_request: NextRequest) {
|
|
454
|
-
const secretKey = process.env.DUCT_SECRET_KEY;
|
|
455
|
-
const shellId = process.env.NEXT_PUBLIC_DUCT_SHELL_ID;
|
|
456
|
-
|
|
457
|
-
if (!secretKey || !shellId) {
|
|
458
|
-
return NextResponse.json(
|
|
459
|
-
{ error: "Duct not configured" },
|
|
460
|
-
{ status: 500 }
|
|
461
|
-
);
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
// Get the current user session — replace with your auth library
|
|
465
|
-
const session = await getServerSession();
|
|
466
|
-
|
|
467
|
-
// If your app requires auth for the shell, return 401 for unauthenticated users
|
|
468
|
-
// if (!session) {
|
|
469
|
-
// return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
470
|
-
// }
|
|
471
|
-
|
|
472
|
-
const { token, expiresAt } = await generateDuctToken({
|
|
473
|
-
secretKey,
|
|
474
|
-
shellId,
|
|
475
|
-
userSession: session?.user ?? {},
|
|
476
|
-
expirySeconds: 300,
|
|
477
|
-
});
|
|
478
|
-
|
|
479
|
-
return NextResponse.json({ token, expiresAt: expiresAt.toISOString() });
|
|
480
|
-
}
|
|
481
|
-
`;
|
|
482
|
-
}
|
|
483
|
-
return `// pages/api/duct/token.ts
|
|
484
|
-
// Generated by @duct/sdk
|
|
485
|
-
// This endpoint generates short-lived Duct tokens for authenticated users.
|
|
486
|
-
|
|
487
|
-
import type { NextApiRequest, NextApiResponse } from "next";
|
|
488
|
-
import { generateDuctToken } from "@duct-sdk/sdk";
|
|
489
|
-
import { getServerSession } from "next-auth"; // or your auth library
|
|
490
|
-
|
|
491
|
-
type TokenResponse = {
|
|
492
|
-
token?: string;
|
|
493
|
-
expiresAt?: string;
|
|
494
|
-
error?: string;
|
|
495
|
-
};
|
|
496
|
-
|
|
497
|
-
export default async function handler(
|
|
498
|
-
req: NextApiRequest,
|
|
499
|
-
res: NextApiResponse<TokenResponse>
|
|
500
|
-
) {
|
|
501
|
-
if (req.method !== "POST") {
|
|
502
|
-
res.setHeader("Allow", "POST");
|
|
503
|
-
return res.status(405).json({ error: "Method not allowed" });
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
const secretKey = process.env.DUCT_SECRET_KEY;
|
|
507
|
-
const shellId = process.env.NEXT_PUBLIC_DUCT_SHELL_ID;
|
|
508
|
-
|
|
509
|
-
if (!secretKey || !shellId) {
|
|
510
|
-
return res.status(500).json({ error: "Duct not configured" });
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
const session = await getServerSession(req, res, {});
|
|
514
|
-
|
|
515
|
-
const { token, expiresAt } = await generateDuctToken({
|
|
516
|
-
secretKey,
|
|
517
|
-
shellId,
|
|
518
|
-
userSession: (session as { user?: Record<string, unknown> })?.user ?? {},
|
|
519
|
-
expirySeconds: 300,
|
|
520
|
-
});
|
|
521
|
-
|
|
522
|
-
return res.status(200).json({ token, expiresAt: expiresAt.toISOString() });
|
|
523
|
-
}
|
|
524
|
-
`;
|
|
338
|
+
function generateTokenApiRoute(config, framework, tokenOptions) {
|
|
339
|
+
return generateStrategyTokenRoute(framework, {
|
|
340
|
+
authStrategy: tokenOptions?.authStrategy ?? config.auth.strategy,
|
|
341
|
+
guestAllowed: tokenOptions?.guestAllowed ?? config.auth.guestAllowed,
|
|
342
|
+
tokenExpirySeconds: tokenOptions?.tokenExpirySeconds ?? config.auth.tokenExpirySeconds,
|
|
343
|
+
});
|
|
525
344
|
}
|
|
526
345
|
// ----------------------------------------------------------------
|
|
527
346
|
// generateReceiver
|
|
@@ -551,14 +370,14 @@ export function generateReceiver(config, framework) {
|
|
|
551
370
|
/**
|
|
552
371
|
* Generate the token API route file (separate from the receiver).
|
|
553
372
|
*/
|
|
554
|
-
export function generateTokenRoute(config, framework) {
|
|
555
|
-
return generateTokenApiRoute(config, framework);
|
|
373
|
+
export function generateTokenRoute(config, framework, tokenOptions) {
|
|
374
|
+
return generateTokenApiRoute(config, framework, tokenOptions);
|
|
556
375
|
}
|
|
557
376
|
// ----------------------------------------------------------------
|
|
558
377
|
// Remix receiver
|
|
559
378
|
// ----------------------------------------------------------------
|
|
560
379
|
function generateRemixReceiver(_config) {
|
|
561
|
-
const docsBase =
|
|
380
|
+
const docsBase = getDocsUrl();
|
|
562
381
|
return `/**
|
|
563
382
|
* Duct Deeplink Receiver — auto-generated by \`npx duct init\`
|
|
564
383
|
*
|
|
@@ -573,7 +392,7 @@ function generateRemixReceiver(_config) {
|
|
|
573
392
|
|
|
574
393
|
import type { ActionFunctionArgs } from "@remix-run/node";
|
|
575
394
|
import { json } from "@remix-run/node";
|
|
576
|
-
import { verifyDeeplinkToken } from "@duct-sdk/sdk";
|
|
395
|
+
import { verifyDeeplinkToken } from "@duct-sdk/sdk/server";
|
|
577
396
|
import ductConfig from "../../duct.config";
|
|
578
397
|
|
|
579
398
|
export async function action({ request }: ActionFunctionArgs) {
|
|
@@ -632,7 +451,7 @@ export async function action({ request }: ActionFunctionArgs) {
|
|
|
632
451
|
// Express/Vite-React receiver
|
|
633
452
|
// ----------------------------------------------------------------
|
|
634
453
|
function generateExpressReceiver(_config) {
|
|
635
|
-
const docsBase =
|
|
454
|
+
const docsBase = getDocsUrl();
|
|
636
455
|
return `/**
|
|
637
456
|
* Duct Deeplink Receiver — auto-generated by \`npx duct init\`
|
|
638
457
|
*
|
|
@@ -646,7 +465,7 @@ function generateExpressReceiver(_config) {
|
|
|
646
465
|
*/
|
|
647
466
|
|
|
648
467
|
import { Router, Request, Response } from "express";
|
|
649
|
-
import { verifyDeeplinkToken } from "@duct-sdk/sdk";
|
|
468
|
+
import { verifyDeeplinkToken } from "@duct-sdk/sdk/server";
|
|
650
469
|
import ductConfig from "../../duct.config";
|
|
651
470
|
|
|
652
471
|
const router = Router();
|
|
@@ -704,7 +523,7 @@ export default router;
|
|
|
704
523
|
// SvelteKit receiver
|
|
705
524
|
// ----------------------------------------------------------------
|
|
706
525
|
function generateSvelteKitReceiver(_config) {
|
|
707
|
-
const docsBase =
|
|
526
|
+
const docsBase = getDocsUrl();
|
|
708
527
|
return `/**
|
|
709
528
|
* Duct Deeplink Receiver — auto-generated by \`npx duct init\`
|
|
710
529
|
*
|
|
@@ -716,7 +535,7 @@ function generateSvelteKitReceiver(_config) {
|
|
|
716
535
|
|
|
717
536
|
import type { RequestHandler } from "@sveltejs/kit";
|
|
718
537
|
import { json } from "@sveltejs/kit";
|
|
719
|
-
import { verifyDeeplinkToken } from "@duct-sdk/sdk";
|
|
538
|
+
import { verifyDeeplinkToken } from "@duct-sdk/sdk/server";
|
|
720
539
|
import ductConfig from "../../../../../duct.config";
|
|
721
540
|
|
|
722
541
|
export const POST: RequestHandler = async ({ request }) => {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AuthStrategy } from "../../core/types.js";
|
|
2
|
+
import type { FrameworkInfo } from "../detectors/framework.js";
|
|
3
|
+
export interface TokenRouteOptions {
|
|
4
|
+
authStrategy: AuthStrategy;
|
|
5
|
+
guestAllowed: boolean;
|
|
6
|
+
tokenExpirySeconds?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare function generateStrategyTokenRoute(framework: FrameworkInfo, options: TokenRouteOptions): string;
|