clinkx 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/README.md +4 -2
- package/clinkx-workflows/dist/architecture-arena/admission.d.ts +16 -0
- package/clinkx-workflows/dist/architecture-arena/admission.js +107 -0
- package/clinkx-workflows/dist/architecture-arena/admission.js.map +1 -0
- package/clinkx-workflows/dist/architecture-arena/contract.d.ts +1368 -0
- package/clinkx-workflows/dist/architecture-arena/contract.js +254 -0
- package/clinkx-workflows/dist/architecture-arena/contract.js.map +1 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.d.ts +53 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.js +511 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.js.map +1 -0
- package/clinkx-workflows/dist/architecture-comparison/admission.d.ts +17 -1
- package/clinkx-workflows/dist/architecture-comparison/admission.js +27 -4
- package/clinkx-workflows/dist/architecture-comparison/admission.js.map +1 -1
- package/clinkx-workflows/dist/architecture-comparison/contract.d.ts +59 -62
- package/clinkx-workflows/dist/architecture-comparison/contract.js +4 -21
- package/clinkx-workflows/dist/architecture-comparison/contract.js.map +1 -1
- package/clinkx-workflows/dist/architecture-comparison/runtime.d.ts +4 -3
- package/clinkx-workflows/dist/architecture-comparison/runtime.js +22 -67
- package/clinkx-workflows/dist/architecture-comparison/runtime.js.map +1 -1
- package/clinkx-workflows/dist/artifacts.d.ts +19 -4
- package/clinkx-workflows/dist/artifacts.js +16 -7
- package/clinkx-workflows/dist/artifacts.js.map +1 -1
- package/clinkx-workflows/dist/backend.d.ts +30 -0
- package/clinkx-workflows/dist/clink-client.d.ts +8 -2
- package/clinkx-workflows/dist/clink-client.js +4 -2
- package/clinkx-workflows/dist/clink-client.js.map +1 -1
- package/clinkx-workflows/dist/context-preflight.d.ts +6 -0
- package/clinkx-workflows/dist/context-preflight.js.map +1 -1
- package/clinkx-workflows/dist/council-execution-policy.d.ts +3 -1
- package/clinkx-workflows/dist/council-execution-policy.js +4 -0
- package/clinkx-workflows/dist/council-execution-policy.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/contracts.d.ts +586 -586
- package/clinkx-workflows/dist/dashboard/progress-report-contract.d.ts +28 -28
- package/clinkx-workflows/dist/dashboard/session-log-contract.d.ts +32 -32
- package/clinkx-workflows/dist/dashboard-ui/assets/index-DRId2DVA.js +119 -0
- package/clinkx-workflows/dist/dashboard-ui/assets/style-CHAw57y5.css +1 -0
- package/clinkx-workflows/dist/dashboard-ui/index.html +2 -2
- package/clinkx-workflows/dist/delegate-signature.d.ts +4 -3
- package/clinkx-workflows/dist/delegate-signature.js +4 -3
- package/clinkx-workflows/dist/delegate-signature.js.map +1 -1
- package/clinkx-workflows/dist/durable-file.d.ts +69 -0
- package/clinkx-workflows/dist/durable-file.js +145 -0
- package/clinkx-workflows/dist/durable-file.js.map +1 -0
- package/clinkx-workflows/dist/engine.d.ts +42 -7
- package/clinkx-workflows/dist/engine.js +196 -24
- package/clinkx-workflows/dist/engine.js.map +1 -1
- package/clinkx-workflows/dist/ping-pong/admission.d.ts +127 -0
- package/clinkx-workflows/dist/ping-pong/admission.js +522 -0
- package/clinkx-workflows/dist/ping-pong/admission.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/contract.d.ts +2539 -0
- package/clinkx-workflows/dist/ping-pong/contract.js +819 -0
- package/clinkx-workflows/dist/ping-pong/contract.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/control.d.ts +67 -0
- package/clinkx-workflows/dist/ping-pong/control.js +12 -0
- package/clinkx-workflows/dist/ping-pong/control.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.d.ts +24 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.js +55 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/runtime.d.ts +235 -0
- package/clinkx-workflows/dist/ping-pong/runtime.js +1183 -0
- package/clinkx-workflows/dist/ping-pong/runtime.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/settle.d.ts +57 -0
- package/clinkx-workflows/dist/ping-pong/settle.js +58 -0
- package/clinkx-workflows/dist/ping-pong/settle.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.d.ts +65 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.js +138 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.js.map +1 -0
- package/clinkx-workflows/dist/plan-review/publication.d.ts +2 -1
- package/clinkx-workflows/dist/plan-review/publication.js +5 -6
- package/clinkx-workflows/dist/plan-review/publication.js.map +1 -1
- package/clinkx-workflows/dist/plan-review/snapshot.d.ts +3 -2
- package/clinkx-workflows/dist/plan-review/snapshot.js +8 -8
- package/clinkx-workflows/dist/plan-review/snapshot.js.map +1 -1
- package/clinkx-workflows/dist/recipe-host.d.ts +315 -0
- package/clinkx-workflows/dist/recipe-host.js +183 -0
- package/clinkx-workflows/dist/recipe-host.js.map +1 -0
- package/clinkx-workflows/dist/recipe-seats.d.ts +96 -0
- package/clinkx-workflows/dist/recipe-seats.js +163 -0
- package/clinkx-workflows/dist/recipe-seats.js.map +1 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.d.ts +67 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.js +111 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.js.map +1 -0
- package/clinkx-workflows/dist/reducers/index.d.ts +9 -1
- package/clinkx-workflows/dist/reducers/index.js +30 -0
- package/clinkx-workflows/dist/reducers/index.js.map +1 -1
- package/clinkx-workflows/dist/run-lock-reclaim.d.ts +44 -0
- package/clinkx-workflows/dist/run-lock-reclaim.js +86 -0
- package/clinkx-workflows/dist/run-lock-reclaim.js.map +1 -0
- package/clinkx-workflows/dist/state-schema.d.ts +11 -0
- package/clinkx-workflows/dist/state-schema.js +4 -1
- package/clinkx-workflows/dist/state-schema.js.map +1 -1
- package/clinkx-workflows/dist/state.d.ts +178 -3
- package/clinkx-workflows/dist/state.js +618 -83
- package/clinkx-workflows/dist/state.js.map +1 -1
- package/clinkx-workflows/dist/transport.d.ts +2 -1
- package/clinkx-workflows/dist/transport.js +5 -0
- package/clinkx-workflows/dist/transport.js.map +1 -1
- package/clinkx-workflows/dist/validators/architecture-arena.d.ts +6 -0
- package/clinkx-workflows/dist/validators/architecture-arena.js +16 -0
- package/clinkx-workflows/dist/validators/architecture-arena.js.map +1 -0
- package/clinkx-workflows/dist/validators/index.js +4 -0
- package/clinkx-workflows/dist/validators/index.js.map +1 -1
- package/clinkx-workflows/dist/validators/ping-pong.d.ts +6 -0
- package/clinkx-workflows/dist/validators/ping-pong.js +10 -0
- package/clinkx-workflows/dist/validators/ping-pong.js.map +1 -0
- package/clinkx-workflows/dist/workflow-tools.d.ts +51 -5
- package/clinkx-workflows/dist/workflow-tools.js +785 -34
- package/clinkx-workflows/dist/workflow-tools.js.map +1 -1
- package/clinkx-workflows/dist/workflow-verification-contract.d.ts +4 -4
- package/clinkx-workflows/templates/architecture-arena/candidate.txt +39 -0
- package/clinkx-workflows/templates/architecture-arena/judge.txt +34 -0
- package/clinkx-workflows/templates/ping-pong/implement.txt +14 -0
- package/clinkx-workflows/templates/ping-pong/remediate.txt +14 -0
- package/clinkx-workflows/templates/ping-pong/review.txt +27 -0
- package/clinkx-workflows/workflows/architecture-arena.yaml +127 -0
- package/clinkx-workflows/workflows/ping-pong.yaml +158 -0
- package/dist/context-builder/bundle.js +4 -3
- package/dist/context-builder/bundle.js.map +1 -1
- package/dist/context-builder/delegate-packet.d.ts +1 -0
- package/dist/context-builder/delegate-packet.js +66 -7
- package/dist/context-builder/delegate-packet.js.map +1 -1
- package/dist/context-builder/delegate-validation.d.ts +12 -3
- package/dist/context-builder/delegate-validation.js +23 -13
- package/dist/context-builder/delegate-validation.js.map +1 -1
- package/dist/context-builder/external-gaps.d.ts +14 -3
- package/dist/context-builder/external-gaps.js +124 -18
- package/dist/context-builder/external-gaps.js.map +1 -1
- package/dist/context-builder/handler.js +108 -191
- package/dist/context-builder/handler.js.map +1 -1
- package/dist/context-builder/review-coverage.js +27 -1
- package/dist/context-builder/review-coverage.js.map +1 -1
- package/dist/context-builder/secrets.js +1 -1
- package/dist/context-builder/secrets.js.map +1 -1
- package/dist/context-builder/versions.d.ts +10 -4
- package/dist/context-builder/versions.js +10 -4
- package/dist/context-builder/versions.js.map +1 -1
- package/dist/context-builder/workflow-runner.js +33 -24
- package/dist/context-builder/workflow-runner.js.map +1 -1
- package/dist/dashboard-cli.d.ts +2 -0
- package/dist/dashboard-cli.js +147 -57
- package/dist/dashboard-cli.js.map +1 -1
- package/dist/dashboard-companion/client.d.ts +10 -0
- package/dist/dashboard-companion/client.js +43 -1
- package/dist/dashboard-companion/client.js.map +1 -1
- package/dist/dashboard-companion/config.d.ts +19 -1
- package/dist/dashboard-companion/config.js +104 -3
- package/dist/dashboard-companion/config.js.map +1 -1
- package/dist/dashboard-companion/protocol.d.ts +16 -0
- package/dist/dashboard-companion/protocol.js +38 -1
- package/dist/dashboard-companion/protocol.js.map +1 -1
- package/dist/dashboard-companion/workstation-binding.d.ts +69 -0
- package/dist/dashboard-companion/workstation-binding.js +192 -0
- package/dist/dashboard-companion/workstation-binding.js.map +1 -0
- package/dist/dashboard-gateway/admin-cli.d.ts +10 -0
- package/dist/dashboard-gateway/admin-cli.js +266 -0
- package/dist/dashboard-gateway/admin-cli.js.map +1 -0
- package/dist/dashboard-gateway/config.d.ts +50 -0
- package/dist/dashboard-gateway/config.js +235 -1
- package/dist/dashboard-gateway/config.js.map +1 -1
- package/dist/dashboard-gateway/connection-registry.d.ts +100 -0
- package/dist/dashboard-gateway/connection-registry.js +228 -0
- package/dist/dashboard-gateway/connection-registry.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/account-auth.d.ts +78 -0
- package/dist/dashboard-gateway/control-plane/account-auth.js +198 -0
- package/dist/dashboard-gateway/control-plane/account-auth.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/billing.d.ts +264 -0
- package/dist/dashboard-gateway/control-plane/billing.js +787 -0
- package/dist/dashboard-gateway/control-plane/billing.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/contracts.d.ts +1293 -0
- package/dist/dashboard-gateway/control-plane/contracts.js +280 -0
- package/dist/dashboard-gateway/control-plane/contracts.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/cookies.d.ts +13 -0
- package/dist/dashboard-gateway/control-plane/cookies.js +46 -0
- package/dist/dashboard-gateway/control-plane/cookies.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/credentials.d.ts +63 -0
- package/dist/dashboard-gateway/control-plane/credentials.js +66 -0
- package/dist/dashboard-gateway/control-plane/credentials.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/entitlements.d.ts +118 -0
- package/dist/dashboard-gateway/control-plane/entitlements.js +329 -0
- package/dist/dashboard-gateway/control-plane/entitlements.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/errors.d.ts +17 -0
- package/dist/dashboard-gateway/control-plane/errors.js +23 -0
- package/dist/dashboard-gateway/control-plane/errors.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/hosts.d.ts +6 -0
- package/dist/dashboard-gateway/control-plane/hosts.js +16 -0
- package/dist/dashboard-gateway/control-plane/hosts.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/http.d.ts +34 -0
- package/dist/dashboard-gateway/control-plane/http.js +331 -0
- package/dist/dashboard-gateway/control-plane/http.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/schema.d.ts +9 -0
- package/dist/dashboard-gateway/control-plane/schema.js +247 -0
- package/dist/dashboard-gateway/control-plane/schema.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/secrets.d.ts +20 -0
- package/dist/dashboard-gateway/control-plane/secrets.js +39 -0
- package/dist/dashboard-gateway/control-plane/secrets.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/store.d.ts +579 -0
- package/dist/dashboard-gateway/control-plane/store.js +1115 -0
- package/dist/dashboard-gateway/control-plane/store.js.map +1 -0
- package/dist/dashboard-gateway/cookie-boundary.d.ts +60 -0
- package/dist/dashboard-gateway/cookie-boundary.js +205 -0
- package/dist/dashboard-gateway/cookie-boundary.js.map +1 -0
- package/dist/dashboard-gateway/index.js +271 -494
- package/dist/dashboard-gateway/index.js.map +1 -1
- package/dist/dashboard-gateway/main.js +7 -2
- package/dist/dashboard-gateway/main.js.map +1 -1
- package/dist/dashboard-gateway/routing.d.ts +43 -0
- package/dist/dashboard-gateway/routing.js +65 -0
- package/dist/dashboard-gateway/routing.js.map +1 -0
- package/dist/dashboard-gateway/tenant-gateway.d.ts +56 -0
- package/dist/dashboard-gateway/tenant-gateway.js +989 -0
- package/dist/dashboard-gateway/tenant-gateway.js.map +1 -0
- package/dist/dashboard-gateway/tenant-main.d.ts +41 -0
- package/dist/dashboard-gateway/tenant-main.js +303 -0
- package/dist/dashboard-gateway/tenant-main.js.map +1 -0
- package/dist/dashboard-gateway/tunnel-errors.d.ts +16 -0
- package/dist/dashboard-gateway/tunnel-errors.js +22 -0
- package/dist/dashboard-gateway/tunnel-errors.js.map +1 -0
- package/dist/dashboard-gateway/tunnel-http.d.ts +21 -0
- package/dist/dashboard-gateway/tunnel-http.js +175 -0
- package/dist/dashboard-gateway/tunnel-http.js.map +1 -0
- package/dist/handler.d.ts +6 -0
- package/dist/handler.js +17 -1
- package/dist/handler.js.map +1 -1
- package/dist/local-clink-backend.d.ts +2 -1
- package/dist/local-clink-backend.js +28 -4
- package/dist/local-clink-backend.js.map +1 -1
- package/dist/parsers/extract.d.ts +19 -1
- package/dist/parsers/extract.js +68 -3
- package/dist/parsers/extract.js.map +1 -1
- package/dist/pipeline.d.ts +17 -1
- package/dist/pipeline.js +34 -6
- package/dist/pipeline.js.map +1 -1
- package/dist/process-group.d.ts +65 -0
- package/dist/process-group.js +153 -0
- package/dist/process-group.js.map +1 -0
- package/dist/progress-report/schema.d.ts +23 -23
- package/dist/project-verification/files.js +1 -1
- package/dist/project-verification/files.js.map +1 -1
- package/dist/project-verification/process.js +23 -44
- package/dist/project-verification/process.js.map +1 -1
- package/dist/project-verification/service.js +9 -1
- package/dist/project-verification/service.js.map +1 -1
- package/dist/prompt.d.ts +7 -0
- package/dist/prompt.js +18 -1
- package/dist/prompt.js.map +1 -1
- package/dist/recipe-host/git-trust.d.ts +43 -0
- package/dist/recipe-host/git-trust.js +360 -0
- package/dist/recipe-host/git-trust.js.map +1 -0
- package/dist/recipe-host/git.d.ts +42 -0
- package/dist/recipe-host/git.js +207 -0
- package/dist/recipe-host/git.js.map +1 -0
- package/dist/recipe-host/native-recipe-host.d.ts +127 -0
- package/dist/recipe-host/native-recipe-host.js +880 -0
- package/dist/recipe-host/native-recipe-host.js.map +1 -0
- package/dist/recipe-host/seat-cwd.d.ts +16 -0
- package/dist/recipe-host/seat-cwd.js +170 -0
- package/dist/recipe-host/seat-cwd.js.map +1 -0
- package/dist/runner.d.ts +43 -1
- package/dist/runner.js +234 -35
- package/dist/runner.js.map +1 -1
- package/dist/server.js +9 -1
- package/dist/server.js.map +1 -1
- package/docs/configuration.md +24 -3
- package/docs/engineering-recipes.md +291 -0
- package/docs/security.md +84 -0
- package/docs/verification/project-checks.md +24 -2
- package/package.json +5 -1
- package/clinkx-workflows/dist/dashboard-ui/assets/index-BdpmbPps.js +0 -115
- package/clinkx-workflows/dist/dashboard-ui/assets/style-Im5i1iAs.css +0 -1
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { AuthCallbackQuery } from "./contracts.js";
|
|
2
|
+
import type { AccountRecord, AccountSessionContext, ControlPlaneStore, TenantMemberContext, TenantRecord } from "./store.js";
|
|
3
|
+
export declare const AUTH_CALLBACK_PATH = "/api/control/auth/callback";
|
|
4
|
+
export interface AccountAuthTtls {
|
|
5
|
+
readonly pendingLoginMs: number;
|
|
6
|
+
readonly accountSessionMs: number;
|
|
7
|
+
}
|
|
8
|
+
export declare const DEFAULT_ACCOUNT_AUTH_TTLS: AccountAuthTtls;
|
|
9
|
+
export interface OidcClientConfig {
|
|
10
|
+
readonly issuer: string;
|
|
11
|
+
readonly clientId: string;
|
|
12
|
+
/** Name of the environment variable holding the client secret; read on every token request. */
|
|
13
|
+
readonly clientSecretEnv: string;
|
|
14
|
+
}
|
|
15
|
+
export interface AccountAuthOptions {
|
|
16
|
+
readonly store: ControlPlaneStore;
|
|
17
|
+
/** The single public origin: canonical `https://host` with no port. */
|
|
18
|
+
readonly publicOrigin: string;
|
|
19
|
+
readonly oidc: OidcClientConfig;
|
|
20
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
21
|
+
readonly ttls?: Partial<AccountAuthTtls>;
|
|
22
|
+
/**
|
|
23
|
+
* Test fixtures only: permits a loopback `http://` issuer through
|
|
24
|
+
* openid-client's explicit `allowInsecureRequests`. Production wiring never sets it.
|
|
25
|
+
*/
|
|
26
|
+
readonly allowInsecureRequests?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export interface IssuedSession {
|
|
29
|
+
readonly secret: string;
|
|
30
|
+
readonly expiresAt: number;
|
|
31
|
+
readonly maxAgeSeconds: number;
|
|
32
|
+
}
|
|
33
|
+
export interface LoginStart {
|
|
34
|
+
readonly authorizationUrl: string;
|
|
35
|
+
readonly loginBinding: string;
|
|
36
|
+
readonly bindingMaxAgeSeconds: number;
|
|
37
|
+
}
|
|
38
|
+
export interface LoginCompletion {
|
|
39
|
+
readonly session: IssuedSession;
|
|
40
|
+
readonly account: AccountRecord;
|
|
41
|
+
readonly workspace: TenantRecord;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Account authority: generic OIDC sign-in and account sessions on the single
|
|
45
|
+
* public origin. The account session is the only browser authority; it
|
|
46
|
+
* resolves to the workspace its account owns. Identity is the provider
|
|
47
|
+
* (issuer, subject) pair; email is never read.
|
|
48
|
+
*/
|
|
49
|
+
export declare class AccountAuthority {
|
|
50
|
+
private readonly store;
|
|
51
|
+
private readonly publicOrigin;
|
|
52
|
+
private readonly oidcConfig;
|
|
53
|
+
private readonly env;
|
|
54
|
+
private readonly ttls;
|
|
55
|
+
private readonly allowInsecureRequests;
|
|
56
|
+
private discovered;
|
|
57
|
+
constructor(options: AccountAuthOptions);
|
|
58
|
+
get redirectUri(): string;
|
|
59
|
+
private readonly clientAuthentication;
|
|
60
|
+
private configuration;
|
|
61
|
+
startLogin(): Promise<LoginStart>;
|
|
62
|
+
/**
|
|
63
|
+
* Finish sign-in and ensure the account's workspace exists. The pending login
|
|
64
|
+
* is consumed before any check runs, so a failed or replayed callback can
|
|
65
|
+
* never be completed later.
|
|
66
|
+
*/
|
|
67
|
+
completeLogin(input: {
|
|
68
|
+
callbackUrl: URL;
|
|
69
|
+
query: AuthCallbackQuery;
|
|
70
|
+
loginBinding: string | null;
|
|
71
|
+
}): Promise<LoginCompletion>;
|
|
72
|
+
resolveAccountSession(secret: string | null): AccountSessionContext | null;
|
|
73
|
+
logoutAccount(secret: string | null): void;
|
|
74
|
+
/** The session's account together with the workspace it owns, or `null`. */
|
|
75
|
+
resolveTenantMember(secret: string | null): TenantMemberContext | null;
|
|
76
|
+
/** Workspace authority for a browser request, from its Cookie header. */
|
|
77
|
+
resolveTenantMemberFromCookies(cookieHeader: string | undefined): TenantMemberContext | null;
|
|
78
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import * as oidc from "openid-client";
|
|
2
|
+
import { ACCOUNT_SESSION_COOKIE, readSingleCookie } from "./cookies.js";
|
|
3
|
+
import { ControlError } from "./errors.js";
|
|
4
|
+
import { SECRET_PREFIX, constantTimeEqual, deriveVerifier, generateControlId, generateOpaqueSecret, isOpaqueSecret, } from "./secrets.js";
|
|
5
|
+
export const AUTH_CALLBACK_PATH = "/api/control/auth/callback";
|
|
6
|
+
export const DEFAULT_ACCOUNT_AUTH_TTLS = {
|
|
7
|
+
pendingLoginMs: 10 * 60_000,
|
|
8
|
+
accountSessionMs: 12 * 60 * 60_000,
|
|
9
|
+
};
|
|
10
|
+
const PROVIDER_FAILURE_CODES = new Set([
|
|
11
|
+
"OAUTH_TIMEOUT",
|
|
12
|
+
"OAUTH_ABORT",
|
|
13
|
+
"OAUTH_RESPONSE_IS_NOT_CONFORM",
|
|
14
|
+
"OAUTH_RESPONSE_IS_NOT_JSON",
|
|
15
|
+
]);
|
|
16
|
+
function classifyProviderError(error) {
|
|
17
|
+
if (error instanceof ControlError)
|
|
18
|
+
return error;
|
|
19
|
+
if (error instanceof TypeError)
|
|
20
|
+
return new ControlError("provider_unavailable", undefined, { cause: error });
|
|
21
|
+
if (error instanceof oidc.ClientError && error.code !== undefined && PROVIDER_FAILURE_CODES.has(error.code)) {
|
|
22
|
+
return new ControlError("provider_unavailable", undefined, { cause: error });
|
|
23
|
+
}
|
|
24
|
+
if (error instanceof oidc.ClientError && error.cause instanceof ControlError)
|
|
25
|
+
return error.cause;
|
|
26
|
+
if (error instanceof oidc.ResponseBodyError && (error.status === 429 || error.status >= 500)) {
|
|
27
|
+
return new ControlError("provider_unavailable", undefined, { cause: error });
|
|
28
|
+
}
|
|
29
|
+
return new ControlError("auth_failed", undefined, { cause: error });
|
|
30
|
+
}
|
|
31
|
+
function issuedSession(secret, expiresAt, ttlMs) {
|
|
32
|
+
return { secret, expiresAt, maxAgeSeconds: Math.floor(ttlMs / 1000) };
|
|
33
|
+
}
|
|
34
|
+
function displayNameFrom(claims) {
|
|
35
|
+
const name = claims.name;
|
|
36
|
+
if (typeof name !== "string")
|
|
37
|
+
return null;
|
|
38
|
+
const trimmed = name.trim();
|
|
39
|
+
return trimmed.length >= 1 && trimmed.length <= 200 ? trimmed : null;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Account authority: generic OIDC sign-in and account sessions on the single
|
|
43
|
+
* public origin. The account session is the only browser authority; it
|
|
44
|
+
* resolves to the workspace its account owns. Identity is the provider
|
|
45
|
+
* (issuer, subject) pair; email is never read.
|
|
46
|
+
*/
|
|
47
|
+
export class AccountAuthority {
|
|
48
|
+
store;
|
|
49
|
+
publicOrigin;
|
|
50
|
+
oidcConfig;
|
|
51
|
+
env;
|
|
52
|
+
ttls;
|
|
53
|
+
allowInsecureRequests;
|
|
54
|
+
discovered = null;
|
|
55
|
+
constructor(options) {
|
|
56
|
+
this.store = options.store;
|
|
57
|
+
this.publicOrigin = options.publicOrigin;
|
|
58
|
+
this.oidcConfig = options.oidc;
|
|
59
|
+
this.env = options.env ?? process.env;
|
|
60
|
+
this.ttls = { ...DEFAULT_ACCOUNT_AUTH_TTLS, ...options.ttls };
|
|
61
|
+
this.allowInsecureRequests = options.allowInsecureRequests === true;
|
|
62
|
+
}
|
|
63
|
+
get redirectUri() {
|
|
64
|
+
return `${this.publicOrigin}${AUTH_CALLBACK_PATH}`;
|
|
65
|
+
}
|
|
66
|
+
clientAuthentication = (as, client, body, headers) => {
|
|
67
|
+
const secret = this.env[this.oidcConfig.clientSecretEnv];
|
|
68
|
+
if (secret === undefined || secret === "") {
|
|
69
|
+
throw new ControlError("provider_unavailable", "OIDC client secret is not configured");
|
|
70
|
+
}
|
|
71
|
+
oidc.ClientSecretBasic(secret)(as, client, body, headers);
|
|
72
|
+
};
|
|
73
|
+
configuration() {
|
|
74
|
+
if (this.discovered === null) {
|
|
75
|
+
const options = this.allowInsecureRequests
|
|
76
|
+
? { execute: [oidc.allowInsecureRequests] }
|
|
77
|
+
: {};
|
|
78
|
+
const pending = oidc
|
|
79
|
+
.discovery(new URL(this.oidcConfig.issuer), this.oidcConfig.clientId, undefined, this.clientAuthentication, options)
|
|
80
|
+
.catch((error) => {
|
|
81
|
+
if (this.discovered === pending)
|
|
82
|
+
this.discovered = null;
|
|
83
|
+
throw new ControlError("provider_unavailable", "OIDC discovery failed", { cause: error });
|
|
84
|
+
});
|
|
85
|
+
this.discovered = pending;
|
|
86
|
+
}
|
|
87
|
+
return this.discovered;
|
|
88
|
+
}
|
|
89
|
+
async startLogin() {
|
|
90
|
+
const config = await this.configuration();
|
|
91
|
+
const state = oidc.randomState();
|
|
92
|
+
const nonce = oidc.randomNonce();
|
|
93
|
+
const codeVerifier = oidc.randomPKCECodeVerifier();
|
|
94
|
+
const codeChallenge = await oidc.calculatePKCECodeChallenge(codeVerifier);
|
|
95
|
+
const loginBinding = generateOpaqueSecret(SECRET_PREFIX.loginBinding);
|
|
96
|
+
const pending = this.store.insertPendingLogin({
|
|
97
|
+
stateVerifier: deriveVerifier("login_state", state),
|
|
98
|
+
bindingVerifier: deriveVerifier("login_binding", loginBinding),
|
|
99
|
+
codeVerifier,
|
|
100
|
+
nonce,
|
|
101
|
+
ttlMs: this.ttls.pendingLoginMs,
|
|
102
|
+
});
|
|
103
|
+
if (pending.status === "rate_limited")
|
|
104
|
+
throw new ControlError("rate_limited", "too many sign-ins are in progress");
|
|
105
|
+
const url = oidc.buildAuthorizationUrl(config, {
|
|
106
|
+
redirect_uri: this.redirectUri,
|
|
107
|
+
response_type: "code",
|
|
108
|
+
scope: "openid profile",
|
|
109
|
+
state,
|
|
110
|
+
nonce,
|
|
111
|
+
code_challenge: codeChallenge,
|
|
112
|
+
code_challenge_method: "S256",
|
|
113
|
+
});
|
|
114
|
+
return {
|
|
115
|
+
authorizationUrl: url.href,
|
|
116
|
+
loginBinding,
|
|
117
|
+
bindingMaxAgeSeconds: Math.floor(this.ttls.pendingLoginMs / 1000),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Finish sign-in and ensure the account's workspace exists. The pending login
|
|
122
|
+
* is consumed before any check runs, so a failed or replayed callback can
|
|
123
|
+
* never be completed later.
|
|
124
|
+
*/
|
|
125
|
+
async completeLogin(input) {
|
|
126
|
+
const consumed = this.store.consumePendingLogin(deriveVerifier("login_state", input.query.state));
|
|
127
|
+
if (consumed.status !== "ok")
|
|
128
|
+
throw new ControlError("auth_failed", "login is unknown, expired, or already used");
|
|
129
|
+
const login = consumed.login;
|
|
130
|
+
if (input.loginBinding === null ||
|
|
131
|
+
!isOpaqueSecret(SECRET_PREFIX.loginBinding, input.loginBinding) ||
|
|
132
|
+
!constantTimeEqual(login.bindingVerifier, deriveVerifier("login_binding", input.loginBinding))) {
|
|
133
|
+
throw new ControlError("auth_failed", "login was started by a different browser");
|
|
134
|
+
}
|
|
135
|
+
if (input.query.error !== undefined)
|
|
136
|
+
throw new ControlError("auth_failed", "provider returned an error");
|
|
137
|
+
let claims;
|
|
138
|
+
try {
|
|
139
|
+
const config = await this.configuration();
|
|
140
|
+
const tokens = await oidc.authorizationCodeGrant(config, input.callbackUrl, {
|
|
141
|
+
pkceCodeVerifier: login.codeVerifier,
|
|
142
|
+
expectedState: input.query.state,
|
|
143
|
+
expectedNonce: login.nonce,
|
|
144
|
+
idTokenExpected: true,
|
|
145
|
+
});
|
|
146
|
+
claims = tokens.claims();
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
throw classifyProviderError(error);
|
|
150
|
+
}
|
|
151
|
+
if (claims === undefined ||
|
|
152
|
+
typeof claims.iss !== "string" ||
|
|
153
|
+
claims.iss.length === 0 ||
|
|
154
|
+
claims.iss.length > 2048 ||
|
|
155
|
+
typeof claims.sub !== "string" ||
|
|
156
|
+
claims.sub.length === 0 ||
|
|
157
|
+
claims.sub.length > 255) {
|
|
158
|
+
throw new ControlError("auth_failed", "ID token lacks a usable issuer and subject");
|
|
159
|
+
}
|
|
160
|
+
const account = this.store.upsertAccount({
|
|
161
|
+
issuer: claims.iss,
|
|
162
|
+
subject: claims.sub,
|
|
163
|
+
displayName: displayNameFrom(claims),
|
|
164
|
+
});
|
|
165
|
+
const workspace = this.store.ensureWorkspace(account.id);
|
|
166
|
+
if (workspace.status !== "ok")
|
|
167
|
+
throw new ControlError("auth_failed", "account vanished during sign-in");
|
|
168
|
+
const secret = generateOpaqueSecret(SECRET_PREFIX.session);
|
|
169
|
+
const expiresAt = this.store.insertAccountSession({
|
|
170
|
+
id: generateControlId("ases"),
|
|
171
|
+
verifier: deriveVerifier("account_session", secret),
|
|
172
|
+
accountId: account.id,
|
|
173
|
+
ttlMs: this.ttls.accountSessionMs,
|
|
174
|
+
});
|
|
175
|
+
return { session: issuedSession(secret, expiresAt, this.ttls.accountSessionMs), account, workspace: workspace.tenant };
|
|
176
|
+
}
|
|
177
|
+
resolveAccountSession(secret) {
|
|
178
|
+
if (secret === null || !isOpaqueSecret(SECRET_PREFIX.session, secret))
|
|
179
|
+
return null;
|
|
180
|
+
return this.store.resolveAccountSession(deriveVerifier("account_session", secret));
|
|
181
|
+
}
|
|
182
|
+
logoutAccount(secret) {
|
|
183
|
+
if (secret === null || !isOpaqueSecret(SECRET_PREFIX.session, secret))
|
|
184
|
+
return;
|
|
185
|
+
this.store.revokeAccountSession(deriveVerifier("account_session", secret));
|
|
186
|
+
}
|
|
187
|
+
/** The session's account together with the workspace it owns, or `null`. */
|
|
188
|
+
resolveTenantMember(secret) {
|
|
189
|
+
if (secret === null || !isOpaqueSecret(SECRET_PREFIX.session, secret))
|
|
190
|
+
return null;
|
|
191
|
+
return this.store.resolveTenantMember(deriveVerifier("account_session", secret));
|
|
192
|
+
}
|
|
193
|
+
/** Workspace authority for a browser request, from its Cookie header. */
|
|
194
|
+
resolveTenantMemberFromCookies(cookieHeader) {
|
|
195
|
+
return this.resolveTenantMember(readSingleCookie(cookieHeader, ACCOUNT_SESSION_COOKIE));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
//# sourceMappingURL=account-auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-auth.js","sourceRoot":"","sources":["../../../src/dashboard-gateway/control-plane/account-auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,eAAe,CAAC;AAGtC,OAAO,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EACL,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EACjB,oBAAoB,EACpB,cAAc,GACf,MAAM,cAAc,CAAC;AAStB,MAAM,CAAC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AO,EAAE,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,CAAC;IACzH,CAAC;IAED,qBAAqB,CAAC,MAAqB;QACzC,IAAI,MAAM,KAAK,IAAI,IAAI,CAAC,cAAc,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC;YAAE,OAAO,IAAI,CAAC;QACnF,OAAO,IAAI,CAAC,KAAK,CAAC,qBAAqB,CAAC,cAAc,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC,CAAC;IACrF,CAAC;IAED,aAAa,CAAC,MAAqB;QACjC,IAAI,MAAM,KAAK,IAAI,IAAI,CAAC,cAAc,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC;YAAE,OAAO;QAC9E,IAAI,CAAC,KAAK,CAAC,oBAAoB,CAAC,cAAc,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC,CAAC;IAC7E,CAAC;IAED,4EAA4E;IAC5E,mBAAmB,CAAC,MAAqB;QACvC,IAAI,MAAM,KAAK,IAAI,IAAI,CAAC,cAAc,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC;YAAE,OAAO,IAAI,CAAC;QACnF,OAAO,IAAI,CAAC,KAAK,CAAC,mBAAmB,CAAC,cAAc,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC,CAAC;IACnF,CAAC;IAED,yEAAyE;IACzE,8BAA8B,CAAC,YAAgC;QAC7D,OAAO,IAAI,CAAC,mBAAmB,CAAC,gBAAgB,CAAC,YAAY,EAAE,sBAAsB,CAAC,CAAC,CAAC;IAC1F,CAAC;CACF"}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
import type { DashboardGatewayStripeBillingConfig } from "../config.js";
|
|
3
|
+
import { type EntitlementStatus, type SubscriptionState } from "./contracts.js";
|
|
4
|
+
import { type SchedulerTimers, type TenantAccessAuthority } from "./entitlements.js";
|
|
5
|
+
import type { BillingEnvironment, BillingEventTrigger, BillingJobRecord, ControlPlaneStore, EntitlementRecord, SessionActor, TenantRecord } from "./store.js";
|
|
6
|
+
/**
|
|
7
|
+
* The Stripe API version every request is pinned to. It is the default of the
|
|
8
|
+
* pinned `stripe` 22.6.2 SDK, whose types describe only this version; the
|
|
9
|
+
* `satisfies` check fails the build if an SDK upgrade moves the default.
|
|
10
|
+
*/
|
|
11
|
+
export declare const STRIPE_API_VERSION = "2026-08-26.dahlia";
|
|
12
|
+
export declare const DEFAULT_WEBHOOK_MAX_BODY_BYTES: number;
|
|
13
|
+
export declare const DEFAULT_BILLING_RETRY_BASE_MS = 5000;
|
|
14
|
+
export declare const DEFAULT_BILLING_RETRY_MAX_MS: number;
|
|
15
|
+
export declare const DEFAULT_BILLING_MAX_ATTEMPTS = 8;
|
|
16
|
+
export declare const DEFAULT_BILLING_JOB_RETENTION_MS: number;
|
|
17
|
+
export declare const DEFAULT_TRANSITION_RETRY_DELAYS_MS: readonly number[];
|
|
18
|
+
/** Provider lists longer than this are refused rather than applied from a truncated view. */
|
|
19
|
+
export declare const MAX_PROVIDER_LIST_ITEMS = 1000;
|
|
20
|
+
export type BillingConfig = DashboardGatewayStripeBillingConfig;
|
|
21
|
+
export interface ProviderSubscriptionItem {
|
|
22
|
+
readonly priceId: string;
|
|
23
|
+
/** UTC seconds, as the provider reports it. */
|
|
24
|
+
readonly currentPeriodEnd: number | null;
|
|
25
|
+
}
|
|
26
|
+
export interface ProviderSubscription {
|
|
27
|
+
readonly id: string;
|
|
28
|
+
readonly customerId: string;
|
|
29
|
+
readonly status: string;
|
|
30
|
+
/** UTC seconds. */
|
|
31
|
+
readonly created: number;
|
|
32
|
+
readonly livemode: boolean;
|
|
33
|
+
readonly items: readonly ProviderSubscriptionItem[];
|
|
34
|
+
}
|
|
35
|
+
export interface ProviderInvoice {
|
|
36
|
+
readonly id: string;
|
|
37
|
+
/** The invoice's own customer and subscription as the provider reports them; `null` when absent or malformed. */
|
|
38
|
+
readonly customerId: string | null;
|
|
39
|
+
readonly subscriptionId: string | null;
|
|
40
|
+
readonly status: string;
|
|
41
|
+
/**
|
|
42
|
+
* UTC seconds when the obligation became due, which anchors its grace period.
|
|
43
|
+
* Stripe: `status_transitions.finalized_at`, falling back to `created`.
|
|
44
|
+
* `null` when the provider reports neither; grace then starts when recorded.
|
|
45
|
+
*/
|
|
46
|
+
readonly openedAt: number | null;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The billing provider operations the gateway uses. Every call is made outside
|
|
50
|
+
* a database transaction. Implementations throw `BillingProviderError` and
|
|
51
|
+
* never put secrets in errors.
|
|
52
|
+
*/
|
|
53
|
+
export interface BillingProvider {
|
|
54
|
+
createCustomer(input: {
|
|
55
|
+
tenantId: string;
|
|
56
|
+
idempotencyKey: string;
|
|
57
|
+
}): Promise<{
|
|
58
|
+
id: string;
|
|
59
|
+
livemode: boolean;
|
|
60
|
+
}>;
|
|
61
|
+
createCheckoutSession(input: {
|
|
62
|
+
customerId: string;
|
|
63
|
+
priceId: string;
|
|
64
|
+
tenantId: string;
|
|
65
|
+
successUrl: string;
|
|
66
|
+
cancelUrl: string;
|
|
67
|
+
}): Promise<{
|
|
68
|
+
url: string;
|
|
69
|
+
}>;
|
|
70
|
+
createPortalSession(input: {
|
|
71
|
+
customerId: string;
|
|
72
|
+
returnUrl: string;
|
|
73
|
+
}): Promise<{
|
|
74
|
+
url: string;
|
|
75
|
+
}>;
|
|
76
|
+
/** Every subscription of a customer, in any status. */
|
|
77
|
+
listSubscriptions(customerId: string): Promise<readonly ProviderSubscription[]>;
|
|
78
|
+
/** Open (unpaid, collectable) invoices of one subscription. */
|
|
79
|
+
listOpenInvoices(subscriptionId: string): Promise<readonly ProviderInvoice[]>;
|
|
80
|
+
}
|
|
81
|
+
/** A provider request failed. `kind` is a short provider error class, never a message. */
|
|
82
|
+
export declare class BillingProviderError extends Error {
|
|
83
|
+
readonly kind: string;
|
|
84
|
+
constructor(kind: string, options?: {
|
|
85
|
+
cause?: unknown;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/** Stored billing state changed during every fetch-compare round; the job retries later. */
|
|
89
|
+
export declare class BillingContentionError extends Error {
|
|
90
|
+
constructor();
|
|
91
|
+
}
|
|
92
|
+
/** An open invoice does not name the subscription and customer it was listed for; nothing is applied and the job retries. */
|
|
93
|
+
export declare class BillingInvoiceIdentityError extends Error {
|
|
94
|
+
constructor();
|
|
95
|
+
}
|
|
96
|
+
/** Reconciliation cannot succeed by retrying (for example, a subscription already bound to another tenant). */
|
|
97
|
+
export declare class BillingPermanentError extends Error {
|
|
98
|
+
constructor(message: string);
|
|
99
|
+
}
|
|
100
|
+
type Environment = Readonly<Record<string, string | undefined>>;
|
|
101
|
+
/** Stripe through the pinned SDK and API version. The secret key is read from the named variable at each use. */
|
|
102
|
+
export declare function createStripeBillingProvider(options: {
|
|
103
|
+
readonly stripe: Pick<BillingConfig["stripe"], "environment" | "secretKeyEnv">;
|
|
104
|
+
readonly env?: Environment;
|
|
105
|
+
readonly timeoutMs?: number;
|
|
106
|
+
/** Tests only: replaces the SDK's network client. */
|
|
107
|
+
readonly httpClient?: NonNullable<Stripe.StripeConfig["httpClient"]>;
|
|
108
|
+
}): BillingProvider;
|
|
109
|
+
export interface VerifiedBillingEvent {
|
|
110
|
+
readonly id: string;
|
|
111
|
+
readonly type: string;
|
|
112
|
+
/** `null` when the event names no customer, or is not a billing-state event. */
|
|
113
|
+
readonly trigger: BillingEventTrigger | null;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Verify a raw webhook body against the signing secret and the configured
|
|
117
|
+
* environment. The body is never parsed before its signature is checked, and
|
|
118
|
+
* nothing in it is applied as state: it only selects reconciliation work.
|
|
119
|
+
*/
|
|
120
|
+
export declare function verifyBillingWebhook(input: {
|
|
121
|
+
rawBody: Buffer;
|
|
122
|
+
signature: string | undefined;
|
|
123
|
+
secret: string;
|
|
124
|
+
environment: BillingEnvironment;
|
|
125
|
+
}): VerifiedBillingEvent;
|
|
126
|
+
/** Provider status to stored state; anything unrecognized is `unknown`, which denies. */
|
|
127
|
+
export declare function subscriptionStateOf(status: string): SubscriptionState;
|
|
128
|
+
/**
|
|
129
|
+
* The customer's current purchase from current provider state alone, so the
|
|
130
|
+
* order events arrived in plays no part. Among subscriptions for a configured
|
|
131
|
+
* Price: (a) the bound subscription while it is active, trialing, or past due;
|
|
132
|
+
* otherwise (b) the active or trialing one with the latest period end, then the
|
|
133
|
+
* newest; otherwise (d) the bound subscription in its terminal state. Pending
|
|
134
|
+
* (`incomplete`) and expired Checkout subscriptions therefore never displace a
|
|
135
|
+
* bound purchase and never grant. Only with no bound subscription in the
|
|
136
|
+
* provider's list does the best remaining state (past due, then unpaid or
|
|
137
|
+
* paused, then incomplete) apply. Stripe allows overlapping active
|
|
138
|
+
* subscriptions; any of them is a paying purchase.
|
|
139
|
+
*/
|
|
140
|
+
export declare function selectCurrentSubscription(input: {
|
|
141
|
+
subscriptions: readonly ProviderSubscription[];
|
|
142
|
+
customerId: string;
|
|
143
|
+
priceIds: readonly string[];
|
|
144
|
+
livemode: boolean;
|
|
145
|
+
boundSubscriptionId: string | null;
|
|
146
|
+
}): ProviderSubscription | null;
|
|
147
|
+
/** The access cutoff: the earliest period end among the subscription's configured-price items, in milliseconds. */
|
|
148
|
+
export declare function periodEndOf(subscription: ProviderSubscription, priceIds: readonly string[]): number | null;
|
|
149
|
+
export type ReconcileOutcome = {
|
|
150
|
+
readonly status: "applied";
|
|
151
|
+
readonly entitlement: EntitlementRecord;
|
|
152
|
+
} | {
|
|
153
|
+
readonly status: "not_found" | "unbound";
|
|
154
|
+
};
|
|
155
|
+
export interface ReconcileContext {
|
|
156
|
+
readonly store: ControlPlaneStore;
|
|
157
|
+
readonly provider: BillingProvider;
|
|
158
|
+
readonly access: Pick<TenantAccessAuthority, "applyPastDue">;
|
|
159
|
+
readonly environment: BillingEnvironment;
|
|
160
|
+
readonly priceIds: readonly string[];
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Bring one tenant's entitlement to current provider state. Provider state is
|
|
164
|
+
* fetched outside any transaction; each write then compares the stored
|
|
165
|
+
* revision and purchase binding, and any change in between restarts the
|
|
166
|
+
* fetch-compare round. Never touches suspension or device credentials.
|
|
167
|
+
*/
|
|
168
|
+
export declare function reconcileTenantBilling(context: ReconcileContext, tenantId: string): Promise<ReconcileOutcome>;
|
|
169
|
+
/** A job failure description safe to persist: error classes only, never provider messages or secrets. */
|
|
170
|
+
export declare function describeBillingFailure(error: unknown): string;
|
|
171
|
+
export interface BillingReconcilerOptions {
|
|
172
|
+
readonly store: ControlPlaneStore;
|
|
173
|
+
readonly provider: BillingProvider;
|
|
174
|
+
readonly access: Pick<TenantAccessAuthority, "applyPastDue">;
|
|
175
|
+
readonly config: {
|
|
176
|
+
readonly stripe: Pick<BillingConfig["stripe"], "environment" | "priceIds">;
|
|
177
|
+
readonly billing: Pick<BillingConfig["billing"], "reconciliationIntervalSeconds">;
|
|
178
|
+
};
|
|
179
|
+
readonly timers?: SchedulerTimers;
|
|
180
|
+
/** Clock for timer delays only; job times come from the store clock. */
|
|
181
|
+
readonly now?: () => number;
|
|
182
|
+
readonly retryBaseMs?: number;
|
|
183
|
+
readonly retryMaxMs?: number;
|
|
184
|
+
readonly maxAttempts?: number;
|
|
185
|
+
readonly jobRetentionMs?: number;
|
|
186
|
+
/**
|
|
187
|
+
* Delays between in-process retries of a job's durable completion or failure
|
|
188
|
+
* record. A job whose record still cannot be written stays `running` until
|
|
189
|
+
* the next drain or sweep reclaims it.
|
|
190
|
+
*/
|
|
191
|
+
readonly transitionRetryDelaysMs?: readonly number[];
|
|
192
|
+
/** Diagnostics. Receives errors only; never webhook payloads or secrets. */
|
|
193
|
+
readonly onError?: (error: unknown, job: Pick<BillingJobRecord, "id" | "trigger" | "attempts"> | null) => void;
|
|
194
|
+
}
|
|
195
|
+
export interface BillingReconciler {
|
|
196
|
+
/**
|
|
197
|
+
* Requeue jobs a stopped process left running, queue the startup repair of
|
|
198
|
+
* every bound purchase, start draining, and arm the periodic repair.
|
|
199
|
+
*/
|
|
200
|
+
start(): void;
|
|
201
|
+
/**
|
|
202
|
+
* Stop timers and wait for the job in progress, if any. Queued jobs stay
|
|
203
|
+
* pending; a drain started after stop runs at most one job.
|
|
204
|
+
*/
|
|
205
|
+
stop(): Promise<void>;
|
|
206
|
+
/** Process due jobs now. Jobs run one at a time, in this process only. */
|
|
207
|
+
kick(): void;
|
|
208
|
+
/** Process every due job; resolves when none is due. Concurrent callers share one serialized drain. */
|
|
209
|
+
drain(): Promise<void>;
|
|
210
|
+
/** Queue a repair job for every bound purchase and drain. */
|
|
211
|
+
sweep(trigger: "startup" | "periodic"): Promise<void>;
|
|
212
|
+
readonly running: boolean;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Serialized, durable reconciliation. Webhooks, the startup run, and the
|
|
216
|
+
* periodic run all become rows in `billing_job` and run through one in-process
|
|
217
|
+
* queue. A failed attempt stays pending with exponential backoff until
|
|
218
|
+
* `maxAttempts`; periodic repair still covers a purchase whose job failed.
|
|
219
|
+
*/
|
|
220
|
+
export declare function createBillingReconciler(options: BillingReconcilerOptions): BillingReconciler;
|
|
221
|
+
/** The billing operations behind `/api/control/billing/*`. */
|
|
222
|
+
export interface BillingHttpService {
|
|
223
|
+
/** Throws `ControlStoreUnavailableError` when the store fails. */
|
|
224
|
+
status(tenantId: string): EntitlementStatus;
|
|
225
|
+
createCheckout(input: {
|
|
226
|
+
tenant: TenantRecord;
|
|
227
|
+
actor: SessionActor;
|
|
228
|
+
}): Promise<{
|
|
229
|
+
url: string;
|
|
230
|
+
}>;
|
|
231
|
+
createPortal(input: {
|
|
232
|
+
tenant: TenantRecord;
|
|
233
|
+
actor: SessionActor;
|
|
234
|
+
}): Promise<{
|
|
235
|
+
url: string;
|
|
236
|
+
}>;
|
|
237
|
+
/** Verify, then durably record the event and its job; resolves only after both commit. */
|
|
238
|
+
receiveWebhook(input: {
|
|
239
|
+
rawBody: Buffer;
|
|
240
|
+
signature: string | undefined;
|
|
241
|
+
}): {
|
|
242
|
+
eventId: string;
|
|
243
|
+
duplicate: boolean;
|
|
244
|
+
};
|
|
245
|
+
readonly webhookMaxBodyBytes: number;
|
|
246
|
+
}
|
|
247
|
+
export interface BillingServiceOptions {
|
|
248
|
+
readonly store: ControlPlaneStore;
|
|
249
|
+
readonly provider: BillingProvider;
|
|
250
|
+
/** The single public origin; Checkout and Portal return the browser here. */
|
|
251
|
+
readonly publicOrigin: string;
|
|
252
|
+
readonly access: Pick<TenantAccessAuthority, "entitlementStatus">;
|
|
253
|
+
readonly config: {
|
|
254
|
+
readonly stripe: Pick<BillingConfig["stripe"], "environment" | "priceIds" | "webhookSecretEnv">;
|
|
255
|
+
};
|
|
256
|
+
/** Started after each recorded webhook job. */
|
|
257
|
+
readonly reconciler?: Pick<BillingReconciler, "kick">;
|
|
258
|
+
readonly env?: Environment;
|
|
259
|
+
readonly webhookMaxBodyBytes?: number;
|
|
260
|
+
/** Diagnostics for provider and configuration failures. Never receives secrets or payloads. */
|
|
261
|
+
readonly onError?: (error: unknown) => void;
|
|
262
|
+
}
|
|
263
|
+
export declare function createBillingService(options: BillingServiceOptions): BillingHttpService;
|
|
264
|
+
export {};
|