@processfocus/runtime-local 0.1.0-next.1 → 0.1.0-next.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -253
- package/dist/authentication-server.mjs +52 -84
- package/dist/{chunk-fn7b5pxw.js → chunk-04r0t2mb.js} +19364 -34104
- package/dist/chunk-727y6qkb.js +156 -0
- package/dist/chunk-bg5r4wpv.js +4755 -0
- package/dist/chunk-cbd36jq1.js +512 -0
- package/dist/chunk-d4ctj079.js +241 -0
- package/dist/{chunk-cq9xscgb.js → chunk-r5s2v669.js} +49079 -33564
- package/dist/dashboard-source/apps/frontend/app/(protected)/act-on-behalf/actions.ts +12 -12
- package/dist/dashboard-source/apps/frontend/app/(protected)/act-on-behalf/delegations-client.tsx +31 -21
- package/dist/dashboard-source/apps/frontend/app/(protected)/act-on-behalf/page.tsx +3 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/client-index.tsx +1 -12
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/@modal/(.)[executionId]/client.tsx +1 -1
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/@modal/(.)[executionId]/page.tsx +2 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/@modal/(.)[executionId]/process-state/page.tsx +2 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/@modal/default.tsx +3 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/[executionId]/page.tsx +3 -1
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/[executionId]/process-state/page.tsx +2 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/client-layout.tsx +19 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/default.tsx +6 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/executions-client.tsx +1637 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/layout.tsx +5 -7
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/page.tsx +4 -1589
- package/dist/dashboard-source/apps/frontend/app/(protected)/executions/process-state-page-client.tsx +1 -1
- package/dist/dashboard-source/apps/frontend/app/(protected)/layout.tsx +41 -33
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/@modal/(.)item/[id]/client.tsx +11 -17
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/@modal/(.)item/[id]/page.tsx +2 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/@modal/default.tsx +3 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/[...listPath]/list-page-client.tsx +153 -16
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/[...listPath]/page.tsx +10 -2
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/components/list-create-form.tsx +29 -13
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/components/list-item-edit-form.tsx +22 -28
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/components/list-item-load-error.tsx +49 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/components/list-schema-form.tsx +28 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/default.tsx +6 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/hooks/metric-control-url-state.ts +7 -16
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/hooks/use-list-deleted-callback.ts +4 -6
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/hooks/use-list-item-detail.ts +45 -142
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/item/[id]/client.tsx +10 -20
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/item/[id]/page.tsx +2 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/layout.tsx +3 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/lib/list-process-dialog.tsx +72 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/lib/list-refresh-provider.tsx +65 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/lib/list-refresh.ts +162 -16
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/utils.tsx +19 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/org-chart/page.tsx +4 -9
- package/dist/dashboard-source/apps/frontend/app/(protected)/page.tsx +4 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/passkeys/actions.ts +15 -18
- package/dist/dashboard-source/apps/frontend/app/(protected)/passkeys/page.tsx +12 -6
- package/dist/dashboard-source/apps/frontend/app/(protected)/passkeys/passkeys-client.tsx +7 -16
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/@modal/(.)start/[...startStepPath]/client.tsx +26 -48
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/@modal/(.)start/[...startStepPath]/page.tsx +4 -7
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/@modal/default.tsx +3 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/client-layout.tsx +28 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/client.tsx +1 -3
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/components/start-process-form.tsx +193 -117
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/default.tsx +6 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/layout.tsx +7 -9
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/page.tsx +3 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/processes-client.tsx +60 -204
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/start/[...startStepPath]/client.tsx +12 -51
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/start/[...startStepPath]/page.tsx +27 -4
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/workflow/[...processPath]/page.tsx +5 -4
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/workflow/[...processPath]/workflow-flow.tsx +0 -5
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/workflow/[...processPath]/workflow-form-preview-sheet.tsx +26 -19
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/cedar/[[...tab]]/page.tsx +5 -2
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/cedar/cedar-authorize.ts +85 -9
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/cedar/cedar-types.ts +12 -1
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/cedar/explorer.tsx +66 -4
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/cedar/layout.tsx +3 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/cedar/load-handbook.ts +41 -13
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/invitations/[invitationId]/page.tsx +2 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/invitations/invitation-detail-client.tsx +442 -365
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/invitations/new/page.tsx +8 -2
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/invitations/page.tsx +7 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/invitations-table.tsx +61 -43
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/page.tsx +6 -67
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/providers/page.tsx +7 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/roles/page.tsx +7 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/settings-client.tsx +111 -189
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/settings-page.tsx +86 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/users/[userId]/client.tsx +141 -256
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/users/[userId]/page.tsx +2 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/users/[userId]/tokens/page.tsx +2 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/users/page.tsx +7 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/settings/users-table.tsx +6 -4
- package/dist/dashboard-source/apps/frontend/app/(protected)/stats/[...statsPath]/page.tsx +3 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/@modal/(.)complete/[...stepPath]/client.tsx +30 -9
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/@modal/(.)complete/[...stepPath]/page.tsx +3 -1
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/@modal/default.tsx +3 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/complete/[...stepPath]/client.tsx +2 -4
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/complete/[...stepPath]/page.tsx +3 -1
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/components/complete-todo-form.tsx +47 -54
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/components/public-completion-correction-form.tsx +37 -54
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/components/todo-form-actions.tsx +1 -1
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/correct/[...stepPath]/page.tsx +3 -1
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/default.tsx +6 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/layout.tsx +3 -0
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/lib/mobile-todo-list.ts +4 -25
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/page.tsx +4 -1315
- package/dist/dashboard-source/apps/frontend/app/(protected)/to-dos/to-dos-client.tsx +1282 -0
- package/dist/dashboard-source/apps/frontend/app/api/auth/callback/route.ts +81 -65
- package/dist/dashboard-source/apps/frontend/app/api/auth/delegation/route.ts +8 -2
- package/dist/dashboard-source/apps/frontend/app/api/auth/login/route.ts +0 -1
- package/dist/dashboard-source/apps/frontend/app/api/auth/logout/route.ts +10 -5
- package/dist/dashboard-source/apps/frontend/app/api/auth/passkey/start/route.ts +1 -3
- package/dist/dashboard-source/apps/frontend/app/api/auth/refresh/route.ts +0 -1
- package/dist/dashboard-source/apps/frontend/app/api/auth/revalidate-google/route.ts +68 -0
- package/dist/dashboard-source/apps/frontend/app/api/auth/switch-role/route.ts +1 -3
- package/dist/dashboard-source/apps/frontend/app/api/embed/lookup/route.ts +25 -7
- package/dist/dashboard-source/apps/frontend/app/api/embed/submit/route.ts +1 -2
- package/dist/dashboard-source/apps/frontend/app/api/public/start/[address]/route.ts +49 -0
- package/dist/dashboard-source/apps/frontend/app/api/public/to-dos/lookup/route.ts +4 -3
- package/dist/dashboard-source/apps/frontend/app/api/revalidate/route.ts +15 -4
- package/dist/dashboard-source/apps/frontend/app/app-shell.tsx +11 -4
- package/dist/dashboard-source/apps/frontend/app/cli-auth/route.ts +2 -1
- package/dist/dashboard-source/apps/frontend/app/embed/[...startStepPath]/embed-client.tsx +31 -18
- package/dist/dashboard-source/apps/frontend/app/embed/[...startStepPath]/page.tsx +5 -1
- package/dist/dashboard-source/apps/frontend/app/login/page.tsx +4 -4
- package/dist/dashboard-source/apps/frontend/app/login/passkey-button.tsx +11 -6
- package/dist/dashboard-source/apps/frontend/app/login/passkey-help/page.tsx +67 -0
- package/dist/dashboard-source/apps/frontend/app/mcp/[[...endpoint]]/route.ts +47 -0
- package/dist/dashboard-source/apps/frontend/app/public/form/[token]/public-todo-client.tsx +44 -25
- package/dist/dashboard-source/apps/frontend/app/public/start/[address]/form/page.tsx +10 -0
- package/dist/dashboard-source/apps/frontend/app/public/start/[address]/layout.tsx +32 -0
- package/dist/dashboard-source/apps/frontend/app/public/start/[address]/page-client.tsx +319 -0
- package/dist/dashboard-source/apps/frontend/app/public/start/[address]/page-content.tsx +41 -0
- package/dist/dashboard-source/apps/frontend/app/public/start/[address]/page.tsx +10 -0
- package/dist/dashboard-source/apps/frontend/app/public.css +1 -1
- package/dist/dashboard-source/apps/frontend/app/register/passkey/register-passkey-client.tsx +17 -0
- package/dist/dashboard-source/apps/frontend/components/DashboardSection.tsx +9 -15
- package/dist/dashboard-source/apps/frontend/components/Header.tsx +4 -8
- package/dist/dashboard-source/apps/frontend/components/anonymous-page-link.tsx +57 -0
- package/dist/dashboard-source/apps/frontend/components/anonymous-start-form.tsx +24 -0
- package/dist/dashboard-source/apps/frontend/components/app-sidebar.tsx +21 -7
- package/dist/dashboard-source/apps/frontend/components/auth-provider.tsx +12 -7
- package/dist/dashboard-source/apps/frontend/components/browser-only.tsx +19 -0
- package/dist/dashboard-source/apps/frontend/components/config-provider.tsx +0 -1
- package/dist/dashboard-source/apps/frontend/components/delegation-expiry.tsx +24 -13
- package/dist/dashboard-source/apps/frontend/components/form-dialog-content.tsx +22 -2
- package/dist/dashboard-source/apps/frontend/components/journey-timeline.tsx +52 -45
- package/dist/dashboard-source/apps/frontend/components/notification-bell.tsx +0 -1
- package/dist/dashboard-source/apps/frontend/components/sidebar-lists.tsx +0 -1
- package/dist/dashboard-source/apps/frontend/components/token-refresh-scheduler.tsx +38 -11
- package/dist/dashboard-source/apps/frontend/components/ui/sidebar.tsx +0 -1
- package/dist/dashboard-source/apps/frontend/components/ui/tab-pill.tsx +26 -15
- package/dist/dashboard-source/apps/frontend/components/workflow/step-node.tsx +0 -1
- package/dist/dashboard-source/apps/frontend/components/workflow/workflow-edge.tsx +5 -0
- package/dist/dashboard-source/apps/frontend/hooks/use-form-metadata.ts +21 -10
- package/dist/dashboard-source/apps/frontend/hooks/use-process-draft.ts +173 -0
- package/dist/dashboard-source/apps/frontend/hooks/use-weekly-execution-stats.ts +1 -3
- package/dist/dashboard-source/apps/frontend/lib/anonymous-page-form-values.ts +6 -0
- package/dist/dashboard-source/apps/frontend/lib/anonymous-page-server.ts +65 -0
- package/dist/dashboard-source/apps/frontend/lib/appsync-events/adapter.ts +9 -3
- package/dist/dashboard-source/apps/frontend/lib/appsync-events/client.ts +7 -340
- package/dist/dashboard-source/apps/frontend/lib/auth/client-session.ts +2 -0
- package/dist/dashboard-source/apps/frontend/lib/auth/delegations.ts +9 -7
- package/dist/dashboard-source/apps/frontend/lib/auth/google-revalidation.ts +111 -0
- package/dist/dashboard-source/apps/frontend/lib/auth/logout.ts +1 -1
- package/dist/dashboard-source/apps/frontend/lib/auth/passkeys.ts +28 -30
- package/dist/dashboard-source/apps/frontend/lib/auth/providers.ts +10 -0
- package/dist/dashboard-source/apps/frontend/lib/auth/realtime-session-token.ts +1 -0
- package/dist/dashboard-source/apps/frontend/lib/auth/reauthentication-state.ts +4 -4
- package/dist/dashboard-source/apps/frontend/lib/auth/session-admission.ts +11 -7
- package/dist/dashboard-source/apps/frontend/lib/auth/session.ts +27 -10
- package/dist/dashboard-source/apps/frontend/lib/auth/ssr-session.ts +76 -9
- package/dist/dashboard-source/apps/frontend/lib/auth/verify-passkey.ts +25 -3
- package/dist/dashboard-source/apps/frontend/lib/collections/collection-provider-factory.tsx +112 -21
- package/dist/dashboard-source/apps/frontend/lib/collections/draft-process-execution.ts +1 -8
- package/dist/dashboard-source/apps/frontend/lib/collections/execution.ts +6 -2
- package/dist/dashboard-source/apps/frontend/lib/collections/process-collection-provider.tsx +3 -0
- package/dist/dashboard-source/apps/frontend/lib/collections/process.ts +8 -2
- package/dist/dashboard-source/apps/frontend/lib/collections/replication-factory.ts +16 -4
- package/dist/dashboard-source/apps/frontend/lib/collections/todo.ts +1 -1
- package/dist/dashboard-source/apps/frontend/lib/effect/navigation.ts +4 -0
- package/dist/dashboard-source/apps/frontend/lib/effect/page-runtime.ts +29 -0
- package/dist/dashboard-source/apps/frontend/lib/effect/params.ts +18 -12
- package/dist/dashboard-source/apps/frontend/lib/effect/run-effect.ts +1 -1
- package/dist/dashboard-source/apps/frontend/lib/effect/runtime.ts +19 -23
- package/dist/dashboard-source/apps/frontend/lib/effect/services/access-token.ts +9 -12
- package/dist/dashboard-source/apps/frontend/lib/effect/services/authorization.ts +12 -12
- package/dist/dashboard-source/apps/frontend/lib/effect/services/form-validation.ts +20 -18
- package/dist/dashboard-source/apps/frontend/lib/effect/services/graphql.ts +32 -16
- package/dist/dashboard-source/apps/frontend/lib/embed-form-server.ts +35 -0
- package/dist/dashboard-source/apps/frontend/lib/embed-manifest.ts +10 -103
- package/dist/dashboard-source/apps/frontend/lib/forms/invitation.ts +43 -0
- package/dist/dashboard-source/apps/frontend/lib/forms/provider-user.ts +64 -0
- package/dist/dashboard-source/apps/frontend/lib/frontend-form-loaders.ts +40 -0
- package/dist/dashboard-source/apps/frontend/lib/frontend-manifest.ts +1 -33
- package/dist/dashboard-source/apps/frontend/lib/generated/gql/gql.ts +123 -33
- package/dist/dashboard-source/apps/frontend/lib/generated/gql/graphql.ts +169 -30
- package/dist/dashboard-source/apps/frontend/lib/generated/gql/index.ts +1 -0
- package/dist/dashboard-source/apps/frontend/lib/graphql/anonymous-page.ts +27 -0
- package/dist/dashboard-source/apps/frontend/lib/graphql/dependent-lookup-query.ts +1 -0
- package/dist/dashboard-source/apps/frontend/lib/graphql/list-queries.ts +38 -29
- package/dist/dashboard-source/apps/frontend/lib/graphql/lookup-provider.tsx +82 -10
- package/dist/dashboard-source/apps/frontend/lib/graphql/org-chart-operations.ts +2 -5
- package/dist/dashboard-source/apps/frontend/lib/graphql/queries.ts +25 -4
- package/dist/dashboard-source/apps/frontend/lib/graphql/server-client.ts +11 -0
- package/dist/dashboard-source/apps/frontend/lib/graphql/settings-queries.ts +13 -104
- package/dist/dashboard-source/apps/frontend/lib/graphql/user-queries.ts +11 -4
- package/dist/dashboard-source/apps/frontend/lib/graphql/workflow-operations.ts +1 -3
- package/dist/dashboard-source/apps/frontend/lib/graphql/workflow-queries.ts +1 -0
- package/dist/dashboard-source/apps/frontend/lib/list-detail-selection.ts +54 -0
- package/dist/dashboard-source/apps/frontend/lib/mcp/executions.ts +110 -0
- package/dist/dashboard-source/apps/frontend/lib/mcp/list-writes.ts +395 -0
- package/dist/dashboard-source/apps/frontend/lib/mcp/lists.ts +263 -0
- package/dist/dashboard-source/apps/frontend/lib/mcp/oauth.ts +900 -0
- package/dist/dashboard-source/apps/frontend/lib/mcp/origin.ts +42 -0
- package/dist/dashboard-source/apps/frontend/lib/mcp/processes.ts +436 -0
- package/dist/dashboard-source/apps/frontend/lib/mcp/read-tool.ts +72 -0
- package/dist/dashboard-source/apps/frontend/lib/mcp/result.ts +4 -0
- package/dist/dashboard-source/apps/frontend/lib/mcp/server.ts +71 -0
- package/dist/dashboard-source/apps/frontend/lib/mcp/store.ts +193 -0
- package/dist/dashboard-source/apps/frontend/lib/mcp/todos.ts +237 -0
- package/dist/dashboard-source/apps/frontend/lib/notifications/notification-preference.ts +0 -1
- package/dist/dashboard-source/apps/frontend/lib/notifications/todo-notification-observer.ts +0 -5
- package/dist/dashboard-source/apps/frontend/lib/org-chart-data.ts +0 -2
- package/dist/dashboard-source/apps/frontend/lib/org-identity.ts +6 -4
- package/dist/dashboard-source/apps/frontend/lib/organisation-form-loaders.ts +4 -0
- package/dist/dashboard-source/apps/frontend/lib/public-todo-form-runtime.ts +0 -1
- package/dist/dashboard-source/apps/frontend/lib/public-todo-model.ts +39 -139
- package/dist/dashboard-source/apps/frontend/lib/public-todo-server.ts +17 -16
- package/dist/dashboard-source/apps/frontend/lib/query-client.ts +0 -1
- package/dist/dashboard-source/apps/frontend/lib/todo-utils.ts +1 -77
- package/dist/dashboard-source/apps/frontend/lib/workflow/layout.ts +3 -7
- package/dist/dashboard-source/apps/frontend/next.config.ts +33 -13
- package/dist/dashboard-source/apps/frontend/package.json +48 -47
- package/dist/dashboard-source/apps/frontend/proxy.ts +72 -14
- package/dist/dashboard-source/modules/auth-local-cedar/src/index.ts +0 -3
- package/dist/dashboard-source/modules/auth-local-cedar/src/lib/auth-local-cedar.ts +165 -233
- package/dist/dashboard-source/modules/auth-local-cedar/src/lib/cedar-reload-notifier.ts +4 -3
- package/dist/dashboard-source/modules/auth-local-cedar/src/lib/config-base.ts +111 -75
- package/dist/dashboard-source/modules/auth-local-cedar/src/lib/policy-watcher.ts +14 -18
- package/dist/dashboard-source/modules/auth-policy/src/index.ts +1 -0
- package/dist/dashboard-source/modules/auth-policy/src/lib/auth-policy.ts +19 -37
- package/dist/dashboard-source/modules/auth-policy/src/lib/current-principal.ts +33 -29
- package/dist/dashboard-source/modules/auth-policy/src/lib/policy-revision.ts +23 -0
- package/dist/dashboard-source/modules/auth-policy/src/lib/types.ts +24 -49
- package/dist/dashboard-source/modules/auth-session/src/index.ts +4 -0
- package/dist/dashboard-source/modules/auth-session/src/lib/project-subscription.ts +13 -0
- package/dist/dashboard-source/modules/auth-session/src/lib/realtime-recipient.ts +10 -2
- package/dist/dashboard-source/modules/auth-session/src/lib/subjects.ts +82 -8
- package/dist/dashboard-source/modules/auth-session/src/lib/workload-access.ts +48 -0
- package/dist/dashboard-source/modules/auth-session/src/lib/workload-audit.ts +52 -0
- package/dist/dashboard-source/modules/aws-common/src/index.ts +0 -1
- package/dist/dashboard-source/modules/effect-json-logger/src/lib/effect-json-logger.ts +6 -9
- package/dist/dashboard-source/modules/form/src/hooks/use-local-storage.ts +5 -3
- package/dist/dashboard-source/modules/form/src/index.ts +1 -10
- package/dist/dashboard-source/modules/form/src/lib/effect-schema-form.tsx +1271 -0
- package/dist/dashboard-source/modules/form/src/lib/form-actions.ts +26 -0
- package/dist/dashboard-source/modules/form/src/lib/form-prefill.tsx +79 -0
- package/dist/dashboard-source/modules/form/src/lib/initial-focus.ts +31 -0
- package/dist/dashboard-source/modules/form/src/lib/lookup-context.tsx +18 -0
- package/dist/dashboard-source/modules/form/src/lib/plugin-registry.ts +4 -0
- package/dist/dashboard-source/modules/form/src/lib/plugin-scripts.ts +26 -0
- package/dist/dashboard-source/modules/form/src/lib/registered-plugin-field.tsx +57 -0
- package/dist/dashboard-source/modules/form/src/lib/schema-lookup.tsx +124 -0
- package/dist/dashboard-source/modules/form/src/lib/schema-typeahead.tsx +522 -0
- package/dist/dashboard-source/modules/form/src/lib/validation-logic.ts +172 -0
- package/dist/dashboard-source/modules/form-schema/src/index.ts +4 -1
- package/dist/dashboard-source/modules/form-schema/src/lib/annotation-helpers.ts +19 -14
- package/dist/dashboard-source/modules/form-schema/src/lib/email.ts +13 -11
- package/dist/dashboard-source/modules/form-schema/src/lib/field-count.ts +15 -13
- package/dist/dashboard-source/modules/form-schema/src/lib/form-annotations.ts +14 -0
- package/dist/dashboard-source/modules/form-schema/src/lib/form-presentation.ts +33 -0
- package/dist/dashboard-source/modules/form-schema/src/lib/form-schema.ts +186 -80
- package/dist/dashboard-source/modules/form-schema/src/lib/number-from-string.ts +26 -0
- package/dist/dashboard-source/modules/form-schema/src/lib/phone.ts +8 -7
- package/dist/dashboard-source/modules/form-schema/src/lib/public-completion-correction.ts +7 -0
- package/dist/dashboard-source/modules/form-schema/src/lib/search-lookup-items.ts +27 -0
- package/dist/dashboard-source/modules/form-submission-schema/src/index.ts +5 -0
- package/dist/dashboard-source/modules/form-submission-schema/src/lib/executable-rules.ts +379 -0
- package/dist/dashboard-source/modules/form-submission-schema/src/lib/form-submission-schema.ts +475 -0
- package/dist/dashboard-source/modules/form-submission-schema/src/lib/json-schema.ts +58 -0
- package/dist/dashboard-source/modules/form-submission-schema/src/lib/schema-defaults.ts +158 -0
- package/dist/dashboard-source/modules/form-submission-schema/src/lib/submission-utils.ts +75 -0
- package/dist/dashboard-source/modules/frontend-manifest/src/lib/browser-plugin-artifact.ts +46 -0
- package/dist/dashboard-source/modules/frontend-manifest/src/lib/frontend-manifest.ts +37 -30
- package/dist/dashboard-source/modules/frontend-plugin-host/src/lib/frontend-plugin-host.ts +9 -1
- package/dist/dashboard-source/modules/openauth/src/client.ts +25 -17
- package/dist/dashboard-source/modules/openauth/src/util.ts +16 -0
- package/dist/dashboard-source/modules/{aws-common/src/lib/project-balance-admission.ts → project-accounting/src/admission.ts} +4 -8
- package/dist/dashboard-source/modules/realtime-client/src/appsync-client.ts +377 -0
- package/dist/dashboard-source/modules/realtime-client/src/index.ts +5 -0
- package/dist/dashboard-source/modules/realtime-client/src/subscription-presence.ts +163 -0
- package/dist/dashboard-source/modules/request-time/src/lib/request-time.ts +6 -24
- package/dist/dashboard-source/modules/rxdb-collections/generated/execution-schema.json +28 -21
- package/dist/dashboard-source/modules/rxdb-collections/src/lib/rxdb-collections.ts +25 -18
- package/dist/dashboard-source/modules/rxdb-collections/src/lib/to-rxdb-schema.ts +60 -10
- package/dist/dashboard-source/modules/shadcn-components/src/index.ts +18 -2
- package/dist/dashboard-source/modules/shadcn-components/src/lib/date-calendar.tsx +93 -0
- package/dist/dashboard-source/modules/shadcn-components/src/lib/date-field.tsx +147 -48
- package/dist/dashboard-source/modules/shadcn-components/src/lib/date-time-display.tsx +272 -0
- package/dist/dashboard-source/modules/shadcn-components/src/lib/local-date-time-input.tsx +108 -0
- package/dist/dashboard-source/modules/shadcn-components/src/lib/lookup-field.tsx +237 -351
- package/dist/dashboard-source/modules/shadcn-components/src/lib/ui/popover.tsx +2 -1
- package/dist/dashboard-source.json +48 -47
- package/dist/graphql-server.mjs +245 -200
- package/dist/job-worker-supervisor.mjs +262 -0
- package/dist/job-worker.mjs +234 -133
- package/dist/main.mjs +23 -4
- package/dist/prepare-dashboard.mjs +8 -8
- package/dist/resources/cedar/policies.cedar +80 -38
- package/dist/resources/cedar/schema.cedarschema +39 -21
- package/dist/resources/graphql-schema/rxdb.graphql +2 -0
- package/dist/resources/graphql-schema/system.graphql +204 -51
- package/package.json +64 -62
- package/dist/chunk-6b6t8tyq.js +0 -85
- package/dist/dashboard-source/apps/frontend/app/(protected)/lists/utils.ts +0 -20
- package/dist/dashboard-source/apps/frontend/app/(protected)/processes/components/process-form-actions.tsx +0 -105
- package/dist/dashboard-source/apps/frontend/hooks/use-draft-process-save.ts +0 -189
- package/dist/dashboard-source/apps/frontend/hooks/use-draft-state.ts +0 -79
- package/dist/dashboard-source/apps/frontend/hooks/use-processes-query.ts +0 -56
- package/dist/dashboard-source/modules/form/src/lib/ajv-standard-schema.ts +0 -152
- package/dist/dashboard-source/modules/form/src/lib/client-components-to-form.tsx +0 -1421
- package/dist/dashboard-source/modules/form/src/lib/friendly-validation-message.ts +0 -85
- package/dist/dashboard-source/modules/form/src/lib/walker.tsx +0 -1549
- package/dist/dashboard-source/modules/form-client-representation/src/lib/client-form-definition.ts +0 -440
- package/dist/dashboard-source/modules/form-client-representation/src/lib/plugin-registry.ts +0 -13
- package/dist/dashboard-source/modules/form-client-representation/src/lib/types.ts +0 -336
- package/dist/dashboard-source/modules/form-rule/src/index.ts +0 -21
- package/dist/dashboard-source/modules/form-rule/src/lib/evaluator.ts +0 -531
- package/dist/dashboard-source/modules/form-rule/src/lib/paths.ts +0 -102
- package/dist/dashboard-source/modules/form-rule/src/lib/types.ts +0 -121
- package/dist/dashboard-source/modules/form-schema/src/lib/property-signature.ts +0 -45
|
@@ -0,0 +1,900 @@
|
|
|
1
|
+
import "server-only"
|
|
2
|
+
import { createHash, randomBytes, timingSafeEqual } from "node:crypto"
|
|
3
|
+
import { Result, Schema } from "effect"
|
|
4
|
+
import { type NextRequest, NextResponse } from "next/server"
|
|
5
|
+
import { type ProviderUserSession, subjects } from "../../../../modules/auth-session/src/index.ts"
|
|
6
|
+
import { type Tokens, createClient } from "../../../../modules/openauth/src/client.ts"
|
|
7
|
+
import { OAuthMetadataFetchError } from "../../../../modules/openauth/src/error.ts"
|
|
8
|
+
import { consentPath, cookieName, loopback, mcpOrigin, opaque } from "./origin.ts"
|
|
9
|
+
import { bucket } from "./store.ts"
|
|
10
|
+
import { getAuthClient } from "@/lib/auth/client"
|
|
11
|
+
import { getFrontendAuthClientConfig } from "@/lib/auth/issuer"
|
|
12
|
+
import { decodeJwtPayload } from "@/lib/auth/jwt"
|
|
13
|
+
import { getCookieNamesFromHost } from "@/lib/auth/session"
|
|
14
|
+
import { acceptVerifiedSession } from "@/lib/auth/ssr-session"
|
|
15
|
+
|
|
16
|
+
const VIEW = "work:read"
|
|
17
|
+
const ACT = "work:act"
|
|
18
|
+
const pendingLifetime = 600_000
|
|
19
|
+
const codeLifetime = 60_000
|
|
20
|
+
const registrationLifetime = 3_600_000
|
|
21
|
+
// Outlast the serving Lambda's 20-second timeout but release after a cold exit.
|
|
22
|
+
const renewalLease = 30_000
|
|
23
|
+
const headers = {
|
|
24
|
+
"Cache-Control": "no-store",
|
|
25
|
+
"Referrer-Policy": "no-referrer",
|
|
26
|
+
"Content-Security-Policy":
|
|
27
|
+
"default-src 'none'; frame-ancestors 'none'; form-action 'self'; base-uri 'none'",
|
|
28
|
+
"X-Content-Type-Options": "nosniff",
|
|
29
|
+
}
|
|
30
|
+
const random = () => randomBytes(32).toString("base64url")
|
|
31
|
+
const hash = (text: string) =>
|
|
32
|
+
createHash("sha256").update(text).digest("base64url")
|
|
33
|
+
const Grant = Schema.Struct({
|
|
34
|
+
id: Schema.String,
|
|
35
|
+
clientId: Schema.String,
|
|
36
|
+
userId: Schema.String,
|
|
37
|
+
scope: Schema.Literals([VIEW, `${VIEW} ${ACT}`]),
|
|
38
|
+
})
|
|
39
|
+
type Grant = typeof Grant.Type
|
|
40
|
+
const Client = Schema.Struct({
|
|
41
|
+
name: Schema.String,
|
|
42
|
+
redirects: Schema.Array(Schema.String),
|
|
43
|
+
grants: Schema.Array(Grant),
|
|
44
|
+
})
|
|
45
|
+
const authFields = {
|
|
46
|
+
clientId: Schema.String,
|
|
47
|
+
redirect: Schema.String,
|
|
48
|
+
challenge: Schema.String,
|
|
49
|
+
state: Schema.String,
|
|
50
|
+
}
|
|
51
|
+
const Human = Schema.Struct({
|
|
52
|
+
email: Schema.String,
|
|
53
|
+
userId: Schema.String,
|
|
54
|
+
tokens: Schema.Struct({
|
|
55
|
+
access: Schema.String,
|
|
56
|
+
refresh: Schema.String,
|
|
57
|
+
expiresIn: Schema.Number,
|
|
58
|
+
refreshExpiresIn: Schema.optional(Schema.Number),
|
|
59
|
+
}),
|
|
60
|
+
expires: Schema.Number,
|
|
61
|
+
ssoDeadline: Schema.optional(Schema.Number),
|
|
62
|
+
})
|
|
63
|
+
type Human = typeof Human.Type
|
|
64
|
+
const Pending = Schema.Struct({
|
|
65
|
+
...authFields,
|
|
66
|
+
browser: Schema.String,
|
|
67
|
+
expires: Schema.Number,
|
|
68
|
+
grantsAtStart: Schema.Array(Grant),
|
|
69
|
+
human: Schema.optional(Human),
|
|
70
|
+
})
|
|
71
|
+
const Code = Schema.Struct({ ...authFields, grant: Grant, human: Human })
|
|
72
|
+
type Code = typeof Code.Type
|
|
73
|
+
const Access = Schema.Struct({ grant: Grant, token: Schema.String })
|
|
74
|
+
const Renewal = Schema.Struct({
|
|
75
|
+
grant: Grant,
|
|
76
|
+
expires: Schema.Number,
|
|
77
|
+
ssoDeadline: Schema.optional(Schema.Number),
|
|
78
|
+
issuerRefreshToken: Schema.String,
|
|
79
|
+
lockedUntil: Schema.Number,
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
const clients = (origin: string) => bucket(origin, "client", Client)
|
|
83
|
+
const pending = (origin: string) => bucket(origin, "pending", Pending)
|
|
84
|
+
const codes = (origin: string) => bucket(origin, "code", Code)
|
|
85
|
+
const access = (origin: string) => bucket(origin, "access", Access)
|
|
86
|
+
const refresh = (origin: string) => bucket(origin, "refresh", Renewal)
|
|
87
|
+
|
|
88
|
+
const registeredClient = (origin: string, clientId: string) =>
|
|
89
|
+
opaque.test(clientId) ? clients(origin).get(clientId) : undefined
|
|
90
|
+
|
|
91
|
+
const activeGrant = async (origin: string, grant: Grant) =>
|
|
92
|
+
(await registeredClient(origin, grant.clientId))?.value.grants.some(
|
|
93
|
+
(current) => current.id === grant.id,
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
const location = (url: URL, status = 303) =>
|
|
97
|
+
new NextResponse(null, {
|
|
98
|
+
status,
|
|
99
|
+
headers: { ...headers, Location: url.href },
|
|
100
|
+
})
|
|
101
|
+
const oauthError = (error: string, status = 400) =>
|
|
102
|
+
Response.json({ error }, { status, headers })
|
|
103
|
+
|
|
104
|
+
const resourceUrl = (origin: string) => `${origin}/mcp`
|
|
105
|
+
const issuerUrl = (origin: string) =>
|
|
106
|
+
process.env["PF_RUNTIME_MODE"] === "deployed" ? resourceUrl(origin) : origin
|
|
107
|
+
export const resourceMetadata = (origin: string): Response =>
|
|
108
|
+
Response.json(
|
|
109
|
+
{
|
|
110
|
+
resource: resourceUrl(origin),
|
|
111
|
+
authorization_servers: [issuerUrl(origin)],
|
|
112
|
+
scopes_supported: [VIEW, ACT],
|
|
113
|
+
bearer_methods_supported: ["header"],
|
|
114
|
+
},
|
|
115
|
+
{ headers },
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
export const authMetadata = (origin: string): Response =>
|
|
119
|
+
Response.json(
|
|
120
|
+
{
|
|
121
|
+
issuer: issuerUrl(origin),
|
|
122
|
+
authorization_endpoint: `${origin}/mcp/authorize`,
|
|
123
|
+
token_endpoint: `${origin}/mcp/token`,
|
|
124
|
+
registration_endpoint: `${origin}/mcp/register`,
|
|
125
|
+
response_types_supported: ["code"],
|
|
126
|
+
code_challenge_methods_supported: ["S256"],
|
|
127
|
+
grant_types_supported: ["authorization_code", "refresh_token"],
|
|
128
|
+
token_endpoint_auth_methods_supported: ["none"],
|
|
129
|
+
scopes_supported: [VIEW, ACT],
|
|
130
|
+
},
|
|
131
|
+
{ headers },
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
const Registration = Schema.Struct({
|
|
135
|
+
client_name: Schema.NonEmptyString.check(Schema.isMaxLength(100)),
|
|
136
|
+
redirect_uris: Schema.NonEmptyArray(
|
|
137
|
+
Schema.String.check(Schema.isMaxLength(2048)),
|
|
138
|
+
).check(Schema.isMaxLength(5)),
|
|
139
|
+
token_endpoint_auth_method: Schema.optional(Schema.Literal("none")),
|
|
140
|
+
})
|
|
141
|
+
const validRedirect = (uri: string) => {
|
|
142
|
+
try {
|
|
143
|
+
const url = new URL(uri)
|
|
144
|
+
return (
|
|
145
|
+
!uri.includes("#") &&
|
|
146
|
+
((loopback(url) && Boolean(url.port)) ||
|
|
147
|
+
(process.env["PF_RUNTIME_MODE"] === "deployed" &&
|
|
148
|
+
url.protocol === "https:" &&
|
|
149
|
+
!url.username &&
|
|
150
|
+
!url.password))
|
|
151
|
+
)
|
|
152
|
+
} catch {
|
|
153
|
+
return false
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export const register = async (
|
|
158
|
+
request: Request,
|
|
159
|
+
origin: string,
|
|
160
|
+
): Promise<Response> => {
|
|
161
|
+
const body = await request.json().catch(() => null)
|
|
162
|
+
const parsed = Schema.decodeUnknownResult(Registration)(body)
|
|
163
|
+
if (Result.isFailure(parsed)) return oauthError("invalid_client_metadata")
|
|
164
|
+
const { client_name, redirect_uris } = parsed.success
|
|
165
|
+
if (!redirect_uris.every(validRedirect))
|
|
166
|
+
return oauthError("invalid_redirect_uri")
|
|
167
|
+
const store = clients(origin)
|
|
168
|
+
if (process.env["PF_RUNTIME_MODE"] !== "deployed") {
|
|
169
|
+
let count = 0
|
|
170
|
+
for await (const _ of store.entries()) {
|
|
171
|
+
if (++count >= 1024) return oauthError("temporarily_unavailable", 503)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const id = random()
|
|
175
|
+
const saved = await store.put(
|
|
176
|
+
id,
|
|
177
|
+
{
|
|
178
|
+
name: client_name,
|
|
179
|
+
redirects: redirect_uris,
|
|
180
|
+
grants: [],
|
|
181
|
+
},
|
|
182
|
+
Date.now() + registrationLifetime,
|
|
183
|
+
)
|
|
184
|
+
if (!saved) return oauthError("temporarily_unavailable", 503)
|
|
185
|
+
return Response.json(
|
|
186
|
+
{
|
|
187
|
+
client_id: id,
|
|
188
|
+
client_name,
|
|
189
|
+
redirect_uris,
|
|
190
|
+
token_endpoint_auth_method: "none",
|
|
191
|
+
grant_types: ["authorization_code", "refresh_token"],
|
|
192
|
+
response_types: ["code"],
|
|
193
|
+
},
|
|
194
|
+
{ status: 201, headers },
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const readAuthorization = (origin: string, query: URLSearchParams) => {
|
|
199
|
+
const clientId = query.get("client_id") ?? ""
|
|
200
|
+
const redirect = query.get("redirect_uri") ?? ""
|
|
201
|
+
const challenge = query.get("code_challenge") ?? ""
|
|
202
|
+
const state = query.get("state") ?? ""
|
|
203
|
+
const resource = query.get("resource")
|
|
204
|
+
if (
|
|
205
|
+
query.get("response_type") !== "code" ||
|
|
206
|
+
query.get("code_challenge_method") !== "S256" ||
|
|
207
|
+
!opaque.test(challenge) ||
|
|
208
|
+
state.length > 512 ||
|
|
209
|
+
(resource !== null && resource !== resourceUrl(origin))
|
|
210
|
+
)
|
|
211
|
+
return undefined
|
|
212
|
+
return { clientId, redirect, challenge, state }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const consentUrl = (origin: string, id: string) =>
|
|
216
|
+
new URL(`${consentPath}?id=${id}`, origin)
|
|
217
|
+
const cookieOptions = (origin: string) =>
|
|
218
|
+
({
|
|
219
|
+
httpOnly: true,
|
|
220
|
+
sameSite: "lax",
|
|
221
|
+
path: "/",
|
|
222
|
+
secure: origin.startsWith("https:"),
|
|
223
|
+
}) as const
|
|
224
|
+
|
|
225
|
+
export const authorize = async (
|
|
226
|
+
request: NextRequest,
|
|
227
|
+
origin: string,
|
|
228
|
+
): Promise<Response> => {
|
|
229
|
+
const authorization = readAuthorization(origin, request.nextUrl.searchParams)
|
|
230
|
+
if (!authorization) return oauthError("invalid_request")
|
|
231
|
+
const client = await registeredClient(origin, authorization.clientId)
|
|
232
|
+
if (!client?.value.redirects.includes(authorization.redirect))
|
|
233
|
+
return oauthError("invalid_request")
|
|
234
|
+
const store = pending(origin)
|
|
235
|
+
if (process.env["PF_RUNTIME_MODE"] !== "deployed") {
|
|
236
|
+
let count = 0
|
|
237
|
+
for await (const _ of store.entries()) {
|
|
238
|
+
if (++count >= 256) return oauthError("temporarily_unavailable", 503)
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const id = random()
|
|
242
|
+
const browser = random()
|
|
243
|
+
const expires = Date.now() + pendingLifetime
|
|
244
|
+
const saved = await store.put(
|
|
245
|
+
id,
|
|
246
|
+
{
|
|
247
|
+
...authorization,
|
|
248
|
+
// Resolve the grant after sign-in, but do not revive one disconnected
|
|
249
|
+
// while this authorization was in flight.
|
|
250
|
+
grantsAtStart: client.value.grants,
|
|
251
|
+
browser,
|
|
252
|
+
expires,
|
|
253
|
+
},
|
|
254
|
+
expires,
|
|
255
|
+
)
|
|
256
|
+
if (!saved) return oauthError("temporarily_unavailable", 503)
|
|
257
|
+
const consent = consentUrl(origin, id)
|
|
258
|
+
const login = new URL("/login", origin)
|
|
259
|
+
login.searchParams.set("redirect", `${consent.pathname}${consent.search}`)
|
|
260
|
+
const response = location(login)
|
|
261
|
+
response.cookies.set(cookieName(origin, id), browser, {
|
|
262
|
+
...cookieOptions(origin),
|
|
263
|
+
maxAge: pendingLifetime / 1000,
|
|
264
|
+
})
|
|
265
|
+
return response
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const browserPending = async (request: NextRequest, target: string | null) => {
|
|
269
|
+
const origin = mcpOrigin(request)
|
|
270
|
+
if (!origin || !target?.startsWith(`${consentPath}?`)) return undefined
|
|
271
|
+
const id = new URL(target, origin).searchParams.get("id") ?? ""
|
|
272
|
+
if (!opaque.test(id)) return undefined
|
|
273
|
+
const browser = request.cookies.get(cookieName(origin, id))?.value
|
|
274
|
+
if (!browser) return undefined
|
|
275
|
+
const record = await pending(origin).get(id)
|
|
276
|
+
if (!record || browser !== record.value.browser) return undefined
|
|
277
|
+
return { record, origin, id }
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const verifyHuman = async (
|
|
281
|
+
tokens: Tokens,
|
|
282
|
+
): Promise<{ human: Human; session: ProviderUserSession } | undefined> => {
|
|
283
|
+
const verified = await getAuthClient().verify(
|
|
284
|
+
subjects,
|
|
285
|
+
tokens.access,
|
|
286
|
+
undefined,
|
|
287
|
+
)
|
|
288
|
+
if (verified.err instanceof OAuthMetadataFetchError) throw verified.err
|
|
289
|
+
if (verified.err) return undefined
|
|
290
|
+
const session = verified.subject.properties
|
|
291
|
+
if (!("email" in session) || !session.humanSession || session.delegation)
|
|
292
|
+
return undefined
|
|
293
|
+
const expiry = decodeJwtPayload(tokens.access)?.exp
|
|
294
|
+
const ssoDeadline = session.sso?.expiresAt
|
|
295
|
+
const expires = Math.min((expiry ?? 0) * 1000, ssoDeadline ?? Infinity)
|
|
296
|
+
if (expires <= Date.now()) return undefined
|
|
297
|
+
return {
|
|
298
|
+
session,
|
|
299
|
+
human: {
|
|
300
|
+
email: session.email,
|
|
301
|
+
userId: session.userId,
|
|
302
|
+
tokens,
|
|
303
|
+
expires,
|
|
304
|
+
ssoDeadline,
|
|
305
|
+
},
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export const finishMcpLogin = async (
|
|
310
|
+
request: NextRequest,
|
|
311
|
+
redirect: string | null,
|
|
312
|
+
tokens: Tokens,
|
|
313
|
+
): Promise<NextResponse | undefined> => {
|
|
314
|
+
if (!redirect?.startsWith(`${consentPath}?`)) return undefined
|
|
315
|
+
const flow = await browserPending(request, redirect)
|
|
316
|
+
if (!flow || flow.record.value.human)
|
|
317
|
+
return NextResponse.json(
|
|
318
|
+
{ error: "Restart the MCP connection." },
|
|
319
|
+
{ status: 400, headers },
|
|
320
|
+
)
|
|
321
|
+
const verified = await verifyHuman(tokens).catch(() => undefined)
|
|
322
|
+
if (
|
|
323
|
+
!verified ||
|
|
324
|
+
!(await acceptVerifiedSession(verified.session, tokens.access).catch(
|
|
325
|
+
() => null,
|
|
326
|
+
))
|
|
327
|
+
)
|
|
328
|
+
return NextResponse.json(
|
|
329
|
+
{ error: "MCP requires a human sign-in." },
|
|
330
|
+
{ status: 403, headers },
|
|
331
|
+
)
|
|
332
|
+
const entry = flow.record.value
|
|
333
|
+
if (
|
|
334
|
+
entry.expires <= Date.now() ||
|
|
335
|
+
!(await pending(flow.origin).put(
|
|
336
|
+
flow.id,
|
|
337
|
+
{ ...entry, human: verified.human },
|
|
338
|
+
entry.expires,
|
|
339
|
+
flow.record,
|
|
340
|
+
))
|
|
341
|
+
)
|
|
342
|
+
return NextResponse.json(
|
|
343
|
+
{ error: "Restart the MCP connection." },
|
|
344
|
+
{ status: 400, headers },
|
|
345
|
+
)
|
|
346
|
+
return location(consentUrl(flow.origin, flow.id))
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const escapeHtml = (text: string) =>
|
|
350
|
+
text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">")
|
|
351
|
+
|
|
352
|
+
const finishAuthorization = async (
|
|
353
|
+
{
|
|
354
|
+
record: { value: entry },
|
|
355
|
+
origin,
|
|
356
|
+
id,
|
|
357
|
+
}: NonNullable<Awaited<ReturnType<typeof browserPending>>>,
|
|
358
|
+
approved?: Pick<Code, "grant" | "human">,
|
|
359
|
+
): Promise<Response> => {
|
|
360
|
+
const redirect = new URL(entry.redirect)
|
|
361
|
+
if (entry.state) redirect.searchParams.set("state", entry.state)
|
|
362
|
+
if (approved) {
|
|
363
|
+
const code = random()
|
|
364
|
+
const saved = await codes(origin).put(
|
|
365
|
+
hash(code),
|
|
366
|
+
{
|
|
367
|
+
clientId: entry.clientId,
|
|
368
|
+
challenge: entry.challenge,
|
|
369
|
+
redirect: entry.redirect,
|
|
370
|
+
state: entry.state,
|
|
371
|
+
...approved,
|
|
372
|
+
},
|
|
373
|
+
Date.now() + codeLifetime,
|
|
374
|
+
)
|
|
375
|
+
if (!saved) return oauthError("temporarily_unavailable", 503)
|
|
376
|
+
redirect.searchParams.set("code", code)
|
|
377
|
+
} else {
|
|
378
|
+
redirect.searchParams.set("error", "access_denied")
|
|
379
|
+
}
|
|
380
|
+
const response = location(redirect)
|
|
381
|
+
response.cookies.set(cookieName(origin, id), "", {
|
|
382
|
+
...cookieOptions(origin),
|
|
383
|
+
maxAge: 0,
|
|
384
|
+
})
|
|
385
|
+
return response
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const sameOriginForm = (request: Request, origin: string) =>
|
|
389
|
+
request.headers.get("origin") === origin &&
|
|
390
|
+
request.headers.get("content-type")?.split(";")[0] ===
|
|
391
|
+
"application/x-www-form-urlencoded"
|
|
392
|
+
|
|
393
|
+
export const manageConnections = async (
|
|
394
|
+
request: NextRequest,
|
|
395
|
+
origin: string,
|
|
396
|
+
): Promise<Response> => {
|
|
397
|
+
const path = "/mcp/connections"
|
|
398
|
+
const { accessToken } = getCookieNamesFromHost(new URL(origin).host)
|
|
399
|
+
const token = request.cookies.get(accessToken)?.value
|
|
400
|
+
if (request.method === "POST" && !sameOriginForm(request, origin))
|
|
401
|
+
return oauthError("invalid_request", 403)
|
|
402
|
+
const verified = token
|
|
403
|
+
? await getAuthClient().verify(subjects, token, undefined)
|
|
404
|
+
: undefined
|
|
405
|
+
if (!token || !verified || verified.err) {
|
|
406
|
+
const login = new URL("/login", origin)
|
|
407
|
+
login.searchParams.set("redirect", path)
|
|
408
|
+
return request.method === "GET"
|
|
409
|
+
? location(login)
|
|
410
|
+
: oauthError("access_denied", 401)
|
|
411
|
+
}
|
|
412
|
+
const session = verified.subject.properties
|
|
413
|
+
if (
|
|
414
|
+
!("email" in session) ||
|
|
415
|
+
!session.humanSession ||
|
|
416
|
+
session.delegation ||
|
|
417
|
+
!(await acceptVerifiedSession(session, token))
|
|
418
|
+
)
|
|
419
|
+
return oauthError("access_denied", 403)
|
|
420
|
+
|
|
421
|
+
const store = clients(origin)
|
|
422
|
+
const owned = []
|
|
423
|
+
for await (const client of store.entries()) {
|
|
424
|
+
const grant = client.value.grants.find(
|
|
425
|
+
(grant) => grant.userId === session.userId,
|
|
426
|
+
)
|
|
427
|
+
if (grant) owned.push({ client, grant })
|
|
428
|
+
}
|
|
429
|
+
if (request.method === "POST") {
|
|
430
|
+
const id = (await request.formData().catch(() => null))?.get("grant")
|
|
431
|
+
const connection = owned.find(({ grant }) => grant.id === id)
|
|
432
|
+
if (!connection) return oauthError("invalid_grant", 404)
|
|
433
|
+
const { client, grant } = connection
|
|
434
|
+
const grants = client.value.grants.filter(({ id }) => id !== grant.id)
|
|
435
|
+
// Codes and tokens check the current grant ID, not just the registration.
|
|
436
|
+
// Revoking it invalidates every credential without touching Dashboard cookies.
|
|
437
|
+
if (
|
|
438
|
+
!(await store.put(
|
|
439
|
+
grant.clientId,
|
|
440
|
+
{ ...client.value, grants },
|
|
441
|
+
grants.length === 0 ? Date.now() + registrationLifetime : undefined,
|
|
442
|
+
client,
|
|
443
|
+
))
|
|
444
|
+
)
|
|
445
|
+
return oauthError("invalid_grant", 409)
|
|
446
|
+
return location(new URL(path, origin))
|
|
447
|
+
}
|
|
448
|
+
const list = owned.map(({ client, grant }) => {
|
|
449
|
+
const name = escapeHtml(client.value.name)
|
|
450
|
+
const scope = grant.scope === VIEW ? "View work" : "View and act"
|
|
451
|
+
return `<li><form method="post" action="${path}" aria-labelledby="${grant.id}"><p id="${grant.id}">${name} — ${scope}</p><button type="submit" name="grant" value="${grant.id}">Disconnect ${name}</button></form></li>`
|
|
452
|
+
})
|
|
453
|
+
return new Response(
|
|
454
|
+
`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>MCP connections</title></head><body><main><h1>MCP connections</h1><p>Environment: ${escapeHtml(origin)}</p><p>Signed in as ${escapeHtml(session.email)}</p>${list.length ? `<ul>${list.join("")}</ul>` : "<p>No connected clients.</p>"}<a href="/">Back to Dashboard</a></main></body></html>`,
|
|
455
|
+
{
|
|
456
|
+
headers: {
|
|
457
|
+
...headers,
|
|
458
|
+
"Referrer-Policy": "same-origin",
|
|
459
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
460
|
+
},
|
|
461
|
+
},
|
|
462
|
+
)
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const consentStyles = `
|
|
466
|
+
* { box-sizing: border-box; }
|
|
467
|
+
body {
|
|
468
|
+
margin: 0;
|
|
469
|
+
padding: clamp(0.75rem, 4vw, 3rem);
|
|
470
|
+
background: #f8fafc;
|
|
471
|
+
color: #0f172a;
|
|
472
|
+
font: 1rem/1.5 system-ui, sans-serif;
|
|
473
|
+
}
|
|
474
|
+
main {
|
|
475
|
+
max-width: 36rem;
|
|
476
|
+
margin-inline: auto;
|
|
477
|
+
padding: clamp(1rem, 5vw, 2rem);
|
|
478
|
+
border: 1px solid #e2e8f0;
|
|
479
|
+
border-radius: 1.5rem;
|
|
480
|
+
background: #fff;
|
|
481
|
+
box-shadow: 0 1px 3px #0f172a14;
|
|
482
|
+
overflow-wrap: anywhere;
|
|
483
|
+
}
|
|
484
|
+
.brand { display: flex; align-items: center; gap: 0.625rem; font-weight: 700; }
|
|
485
|
+
.mark {
|
|
486
|
+
display: grid;
|
|
487
|
+
place-items: center;
|
|
488
|
+
inline-size: 2.25rem;
|
|
489
|
+
block-size: 2.25rem;
|
|
490
|
+
border-radius: 0.625rem;
|
|
491
|
+
background: #065f46;
|
|
492
|
+
color: #fff;
|
|
493
|
+
font-size: 0.875rem;
|
|
494
|
+
}
|
|
495
|
+
.eyebrow {
|
|
496
|
+
margin: 1.5rem 0 0.25rem;
|
|
497
|
+
color: #065f46;
|
|
498
|
+
font-size: 0.75rem;
|
|
499
|
+
font-weight: 700;
|
|
500
|
+
letter-spacing: 0.08em;
|
|
501
|
+
text-transform: uppercase;
|
|
502
|
+
}
|
|
503
|
+
h1 { margin: 0; font-size: clamp(1.5rem, 5vw, 2rem); line-height: 1.2; }
|
|
504
|
+
.details {
|
|
505
|
+
margin: 1.25rem 0;
|
|
506
|
+
padding: 0.875rem 1rem;
|
|
507
|
+
border-radius: 0.875rem;
|
|
508
|
+
background: #f1f5f9;
|
|
509
|
+
}
|
|
510
|
+
.details p { margin: 0; }
|
|
511
|
+
.details p + p { margin-block-start: 0.375rem; }
|
|
512
|
+
.notice, .description { color: #475569; font-size: 0.9375rem; }
|
|
513
|
+
fieldset { min-inline-size: 0; margin: 1.25rem 0 0; padding: 0; border: 0; }
|
|
514
|
+
legend { padding: 0; font-weight: 700; }
|
|
515
|
+
.option {
|
|
516
|
+
display: grid;
|
|
517
|
+
grid-template-columns: auto minmax(0, 1fr);
|
|
518
|
+
align-items: start;
|
|
519
|
+
gap: 0.75rem;
|
|
520
|
+
margin-block-start: 0.5rem;
|
|
521
|
+
padding: 0.75rem;
|
|
522
|
+
border: 1px solid #cbd5e1;
|
|
523
|
+
border-radius: 0.875rem;
|
|
524
|
+
cursor: pointer;
|
|
525
|
+
}
|
|
526
|
+
.option:has(input:checked) { border-color: #047857; background: #ecfdf5; }
|
|
527
|
+
input[type="radio"] { margin: 0.2rem 0 0; inline-size: 1.125rem; block-size: 1.125rem; }
|
|
528
|
+
.description { display: block; }
|
|
529
|
+
.actions { display: flex; flex-wrap: wrap; gap: 0.75rem; margin-block-start: 1.5rem; }
|
|
530
|
+
button {
|
|
531
|
+
min-block-size: 3rem;
|
|
532
|
+
padding: 0.625rem 1.25rem;
|
|
533
|
+
border: 1px solid #64748b;
|
|
534
|
+
border-radius: 999px;
|
|
535
|
+
background: #fff;
|
|
536
|
+
color: #0f172a;
|
|
537
|
+
font: inherit;
|
|
538
|
+
font-weight: 600;
|
|
539
|
+
cursor: pointer;
|
|
540
|
+
}
|
|
541
|
+
button:hover { background: #f1f5f9; }
|
|
542
|
+
.primary { border-color: #065f46; background: #065f46; color: #fff; }
|
|
543
|
+
.primary:hover { background: #064e3b; }
|
|
544
|
+
.manage { margin: 1.25rem 0 0; }
|
|
545
|
+
a { color: #065f46; text-underline-offset: 0.15em; }
|
|
546
|
+
:focus-visible { outline: 3px solid #0f766e; outline-offset: 3px; }
|
|
547
|
+
`
|
|
548
|
+
const consentStyleHash = createHash("sha256")
|
|
549
|
+
.update(consentStyles)
|
|
550
|
+
.digest("base64")
|
|
551
|
+
|
|
552
|
+
export const consent = async (request: NextRequest): Promise<Response> => {
|
|
553
|
+
const flow = await browserPending(
|
|
554
|
+
request,
|
|
555
|
+
`${request.nextUrl.pathname}${request.nextUrl.search}`,
|
|
556
|
+
)
|
|
557
|
+
const human = flow?.record.value.human
|
|
558
|
+
if (!flow || !human) return oauthError("invalid_request")
|
|
559
|
+
const { record, origin, id } = flow
|
|
560
|
+
const entry = record.value
|
|
561
|
+
const client = await registeredClient(origin, entry.clientId)
|
|
562
|
+
if (!client) return oauthError("invalid_client")
|
|
563
|
+
const existing = entry.grantsAtStart.find(
|
|
564
|
+
(grant) => grant.userId === human.userId,
|
|
565
|
+
)
|
|
566
|
+
if (
|
|
567
|
+
client.value.grants.find((grant) => grant.userId === human.userId)?.id !==
|
|
568
|
+
existing?.id ||
|
|
569
|
+
human.expires <= Date.now()
|
|
570
|
+
)
|
|
571
|
+
return oauthError("invalid_request")
|
|
572
|
+
if (request.method === "GET") {
|
|
573
|
+
const name = escapeHtml(client.value.name)
|
|
574
|
+
const accessDetails = existing
|
|
575
|
+
? `<p>Existing access: ${existing.scope === VIEW ? "View work" : "View and act"}.</p>
|
|
576
|
+
<p class="notice">Reconnecting only restores access. It does not run an interrupted action.</p>`
|
|
577
|
+
: `<fieldset>
|
|
578
|
+
<legend>Choose access</legend>
|
|
579
|
+
<label class="option">
|
|
580
|
+
<input type="radio" name="access" value="read" aria-labelledby="read-title" aria-describedby="read-description" checked>
|
|
581
|
+
<span><strong id="read-title">View work</strong>
|
|
582
|
+
<span class="description" id="read-description">Read-only access to work and forms.</span>
|
|
583
|
+
</span>
|
|
584
|
+
</label>
|
|
585
|
+
<label class="option">
|
|
586
|
+
<input type="radio" name="access" value="act" aria-labelledby="act-title" aria-describedby="act-description">
|
|
587
|
+
<span><strong id="act-title">View and act</strong>
|
|
588
|
+
<span class="description" id="act-description">View work, start Processes, complete To-Dos, and create, edit, or delete List records where your runtime permissions allow.</span>
|
|
589
|
+
</span>
|
|
590
|
+
</label>
|
|
591
|
+
</fieldset>`
|
|
592
|
+
const approve = existing
|
|
593
|
+
? `<button class="primary" type="submit" name="action" value="reconnect">Reconnect</button>`
|
|
594
|
+
: `<button class="primary" type="submit">Allow access</button>`
|
|
595
|
+
return new Response(
|
|
596
|
+
`<!doctype html>
|
|
597
|
+
<html lang="en">
|
|
598
|
+
<head>
|
|
599
|
+
<meta charset="utf-8">
|
|
600
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
601
|
+
<title>${existing ? "Reconnect" : "Connect"} to Process Focus</title>
|
|
602
|
+
<style>${consentStyles}</style>
|
|
603
|
+
</head>
|
|
604
|
+
<body>
|
|
605
|
+
<main>
|
|
606
|
+
<header>
|
|
607
|
+
<div class="brand"><span class="mark" aria-hidden="true">PF</span> Process Focus</div>
|
|
608
|
+
<p class="eyebrow">MCP connection</p>
|
|
609
|
+
<h1>${existing ? "Reconnect" : "Connect"} ${name} to Process Focus?</h1>
|
|
610
|
+
</header>
|
|
611
|
+
<div class="details">
|
|
612
|
+
<p><strong>Environment:</strong> ${escapeHtml(origin)}</p>
|
|
613
|
+
<p><strong>Signed in as</strong> ${escapeHtml(human.email)}</p>
|
|
614
|
+
</div>
|
|
615
|
+
<p class="notice">Only approve a connection you just started in this client.</p>
|
|
616
|
+
<form method="post" action="${consentPath}?id=${id}">
|
|
617
|
+
${accessDetails}
|
|
618
|
+
<div class="actions">
|
|
619
|
+
${approve}
|
|
620
|
+
<button type="submit" name="action" value="deny">Deny</button>
|
|
621
|
+
</div>
|
|
622
|
+
</form>
|
|
623
|
+
<p class="manage"><a href="/mcp/connections">Manage MCP connections</a></p>
|
|
624
|
+
</main>
|
|
625
|
+
</body>
|
|
626
|
+
</html>`,
|
|
627
|
+
{
|
|
628
|
+
headers: {
|
|
629
|
+
...headers,
|
|
630
|
+
// Keep Origin on the same-origin POST without disclosing the consent URL to the client.
|
|
631
|
+
"Referrer-Policy": "same-origin",
|
|
632
|
+
// Browsers can apply form-action to the OAuth redirect after POST.
|
|
633
|
+
"Content-Security-Policy": `default-src 'none'; frame-ancestors 'none'; form-action 'self' ${new URL(entry.redirect).origin}; base-uri 'none'; style-src 'sha256-${consentStyleHash}'`,
|
|
634
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
635
|
+
},
|
|
636
|
+
},
|
|
637
|
+
)
|
|
638
|
+
}
|
|
639
|
+
if (!sameOriginForm(request, origin))
|
|
640
|
+
return oauthError("invalid_request", 403)
|
|
641
|
+
const form = await request.formData().catch(() => null)
|
|
642
|
+
if (!form || entry.expires <= Date.now() || human.expires <= Date.now())
|
|
643
|
+
return oauthError("invalid_request")
|
|
644
|
+
const denied = form.get("action") === "deny"
|
|
645
|
+
const choice = form.get("access")
|
|
646
|
+
if (
|
|
647
|
+
!denied &&
|
|
648
|
+
(existing
|
|
649
|
+
? form.get("action") !== "reconnect"
|
|
650
|
+
: choice !== "read" && choice !== "act")
|
|
651
|
+
)
|
|
652
|
+
return oauthError("invalid_request")
|
|
653
|
+
if (!(await pending(origin).remove(id, record)))
|
|
654
|
+
return oauthError("invalid_request")
|
|
655
|
+
if (denied) return finishAuthorization(flow)
|
|
656
|
+
if (existing) {
|
|
657
|
+
// Resume the existing scope; the OAuth flow never carries an MCP request.
|
|
658
|
+
return (await activeGrant(origin, existing))
|
|
659
|
+
? finishAuthorization(flow, { grant: existing, human })
|
|
660
|
+
: oauthError("invalid_request")
|
|
661
|
+
}
|
|
662
|
+
const grant: Grant = {
|
|
663
|
+
id: random(),
|
|
664
|
+
clientId: entry.clientId,
|
|
665
|
+
userId: human.userId,
|
|
666
|
+
scope: choice === "act" ? `${VIEW} ${ACT}` : VIEW,
|
|
667
|
+
}
|
|
668
|
+
if (
|
|
669
|
+
!(await clients(origin).put(
|
|
670
|
+
entry.clientId,
|
|
671
|
+
{
|
|
672
|
+
...client.value,
|
|
673
|
+
grants: [...client.value.grants, grant],
|
|
674
|
+
},
|
|
675
|
+
undefined,
|
|
676
|
+
client,
|
|
677
|
+
))
|
|
678
|
+
)
|
|
679
|
+
return oauthError("invalid_request")
|
|
680
|
+
return finishAuthorization(flow, { grant, human })
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
const issueTokens = async (
|
|
684
|
+
origin: string,
|
|
685
|
+
grant: Grant,
|
|
686
|
+
human: Human,
|
|
687
|
+
): Promise<Response> => {
|
|
688
|
+
if (!(await activeGrant(origin, grant))) return oauthError("invalid_grant")
|
|
689
|
+
const token = random()
|
|
690
|
+
const renewal = random()
|
|
691
|
+
const now = Date.now()
|
|
692
|
+
const expiresIn = Math.min(300, Math.floor((human.expires - now) / 1000))
|
|
693
|
+
if (expiresIn <= 0) return oauthError("invalid_grant")
|
|
694
|
+
const refreshExpires = Math.min(
|
|
695
|
+
now + (human.tokens.refreshExpiresIn ?? 86_400) * 1000,
|
|
696
|
+
human.ssoDeadline ?? Infinity,
|
|
697
|
+
)
|
|
698
|
+
const savedAccess = await access(origin).put(
|
|
699
|
+
hash(token),
|
|
700
|
+
{
|
|
701
|
+
grant,
|
|
702
|
+
token: human.tokens.access,
|
|
703
|
+
},
|
|
704
|
+
now + expiresIn * 1000,
|
|
705
|
+
)
|
|
706
|
+
const savedRefresh = await refresh(origin).put(
|
|
707
|
+
hash(renewal),
|
|
708
|
+
{
|
|
709
|
+
grant,
|
|
710
|
+
expires: refreshExpires,
|
|
711
|
+
ssoDeadline: human.ssoDeadline,
|
|
712
|
+
issuerRefreshToken: human.tokens.refresh,
|
|
713
|
+
lockedUntil: 0,
|
|
714
|
+
},
|
|
715
|
+
refreshExpires,
|
|
716
|
+
)
|
|
717
|
+
if (!savedAccess || !savedRefresh)
|
|
718
|
+
return oauthError("temporarily_unavailable", 503)
|
|
719
|
+
return Response.json(
|
|
720
|
+
{
|
|
721
|
+
access_token: token,
|
|
722
|
+
refresh_token: renewal,
|
|
723
|
+
expires_in: expiresIn,
|
|
724
|
+
token_type: "Bearer",
|
|
725
|
+
scope: grant.scope,
|
|
726
|
+
},
|
|
727
|
+
{ headers },
|
|
728
|
+
)
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
export const token = async (
|
|
732
|
+
request: Request,
|
|
733
|
+
origin: string,
|
|
734
|
+
): Promise<Response> => {
|
|
735
|
+
if (
|
|
736
|
+
request.headers.get("content-type")?.split(";")[0] !==
|
|
737
|
+
"application/x-www-form-urlencoded"
|
|
738
|
+
)
|
|
739
|
+
return oauthError("invalid_request")
|
|
740
|
+
const form = await request.formData().catch(() => null)
|
|
741
|
+
if (!form) return oauthError("invalid_request")
|
|
742
|
+
const clientId = form.get("client_id")
|
|
743
|
+
if (
|
|
744
|
+
typeof clientId !== "string" ||
|
|
745
|
+
!(await registeredClient(origin, clientId))
|
|
746
|
+
)
|
|
747
|
+
return oauthError("invalid_client")
|
|
748
|
+
const resource = form.get("resource")
|
|
749
|
+
if (resource !== null && resource !== resourceUrl(origin))
|
|
750
|
+
return oauthError("invalid_target")
|
|
751
|
+
if (form.get("grant_type") === "authorization_code") {
|
|
752
|
+
const raw = form.get("code")
|
|
753
|
+
const verifier = form.get("code_verifier")
|
|
754
|
+
if (
|
|
755
|
+
typeof raw !== "string" ||
|
|
756
|
+
!opaque.test(raw) ||
|
|
757
|
+
typeof verifier !== "string" ||
|
|
758
|
+
!/^[A-Za-z0-9._~-]{43,128}$/.test(verifier)
|
|
759
|
+
)
|
|
760
|
+
return oauthError("invalid_grant")
|
|
761
|
+
const id = hash(raw)
|
|
762
|
+
const store = codes(origin)
|
|
763
|
+
const record = await store.get(id)
|
|
764
|
+
const code = record?.value
|
|
765
|
+
if (
|
|
766
|
+
!record ||
|
|
767
|
+
!code ||
|
|
768
|
+
code.clientId !== clientId ||
|
|
769
|
+
code.redirect !== form.get("redirect_uri") ||
|
|
770
|
+
!timingSafeEqual(
|
|
771
|
+
Buffer.from(code.challenge),
|
|
772
|
+
Buffer.from(hash(verifier)),
|
|
773
|
+
) ||
|
|
774
|
+
!(await store.remove(id, record))
|
|
775
|
+
)
|
|
776
|
+
return oauthError("invalid_grant")
|
|
777
|
+
return issueTokens(origin, code.grant, code.human)
|
|
778
|
+
}
|
|
779
|
+
if (form.get("grant_type") === "refresh_token") {
|
|
780
|
+
const raw = form.get("refresh_token")
|
|
781
|
+
if (typeof raw !== "string" || !opaque.test(raw))
|
|
782
|
+
return oauthError("invalid_grant")
|
|
783
|
+
const id = hash(raw)
|
|
784
|
+
const store = refresh(origin)
|
|
785
|
+
const original = await store.get(id)
|
|
786
|
+
const entry = original?.value
|
|
787
|
+
if (
|
|
788
|
+
!original ||
|
|
789
|
+
!entry ||
|
|
790
|
+
entry.grant.clientId !== clientId ||
|
|
791
|
+
!(await activeGrant(origin, entry.grant))
|
|
792
|
+
)
|
|
793
|
+
return oauthError("invalid_grant")
|
|
794
|
+
const scope = form.get("scope")
|
|
795
|
+
if (scope !== null && scope !== entry.grant.scope)
|
|
796
|
+
return oauthError("invalid_scope")
|
|
797
|
+
const now = Date.now()
|
|
798
|
+
if (entry.lockedUntil > now)
|
|
799
|
+
return oauthError("temporarily_unavailable", 503)
|
|
800
|
+
let locked = await store.put(
|
|
801
|
+
id,
|
|
802
|
+
{ ...entry, lockedUntil: now + renewalLease },
|
|
803
|
+
entry.expires,
|
|
804
|
+
original,
|
|
805
|
+
)
|
|
806
|
+
if (!locked) return oauthError("temporarily_unavailable", 503)
|
|
807
|
+
try {
|
|
808
|
+
const result = await createClient({
|
|
809
|
+
...getFrontendAuthClientConfig(),
|
|
810
|
+
fetch: async (input, init) => {
|
|
811
|
+
const response = await fetch(input, init)
|
|
812
|
+
// OpenAuth maps other HTTP failures to InvalidRefreshTokenError.
|
|
813
|
+
if (response.status === 429 || response.status >= 500)
|
|
814
|
+
throw new Error("Issuer temporarily unavailable")
|
|
815
|
+
return response
|
|
816
|
+
},
|
|
817
|
+
}).refresh(entry.issuerRefreshToken)
|
|
818
|
+
if (result.err || !result.tokens) return oauthError("invalid_grant")
|
|
819
|
+
// The issuer has rotated its token. Keep that rotation even if
|
|
820
|
+
// signature verification cannot reach the issuer's metadata.
|
|
821
|
+
const rotated = await store.put(
|
|
822
|
+
id,
|
|
823
|
+
{
|
|
824
|
+
...locked.value,
|
|
825
|
+
issuerRefreshToken: result.tokens.refresh,
|
|
826
|
+
},
|
|
827
|
+
entry.expires,
|
|
828
|
+
locked,
|
|
829
|
+
)
|
|
830
|
+
if (!rotated) return oauthError("invalid_grant")
|
|
831
|
+
locked = rotated
|
|
832
|
+
// The issuer renews the human session. GraphQL rechecks current
|
|
833
|
+
// runtime authority on each tool call, not when rotating credentials.
|
|
834
|
+
const verified = await verifyHuman(result.tokens)
|
|
835
|
+
if (
|
|
836
|
+
!verified ||
|
|
837
|
+
verified.human.userId !== entry.grant.userId ||
|
|
838
|
+
verified.human.ssoDeadline !== entry.ssoDeadline ||
|
|
839
|
+
entry.expires <= Date.now()
|
|
840
|
+
)
|
|
841
|
+
return oauthError("invalid_grant")
|
|
842
|
+
const response = await issueTokens(origin, entry.grant, verified.human)
|
|
843
|
+
if (response.ok) {
|
|
844
|
+
if (!(await store.remove(id, locked)))
|
|
845
|
+
return oauthError("invalid_grant")
|
|
846
|
+
locked = undefined
|
|
847
|
+
}
|
|
848
|
+
return response
|
|
849
|
+
} catch {
|
|
850
|
+
return oauthError("temporarily_unavailable", 503)
|
|
851
|
+
} finally {
|
|
852
|
+
if (locked)
|
|
853
|
+
await store.put(
|
|
854
|
+
id,
|
|
855
|
+
{ ...locked.value, lockedUntil: 0 },
|
|
856
|
+
entry.expires,
|
|
857
|
+
locked,
|
|
858
|
+
)
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
return oauthError("unsupported_grant_type")
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
export type McpAccess = {
|
|
865
|
+
readonly accessToken: string
|
|
866
|
+
readonly userId: string
|
|
867
|
+
readonly canAct: boolean
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
export const readMcpAccess = async (
|
|
871
|
+
request: Request,
|
|
872
|
+
origin: string,
|
|
873
|
+
): Promise<McpAccess | undefined> => {
|
|
874
|
+
const authorization = /^Bearer ([A-Za-z0-9_-]{43})$/i.exec(
|
|
875
|
+
request.headers.get("authorization") ?? "",
|
|
876
|
+
)
|
|
877
|
+
const value = authorization?.[1]
|
|
878
|
+
if (!value) return undefined
|
|
879
|
+
const entry = (await access(origin).get(hash(value)))?.value
|
|
880
|
+
if (
|
|
881
|
+
!entry ||
|
|
882
|
+
!(await activeGrant(origin, entry.grant)) ||
|
|
883
|
+
!entry.grant.scope.split(" ").includes(VIEW)
|
|
884
|
+
)
|
|
885
|
+
return undefined
|
|
886
|
+
return {
|
|
887
|
+
accessToken: entry.token,
|
|
888
|
+
userId: entry.grant.userId,
|
|
889
|
+
canAct: entry.grant.scope.split(" ").includes(ACT),
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
export const challenge = (origin: string): Response =>
|
|
894
|
+
new Response(null, {
|
|
895
|
+
status: 401,
|
|
896
|
+
headers: {
|
|
897
|
+
...headers,
|
|
898
|
+
"WWW-Authenticate": `Bearer resource_metadata="${origin}/.well-known/oauth-protected-resource/mcp"`,
|
|
899
|
+
},
|
|
900
|
+
})
|