@tangle-network/hub-sdk 0.15.3-develop.20260731071553.b437581 → 0.17.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/README.md CHANGED
@@ -30,9 +30,7 @@ SaaS vs on-prem flips via env only — there is no per-client default URL.
30
30
  | `TANGLE_API_KEY` | One of | Long-lived API key (Bearer). User / API key principal. |
31
31
  | `TANGLE_HUB_CAPABILITY_TOKEN` | One of | Short-lived capability token (Bearer). Sandbox-runtime principal. Auto-minted server-side per action. |
32
32
 
33
- The two auth credentials are mutually exclusive — set exactly one. This
34
- mirrors the platform-api `hubSandboxEnvironmentSchema` rule
35
- (`exactly-one-api-key-or-capability-token`).
33
+ The two auth credentials are mutually exclusive — set exactly one.
36
34
 
37
35
  ### Examples
38
36
 
@@ -218,19 +216,99 @@ product does not need a second workflow-run request.
218
216
  Callbacks may be retried; use `delivery.runId` as the durable idempotency key
219
217
  before starting product work.
220
218
 
219
+ ## Allowances and the paywall decision
220
+
221
+ A product that bills its end users sets a free allowance, and optionally a paid one, on a meter.
222
+ Hub admits every turn against it, counts turns per member and per line, and reads each payer's dollars from the ledger.
223
+ The product keeps only its checkout: after a payment it records the member as paid until the period ends.
224
+
225
+ ```ts
226
+ await hub.allowances.setPlan(`agent:${agentId}`, {
227
+ free: { turnsPerDay: 20, usdPerDay: 2 },
228
+ paid: { turnsPerDay: 200, usdPerDay: 5, priceUsdMonthly: 10 },
229
+ spendResourceType: "hosted_enrollment",
230
+ });
231
+
232
+ const turn = await hub.allowances.admit(`agent:${agentId}`, {
233
+ member: enrollmentId,
234
+ turnId: messageId,
235
+ line: lineId,
236
+ });
237
+ if (turn.decision === "paywall") return sendCheckoutLink();
238
+ if (turn.decision === "allowance_reached") return sendComeBackTomorrow();
239
+
240
+ // From the checkout webhook:
241
+ await hub.allowances.grant(`agent:${agentId}`, enrollmentId, {
242
+ expiresAt: periodEnd.toISOString(),
243
+ reference: subscriptionId,
244
+ });
245
+ ```
246
+
247
+ `admit` is idempotent by `turnId`, so a retried turn is never counted twice.
248
+ A member's dollars are the sum of today's ledger charges on keys bound to `(spendResourceType, member)`, so delegate each member's key with that resource.
249
+ Only the meter's owner may call these, signed in or with a root key of their own.
250
+ A delegated or resource-bound key is refused, so an end user's sandbox cannot grant itself the paid tier.
251
+
221
252
  ## Exports
222
253
 
223
254
  - `HubClient`, `HubClient.fromEnv(options?)`
224
255
  - `HubConnectionsClient`, `HubPermissionsClient`, `HubTokensClient`,
225
256
  `HubChannelsClient`, `HubEventSubscriptionsClient`, `HubToolsClient`,
226
- `HubApprovalsClient`, `HubAuditClient`, `HubWorkflowsClient`
257
+ `HubApprovalsClient`, `HubAuditClient`, `HubWorkflowsClient`,
258
+ `HubAllowancesClient`
227
259
  - `deriveHubEventCallbackSecret`, `authenticateHubEventRequest`,
228
260
  `verifyHubEventSignature`, `parseHubEventDelivery`, `HubEventDeliveryError`
229
261
  - `HubSdkError` — typed `code: HubErrorCode`, redacted `details`, optional
230
262
  HTTP `status`
231
263
  - `HUB_URL_ENV_VAR`, `HUB_API_KEY_ENV_VAR`, `HUB_CAPABILITY_TOKEN_ENV_VAR` —
232
- string constants for the env-var names (mirrored by the platform-api
233
- `hub-sandbox-contract.ts` `z.literal(...)`)
264
+ string constants for the env-var names
234
265
  - `resolveHubBaseUrl(env?)`, `resolveHubAuth(env?)` — standalone resolvers
235
266
  with the same fail-loud contract as `fromEnv`
236
267
  - All request/response types from the `/v1/hub/*` contract
268
+
269
+
270
+ ## Link application login to explicit Hub permissions
271
+
272
+ Use the same `HubClient` for application management; do not add a product-local
273
+ HTTP client. These operations require the authenticated developer account, not
274
+ an app's OIDC access token. Customer grants are created only by the existing
275
+ session-authenticated `/cross-site/app-consent` flow.
276
+
277
+ ```ts
278
+ import { HubClient } from "@tangle-network/hub-sdk";
279
+
280
+ const hub = HubClient.fromEnv(); // developer account for app administration
281
+ const { app, clientSecret } = await hub.apps.create({
282
+ name: "Support assistant",
283
+ redirectUris: ["https://assistant.example/hub/callback"],
284
+ allowedScopes: ["github.issues.create"],
285
+ });
286
+ // Save clientSecret once in the application's server-side secret store.
287
+ await hub.apps.linkOAuthClient(app.id, oidcClientId);
288
+ const apps = await hub.apps.list(); // includes oauthClientId; never the secret
289
+ ```
290
+
291
+ `oidcClientId` is the application's already-registered Sign in with Tangle client.
292
+ Linking requires the same owner on both records and is immutable and one-to-one.
293
+ It does not reuse the two clients' secrets, change their IDs, create a user grant,
294
+ or authorize spending. Use separate app/client pairs for separate deployments.
295
+ Existing unlinked broker apps remain compatible. There is no automatic link by
296
+ name, callback domain, email, or ownership alone.
297
+
298
+ With a customer's authenticated Hub client, `hub.apps.grants.list()` returns only
299
+ that customer's active resource grants and `hub.apps.grants.revoke(grantId)`
300
+ revokes one idempotently. App administrators use `hub.apps.revoke(appId)` to
301
+ suspend the app, revoke all its customer grants, and disable its owned linked
302
+ OIDC client. An app's broker credential cannot call these management methods.
303
+
304
+ Broker execution keeps the one approved connection even when the invocation
305
+ omits `connectionId`; it never substitutes another account. Each one-shot broker
306
+ token is checked against current app policy and the exact active grant when
307
+ minted and consumed. Changing consent revokes the old binding, tokens and codes;
308
+ the fresh consent response supplies a new grant ID. Login disconnect revokes the
309
+ linked app's existing grants for that customer, not other customers or apps.
310
+ An operation already admitted to a provider may finish; revocation blocks new
311
+ admissions and minting, not external side effects already underway.
312
+
313
+ This is identity and resource delegation, **not** generalized spending delegation.
314
+ Existing product billing checks and action approvals continue to apply.