@duct-sdk/sdk 0.5.13 → 0.7.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/CHANGELOG.md +61 -0
- package/README.md +418 -264
- package/TOKEN_SPEC.md +243 -243
- package/dist/cli/action-confusion-format.d.ts +17 -0
- package/dist/cli/action-confusion-format.js +24 -0
- package/dist/cli/add.d.ts +7 -0
- package/dist/cli/add.js +152 -0
- package/dist/cli/agent-prompt.d.ts +3 -0
- package/dist/cli/agent-prompt.js +39 -0
- package/dist/cli/api-sources.d.ts +26 -0
- package/dist/cli/api-sources.js +222 -0
- package/dist/cli/cloud.d.ts +162 -0
- package/dist/cli/cloud.js +306 -0
- package/dist/cli/config-loader.d.ts +14 -0
- package/dist/cli/config-loader.js +56 -7
- package/dist/cli/device-login.d.ts +17 -0
- package/dist/cli/device-login.js +75 -0
- package/dist/cli/drift.js +6 -6
- package/dist/cli/generators/any-stack.d.ts +3 -0
- package/dist/cli/generators/any-stack.js +97 -0
- package/dist/cli/generators/config.js +3 -2
- package/dist/cli/generators/receiver.js +7 -7
- package/dist/cli/generators/shell.js +12 -12
- package/dist/cli/generators/token-route.js +245 -245
- package/dist/cli/generators/wiring-snippets.d.ts +0 -14
- package/dist/cli/generators/wiring-snippets.js +188 -305
- package/dist/cli/index.js +396 -611
- package/dist/cli/mcp-core.d.ts +42 -0
- package/dist/cli/mcp-core.js +161 -0
- package/dist/cli/mcp-setup.d.ts +4 -0
- package/dist/cli/mcp-setup.js +352 -0
- package/dist/cli/mcp.d.ts +2 -0
- package/dist/cli/mcp.js +457 -0
- package/dist/cli/migrate-intershell.js +5 -5
- package/dist/cli/policy-defaults.d.ts +37 -0
- package/dist/cli/policy-defaults.js +127 -0
- package/dist/cli/policy-replay-format.d.ts +9 -0
- package/dist/cli/policy-replay-format.js +46 -0
- package/dist/cli/processors/integration-audit.js +2 -2
- package/dist/cli/processors/openapi-parser.d.ts +5 -0
- package/dist/cli/processors/openapi-parser.js +8 -3
- package/dist/cli/project-loader.d.ts +58 -0
- package/dist/cli/project-loader.js +198 -0
- package/dist/cli/project-writer.d.ts +23 -0
- package/dist/cli/project-writer.js +171 -0
- package/dist/cli/push.d.ts +43 -0
- package/dist/cli/push.js +308 -0
- package/dist/cli/safety-checks.d.ts +9 -0
- package/dist/cli/safety-checks.js +29 -0
- package/dist/cli/setup.d.ts +20 -0
- package/dist/cli/setup.js +349 -0
- package/dist/cli/shell-name.d.ts +8 -0
- package/dist/cli/shell-name.js +29 -0
- package/dist/cli/shells.d.ts +22 -0
- package/dist/cli/shells.js +122 -0
- package/dist/cli/split.d.ts +7 -0
- package/dist/cli/split.js +71 -0
- package/dist/cli/sync.d.ts +25 -0
- package/dist/cli/sync.js +120 -0
- package/dist/cli/test-runner.d.ts +21 -0
- package/dist/cli/test-runner.js +281 -0
- package/dist/core/compile.d.ts +43 -0
- package/dist/core/compile.js +512 -0
- package/dist/core/config.d.ts +538 -19
- package/dist/core/config.js +22 -14
- package/dist/core/login-handoff.d.ts +1 -1
- package/dist/core/login-handoff.js +1 -1
- package/dist/core/project.d.ts +1864 -0
- package/dist/core/project.js +291 -0
- package/dist/core/readiness-checks.js +2 -2
- package/dist/core/types.d.ts +10 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/project.d.ts +4 -0
- package/dist/project.js +8 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.js +1 -0
- package/package.json +94 -95
- package/dist/adapters/react/page-context-guard.test.d.ts +0 -1
- package/dist/adapters/react/page-context-guard.test.js +0 -15
- package/dist/cli/agents/api-agent.d.ts +0 -22
- package/dist/cli/agents/api-agent.js +0 -761
- package/dist/cli/agents/auth-agent.d.ts +0 -7
- package/dist/cli/agents/auth-agent.js +0 -212
- package/dist/cli/agents/deeplink-agent.d.ts +0 -6
- package/dist/cli/agents/deeplink-agent.js +0 -285
- package/dist/cli/agents/llm.d.ts +0 -33
- package/dist/cli/agents/llm.js +0 -309
- package/dist/cli/agents/pipeline.d.ts +0 -72
- package/dist/cli/agents/pipeline.js +0 -421
- package/dist/cli/agents/reviewer-agent.d.ts +0 -13
- package/dist/cli/agents/reviewer-agent.js +0 -450
- package/dist/cli/agents/route-agent.d.ts +0 -17
- package/dist/cli/agents/route-agent.js +0 -383
- package/dist/cli/config-loader.test.d.ts +0 -1
- package/dist/cli/config-loader.test.js +0 -63
- package/dist/cli/detectors/topology.d.ts +0 -46
- package/dist/cli/detectors/topology.js +0 -417
- package/dist/cli/generators/config.test.d.ts +0 -1
- package/dist/cli/generators/config.test.js +0 -222
- package/dist/cli/generators/shell.test.d.ts +0 -1
- package/dist/cli/generators/shell.test.js +0 -73
- package/dist/cli/generators/token-route.test.d.ts +0 -1
- package/dist/cli/generators/token-route.test.js +0 -53
- package/dist/cli/generators/wiring-snippets.test.d.ts +0 -1
- package/dist/cli/generators/wiring-snippets.test.js +0 -80
- package/dist/cli/init.d.ts +0 -32
- package/dist/cli/init.js +0 -1336
- package/dist/cli/manifest-diff.test.d.ts +0 -1
- package/dist/cli/manifest-diff.test.js +0 -144
- package/dist/cli/processors/curl-parser.test.d.ts +0 -1
- package/dist/cli/processors/curl-parser.test.js +0 -24
- package/dist/cli/processors/integration-audit.test.d.ts +0 -1
- package/dist/cli/processors/integration-audit.test.js +0 -154
- package/dist/cli/processors/postprocess.d.ts +0 -22
- package/dist/cli/processors/postprocess.js +0 -107
- package/dist/cli/processors/prisma-parser.d.ts +0 -35
- package/dist/cli/processors/prisma-parser.js +0 -117
- package/dist/cli/processors/verify-discovery.d.ts +0 -33
- package/dist/cli/processors/verify-discovery.js +0 -193
- package/dist/cli/processors/verify-discovery.test.d.ts +0 -1
- package/dist/cli/processors/verify-discovery.test.js +0 -153
- package/dist/cli/processors/zod-extractor.d.ts +0 -27
- package/dist/cli/processors/zod-extractor.js +0 -153
- package/dist/cli/prompts/review.d.ts +0 -61
- package/dist/cli/prompts/review.js +0 -410
- package/dist/cli/selectors/express.d.ts +0 -12
- package/dist/cli/selectors/express.js +0 -117
- package/dist/cli/selectors/fastapi.d.ts +0 -12
- package/dist/cli/selectors/fastapi.js +0 -122
- package/dist/cli/selectors/generic.d.ts +0 -10
- package/dist/cli/selectors/generic.js +0 -74
- package/dist/cli/selectors/index.d.ts +0 -37
- package/dist/cli/selectors/index.js +0 -315
- package/dist/cli/selectors/next.d.ts +0 -12
- package/dist/cli/selectors/next.js +0 -130
- package/dist/core/config-policy.test.d.ts +0 -1
- package/dist/core/config-policy.test.js +0 -101
- package/dist/core/config-roundtrip.test.d.ts +0 -1
- package/dist/core/config-roundtrip.test.js +0 -180
- package/dist/core/endpoint-guard.test.d.ts +0 -1
- package/dist/core/endpoint-guard.test.js +0 -57
- package/dist/core/env-diagnostics.test.d.ts +0 -1
- package/dist/core/env-diagnostics.test.js +0 -31
- package/dist/core/evidence-bundle.conformance.test.d.ts +0 -1
- package/dist/core/evidence-bundle.conformance.test.js +0 -110
- package/dist/core/evidence-bundle.test.d.ts +0 -1
- package/dist/core/evidence-bundle.test.js +0 -58
- package/dist/core/invariants.test.d.ts +0 -1
- package/dist/core/invariants.test.js +0 -217
- package/dist/core/login-handoff.test.d.ts +0 -1
- package/dist/core/login-handoff.test.js +0 -31
- package/dist/core/profile.test.d.ts +0 -1
- package/dist/core/profile.test.js +0 -95
- package/dist/core/readiness-checks.test.d.ts +0 -1
- package/dist/core/readiness-checks.test.js +0 -106
- package/dist/core/receipt.test.d.ts +0 -1
- package/dist/core/receipt.test.js +0 -75
- package/dist/core/sdk-entrypoint.test.d.ts +0 -1
- package/dist/core/sdk-entrypoint.test.js +0 -8
- package/dist/core/side-effects-review.test.d.ts +0 -1
- package/dist/core/side-effects-review.test.js +0 -28
- package/dist/core/token.conformance.test.d.ts +0 -1
- package/dist/core/token.conformance.test.js +0 -43
- package/dist/core/token.test.d.ts +0 -1
- package/dist/core/token.test.js +0 -66
|
@@ -35,203 +35,203 @@ export function generateVerifyDuctTokenMiddlewareSnippet(framework, middlewarePa
|
|
|
35
35
|
const target = middlewarePath ?? "your API auth middleware";
|
|
36
36
|
const isExpress = framework.name === "vite-react";
|
|
37
37
|
if (isExpress) {
|
|
38
|
-
return `/**
|
|
39
|
-
* Add to ${target} — verify Duct delegated tokens on proxied action calls.
|
|
40
|
-
* Import from @duct-sdk/sdk/server (not the root @duct-sdk/sdk export).
|
|
41
|
-
*/
|
|
42
|
-
import type { Request, Response, NextFunction } from "express";
|
|
43
|
-
import { verifyDuctToken } from "@duct-sdk/sdk/server";
|
|
44
|
-
|
|
45
|
-
export async function ductDelegatedAuth(req: Request, res: Response, next: NextFunction) {
|
|
46
|
-
const authHeader = req.headers.authorization;
|
|
47
|
-
if (!authHeader?.startsWith("Bearer ")) {
|
|
48
|
-
return next();
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const token = authHeader.slice(7);
|
|
52
|
-
const secretKey = process.env.DUCT_SECRET_KEY;
|
|
53
|
-
const shellId = process.env.DUCT_SHELL_ID;
|
|
54
|
-
if (!secretKey || !shellId) {
|
|
55
|
-
return res.status(500).json({ error: "Duct not configured" });
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const result = await verifyDuctToken(token, { secretKey, expectedShellId: shellId });
|
|
59
|
-
if (!result.valid) {
|
|
60
|
-
return res.status(401).json({ error: result.error ?? "Invalid delegated token" });
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
(req as Request & { user?: Record<string, unknown> }).user = result.payload!.session;
|
|
64
|
-
return next();
|
|
65
|
-
}
|
|
38
|
+
return `/**
|
|
39
|
+
* Add to ${target} — verify Duct delegated tokens on proxied action calls.
|
|
40
|
+
* Import from @duct-sdk/sdk/server (not the root @duct-sdk/sdk export).
|
|
41
|
+
*/
|
|
42
|
+
import type { Request, Response, NextFunction } from "express";
|
|
43
|
+
import { verifyDuctToken } from "@duct-sdk/sdk/server";
|
|
44
|
+
|
|
45
|
+
export async function ductDelegatedAuth(req: Request, res: Response, next: NextFunction) {
|
|
46
|
+
const authHeader = req.headers.authorization;
|
|
47
|
+
if (!authHeader?.startsWith("Bearer ")) {
|
|
48
|
+
return next();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const token = authHeader.slice(7);
|
|
52
|
+
const secretKey = process.env.DUCT_SECRET_KEY;
|
|
53
|
+
const shellId = process.env.DUCT_SHELL_ID;
|
|
54
|
+
if (!secretKey || !shellId) {
|
|
55
|
+
return res.status(500).json({ error: "Duct not configured" });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const result = await verifyDuctToken(token, { secretKey, expectedShellId: shellId });
|
|
59
|
+
if (!result.valid) {
|
|
60
|
+
return res.status(401).json({ error: result.error ?? "Invalid delegated token" });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
(req as Request & { user?: Record<string, unknown> }).user = result.payload!.session;
|
|
64
|
+
return next();
|
|
65
|
+
}
|
|
66
66
|
`;
|
|
67
67
|
}
|
|
68
|
-
return `/**
|
|
69
|
-
* Add to ${target} — fall back to verifyDuctToken when native session auth is absent.
|
|
70
|
-
* The agent proxy sends: Authorization: Bearer <duct_delegated>, X-Duct-Proxy: 1
|
|
71
|
-
*/
|
|
72
|
-
import { verifyDuctToken } from "@duct-sdk/sdk/server";
|
|
73
|
-
|
|
74
|
-
export async function resolveUserFromRequest(request: Request): Promise<Record<string, unknown> | null> {
|
|
75
|
-
// 1) Try your native session/JWT first (NextAuth, Clerk, etc.)
|
|
76
|
-
// const session = await auth();
|
|
77
|
-
// if (session?.user) return session.user as Record<string, unknown>;
|
|
78
|
-
|
|
79
|
-
const authHeader = request.headers.get("authorization");
|
|
80
|
-
if (!authHeader?.startsWith("Bearer ")) {
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const token = authHeader.slice(7);
|
|
85
|
-
const secretKey = process.env.DUCT_SECRET_KEY!;
|
|
86
|
-
const shellId = process.env.DUCT_SHELL_ID ?? process.env.NEXT_PUBLIC_DUCT_SHELL_ID!;
|
|
87
|
-
|
|
88
|
-
const result = await verifyDuctToken(token, { secretKey, expectedShellId: shellId });
|
|
89
|
-
if (!result.valid) {
|
|
90
|
-
throw new Error(result.error ?? "Invalid delegated token");
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return result.payload.session;
|
|
94
|
-
}
|
|
68
|
+
return `/**
|
|
69
|
+
* Add to ${target} — fall back to verifyDuctToken when native session auth is absent.
|
|
70
|
+
* The agent proxy sends: Authorization: Bearer <duct_delegated>, X-Duct-Proxy: 1
|
|
71
|
+
*/
|
|
72
|
+
import { verifyDuctToken } from "@duct-sdk/sdk/server";
|
|
73
|
+
|
|
74
|
+
export async function resolveUserFromRequest(request: Request): Promise<Record<string, unknown> | null> {
|
|
75
|
+
// 1) Try your native session/JWT first (NextAuth, Clerk, etc.)
|
|
76
|
+
// const session = await auth();
|
|
77
|
+
// if (session?.user) return session.user as Record<string, unknown>;
|
|
78
|
+
|
|
79
|
+
const authHeader = request.headers.get("authorization");
|
|
80
|
+
if (!authHeader?.startsWith("Bearer ")) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const token = authHeader.slice(7);
|
|
85
|
+
const secretKey = process.env.DUCT_SECRET_KEY!;
|
|
86
|
+
const shellId = process.env.DUCT_SHELL_ID ?? process.env.NEXT_PUBLIC_DUCT_SHELL_ID!;
|
|
87
|
+
|
|
88
|
+
const result = await verifyDuctToken(token, { secretKey, expectedShellId: shellId });
|
|
89
|
+
if (!result.valid) {
|
|
90
|
+
throw new Error(result.error ?? "Invalid delegated token");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return result.payload.session;
|
|
94
|
+
}
|
|
95
95
|
`;
|
|
96
96
|
}
|
|
97
97
|
export function generateUctVerificationSnippet(framework) {
|
|
98
98
|
const isExpress = framework.name === "vite-react";
|
|
99
99
|
if (isExpress) {
|
|
100
|
-
return `/**
|
|
101
|
-
* Third-party agent user identity — verify Duct consent tokens (uct_).
|
|
102
|
-
* POST \${DUCT_AUTH_URL}/v1/verify/consent-token
|
|
103
|
-
*
|
|
104
|
-
* Identity-only: { token } is enough to resolve user_ref.
|
|
105
|
-
* Policy mode (recommended when Duct proxies the call): also send
|
|
106
|
-
* target_shell + required_action from X-Duct-Shell-ID / X-Duct-Action-ID.
|
|
107
|
-
*
|
|
108
|
-
* On success, req.ductUserRef contains the opaque company user identifier you
|
|
109
|
-
* supplied at consent-request time. Resolve mutable data (email, roles) from
|
|
110
|
-
* YOUR own DB using that id — never trust the JWT payload for live attributes.
|
|
111
|
-
*/
|
|
112
|
-
import type { Request, Response, NextFunction } from "express";
|
|
113
|
-
import { verifyDuctToken } from "@duct-sdk/sdk/server";
|
|
114
|
-
|
|
115
|
-
const DUCT_AUTH_URL = process.env.DUCT_AUTH_URL ?? "http://localhost:3001";
|
|
116
|
-
|
|
117
|
-
interface UctVerifyResult {
|
|
118
|
-
userRef?: string;
|
|
119
|
-
allowedActions?: string[];
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async function verifyUct(
|
|
123
|
-
token: string,
|
|
124
|
-
opts?: { targetShell?: string; requiredAction?: string },
|
|
125
|
-
): Promise<UctVerifyResult | null> {
|
|
126
|
-
const body: Record<string, string> = { token };
|
|
127
|
-
if (opts?.targetShell && opts?.requiredAction) {
|
|
128
|
-
body.target_shell = opts.targetShell;
|
|
129
|
-
body.required_action = opts.requiredAction;
|
|
130
|
-
}
|
|
131
|
-
const res = await fetch(\`\${DUCT_AUTH_URL}/v1/verify/consent-token\`, {
|
|
132
|
-
method: "POST",
|
|
133
|
-
headers: { "Content-Type": "application/json" },
|
|
134
|
-
body: JSON.stringify(body),
|
|
135
|
-
});
|
|
136
|
-
if (!res.ok) return null;
|
|
137
|
-
const parsed = (await res.json()) as { valid?: boolean; user_ref?: string; allowed_actions?: string[] };
|
|
138
|
-
if (!parsed.valid) return null;
|
|
139
|
-
return { userRef: parsed.user_ref, allowedActions: parsed.allowed_actions };
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export async function ductOrConsentAuth(req: Request, res: Response, next: NextFunction) {
|
|
143
|
-
const authHeader = req.headers.authorization;
|
|
144
|
-
if (!authHeader?.startsWith("Bearer ")) return next();
|
|
145
|
-
|
|
146
|
-
const token = authHeader.slice(7);
|
|
147
|
-
if (token.startsWith("uct_")) {
|
|
148
|
-
const shellId = typeof req.headers["x-duct-shell-id"] === "string" ? req.headers["x-duct-shell-id"] : undefined;
|
|
149
|
-
const actionId = typeof req.headers["x-duct-action-id"] === "string" ? req.headers["x-duct-action-id"] : undefined;
|
|
150
|
-
const result = await verifyUct(token, { targetShell: shellId, requiredAction: actionId });
|
|
151
|
-
if (!result) return res.status(401).json({ success: false, message: "Invalid Duct user consent token" });
|
|
152
|
-
(req as Request & { ductUserRef?: string }).ductUserRef = result.userRef;
|
|
153
|
-
return next();
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const secretKey = process.env.DUCT_SECRET_KEY;
|
|
157
|
-
const shellId = process.env.DUCT_SHELL_ID;
|
|
158
|
-
if (!secretKey || !shellId) {
|
|
159
|
-
return res.status(500).json({ error: "Duct not configured" });
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const delegated = await verifyDuctToken(token, { secretKey, expectedShellId: shellId });
|
|
163
|
-
if (!delegated.valid) {
|
|
164
|
-
return res.status(401).json({ error: delegated.error ?? "Invalid delegated token" });
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
(req as Request & { user?: Record<string, unknown> }).user = delegated.payload!.session;
|
|
168
|
-
return next();
|
|
169
|
-
}
|
|
100
|
+
return `/**
|
|
101
|
+
* Third-party agent user identity — verify Duct consent tokens (uct_).
|
|
102
|
+
* POST \${DUCT_AUTH_URL}/v1/verify/consent-token
|
|
103
|
+
*
|
|
104
|
+
* Identity-only: { token } is enough to resolve user_ref.
|
|
105
|
+
* Policy mode (recommended when Duct proxies the call): also send
|
|
106
|
+
* target_shell + required_action from X-Duct-Shell-ID / X-Duct-Action-ID.
|
|
107
|
+
*
|
|
108
|
+
* On success, req.ductUserRef contains the opaque company user identifier you
|
|
109
|
+
* supplied at consent-request time. Resolve mutable data (email, roles) from
|
|
110
|
+
* YOUR own DB using that id — never trust the JWT payload for live attributes.
|
|
111
|
+
*/
|
|
112
|
+
import type { Request, Response, NextFunction } from "express";
|
|
113
|
+
import { verifyDuctToken } from "@duct-sdk/sdk/server";
|
|
114
|
+
|
|
115
|
+
const DUCT_AUTH_URL = process.env.DUCT_AUTH_URL ?? "http://localhost:3001";
|
|
116
|
+
|
|
117
|
+
interface UctVerifyResult {
|
|
118
|
+
userRef?: string;
|
|
119
|
+
allowedActions?: string[];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function verifyUct(
|
|
123
|
+
token: string,
|
|
124
|
+
opts?: { targetShell?: string; requiredAction?: string },
|
|
125
|
+
): Promise<UctVerifyResult | null> {
|
|
126
|
+
const body: Record<string, string> = { token };
|
|
127
|
+
if (opts?.targetShell && opts?.requiredAction) {
|
|
128
|
+
body.target_shell = opts.targetShell;
|
|
129
|
+
body.required_action = opts.requiredAction;
|
|
130
|
+
}
|
|
131
|
+
const res = await fetch(\`\${DUCT_AUTH_URL}/v1/verify/consent-token\`, {
|
|
132
|
+
method: "POST",
|
|
133
|
+
headers: { "Content-Type": "application/json" },
|
|
134
|
+
body: JSON.stringify(body),
|
|
135
|
+
});
|
|
136
|
+
if (!res.ok) return null;
|
|
137
|
+
const parsed = (await res.json()) as { valid?: boolean; user_ref?: string; allowed_actions?: string[] };
|
|
138
|
+
if (!parsed.valid) return null;
|
|
139
|
+
return { userRef: parsed.user_ref, allowedActions: parsed.allowed_actions };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export async function ductOrConsentAuth(req: Request, res: Response, next: NextFunction) {
|
|
143
|
+
const authHeader = req.headers.authorization;
|
|
144
|
+
if (!authHeader?.startsWith("Bearer ")) return next();
|
|
145
|
+
|
|
146
|
+
const token = authHeader.slice(7);
|
|
147
|
+
if (token.startsWith("uct_")) {
|
|
148
|
+
const shellId = typeof req.headers["x-duct-shell-id"] === "string" ? req.headers["x-duct-shell-id"] : undefined;
|
|
149
|
+
const actionId = typeof req.headers["x-duct-action-id"] === "string" ? req.headers["x-duct-action-id"] : undefined;
|
|
150
|
+
const result = await verifyUct(token, { targetShell: shellId, requiredAction: actionId });
|
|
151
|
+
if (!result) return res.status(401).json({ success: false, message: "Invalid Duct user consent token" });
|
|
152
|
+
(req as Request & { ductUserRef?: string }).ductUserRef = result.userRef;
|
|
153
|
+
return next();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const secretKey = process.env.DUCT_SECRET_KEY;
|
|
157
|
+
const shellId = process.env.DUCT_SHELL_ID;
|
|
158
|
+
if (!secretKey || !shellId) {
|
|
159
|
+
return res.status(500).json({ error: "Duct not configured" });
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const delegated = await verifyDuctToken(token, { secretKey, expectedShellId: shellId });
|
|
163
|
+
if (!delegated.valid) {
|
|
164
|
+
return res.status(401).json({ error: delegated.error ?? "Invalid delegated token" });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
(req as Request & { user?: Record<string, unknown> }).user = delegated.payload!.session;
|
|
168
|
+
return next();
|
|
169
|
+
}
|
|
170
170
|
`;
|
|
171
171
|
}
|
|
172
|
-
return `/**
|
|
173
|
-
* Third-party agent user identity — accept uct_ consent tokens OR duct_delegated JWTs.
|
|
174
|
-
* Configure DUCT_AUTH_URL in server env (never NEXT_PUBLIC_* / VITE_*).
|
|
175
|
-
*
|
|
176
|
-
* Identity-only: { token } is enough to resolve user_ref.
|
|
177
|
-
* When Duct proxies the call it also sends X-Duct-Shell-ID + X-Duct-Action-ID —
|
|
178
|
-
* pass those as target_shell / required_action for policy-bound verification.
|
|
179
|
-
*
|
|
180
|
-
* When a uct_ token is present, verify returns user_ref — an opaque handle YOU assigned
|
|
181
|
-
* at identity-resolution time (a Duct-facing alias; not necessarily a storage primary key).
|
|
182
|
-
* Map it to the live user on your server. Do NOT store PII in the token itself.
|
|
183
|
-
*/
|
|
184
|
-
import { verifyDuctToken } from "@duct-sdk/sdk/server";
|
|
185
|
-
|
|
186
|
-
const DUCT_AUTH_URL = process.env.DUCT_AUTH_URL ?? "http://localhost:3001";
|
|
187
|
-
|
|
188
|
-
interface ConsentVerifyResult {
|
|
189
|
-
user_ref?: string;
|
|
190
|
-
allowed_actions?: string[];
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async function verifyConsentToken(
|
|
194
|
-
token: string,
|
|
195
|
-
opts?: { targetShell?: string; requiredAction?: string },
|
|
196
|
-
): Promise<ConsentVerifyResult | null> {
|
|
197
|
-
const body: Record<string, string> = { token };
|
|
198
|
-
if (opts?.targetShell && opts?.requiredAction) {
|
|
199
|
-
body.target_shell = opts.targetShell;
|
|
200
|
-
body.required_action = opts.requiredAction;
|
|
201
|
-
}
|
|
202
|
-
const res = await fetch(\`\${DUCT_AUTH_URL}/v1/verify/consent-token\`, {
|
|
203
|
-
method: "POST",
|
|
204
|
-
headers: { "Content-Type": "application/json" },
|
|
205
|
-
body: JSON.stringify(body),
|
|
206
|
-
});
|
|
207
|
-
if (!res.ok) return null;
|
|
208
|
-
const parsed = (await res.json()) as { valid?: boolean; user_ref?: string; allowed_actions?: string[] };
|
|
209
|
-
if (!parsed.valid) return null;
|
|
210
|
-
return { user_ref: parsed.user_ref, allowed_actions: parsed.allowed_actions };
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export async function resolveAgentAwareUser(
|
|
214
|
-
request: Request,
|
|
215
|
-
): Promise<{ userRef?: string; session?: Record<string, unknown> } | null> {
|
|
216
|
-
const authHeader = request.headers.get("authorization");
|
|
217
|
-
if (!authHeader?.startsWith("Bearer ")) return null;
|
|
218
|
-
|
|
219
|
-
const token = authHeader.slice(7);
|
|
220
|
-
if (token.startsWith("uct_")) {
|
|
221
|
-
const result = await verifyConsentToken(token, {
|
|
222
|
-
targetShell: request.headers.get("x-duct-shell-id") ?? undefined,
|
|
223
|
-
requiredAction: request.headers.get("x-duct-action-id") ?? undefined,
|
|
224
|
-
});
|
|
225
|
-
if (!result) return null;
|
|
226
|
-
// Map user_ref to the live user with your server-side lookup.
|
|
227
|
-
return { userRef: result.user_ref };
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
const secretKey = process.env.DUCT_SECRET_KEY!;
|
|
231
|
-
const shellId = process.env.DUCT_SHELL_ID ?? process.env.NEXT_PUBLIC_DUCT_SHELL_ID!;
|
|
232
|
-
const delegated = await verifyDuctToken(token, { secretKey, expectedShellId: shellId });
|
|
233
|
-
return delegated.valid ? { session: delegated.payload.session as Record<string, unknown> } : null;
|
|
234
|
-
}
|
|
172
|
+
return `/**
|
|
173
|
+
* Third-party agent user identity — accept uct_ consent tokens OR duct_delegated JWTs.
|
|
174
|
+
* Configure DUCT_AUTH_URL in server env (never NEXT_PUBLIC_* / VITE_*).
|
|
175
|
+
*
|
|
176
|
+
* Identity-only: { token } is enough to resolve user_ref.
|
|
177
|
+
* When Duct proxies the call it also sends X-Duct-Shell-ID + X-Duct-Action-ID —
|
|
178
|
+
* pass those as target_shell / required_action for policy-bound verification.
|
|
179
|
+
*
|
|
180
|
+
* When a uct_ token is present, verify returns user_ref — an opaque handle YOU assigned
|
|
181
|
+
* at identity-resolution time (a Duct-facing alias; not necessarily a storage primary key).
|
|
182
|
+
* Map it to the live user on your server. Do NOT store PII in the token itself.
|
|
183
|
+
*/
|
|
184
|
+
import { verifyDuctToken } from "@duct-sdk/sdk/server";
|
|
185
|
+
|
|
186
|
+
const DUCT_AUTH_URL = process.env.DUCT_AUTH_URL ?? "http://localhost:3001";
|
|
187
|
+
|
|
188
|
+
interface ConsentVerifyResult {
|
|
189
|
+
user_ref?: string;
|
|
190
|
+
allowed_actions?: string[];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function verifyConsentToken(
|
|
194
|
+
token: string,
|
|
195
|
+
opts?: { targetShell?: string; requiredAction?: string },
|
|
196
|
+
): Promise<ConsentVerifyResult | null> {
|
|
197
|
+
const body: Record<string, string> = { token };
|
|
198
|
+
if (opts?.targetShell && opts?.requiredAction) {
|
|
199
|
+
body.target_shell = opts.targetShell;
|
|
200
|
+
body.required_action = opts.requiredAction;
|
|
201
|
+
}
|
|
202
|
+
const res = await fetch(\`\${DUCT_AUTH_URL}/v1/verify/consent-token\`, {
|
|
203
|
+
method: "POST",
|
|
204
|
+
headers: { "Content-Type": "application/json" },
|
|
205
|
+
body: JSON.stringify(body),
|
|
206
|
+
});
|
|
207
|
+
if (!res.ok) return null;
|
|
208
|
+
const parsed = (await res.json()) as { valid?: boolean; user_ref?: string; allowed_actions?: string[] };
|
|
209
|
+
if (!parsed.valid) return null;
|
|
210
|
+
return { user_ref: parsed.user_ref, allowed_actions: parsed.allowed_actions };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export async function resolveAgentAwareUser(
|
|
214
|
+
request: Request,
|
|
215
|
+
): Promise<{ userRef?: string; session?: Record<string, unknown> } | null> {
|
|
216
|
+
const authHeader = request.headers.get("authorization");
|
|
217
|
+
if (!authHeader?.startsWith("Bearer ")) return null;
|
|
218
|
+
|
|
219
|
+
const token = authHeader.slice(7);
|
|
220
|
+
if (token.startsWith("uct_")) {
|
|
221
|
+
const result = await verifyConsentToken(token, {
|
|
222
|
+
targetShell: request.headers.get("x-duct-shell-id") ?? undefined,
|
|
223
|
+
requiredAction: request.headers.get("x-duct-action-id") ?? undefined,
|
|
224
|
+
});
|
|
225
|
+
if (!result) return null;
|
|
226
|
+
// Map user_ref to the live user with your server-side lookup.
|
|
227
|
+
return { userRef: result.user_ref };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const secretKey = process.env.DUCT_SECRET_KEY!;
|
|
231
|
+
const shellId = process.env.DUCT_SHELL_ID ?? process.env.NEXT_PUBLIC_DUCT_SHELL_ID!;
|
|
232
|
+
const delegated = await verifyDuctToken(token, { secretKey, expectedShellId: shellId });
|
|
233
|
+
return delegated.valid ? { session: delegated.payload.session as Record<string, unknown> } : null;
|
|
234
|
+
}
|
|
235
235
|
`;
|
|
236
236
|
}
|
|
237
237
|
export function buildWiringPlan(outputPaths, framework, authDetection, thirdPartyAgentMode) {
|
|
@@ -278,20 +278,11 @@ export function buildWiringPlan(outputPaths, framework, authDetection, thirdPart
|
|
|
278
278
|
if (thirdPartyAgentMode === "duct-consent") {
|
|
279
279
|
plan.uctSnippetPath = ".duct/snippets/uct-consent-auth.ts";
|
|
280
280
|
plan.uctSnippet = generateUctVerificationSnippet(framework);
|
|
281
|
-
plan.consentCallbackSnippetPath = ".duct/snippets/duct-consent-callback.ts";
|
|
282
|
-
plan.consentCallbackSnippet = generateConsentCallbackSnippet(framework);
|
|
283
281
|
items.push({
|
|
284
282
|
path: middlewareTarget,
|
|
285
283
|
action: "modify",
|
|
286
284
|
summary: "Verify uct_ consent tokens for third-party agent /message calls",
|
|
287
285
|
});
|
|
288
|
-
items.push({
|
|
289
|
-
path: framework.name === "next-app" || framework.name === "next-pages"
|
|
290
|
-
? "app/api/duct/callback/route.ts"
|
|
291
|
-
: "routes/duct/callback.ts",
|
|
292
|
-
action: "create",
|
|
293
|
-
summary: "Consent callback — receives uct_ + user_ref after human approval, stores for agent",
|
|
294
|
-
});
|
|
295
286
|
}
|
|
296
287
|
if (thirdPartyAgentMode === "oauth") {
|
|
297
288
|
items.push({
|
|
@@ -302,114 +293,6 @@ export function buildWiringPlan(outputPaths, framework, authDetection, thirdPart
|
|
|
302
293
|
}
|
|
303
294
|
return plan;
|
|
304
295
|
}
|
|
305
|
-
/**
|
|
306
|
-
* Scaffold for the consent callback route your app must expose when using
|
|
307
|
-
* the duct-consent third-party agent mode.
|
|
308
|
-
*
|
|
309
|
-
* This route receives the redirect from Duct after a user approves an agent
|
|
310
|
-
* consent request. It stores the uct_ token and user_ref so the agent can
|
|
311
|
-
* retrieve them for subsequent calls.
|
|
312
|
-
*
|
|
313
|
-
* Replace the TODO sections with your own DB / queue / webhook logic.
|
|
314
|
-
*/
|
|
315
|
-
export function generateConsentCallbackSnippet(framework) {
|
|
316
|
-
const isNextJs = framework.name === "next-app" ||
|
|
317
|
-
framework.name === "next-pages";
|
|
318
|
-
if (isNextJs) {
|
|
319
|
-
return `// app/api/duct/callback/route.ts
|
|
320
|
-
// Receives the redirect from Duct after a user approves an agent consent request.
|
|
321
|
-
import { NextRequest, NextResponse } from "next/server";
|
|
322
|
-
|
|
323
|
-
export const dynamic = "force-dynamic";
|
|
324
|
-
|
|
325
|
-
export async function GET(request: NextRequest) {
|
|
326
|
-
const { searchParams } = new URL(request.url);
|
|
327
|
-
|
|
328
|
-
const consentCode = searchParams.get("duct_consent_code");
|
|
329
|
-
const error = searchParams.get("error");
|
|
330
|
-
|
|
331
|
-
if (error) {
|
|
332
|
-
return NextResponse.redirect(new URL("/agent-access-denied", request.url));
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
if (!consentCode) {
|
|
336
|
-
return NextResponse.json({ error: "missing_code" }, { status: 400 });
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
const authUrl = process.env.DUCT_AUTH_URL ?? "http://localhost:3001";
|
|
340
|
-
const redeemResp = await fetch(\`\${authUrl}/v1/consent/redeem\`, {
|
|
341
|
-
method: "POST",
|
|
342
|
-
headers: { "Content-Type": "application/json" },
|
|
343
|
-
body: JSON.stringify({
|
|
344
|
-
shellId: process.env.DUCT_SHELL_ID,
|
|
345
|
-
clientSecret: process.env.DUCT_SECRET_KEY,
|
|
346
|
-
code: consentCode,
|
|
347
|
-
}),
|
|
348
|
-
cache: "no-store",
|
|
349
|
-
});
|
|
350
|
-
const redeemData = (await redeemResp.json()) as {
|
|
351
|
-
user_consent_token?: string;
|
|
352
|
-
user_ref?: string;
|
|
353
|
-
error?: string;
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
if (!redeemResp.ok || !redeemData.user_consent_token) {
|
|
357
|
-
return NextResponse.json({ error: redeemData.error ?? "redeem_failed" }, { status: 400 });
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
// TODO: store { userConsentToken: redeemData.user_consent_token, userRef: redeemData.user_ref }
|
|
361
|
-
// server-side so the agent can retrieve it — never keep uct_ in the browser URL.
|
|
362
|
-
|
|
363
|
-
const clean = new URL("/agent-connected", request.url);
|
|
364
|
-
return NextResponse.redirect(clean);
|
|
365
|
-
}
|
|
366
|
-
`;
|
|
367
|
-
}
|
|
368
|
-
return `// routes/duct/callback.ts (Express)
|
|
369
|
-
// Receives the redirect from Duct after a user approves an agent consent request.
|
|
370
|
-
import { Router, Request, Response } from "express";
|
|
371
|
-
|
|
372
|
-
const router = Router();
|
|
373
|
-
|
|
374
|
-
router.get("/", async (req: Request, res: Response) => {
|
|
375
|
-
const { duct_consent_code: consentCode, error } = req.query as Record<string, string>;
|
|
376
|
-
|
|
377
|
-
if (error) {
|
|
378
|
-
return res.redirect("/agent-access-denied");
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
if (!consentCode) {
|
|
382
|
-
return res.status(400).json({ error: "missing_code" });
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
const authUrl = process.env.DUCT_AUTH_URL ?? "http://localhost:3001";
|
|
386
|
-
const redeemResp = await fetch(\`\${authUrl}/v1/consent/redeem\`, {
|
|
387
|
-
method: "POST",
|
|
388
|
-
headers: { "Content-Type": "application/json" },
|
|
389
|
-
body: JSON.stringify({
|
|
390
|
-
shellId: process.env.DUCT_SHELL_ID,
|
|
391
|
-
clientSecret: process.env.DUCT_SECRET_KEY,
|
|
392
|
-
code: consentCode,
|
|
393
|
-
}),
|
|
394
|
-
});
|
|
395
|
-
const redeemData = (await redeemResp.json()) as {
|
|
396
|
-
user_consent_token?: string;
|
|
397
|
-
user_ref?: string;
|
|
398
|
-
error?: string;
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
if (!redeemResp.ok || !redeemData.user_consent_token) {
|
|
402
|
-
return res.status(400).json({ error: redeemData.error ?? "redeem_failed" });
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
// TODO: store redeemData.user_consent_token + redeemData.user_ref server-side for the agent.
|
|
406
|
-
|
|
407
|
-
return res.redirect("/agent-connected");
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
export default router;
|
|
411
|
-
`;
|
|
412
|
-
}
|
|
413
296
|
export function sideEffectActionsApproved(config) {
|
|
414
297
|
return (config.actions ?? []).some((action) => action.sideEffects);
|
|
415
298
|
}
|