@agent-custody/receipts 0.8.0 → 0.9.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/dist/index.js CHANGED
@@ -3,10 +3,13 @@ export { AUTHORIZATION_PREDICATE_TYPE, buildAuthorizationStatement } from "./rec
3
3
  export { buildRequest, restUpstream } from "./rest.js";
4
4
  export { openExporter, otlpExporter, spanFor } from "./otel.js";
5
5
  export { hecEvent, splunkExporter } from "./splunk.js";
6
+ export { serveConsole } from "./console.js";
7
+ export { checkConfig, formatFindings } from "./check.js";
6
8
  export { GRANT_HEADER, grantHeader, parseGrantHeader, serveHttp } from "./gateway-http.js";
7
9
  export * from "./capture.js";
8
10
  export { A2A_CARD_PATH, A2A_RECEIPT_METADATA_KEY, A2A_STREAM_META_KEY, a2aUpstream, fetchAgentCard, serveA2a, textOf } from "./a2a.js";
9
- export { deliverMail, founderNote, PortalStore, portalHandler, readSession, sendFollowUps, servePortal, signSession, stripeRequest, verifyStripeSignature } from "./portal.js";
11
+ export { deliverMail, founderNote, PortalStore, portalHandler, readSession, readSessionVia, sendFollowUps, servePortal, signSession, SsoRefusedError, stripeRequest, verifyStripeSignature } from "./portal.js";
12
+ export { oidcAuthorizeUrl, oidcExchange, pkce, samlAuthorizeUrl, samlValidate } from "./sso.js";
10
13
  export { exportLog, formatExport } from "./log-export.js";
11
14
  export { fileBackend, importLogFile, PostgresLog, PostgresTenancy, RateLimiter } from "./log-store.js";
12
15
  export { PLAN_QUOTAS, PLANS } from "./log-store.js";
@@ -88,7 +88,7 @@ export interface AuditEntry {
88
88
  id: number;
89
89
  at: string;
90
90
  actor: string;
91
- action: "tenant.add" | "tenant.disable" | "tenant.plan" | "token.add" | "token.revoke" | "policy.add" | "policy.remove";
91
+ action: "tenant.add" | "tenant.disable" | "tenant.plan" | "token.add" | "token.revoke" | "policy.add" | "policy.remove" | "sso.set" | "sso.remove" | "user.mfa-reset";
92
92
  tenantId: string | null;
93
93
  detail: Record<string, unknown>;
94
94
  }
@@ -116,7 +116,8 @@ export declare class PostgresTenancy {
116
116
  quotas?: Partial<Record<Plan, number | null>>;
117
117
  });
118
118
  private init;
119
- private record;
119
+ /** Writes one audit entry. `detail` never holds a secret or a token. */
120
+ record(actor: string | undefined, action: AuditEntry["action"], tenantId: string | null, detail: Record<string, unknown>): Promise<void>;
120
121
  /** Administrative actions, newest first; for one tenant when given. What the admin page shows and a tenant's export carries. */
121
122
  audit(opts?: {
122
123
  tenant?: string;
package/dist/log-store.js CHANGED
@@ -204,7 +204,9 @@ export class PostgresTenancy {
204
204
  }
205
205
  return this.ready;
206
206
  }
207
+ /** Writes one audit entry. `detail` never holds a secret or a token. */
207
208
  async record(actor, action, tenantId, detail) {
209
+ await this.init();
208
210
  await this.client.query(`INSERT INTO ${this.prefix}audit (actor, action, tenant_id, detail) VALUES ($1, $2, $3, $4)`, [actor ?? "unattributed", action, tenantId, JSON.stringify(detail)]);
209
211
  }
210
212
  /** Administrative actions, newest first; for one tenant when given. What the admin page shows and a tenant's export carries. */
package/dist/policy.d.ts CHANGED
@@ -14,6 +14,8 @@ export interface PolicyRequest {
14
14
  /** Cedar context. Numbers must be integers; Cedar has no floats. */
15
15
  context: Record<string, unknown>;
16
16
  }
17
+ /** True when the policy set contains a permit rule. Cedar answers, so a `permit(` in a comment does not count. */
18
+ export declare function hasPermitRule(policyText: string): boolean;
17
19
  export declare function policyDigest(policyText: string): string;
18
20
  /**
19
21
  * Cedar's JSON context reads `{ "__entity": … }` and `{ "__extn": … }` as typed values. Arguments come from the agent,
package/dist/policy.js CHANGED
@@ -1,6 +1,16 @@
1
1
  // Cedar policy evaluation. Fails closed: any evaluation error is a deny.
2
2
  import * as cedar from "@cedar-policy/cedar-wasm/nodejs";
3
3
  import { sha256Hex } from "./crypto.js";
4
+ /** True when the policy set contains a permit rule. Cedar answers, so a `permit(` in a comment does not count. */
5
+ export function hasPermitRule(policyText) {
6
+ const parts = cedar.policySetTextToParts(policyText);
7
+ if (parts.type !== "success")
8
+ return false;
9
+ return parts.policies.some((p) => {
10
+ const j = cedar.policyToJson(p);
11
+ return j.type === "success" && j.json.effect === "permit";
12
+ });
13
+ }
4
14
  export function policyDigest(policyText) {
5
15
  return sha256Hex(policyText);
6
16
  }
package/dist/portal.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { type IncomingMessage, type ServerResponse } from "node:http";
2
2
  import { type PostgresLike, type PostgresTenancy } from "./log-store.ts";
3
+ import { type OidcConfig, type SamlConfig, type SsoIdentity } from "./sso.ts";
3
4
  export interface StripeOptions {
4
5
  secretKey: string;
5
6
  webhookSecret: string;
@@ -46,6 +47,8 @@ export interface PortalOptions {
46
47
  stripe?: StripeOptions;
47
48
  mail?: MailOptions;
48
49
  oauth?: OAuthOptions;
50
+ /** the fetch for OIDC discovery, keys, and tokens; the global one by default */
51
+ ssoFetch?: typeof fetch;
49
52
  /** key throttles by X-Forwarded-For; only behind a proxy you run. Also marks cookies Secure. */
50
53
  trustProxy?: boolean;
51
54
  /** table prefix; default portal_ */
@@ -64,6 +67,18 @@ export declare class UnverifiedAccountError extends Error {
64
67
  email: string;
65
68
  constructor(email: string);
66
69
  }
70
+ /** A single sign-on identity that the tenant's rules refuse: another domain, or an account in another tenant. */
71
+ export declare class SsoRefusedError extends Error {
72
+ }
73
+ /** One tenant's enterprise identity provider. Set by the operator only, because an IdP is trusted for the domains that the operator checked. */
74
+ export interface SsoSetting {
75
+ tenant: string;
76
+ protocol: "oidc" | "saml";
77
+ config: OidcConfig | SamlConfig;
78
+ domains: string[];
79
+ defaultRole: Role;
80
+ enforced: boolean;
81
+ }
67
82
  /** Users, memberships, and billing records, beside the log's tables. */
68
83
  /** What a registration says about the person and the organisation; every field optional in the store, the route decides what it requires. */
69
84
  export interface Profile {
@@ -73,6 +88,13 @@ export interface Profile {
73
88
  phone?: string | null;
74
89
  telegram?: string | null;
75
90
  }
91
+ /** Lowest first. */
92
+ export declare const ROLES: readonly ["readonly", "reviewer", "admin", "owner"];
93
+ export type Role = (typeof ROLES)[number];
94
+ export declare const base32: (b: Buffer) => string;
95
+ export declare function totp(key: Buffer, step: number): string;
96
+ /** The time step the code matches, one step either side of now allowed; null when none. */
97
+ export declare function totpStep(key: Buffer, code: string, now?: number): number | null;
76
98
  export declare class PortalStore {
77
99
  private readonly client;
78
100
  private readonly p;
@@ -115,7 +137,56 @@ export declare class PortalStore {
115
137
  findOrCreateOAuthUser(provider: string, subject: string, email: string): Promise<PortalUser>;
116
138
  authenticate(email: string, password: string): Promise<PortalUser | null>;
117
139
  user(id: string): Promise<PortalUser | null>;
118
- addMember(userId: string, tenantId: string): Promise<void>;
140
+ addMember(userId: string, tenantId: string, role?: Role): Promise<void>;
141
+ roleOf(userId: string, tenantId: string): Promise<Role | null>;
142
+ members(tenantId: string): Promise<{
143
+ id: string;
144
+ email: string;
145
+ role: Role;
146
+ joinedAt: string;
147
+ mfa: boolean;
148
+ }[]>;
149
+ setRole(tenantId: string, userId: string, role: Role): Promise<boolean>;
150
+ removeMember(tenantId: string, userId: string): Promise<boolean>;
151
+ invite(tenantId: string, email: string, role: Role, by: string): Promise<void>;
152
+ invitesOf(tenantId: string): Promise<{
153
+ email: string;
154
+ role: Role;
155
+ invitedBy: string;
156
+ createdAt: string;
157
+ }[]>;
158
+ invitesFor(email: string): Promise<{
159
+ tenant: string;
160
+ role: Role;
161
+ }[]>;
162
+ removeInvite(tenantId: string, email: string): Promise<boolean>;
163
+ takeInvite(tenantId: string, email: string): Promise<Role | null>;
164
+ mfa(userId: string): Promise<{
165
+ secret: string | null;
166
+ enabled: boolean;
167
+ }>;
168
+ setMfaSecret(userId: string, secretHex: string): Promise<boolean>;
169
+ /** False when this step or a later one was used already. */
170
+ takeMfaStep(userId: string, step: number): Promise<boolean>;
171
+ enableMfa(userId: string): Promise<void>;
172
+ resetMfa(email: string): Promise<boolean>;
173
+ /** Replaces the tenant's setting. A domain belongs to one tenant, so a domain that another tenant holds is refused. */
174
+ setSso(s: SsoSetting): Promise<void>;
175
+ private ssoWhere;
176
+ ssoOf(tenant: string): Promise<SsoSetting | null>;
177
+ ssoByDomain(domain: string): Promise<SsoSetting | null>;
178
+ removeSso(tenant: string): Promise<boolean>;
179
+ /**
180
+ * The account behind an identity that the tenant's IdP vouched for: the linked one; else the account with that
181
+ * email, linked from now on, if it verified its address; else a new verified account with a random password.
182
+ * The IdP proves the address, so an account that never verified it loses the address: the account goes, with its
183
+ * password, its sessions, and its links. A tenant that it owned stays for the operator. There are two exceptions: an
184
+ * unverified member of this same tenant, who verifies by password first (UnverifiedAccountError), and an
185
+ * unverified member of another tenant that has other members, who is refused. An account with
186
+ * no tenant joins with the default role. A member that an admin removed joins again only through a new invite,
187
+ * with the invited role.
188
+ */
189
+ findOrCreateSsoUser(s: SsoSetting, id: SsoIdentity): Promise<PortalUser>;
119
190
  /** the user's tenant; one per account today */
120
191
  tenantOf(userId: string): Promise<string | null>;
121
192
  setBilling(tenantId: string, b: {
@@ -134,7 +205,7 @@ export declare class PortalStore {
134
205
  stripeEventSeen(id: string): Promise<boolean>;
135
206
  /** Records a Stripe event id as applied, after it was applied, so a failure before this point lets Stripe's retry through. */
136
207
  noteStripeEvent(id: string): Promise<void>;
137
- /** Every portal registration with its tenant and billing state, oldest first: what the operator's admin page lists. */
208
+ /** Every portal registration with its tenant and billing state, oldest first: what the operator's admin page lists. Owners only. */
138
209
  registrations(): Promise<({
139
210
  tenantId: string;
140
211
  email: string;
@@ -143,8 +214,11 @@ export declare class PortalStore {
143
214
  } & Profile)[]>;
144
215
  tenantBySubscription(subscriptionId: string): Promise<string | null>;
145
216
  }
146
- export declare function signSession(secret: string, userId: string, ttlMs?: number): string;
147
- export declare function readSession(secret: string, cookie: string | undefined): string | null;
217
+ /** `via` records how the user signed in, for example `sso:acme`. */
218
+ export declare function signSession(secret: string, userId: string, ttlMs?: number, via?: string): string;
219
+ export declare const readSession: (secret: string, cookie: string | undefined) => string | null;
220
+ /** The `via` of a valid session, or null. */
221
+ export declare const readSessionVia: (secret: string, cookie: string | undefined) => string | null;
148
222
  export declare function stripeRequest(s: StripeOptions, path: string, body: Record<string, string>): Promise<Record<string, unknown>>;
149
223
  /** Stripe-Signature: t=<unix>,v1=<hmac>; the mac is over `${t}.${rawBody}` with the endpoint secret. */
150
224
  export declare function verifyStripeSignature(header: string | undefined, rawBody: string, secret: string, now?: number, toleranceMs?: number): boolean;