@capybara-hq/kernel 0.1.0-rc.104 → 0.1.0-rc.106
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/agents.schemas.d.ts +9 -9
- package/dist/{schedules/schedules.controller.d.ts → automations/automations.controller.d.ts} +37 -32
- package/dist/{schedules/schedules.controller.js → automations/automations.controller.js} +68 -57
- package/dist/automations/automations.module.d.ts +16 -0
- package/dist/{schedules/schedules.module.js → automations/automations.module.js} +18 -15
- package/dist/{schedules/schedules.schemas.d.ts → automations/automations.schemas.d.ts} +151 -50
- package/dist/automations/automations.schemas.js +253 -0
- package/dist/{schedules/scheduled-tasks.service.d.ts → automations/automations.service.d.ts} +99 -52
- package/dist/{schedules/scheduled-tasks.service.js → automations/automations.service.js} +303 -134
- package/dist/{schedules → automations}/calendar.d.ts +8 -8
- package/dist/{schedules → automations}/calendar.js +2 -2
- package/dist/automations/effective-settings.d.ts +28 -0
- package/dist/automations/effective-settings.js +25 -0
- package/dist/automations/errors.d.ts +19 -0
- package/dist/automations/errors.js +19 -0
- package/dist/automations/events/coalesce.d.ts +28 -0
- package/dist/automations/events/coalesce.js +131 -0
- package/dist/automations/events/deadline.d.ts +10 -0
- package/dist/automations/events/deadline.js +24 -0
- package/dist/automations/events/dispatcher.d.ts +74 -0
- package/dist/automations/events/dispatcher.js +353 -0
- package/dist/automations/events/events.module.d.ts +7 -0
- package/dist/automations/events/events.module.js +26 -0
- package/dist/automations/events/guards.d.ts +32 -0
- package/dist/automations/events/guards.js +87 -0
- package/dist/automations/events/sweep.job.d.ts +49 -0
- package/dist/automations/events/sweep.job.js +202 -0
- package/dist/automations/events/token.d.ts +5 -0
- package/dist/automations/events/token.js +5 -0
- package/dist/automations/events/trigger-events.service.d.ts +47 -0
- package/dist/automations/events/trigger-events.service.js +254 -0
- package/dist/{schedules → automations}/fire.d.ts +57 -17
- package/dist/{schedules → automations}/fire.js +311 -79
- package/dist/{schedules → automations}/grant-rules.d.ts +1 -1
- package/dist/{schedules → automations}/grant-rules.js +5 -5
- package/dist/automations/job-ids.d.ts +113 -0
- package/dist/automations/job-ids.js +117 -0
- package/dist/{schedules → automations}/pause.d.ts +3 -3
- package/dist/{schedules → automations}/pause.js +3 -3
- package/dist/{schedules → automations}/run-tracker.d.ts +21 -10
- package/dist/{schedules → automations}/run-tracker.js +68 -56
- package/dist/{schedules → automations}/scan.job.d.ts +12 -8
- package/dist/{schedules → automations}/scan.job.js +39 -23
- package/dist/{schedules → automations}/schedule-math.d.ts +19 -12
- package/dist/{schedules → automations}/schedule-math.js +17 -7
- package/dist/{schedules → automations}/store.d.ts +57 -27
- package/dist/{schedules → automations}/store.js +102 -52
- package/dist/automations/store.module.d.ts +6 -0
- package/dist/{schedules → automations}/store.module.js +9 -9
- package/dist/automations/types.d.ts +178 -0
- package/dist/{schedules → automations}/unattended.d.ts +22 -11
- package/dist/{schedules → automations}/unattended.js +53 -30
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/channels/channels.module.js +3 -3
- package/dist/channels/outbound.d.ts +5 -5
- package/dist/channels/outbound.js +17 -17
- package/dist/channels/threads.d.ts +1 -1
- package/dist/channels/threads.js +1 -1
- package/dist/config/schema.d.ts +5 -1
- package/dist/config/schema.js +44 -6
- package/dist/conversations/approval-profile.d.ts +2 -2
- package/dist/conversations/approval-profile.js +3 -3
- package/dist/conversations/conversations.controller.d.ts +7 -1
- package/dist/conversations/conversations.controller.js +26 -10
- package/dist/conversations/conversations.service.d.ts +103 -25
- package/dist/conversations/conversations.service.js +138 -33
- package/dist/conversations/events.d.ts +48 -1
- package/dist/conversations/events.js +61 -9
- package/dist/conversations/message-context.d.ts +2 -2
- package/dist/conversations/message-context.js +1 -1
- package/dist/conversations/permission-profile-store.d.ts +2 -1
- package/dist/conversations/permission-profiles.controller.d.ts +3 -0
- package/dist/conversations/permission-profiles.service.js +1 -1
- package/dist/conversations/prompt-suggestion-request.d.ts +1 -1
- package/dist/conversations/prompt-suggestion-request.js +2 -1
- package/dist/conversations/queue.d.ts +7 -4
- package/dist/conversations/queue.js +6 -2
- package/dist/conversations/queue.service.d.ts +5 -5
- package/dist/conversations/queue.service.js +20 -14
- package/dist/ctx/create-ctx.d.ts +20 -1
- package/dist/ctx/create-ctx.js +59 -2
- package/dist/db/kernel-schema.d.ts +104 -25
- package/dist/devices/device-project-files.service.d.ts +88 -0
- package/dist/devices/device-project-files.service.js +249 -0
- package/dist/devices/device-projects.service.d.ts +40 -0
- package/dist/devices/device-projects.service.js +16 -13
- package/dist/devices/devices.controller.d.ts +9 -3
- package/dist/devices/devices.controller.js +27 -5
- package/dist/devices/devices.module.js +9 -1
- package/dist/devices/devices.schemas.js +6 -1
- package/dist/devices/dto.d.ts +15 -0
- package/dist/devices/dto.js +11 -0
- package/dist/devices/project-paths.d.ts +29 -0
- package/dist/devices/project-paths.js +66 -0
- package/dist/devices/project-rules.d.ts +5 -1
- package/dist/devices/project-rules.js +5 -1
- package/dist/errors.d.ts +47 -13
- package/dist/errors.js +47 -13
- package/dist/evals/reconstruct.d.ts +10 -1
- package/dist/evals/reconstruct.js +7 -0
- package/dist/extensions/host.d.ts +6 -1
- package/dist/extensions/host.js +10 -0
- package/dist/extensions/permission-set.d.ts +17 -1
- package/dist/extensions/permission-set.js +13 -1
- package/dist/extensions/permission-view.d.ts +1 -1
- package/dist/extensions/permission-view.js +4 -1
- package/dist/git/gateway.js +2 -2
- package/dist/handoff/app-schemes.d.ts +28 -0
- package/dist/handoff/app-schemes.js +41 -0
- package/dist/handoff/handoff-paths.d.ts +26 -0
- package/dist/handoff/handoff-paths.js +25 -0
- package/dist/handoff/handoff-tickets.d.ts +28 -0
- package/dist/handoff/handoff-tickets.js +111 -0
- package/dist/handoff/handoff.controller.d.ts +24 -0
- package/dist/handoff/handoff.controller.js +74 -0
- package/dist/handoff/handoff.module.d.ts +7 -0
- package/dist/handoff/handoff.module.js +26 -0
- package/dist/handoff/handoff.schemas.d.ts +13 -0
- package/dist/handoff/handoff.schemas.js +3 -0
- package/dist/handoff/handoff.service.d.ts +49 -0
- package/dist/handoff/handoff.service.js +96 -0
- package/dist/http/api.module.js +13 -5
- package/dist/http/meta.controller.d.ts +1 -0
- package/dist/http/meta.controller.js +7 -0
- package/dist/http/request-signal.d.ts +8 -0
- package/dist/http/request-signal.js +14 -0
- package/dist/identity/sso.controller.d.ts +30 -6
- package/dist/identity/sso.controller.js +51 -17
- package/dist/identity/sso.d.ts +10 -2
- package/dist/identity/sso.js +15 -3
- package/dist/inbox/inbox.module.js +2 -2
- package/dist/inbox/inbox.service.d.ts +8 -8
- package/dist/inbox/inbox.service.js +7 -7
- package/dist/infra/config/kernel-config.service.js +10 -5
- package/dist/infra/outbox/registry.d.ts +29 -2
- package/dist/infra/outbox/registry.js +30 -7
- package/dist/kernel.module.js +3 -0
- package/dist/orgs/roles.d.ts +1 -1
- package/dist/orgs/roles.js +3 -3
- package/dist/orgs/settings.controller.d.ts +1 -1
- package/dist/orgs/settings.service.d.ts +27 -11
- package/dist/orgs/settings.service.js +42 -24
- package/dist/overrides.d.ts +4 -4
- package/dist/overrides.js +1 -1
- package/dist/platform/exclusions.js +5 -0
- package/dist/platform/operation.js +2 -2
- package/dist/platform/ops/{schedules.ops.d.ts → automations.ops.d.ts} +4 -4
- package/dist/platform/ops/automations.ops.js +560 -0
- package/dist/platform/ops/connectors.ops.js +4 -2
- package/dist/platform/ops/devices.ops.d.ts +3 -1
- package/dist/platform/ops/devices.ops.js +77 -3
- package/dist/platform/ops/inbox.ops.js +4 -4
- package/dist/platform/ops/orgs.ops.js +1 -1
- package/dist/platform/ops/platform.ops.js +4 -4
- package/dist/platform/platform-ops.module.d.ts +2 -2
- package/dist/platform/platform-ops.module.js +5 -5
- package/dist/platform/route-schemas.js +3 -3
- package/dist/push/jobs.d.ts +3 -3
- package/dist/push/jobs.js +2 -2
- package/dist/push/preferences.service.js +1 -1
- package/dist/push/push-owed.d.ts +3 -3
- package/dist/push/push-owed.js +6 -6
- package/dist/push/push.schemas.d.ts +4 -4
- package/dist/push/push.schemas.js +3 -3
- package/dist/push/recipients.d.ts +4 -4
- package/dist/push/recipients.js +23 -23
- package/dist/push/texts.d.ts +3 -3
- package/dist/push/texts.js +2 -2
- package/dist/push/triggers.d.ts +1 -1
- package/dist/roles/worker.module.js +3 -3
- package/dist/runtime/compaction.service.d.ts +3 -1
- package/dist/runtime/compaction.service.js +3 -1
- package/dist/runtime/context/build.d.ts +6 -0
- package/dist/runtime/context/build.js +14 -3
- package/dist/runtime/context/compaction.d.ts +27 -1
- package/dist/runtime/context/compaction.js +102 -37
- package/dist/runtime/context/run-context.d.ts +38 -0
- package/dist/runtime/context/run-context.js +54 -0
- package/dist/runtime/maintenance.d.ts +8 -4
- package/dist/runtime/maintenance.js +37 -16
- package/dist/runtime/model-call.d.ts +6 -4
- package/dist/runtime/model-call.js +17 -7
- package/dist/runtime/project/knowledge.service.d.ts +16 -7
- package/dist/runtime/project/knowledge.service.js +31 -13
- package/dist/runtime/run-finish.d.ts +4 -4
- package/dist/runtime/run-finish.js +3 -3
- package/dist/runtime/run-queue.js +12 -7
- package/dist/runtime/run-worker.js +7 -1
- package/dist/runtime/runtime.module.js +3 -3
- package/dist/runtime/subagents/dispatch.js +11 -3
- package/dist/sandbox/browse-roots.d.ts +8 -0
- package/dist/sandbox/browse-roots.js +22 -0
- package/dist/sandbox/network-grants.d.ts +4 -4
- package/dist/sandbox/network-grants.js +7 -7
- package/dist/sandbox/operation-authority.d.ts +10 -3
- package/dist/sandbox/operation-authority.js +20 -10
- package/dist/sandbox/operation-intents.d.ts +3 -1
- package/dist/sandbox/operation-intents.js +11 -4
- package/dist/sandbox/permission-call-grants.js +2 -2
- package/dist/sandbox/permission-facts.js +1 -1
- package/dist/sandbox/review/classifier.d.ts +2 -2
- package/dist/sandbox/review/classifier.js +22 -11
- package/dist/sandbox/review/permission-candidate.d.ts +1 -1
- package/dist/sandbox/review/permission-candidate.js +1 -1
- package/dist/sandbox/sandbox.service.d.ts +15 -5
- package/dist/sandbox/sandbox.service.js +14 -8
- package/dist/storage/purge.d.ts +1 -1
- package/dist/storage/purge.js +2 -2
- package/dist/tools/approvals.service.d.ts +6 -6
- package/dist/tools/approvals.service.js +19 -19
- package/dist/tools/builtin-names.d.ts +12 -12
- package/dist/tools/builtin-names.js +29 -29
- package/dist/tools/builtins/{schedules.d.ts → automations.d.ts} +35 -33
- package/dist/tools/builtins/{schedules.js → automations.js} +236 -105
- package/dist/tools/builtins/builtin-tools.d.ts +8 -8
- package/dist/tools/builtins/builtin-tools.js +16 -16
- package/dist/tools/call-scope.d.ts +1 -1
- package/dist/tools/connectors.service.d.ts +4 -13
- package/dist/tools/connectors.service.js +12 -21
- package/dist/tools/git-asks.js +1 -1
- package/dist/tools/git-push-policy.d.ts +3 -3
- package/dist/tools/git-push-policy.js +6 -6
- package/dist/tools/network-asks.d.ts +3 -3
- package/dist/tools/network-asks.js +3 -3
- package/dist/tools/oauth/flow.d.ts +16 -2
- package/dist/tools/oauth/flow.js +23 -4
- package/dist/tools/oauth/oauth.controller.d.ts +19 -7
- package/dist/tools/oauth/oauth.controller.js +42 -12
- package/dist/tools/oauth/oauth.schemas.d.ts +14 -1
- package/dist/tools/oauth/oauth.schemas.js +4 -1
- package/dist/tools/pipeline.d.ts +4 -4
- package/dist/tools/pipeline.js +2 -2
- package/dist/tools/policy.d.ts +3 -3
- package/dist/tools/policy.js +3 -3
- package/dist/tools/tools.module.js +9 -9
- package/package.json +3 -3
- package/src/migrations/0188_automations_rename.sql +309 -0
- package/src/migrations/0189_event_automations.sql +136 -0
- package/dist/platform/ops/schedules.ops.js +0 -509
- package/dist/schedules/effective-settings.d.ts +0 -22
- package/dist/schedules/effective-settings.js +0 -18
- package/dist/schedules/errors.d.ts +0 -8
- package/dist/schedules/errors.js +0 -8
- package/dist/schedules/job-ids.d.ts +0 -81
- package/dist/schedules/job-ids.js +0 -95
- package/dist/schedules/schedules.module.d.ts +0 -16
- package/dist/schedules/schedules.schemas.js +0 -213
- package/dist/schedules/store.module.d.ts +0 -6
- package/dist/schedules/types.d.ts +0 -110
- /package/dist/{schedules → automations}/types.js +0 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ApiError } from "../errors.js";
|
|
3
|
+
// The URL schemes of the native shells a browser handoff may send the person back to: pure, no
|
|
4
|
+
// Nest (shell external authorization spec §1, §3).
|
|
5
|
+
/** A plain URL scheme (RFC 3986 letters, lowercase only so the match stays exact). */
|
|
6
|
+
export const APP_SCHEME_RE = /^[a-z][a-z0-9+.-]{0,31}$/;
|
|
7
|
+
/** The app sign-in redirect URI a shell registers under its scheme (desktop client spec §6.5). */
|
|
8
|
+
const APP_CALLBACK_SUFFIX = "://auth/callback";
|
|
9
|
+
/**
|
|
10
|
+
* The schemes `s` for which an entry of `auth.appRedirectUris` is exactly `${s}://auth/callback`,
|
|
11
|
+
* in list order; `[]` without `http.publicUrl` (no app sign-in, so no shell to return to).
|
|
12
|
+
*/
|
|
13
|
+
export function appSchemes(config) {
|
|
14
|
+
if (!config.http.publicUrl)
|
|
15
|
+
return [];
|
|
16
|
+
const out = [];
|
|
17
|
+
for (const uri of config.auth.appRedirectUris) {
|
|
18
|
+
if (!uri.endsWith(APP_CALLBACK_SUFFIX))
|
|
19
|
+
continue;
|
|
20
|
+
const scheme = uri.slice(0, -APP_CALLBACK_SUFFIX.length);
|
|
21
|
+
if (APP_SCHEME_RE.test(scheme) && !out.includes(scheme))
|
|
22
|
+
out.push(scheme);
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
/** A handoff start's return scheme: none passes; otherwise it must be one of {@link appSchemes}. */
|
|
27
|
+
export function assertReturnSchemeAllowed(config, scheme) {
|
|
28
|
+
if (scheme === undefined)
|
|
29
|
+
return;
|
|
30
|
+
if (!appSchemes(config).includes(scheme)) {
|
|
31
|
+
throw new ApiError("handoff.scheme_not_allowed", "This platform does not allow this app to finish authorizations in the browser (its <scheme>://auth/callback is not in auth.appRedirectUris)");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The `handoff` field of the connector connect and the login-method link start: present when a
|
|
36
|
+
* native shell asks to finish the flow in the system browser. `returnScheme` is the shell's own
|
|
37
|
+
* URL scheme (for the done page's way back into the app).
|
|
38
|
+
*/
|
|
39
|
+
export const HandoffRequestSchema = z
|
|
40
|
+
.object({ returnScheme: z.string().min(1).max(64).optional() })
|
|
41
|
+
.strict();
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** What a handoff ticket holds (Redis, under the ticket's hash). */
|
|
2
|
+
export type HandoffRecord = {
|
|
3
|
+
kind: "connector";
|
|
4
|
+
accountId: string;
|
|
5
|
+
orgId: string;
|
|
6
|
+
connectorId: string;
|
|
7
|
+
/** The site path the shell started from (already checked as a site path). */
|
|
8
|
+
returnTo: string;
|
|
9
|
+
returnScheme?: string;
|
|
10
|
+
} | {
|
|
11
|
+
kind: "identity-link";
|
|
12
|
+
accountId: string;
|
|
13
|
+
plugin: string;
|
|
14
|
+
returnTo: string;
|
|
15
|
+
returnScheme?: string;
|
|
16
|
+
};
|
|
17
|
+
/** The web page that redeems a ticket: `<publicUrl>/handoff/<ticket>`. */
|
|
18
|
+
export declare const HANDOFF_PAGE_PATH = "/handoff";
|
|
19
|
+
/** The web page a handoff flow's callback returns to (next-intl adds the language prefix). */
|
|
20
|
+
export declare const HANDOFF_DONE_PATH = "/handoff/done";
|
|
21
|
+
/**
|
|
22
|
+
* The done page path for a handoff flow, used as the OAuth / SSO state's return page: what was
|
|
23
|
+
* authorized, the page the shell started from and its scheme (when it gave one). The callbacks
|
|
24
|
+
* append their result parameters (`connected` / `oauth_error`, `error`) as for any return page.
|
|
25
|
+
*/
|
|
26
|
+
export declare function handoffDonePath(rec: HandoffRecord): string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// The browser handoff's web paths and the record a ticket holds: a leaf module (no service
|
|
2
|
+
// imports), so both the OAuth connect flow and the SSO flow can build the done page path.
|
|
3
|
+
/** The web page that redeems a ticket: `<publicUrl>/handoff/<ticket>`. */
|
|
4
|
+
export const HANDOFF_PAGE_PATH = "/handoff";
|
|
5
|
+
/** The web page a handoff flow's callback returns to (next-intl adds the language prefix). */
|
|
6
|
+
export const HANDOFF_DONE_PATH = "/handoff/done";
|
|
7
|
+
/**
|
|
8
|
+
* The done page path for a handoff flow, used as the OAuth / SSO state's return page: what was
|
|
9
|
+
* authorized, the page the shell started from and its scheme (when it gave one). The callbacks
|
|
10
|
+
* append their result parameters (`connected` / `oauth_error`, `error`) as for any return page.
|
|
11
|
+
*/
|
|
12
|
+
export function handoffDonePath(rec) {
|
|
13
|
+
const q = new URLSearchParams({ kind: rec.kind });
|
|
14
|
+
if (rec.kind === "connector") {
|
|
15
|
+
q.set("org", rec.orgId);
|
|
16
|
+
q.set("connector", rec.connectorId);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
q.set("plugin", rec.plugin);
|
|
20
|
+
}
|
|
21
|
+
q.set("returnTo", rec.returnTo);
|
|
22
|
+
if (rec.returnScheme !== undefined)
|
|
23
|
+
q.set("scheme", rec.returnScheme);
|
|
24
|
+
return `${HANDOFF_DONE_PATH}?${q.toString()}`;
|
|
25
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Redis } from "ioredis";
|
|
2
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
3
|
+
import { type HandoffRecord } from "./handoff-paths.js";
|
|
4
|
+
export { HANDOFF_DONE_PATH, type HandoffRecord, handoffDonePath } from "./handoff-paths.js";
|
|
5
|
+
/** A ticket's lifetime (seconds): the same as the OAuth state and the app sign-in attempt. */
|
|
6
|
+
export declare const HANDOFF_TTL_SECONDS = 600;
|
|
7
|
+
/**
|
|
8
|
+
* Browser handoff tickets (shell external authorization spec §1): a native shell's start stores
|
|
9
|
+
* what it checked and gets a single-use ticket for the web page `/handoff/<ticket>`, which the
|
|
10
|
+
* person opens in the system browser. 32 random bytes, stored hashed in Redis for 10 minutes,
|
|
11
|
+
* taken with GETDEL (one redeem wins across replicas). Nothing lives in process memory.
|
|
12
|
+
*/
|
|
13
|
+
export declare class HandoffTickets {
|
|
14
|
+
private readonly redis;
|
|
15
|
+
private readonly config;
|
|
16
|
+
constructor(redis: Pick<Redis, "set" | "getdel">, config: Pick<KernelConfigService, "get">);
|
|
17
|
+
/** Stores the record and returns its ticket. */
|
|
18
|
+
create(rec: HandoffRecord): Promise<string>;
|
|
19
|
+
/**
|
|
20
|
+
* Takes the ticket's record and deletes it in one step; null when the ticket is malformed,
|
|
21
|
+
* unknown or used, or the stored value is not a handoff record.
|
|
22
|
+
*/
|
|
23
|
+
take(ticket: string): Promise<HandoffRecord | null>;
|
|
24
|
+
/** The web page that redeems the ticket; needs `http.publicUrl`. */
|
|
25
|
+
url(ticket: string): string;
|
|
26
|
+
}
|
|
27
|
+
export declare class HandoffTicketsModule {
|
|
28
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
11
|
+
import { Inject, Injectable, Module } from "@nestjs/common";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
14
|
+
import { REDIS } from "../infra/tokens.js";
|
|
15
|
+
import { isSafeReturnPath, publicUrlRequired } from "../tools/oauth/flow-paths.js";
|
|
16
|
+
import { APP_SCHEME_RE } from "./app-schemes.js";
|
|
17
|
+
import { HANDOFF_PAGE_PATH } from "./handoff-paths.js";
|
|
18
|
+
export { HANDOFF_DONE_PATH, handoffDonePath } from "./handoff-paths.js";
|
|
19
|
+
/** A ticket's lifetime (seconds): the same as the OAuth state and the app sign-in attempt. */
|
|
20
|
+
export const HANDOFF_TTL_SECONDS = 600;
|
|
21
|
+
/** The `cap_` prefix keeps the ticket masked in logged paths and queries. */
|
|
22
|
+
const TICKET_PREFIX = "cap_ho_";
|
|
23
|
+
const TICKET_RE = /^cap_ho_[A-Za-z0-9_-]{43}$/;
|
|
24
|
+
/**
|
|
25
|
+
* What a stored record must look like to be redeemed: a value that does not parse (another
|
|
26
|
+
* writer, a format from another version) is treated as no ticket, never trusted as written.
|
|
27
|
+
*/
|
|
28
|
+
const ReturnTo = z.string().refine(isSafeReturnPath);
|
|
29
|
+
const ReturnScheme = z.string().regex(APP_SCHEME_RE).optional();
|
|
30
|
+
const HandoffRecordSchema = z.discriminatedUnion("kind", [
|
|
31
|
+
z.object({
|
|
32
|
+
kind: z.literal("connector"),
|
|
33
|
+
accountId: z.string().min(1),
|
|
34
|
+
orgId: z.string().min(1),
|
|
35
|
+
connectorId: z.string().min(1),
|
|
36
|
+
returnTo: ReturnTo,
|
|
37
|
+
returnScheme: ReturnScheme,
|
|
38
|
+
}),
|
|
39
|
+
z.object({
|
|
40
|
+
kind: z.literal("identity-link"),
|
|
41
|
+
accountId: z.string().min(1),
|
|
42
|
+
plugin: z.string().min(1),
|
|
43
|
+
returnTo: ReturnTo,
|
|
44
|
+
returnScheme: ReturnScheme,
|
|
45
|
+
}),
|
|
46
|
+
]);
|
|
47
|
+
const sha256 = (s) => createHash("sha256").update(s).digest("base64url");
|
|
48
|
+
/** Redis holds only the ticket's hash: reading Redis gives no usable ticket. */
|
|
49
|
+
const ticketKey = (ticket) => `capk:handoff:${sha256(ticket)}`;
|
|
50
|
+
/**
|
|
51
|
+
* Browser handoff tickets (shell external authorization spec §1): a native shell's start stores
|
|
52
|
+
* what it checked and gets a single-use ticket for the web page `/handoff/<ticket>`, which the
|
|
53
|
+
* person opens in the system browser. 32 random bytes, stored hashed in Redis for 10 minutes,
|
|
54
|
+
* taken with GETDEL (one redeem wins across replicas). Nothing lives in process memory.
|
|
55
|
+
*/
|
|
56
|
+
let HandoffTickets = class HandoffTickets {
|
|
57
|
+
redis;
|
|
58
|
+
config;
|
|
59
|
+
constructor(redis, config) {
|
|
60
|
+
this.redis = redis;
|
|
61
|
+
this.config = config;
|
|
62
|
+
}
|
|
63
|
+
/** Stores the record and returns its ticket. */
|
|
64
|
+
async create(rec) {
|
|
65
|
+
const ticket = `${TICKET_PREFIX}${randomBytes(32).toString("base64url")}`;
|
|
66
|
+
await this.redis.set(ticketKey(ticket), JSON.stringify(rec), "EX", HANDOFF_TTL_SECONDS);
|
|
67
|
+
return ticket;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Takes the ticket's record and deletes it in one step; null when the ticket is malformed,
|
|
71
|
+
* unknown or used, or the stored value is not a handoff record.
|
|
72
|
+
*/
|
|
73
|
+
async take(ticket) {
|
|
74
|
+
if (!TICKET_RE.test(ticket))
|
|
75
|
+
return null;
|
|
76
|
+
const raw = await this.redis.getdel(ticketKey(ticket));
|
|
77
|
+
if (raw === null)
|
|
78
|
+
return null;
|
|
79
|
+
let value;
|
|
80
|
+
try {
|
|
81
|
+
value = JSON.parse(raw);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
const parsed = HandoffRecordSchema.safeParse(value);
|
|
87
|
+
if (!parsed.success)
|
|
88
|
+
return null;
|
|
89
|
+
const { returnScheme, ...rest } = parsed.data;
|
|
90
|
+
return { ...rest, ...(returnScheme !== undefined && { returnScheme }) };
|
|
91
|
+
}
|
|
92
|
+
/** The web page that redeems the ticket; needs `http.publicUrl`. */
|
|
93
|
+
url(ticket) {
|
|
94
|
+
const publicUrl = this.config.get().http.publicUrl;
|
|
95
|
+
if (!publicUrl)
|
|
96
|
+
throw publicUrlRequired();
|
|
97
|
+
return `${publicUrl.replace(/\/+$/, "")}${HANDOFF_PAGE_PATH}/${ticket}`;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
HandoffTickets = __decorate([
|
|
101
|
+
Injectable(),
|
|
102
|
+
__param(0, Inject(REDIS)),
|
|
103
|
+
__param(1, Inject(KernelConfigService))
|
|
104
|
+
], HandoffTickets);
|
|
105
|
+
export { HandoffTickets };
|
|
106
|
+
let HandoffTicketsModule = class HandoffTicketsModule {
|
|
107
|
+
};
|
|
108
|
+
HandoffTicketsModule = __decorate([
|
|
109
|
+
Module({ providers: [HandoffTickets], exports: [HandoffTickets] })
|
|
110
|
+
], HandoffTicketsModule);
|
|
111
|
+
export { HandoffTicketsModule };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { FastifyReply, FastifyRequest } from "fastify";
|
|
2
|
+
import { type Principal } from "../identity/principal.js";
|
|
3
|
+
import { SsoFlow } from "../identity/sso.js";
|
|
4
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
5
|
+
import { type HandoffRedeemBody, type HandoffRedeemResponse } from "./handoff.schemas.js";
|
|
6
|
+
import { HandoffService } from "./handoff.service.js";
|
|
7
|
+
/**
|
|
8
|
+
* Browser handoffs (shell external authorization spec §2): the web page `/handoff/<ticket>`,
|
|
9
|
+
* opened by a native shell in the system browser, redeems its ticket here.
|
|
10
|
+
*/
|
|
11
|
+
export declare class HandoffController {
|
|
12
|
+
private readonly handoffs;
|
|
13
|
+
private readonly config;
|
|
14
|
+
private readonly sso;
|
|
15
|
+
constructor(handoffs: HandoffService, config: KernelConfigService, sso: SsoFlow);
|
|
16
|
+
/**
|
|
17
|
+
* Redeems a ticket in this signed-in browser (web session only, so the CSRF header of the global
|
|
18
|
+
* guard applies): starts the ticket's OAuth connect or login-method link here, writes this
|
|
19
|
+
* browser's binding cookie (`cap_oauth` / `cap_sso`, the same as the normal starts) and answers
|
|
20
|
+
* where the browser goes next. Errors: `handoff.invalid`, `handoff.account_mismatch`, and for a
|
|
21
|
+
* connector the start's own (`org.not_found`, `forbidden`, `connector.*`).
|
|
22
|
+
*/
|
|
23
|
+
redeem(principal: Principal, body: HandoffRedeemBody, req: FastifyRequest, reply: FastifyReply): Promise<HandoffRedeemResponse>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { Controller, HttpCode, Inject, Post, Req, Res } from "@nestjs/common";
|
|
11
|
+
import { CurrentPrincipal, requireUser, SessionOnly, } from "../identity/principal.js";
|
|
12
|
+
import { setSsoBindingCookie } from "../identity/sso.controller.js";
|
|
13
|
+
import { SSO_BINDING_COOKIE, SsoFlow } from "../identity/sso.js";
|
|
14
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
15
|
+
import { createZodDto } from "../infra/validation/create-zod-dto.js";
|
|
16
|
+
import { ZBody } from "../infra/validation/zod-params.js";
|
|
17
|
+
import { OAUTH_BINDING_COOKIE } from "../tools/oauth/flow.js";
|
|
18
|
+
import { setOAuthBindingCookie } from "../tools/oauth/oauth.controller.js";
|
|
19
|
+
import { HandoffRedeemBodySchema, } from "./handoff.schemas.js";
|
|
20
|
+
import { HandoffService } from "./handoff.service.js";
|
|
21
|
+
class RedeemBody extends createZodDto(HandoffRedeemBodySchema, "HandoffRedeemBody") {
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Browser handoffs (shell external authorization spec §2): the web page `/handoff/<ticket>`,
|
|
25
|
+
* opened by a native shell in the system browser, redeems its ticket here.
|
|
26
|
+
*/
|
|
27
|
+
let HandoffController = class HandoffController {
|
|
28
|
+
handoffs;
|
|
29
|
+
config;
|
|
30
|
+
sso;
|
|
31
|
+
constructor(handoffs, config, sso) {
|
|
32
|
+
this.handoffs = handoffs;
|
|
33
|
+
this.config = config;
|
|
34
|
+
this.sso = sso;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Redeems a ticket in this signed-in browser (web session only, so the CSRF header of the global
|
|
38
|
+
* guard applies): starts the ticket's OAuth connect or login-method link here, writes this
|
|
39
|
+
* browser's binding cookie (`cap_oauth` / `cap_sso`, the same as the normal starts) and answers
|
|
40
|
+
* where the browser goes next. Errors: `handoff.invalid`, `handoff.account_mismatch`, and for a
|
|
41
|
+
* connector the start's own (`org.not_found`, `forbidden`, `connector.*`).
|
|
42
|
+
*/
|
|
43
|
+
async redeem(principal, body, req, reply) {
|
|
44
|
+
const p = requireUser(principal);
|
|
45
|
+
reply.header("cache-control", "no-store");
|
|
46
|
+
const out = await this.handoffs.redeem(body.ticket, p, {
|
|
47
|
+
oauth: req.cookies[OAUTH_BINDING_COOKIE],
|
|
48
|
+
sso: req.cookies[SSO_BINDING_COOKIE],
|
|
49
|
+
});
|
|
50
|
+
if (out.binding.flow === "oauth") {
|
|
51
|
+
setOAuthBindingCookie(reply, out.binding.value, this.config.get());
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
setSsoBindingCookie(reply, out.binding.value, this.sso.cookieSecure());
|
|
55
|
+
}
|
|
56
|
+
return { location: out.location };
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
__decorate([
|
|
60
|
+
Post("redeem"),
|
|
61
|
+
SessionOnly(),
|
|
62
|
+
HttpCode(200),
|
|
63
|
+
__param(0, CurrentPrincipal()),
|
|
64
|
+
__param(1, ZBody(RedeemBody)),
|
|
65
|
+
__param(2, Req()),
|
|
66
|
+
__param(3, Res({ passthrough: true }))
|
|
67
|
+
], HandoffController.prototype, "redeem", null);
|
|
68
|
+
HandoffController = __decorate([
|
|
69
|
+
Controller("api/v1/auth/handoffs"),
|
|
70
|
+
__param(0, Inject(HandoffService)),
|
|
71
|
+
__param(1, Inject(KernelConfigService)),
|
|
72
|
+
__param(2, Inject(SsoFlow))
|
|
73
|
+
], HandoffController);
|
|
74
|
+
export { HandoffController };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser handoffs (shell external authorization spec): the redeem that starts a ticket's OAuth
|
|
3
|
+
* connect (`ToolsModule`'s `OAuthFlow`) or login-method link (`IdentityModule`'s `SsoFlow`) in the
|
|
4
|
+
* person's signed-in browser. The tickets themselves are `HandoffTicketsModule`'s.
|
|
5
|
+
*/
|
|
6
|
+
export declare class HandoffModule {
|
|
7
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { Module } from "@nestjs/common";
|
|
8
|
+
import { IdentityModule } from "../identity/identity.module.js";
|
|
9
|
+
import { ToolsModule } from "../tools/tools.module.js";
|
|
10
|
+
import { HandoffService } from "./handoff.service.js";
|
|
11
|
+
import { HandoffTicketsModule } from "./handoff-tickets.js";
|
|
12
|
+
/**
|
|
13
|
+
* Browser handoffs (shell external authorization spec): the redeem that starts a ticket's OAuth
|
|
14
|
+
* connect (`ToolsModule`'s `OAuthFlow`) or login-method link (`IdentityModule`'s `SsoFlow`) in the
|
|
15
|
+
* person's signed-in browser. The tickets themselves are `HandoffTicketsModule`'s.
|
|
16
|
+
*/
|
|
17
|
+
let HandoffModule = class HandoffModule {
|
|
18
|
+
};
|
|
19
|
+
HandoffModule = __decorate([
|
|
20
|
+
Module({
|
|
21
|
+
imports: [IdentityModule, ToolsModule, HandoffTicketsModule],
|
|
22
|
+
providers: [HandoffService],
|
|
23
|
+
exports: [HandoffService],
|
|
24
|
+
})
|
|
25
|
+
], HandoffModule);
|
|
26
|
+
export { HandoffModule };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/** `POST /api/v1/auth/handoffs/redeem`: the ticket from the web page's path `/handoff/<ticket>`. */
|
|
3
|
+
export declare const HandoffRedeemBodySchema: z.ZodObject<{
|
|
4
|
+
ticket: z.ZodString;
|
|
5
|
+
}, z.core.$strict>;
|
|
6
|
+
export type HandoffRedeemBody = z.infer<typeof HandoffRedeemBodySchema>;
|
|
7
|
+
/**
|
|
8
|
+
* The redeem's answer: where this browser goes next (the authorization server's or identity
|
|
9
|
+
* source's authorize URL, or the handoff done page with `?error=<code>` when a link cannot start).
|
|
10
|
+
*/
|
|
11
|
+
export type HandoffRedeemResponse = {
|
|
12
|
+
location: string;
|
|
13
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { AccountsService } from "../identity/accounts.js";
|
|
2
|
+
import type { UserPrincipal } from "../identity/principal.js";
|
|
3
|
+
import { SsoFlow } from "../identity/sso.js";
|
|
4
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
5
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
6
|
+
import { OAuthFlow } from "../tools/oauth/flow.js";
|
|
7
|
+
import { HandoffTickets } from "./handoff-tickets.js";
|
|
8
|
+
/** The binding cookie values this browser already holds (either may be missing or invalid). */
|
|
9
|
+
export interface HandoffBrowserBindings {
|
|
10
|
+
oauth?: string | undefined;
|
|
11
|
+
sso?: string | undefined;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* What a redeem answers, and which binding cookie the controller writes on this browser with
|
|
15
|
+
* which value: `oauth` (`cap_oauth`, the connector flow) or `sso` (`cap_sso`, the link flow).
|
|
16
|
+
*/
|
|
17
|
+
export interface HandoffRedeemResult {
|
|
18
|
+
location: string;
|
|
19
|
+
binding: {
|
|
20
|
+
flow: "oauth" | "sso";
|
|
21
|
+
value: string;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Redeems a browser handoff ticket (shell external authorization spec §2) in the person's own
|
|
26
|
+
* signed-in browser: the ticket is taken first (GETDEL, one redeem wins across replicas), so a
|
|
27
|
+
* mismatch or a refusal spends it too. The flow then starts as a normal start would, in this
|
|
28
|
+
* browser, with the binding value the controller writes as this browser's cookie and the handoff
|
|
29
|
+
* done page as the return page.
|
|
30
|
+
*/
|
|
31
|
+
export declare class HandoffService {
|
|
32
|
+
private readonly tickets;
|
|
33
|
+
private readonly accounts;
|
|
34
|
+
private readonly db;
|
|
35
|
+
private readonly config;
|
|
36
|
+
private readonly oauthFlow;
|
|
37
|
+
private readonly ssoFlow;
|
|
38
|
+
constructor(tickets: HandoffTickets, accounts: AccountsService, db: KernelDb, config: KernelConfigService, oauthFlow: OAuthFlow, ssoFlow: SsoFlow);
|
|
39
|
+
/**
|
|
40
|
+
* Unknown, used, expired or malformed ticket → `handoff.invalid`. The session's account is not
|
|
41
|
+
* the ticket's → `handoff.account_mismatch` naming the ticket's account (details
|
|
42
|
+
* `[{ login, displayName, email }]`, `login` its local login or null), and no flow starts. The
|
|
43
|
+
* ticket's return scheme is checked again (`handoff.scheme_not_allowed` once a super admin
|
|
44
|
+
* removed it). A connector ticket re-checks the org role as the start's org guard does, under
|
|
45
|
+
* the ticket's org scope (non-member or deleted org → `org.not_found`, role too low →
|
|
46
|
+
* `forbidden`), then starts the OAuth flow; a link ticket starts the SSO link intent.
|
|
47
|
+
*/
|
|
48
|
+
redeem(ticket: string, principal: UserPrincipal, bindings: HandoffBrowserBindings): Promise<HandoffRedeemResult>;
|
|
49
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
11
|
+
import { ApiError } from "../errors.js";
|
|
12
|
+
import { AccountsService } from "../identity/accounts.js";
|
|
13
|
+
import { SsoFlow, ssoBrowserBinding } from "../identity/sso.js";
|
|
14
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
15
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
16
|
+
import { withOrgScope } from "../infra/database/org-scope.js";
|
|
17
|
+
import { authorizeOrgAction } from "../orgs/org.guard.js";
|
|
18
|
+
import { browserBinding, OAuthFlow } from "../tools/oauth/flow.js";
|
|
19
|
+
import { assertReturnSchemeAllowed } from "./app-schemes.js";
|
|
20
|
+
import { HandoffTickets } from "./handoff-tickets.js";
|
|
21
|
+
/**
|
|
22
|
+
* Redeems a browser handoff ticket (shell external authorization spec §2) in the person's own
|
|
23
|
+
* signed-in browser: the ticket is taken first (GETDEL, one redeem wins across replicas), so a
|
|
24
|
+
* mismatch or a refusal spends it too. The flow then starts as a normal start would, in this
|
|
25
|
+
* browser, with the binding value the controller writes as this browser's cookie and the handoff
|
|
26
|
+
* done page as the return page.
|
|
27
|
+
*/
|
|
28
|
+
let HandoffService = class HandoffService {
|
|
29
|
+
tickets;
|
|
30
|
+
accounts;
|
|
31
|
+
db;
|
|
32
|
+
config;
|
|
33
|
+
oauthFlow;
|
|
34
|
+
ssoFlow;
|
|
35
|
+
constructor(tickets, accounts, db, config, oauthFlow, ssoFlow) {
|
|
36
|
+
this.tickets = tickets;
|
|
37
|
+
this.accounts = accounts;
|
|
38
|
+
this.db = db;
|
|
39
|
+
this.config = config;
|
|
40
|
+
this.oauthFlow = oauthFlow;
|
|
41
|
+
this.ssoFlow = ssoFlow;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Unknown, used, expired or malformed ticket → `handoff.invalid`. The session's account is not
|
|
45
|
+
* the ticket's → `handoff.account_mismatch` naming the ticket's account (details
|
|
46
|
+
* `[{ login, displayName, email }]`, `login` its local login or null), and no flow starts. The
|
|
47
|
+
* ticket's return scheme is checked again (`handoff.scheme_not_allowed` once a super admin
|
|
48
|
+
* removed it). A connector ticket re-checks the org role as the start's org guard does, under
|
|
49
|
+
* the ticket's org scope (non-member or deleted org → `org.not_found`, role too low →
|
|
50
|
+
* `forbidden`), then starts the OAuth flow; a link ticket starts the SSO link intent.
|
|
51
|
+
*/
|
|
52
|
+
async redeem(ticket, principal, bindings) {
|
|
53
|
+
const accountId = principal.account.id;
|
|
54
|
+
const rec = await this.tickets.take(ticket);
|
|
55
|
+
if (rec === null) {
|
|
56
|
+
throw new ApiError("handoff.invalid", "This authorization link has expired or was already used; start again in the app");
|
|
57
|
+
}
|
|
58
|
+
if (rec.accountId !== accountId) {
|
|
59
|
+
const [owner, logins] = await Promise.all([
|
|
60
|
+
this.accounts.get(rec.accountId),
|
|
61
|
+
this.accounts.localLogins([rec.accountId]),
|
|
62
|
+
]);
|
|
63
|
+
throw new ApiError("handoff.account_mismatch", "This authorization link belongs to another account; sign in as that account or start again in the app", [
|
|
64
|
+
{
|
|
65
|
+
login: logins.get(rec.accountId) ?? null,
|
|
66
|
+
displayName: owner?.displayName ?? null,
|
|
67
|
+
email: owner?.email ?? null,
|
|
68
|
+
},
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
assertReturnSchemeAllowed(this.config.get(), rec.returnScheme);
|
|
72
|
+
if (rec.kind === "connector") {
|
|
73
|
+
const value = browserBinding(bindings.oauth);
|
|
74
|
+
// the entry point of this request's org work: the scope comes from the ticket
|
|
75
|
+
const { authorizationUrl } = await withOrgScope(rec.orgId, async () => {
|
|
76
|
+
await authorizeOrgAction(this.db.db, principal, rec.orgId, "connectors.use");
|
|
77
|
+
return this.oauthFlow.start(rec.orgId, rec.connectorId, accountId, rec.returnTo, value, rec);
|
|
78
|
+
});
|
|
79
|
+
return { location: authorizationUrl, binding: { flow: "oauth", value } };
|
|
80
|
+
}
|
|
81
|
+
const value = ssoBrowserBinding(bindings.sso);
|
|
82
|
+
// a link that cannot start answers the done page with `?error=<code>`: the same location
|
|
83
|
+
const location = await this.ssoFlow.start(rec.plugin, rec.returnTo, value, accountId, "link", rec);
|
|
84
|
+
return { location, binding: { flow: "sso", value } };
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
HandoffService = __decorate([
|
|
88
|
+
Injectable(),
|
|
89
|
+
__param(0, Inject(HandoffTickets)),
|
|
90
|
+
__param(1, Inject(AccountsService)),
|
|
91
|
+
__param(2, Inject(KernelDb)),
|
|
92
|
+
__param(3, Inject(KernelConfigService)),
|
|
93
|
+
__param(4, Inject(OAuthFlow)),
|
|
94
|
+
__param(5, Inject(SsoFlow))
|
|
95
|
+
], HandoffService);
|
|
96
|
+
export { HandoffService };
|
package/dist/http/api.module.js
CHANGED
|
@@ -23,6 +23,8 @@ import { ModelsController } from "../agents/models.controller.js";
|
|
|
23
23
|
import { PendingConnectorsController } from "../agents/pending-connectors.controller.js";
|
|
24
24
|
import { ReadinessController } from "../agents/readiness.controller.js";
|
|
25
25
|
import { AgentSaveAsTemplateController, AgentTemplatesController, } from "../agents/templates.controller.js";
|
|
26
|
+
import { AutomationsController } from "../automations/automations.controller.js";
|
|
27
|
+
import { AutomationsModule } from "../automations/automations.module.js";
|
|
26
28
|
import { BackgroundController } from "../background/background.controller.js";
|
|
27
29
|
import { BackgroundModule } from "../background/background.module.js";
|
|
28
30
|
import { AdminCatalogController } from "../catalog/admin-catalog.controller.js";
|
|
@@ -56,6 +58,9 @@ import { ExtensionPointDefaultsController } from "../extensions/point-defaults.c
|
|
|
56
58
|
import { FilesModule } from "../files/files.module.js";
|
|
57
59
|
import { FilePreviewersController } from "../files/previewers.controller.js";
|
|
58
60
|
import { GitModule } from "../git/git.module.js";
|
|
61
|
+
import { HandoffController } from "../handoff/handoff.controller.js";
|
|
62
|
+
import { HandoffModule } from "../handoff/handoff.module.js";
|
|
63
|
+
import { HandoffTicketsModule } from "../handoff/handoff-tickets.js";
|
|
59
64
|
import { HtmlModule } from "../html/html.module.js";
|
|
60
65
|
import { HtmlDocumentController } from "../html/html-document.controller.js";
|
|
61
66
|
import { AccountAvatarController } from "../identity/account-avatar.controller.js";
|
|
@@ -83,8 +88,6 @@ import { PushController } from "../push/push.controller.js";
|
|
|
83
88
|
import { PushModule } from "../push/push.module.js";
|
|
84
89
|
import { RuntimeModule } from "../runtime/runtime.module.js";
|
|
85
90
|
import { ConversationStreamController } from "../runtime/stream.controller.js";
|
|
86
|
-
import { SchedulesController } from "../schedules/schedules.controller.js";
|
|
87
|
-
import { SchedulesModule } from "../schedules/schedules.module.js";
|
|
88
91
|
import { SetupModule } from "../setup/setup.module.js";
|
|
89
92
|
import { SetupChecksController } from "../setup/setup-checks.controller.js";
|
|
90
93
|
import { SetupGuidesController } from "../setup/setup-guides.controller.js";
|
|
@@ -158,7 +161,7 @@ const CONTROLLERS = [
|
|
|
158
161
|
PermissionProfilesController,
|
|
159
162
|
BackgroundController,
|
|
160
163
|
DevicesController,
|
|
161
|
-
|
|
164
|
+
AutomationsController,
|
|
162
165
|
AttachmentsController,
|
|
163
166
|
SkillsController,
|
|
164
167
|
SkillChangesController,
|
|
@@ -176,6 +179,8 @@ const CONTROLLERS = [
|
|
|
176
179
|
OAuthConnectController,
|
|
177
180
|
OAuthCallbackController,
|
|
178
181
|
SsoController,
|
|
182
|
+
// a shell's browser handoff redeemed in the person's signed-in browser
|
|
183
|
+
HandoffController,
|
|
179
184
|
BindController,
|
|
180
185
|
MeIdentitiesController,
|
|
181
186
|
ChannelConnectionsController,
|
|
@@ -253,14 +258,17 @@ ApiModule = __decorate([
|
|
|
253
258
|
ThemesModule,
|
|
254
259
|
ClientModule,
|
|
255
260
|
SetupModule,
|
|
256
|
-
//
|
|
257
|
-
|
|
261
|
+
// automations (the controller comes with the HTTP routes)
|
|
262
|
+
AutomationsModule,
|
|
258
263
|
// the git gateway (run tickets; the gateway route comes with it)
|
|
259
264
|
GitModule,
|
|
260
265
|
// file sharing (shares of conversation files)
|
|
261
266
|
SharesModule,
|
|
262
267
|
// HTML files rendered as sandboxed documents (the document token and route)
|
|
263
268
|
HtmlModule,
|
|
269
|
+
// the connect and link starts' handoff tickets (OAuthConnectController, SsoController)
|
|
270
|
+
HandoffTicketsModule,
|
|
271
|
+
HandoffModule,
|
|
264
272
|
],
|
|
265
273
|
controllers: CONTROLLERS,
|
|
266
274
|
providers: [{ provide: APP_GUARD, useClass: AuthGuard }],
|
|
@@ -26,6 +26,7 @@ export declare const MetaResponseSchema: z.ZodObject<{
|
|
|
26
26
|
}, z.core.$strip>>;
|
|
27
27
|
}, z.core.$strip>;
|
|
28
28
|
appRedirectUris: z.ZodArray<z.ZodString>;
|
|
29
|
+
appSchemes: z.ZodArray<z.ZodString>;
|
|
29
30
|
}, z.core.$strip>;
|
|
30
31
|
export type MetaResponse = z.infer<typeof MetaResponseSchema>;
|
|
31
32
|
/**
|
|
@@ -11,6 +11,7 @@ import { DEVICE_DIAL_IN_PROTOCOL, DEVICE_PROTOCOL_VERSION, } from "@capybara-hq/
|
|
|
11
11
|
import { Controller, Get, Header, Inject } from "@nestjs/common";
|
|
12
12
|
import { z } from "zod";
|
|
13
13
|
import { DialInHandlers } from "../dial-in/handlers.js";
|
|
14
|
+
import { appSchemes } from "../handoff/app-schemes.js";
|
|
14
15
|
import { Public } from "../identity/principal.js";
|
|
15
16
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
16
17
|
import { PushProviderHost } from "../push/provider-host.js";
|
|
@@ -38,6 +39,11 @@ export const MetaResponseSchema = z.object({
|
|
|
38
39
|
}),
|
|
39
40
|
/** The redirect URIs app sign-in accepts (`auth.appRedirectUris`); [] while appSignIn is off. */
|
|
40
41
|
appRedirectUris: z.array(z.string()),
|
|
42
|
+
/**
|
|
43
|
+
* The shell schemes a browser handoff's done page may link back to: those whose
|
|
44
|
+
* `<scheme>://auth/callback` is in `auth.appRedirectUris`; [] while appSignIn is off.
|
|
45
|
+
*/
|
|
46
|
+
appSchemes: z.array(z.string()),
|
|
41
47
|
});
|
|
42
48
|
/** The protocol name `capybara-device` speaks (`capybara-device` of `capybara-device/<major>`). */
|
|
43
49
|
const DEVICE_PROTOCOL_NAME = DEVICE_DIAL_IN_PROTOCOL.slice(0, DEVICE_DIAL_IN_PROTOCOL.indexOf("/"));
|
|
@@ -92,6 +98,7 @@ let MetaController = class MetaController {
|
|
|
92
98
|
},
|
|
93
99
|
// Not secret: an app that finds its own URI missing says so before opening the browser.
|
|
94
100
|
appRedirectUris: appSignIn ? [...config.auth.appRedirectUris] : [],
|
|
101
|
+
appSchemes: appSchemes(config),
|
|
95
102
|
};
|
|
96
103
|
}
|
|
97
104
|
/** A customised platform icon's versioned URL (the one the manifest uses); null for the default. */
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FastifyReply } from "fastify";
|
|
2
|
+
/**
|
|
3
|
+
* An `AbortSignal` tied to one HTTP request: aborted when the connection closes before the
|
|
4
|
+
* response is written (the client hung up), never by a close after a finished response. The same
|
|
5
|
+
* rule as the plugin routes' `req.signal` and the channel webhooks. For a route whose handler asks
|
|
6
|
+
* a device (or another remote) on the request's behalf, so the call stops when nobody waits.
|
|
7
|
+
*/
|
|
8
|
+
export declare function requestSignal(reply: FastifyReply): AbortSignal;
|