@sylphx/sdk 0.19.2 → 0.19.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.
@@ -463,6 +463,31 @@ interface AccessTokenPayload {
463
463
  role?: string;
464
464
  /** Project session id when the token was minted from a BaaS auth session. */
465
465
  sid?: string;
466
+ /**
467
+ * Org context claims — present on org-scoped tokens (switch-org exchanges
468
+ * and service-account client-credentials grants; ADR-2062). Resource
469
+ * servers MUST compare `org_id` against the addressed resource's org and
470
+ * assert `org_role` against their allowlist before granting org-scoped
471
+ * access. Absent on plain user tokens.
472
+ *
473
+ * `org_role` is one of `super_admin, admin, billing, analytics, developer,
474
+ * viewer` (or a project-defined custom RBAC key). Service accounts emit
475
+ * only their configured binding role ∈ `{admin, developer, viewer}`.
476
+ * `owner` does not exist and is never emitted.
477
+ */
478
+ org_id?: string;
479
+ org_slug?: string;
480
+ org_role?: string;
481
+ /**
482
+ * RFC 7800 confirmation claim — present when the token is sender-
483
+ * constrained (DPoP-bound, RFC 9449). `cnf.jkt` is the SHA-256 thumbprint
484
+ * of the client's DPoP public key. Resource servers enforcing DPoP MUST
485
+ * validate the inbound `DPoP` proof against this thumbprint. Narrowed to
486
+ * `{ jkt? }` — never trust an unparsed claim shape.
487
+ */
488
+ cnf?: {
489
+ jkt?: string;
490
+ };
466
491
  iat: number;
467
492
  exp: number;
468
493
  }
@@ -480,9 +505,10 @@ interface AccessTokenPayload {
480
505
  * 4. No silent fixes - Transparency over convenience (but warn + continue)
481
506
  * 5. Single Source of Truth - All key logic in one place
482
507
  *
483
- * Key Formats (ADR-021):
484
- * - Publishable key: pk_(dev|stg|prod|prev)_{ref}_{32hex} — client-safe (new)
485
- * - Secret Key: sk_(dev|stg|prod|prev)_{ref}_{64hex} — server-side only
508
+ * Key Formats (ADR-055):
509
+ * - Publishable key: pk_(dev|stg|prod|prev)_{32hex} — client-safe
510
+ * - Secret Key: sk_(dev|stg|prod|prev)_{64hex} — server-side only
511
+ * - Legacy compatibility accepts embedded-ref keys from the superseded ADR-021 path.
486
512
  *
487
513
  * Legacy Key Formats (backward-compat):
488
514
  * - App ID (old): app_(dev|stg|prod|prev)_[identifier] — Public identifier
@@ -514,7 +540,7 @@ interface KeyValidationResult {
514
540
  /**
515
541
  * Validate a legacy App ID (app_*) and return detailed results.
516
542
  *
517
- * @deprecated Use validatePublicKey() for new pk_* keys (ADR-021).
543
+ * @deprecated Use validatePublicKey() for ADR-055 pk_* keys.
518
544
  *
519
545
  * @example
520
546
  * ```typescript
@@ -531,7 +557,7 @@ declare function validateAppId(key: string | undefined | null): KeyValidationRes
531
557
  /**
532
558
  * Validate and sanitize App ID, logging warnings.
533
559
  *
534
- * @deprecated Use validateAndSanitizePublicKey() for new pk_* keys (ADR-021).
560
+ * @deprecated Use validateAndSanitizePublicKey() for ADR-055 pk_* keys.
535
561
  * @throws Error if the key is invalid and cannot be sanitized
536
562
  * @returns The sanitized App ID
537
563
  */
@@ -1514,11 +1540,57 @@ declare function resetJwksCache(): void;
1514
1540
  */
1515
1541
  declare function getJwks(platformUrl?: string): Promise<JsonWebKey[]>;
1516
1542
  /**
1517
- * Verify an access token from the platform
1543
+ * Verify an access token from the platform.
1544
+ *
1545
+ * Verifies the RS256 signature against the cached JWKS and the `iss` claim.
1546
+ *
1547
+ * ## Audience
1548
+ *
1549
+ * Pass `audience` to bind the token to the resource server it was minted for.
1550
+ * Platform-issued tokens carry `aud === app_id` (literal `'platform'` for
1551
+ * Platform-app tokens; the customer app's identity otherwise — see ADR-2062).
1552
+ * When supplied, `jose` rejects any token whose `aud` does not match — a token
1553
+ * signed for a different audience is refused even though its signature is
1554
+ * valid. **Always pass `audience` on a resource server**; omitting it accepts
1555
+ * any validly-signed token regardless of its intended audience (a fail-open
1556
+ * trap). The option is optional only for backward compatibility with existing
1557
+ * callers; new code should always pass it.
1558
+ *
1559
+ * The canonical verifier with mandatory audience + kid-rotation is the root
1560
+ * `verifyAccessToken` from `@sylphx/sdk` (`{ baseUrl, audience }`); prefer it
1561
+ * for Platform-internal resource servers. This `/server` variant exists for
1562
+ * customer apps verifying their own end-user tokens.
1563
+ *
1564
+ * ## Returned claims
1565
+ *
1566
+ * Org context (`org_id`/`org_slug`/`org_role`) and the RFC 7800 `cnf`
1567
+ * confirmation claim are preserved when present. Downstream org-scoped
1568
+ * authorization and DPoP binding depend on these — a resource server MUST
1569
+ * compare `org_id` against the addressed resource and assert `org_role`
1570
+ * against its allowlist before granting org-scoped access.
1571
+ *
1572
+ * @example
1573
+ * ```typescript
1574
+ * import { verifyAccessToken } from '@sylphx/sdk/server'
1575
+ *
1576
+ * const claims = await verifyAccessToken(bearer, {
1577
+ * platformUrl: 'https://your-app.api.sylphx.com',
1578
+ * audience: 'your-app', // pins `aud`; reject tokens for other audiences
1579
+ * })
1580
+ * if (claims.org_id !== resource.orgId) throw new Error('cross-org access')
1581
+ * ```
1518
1582
  */
1519
1583
  declare function verifyAccessToken(token: string, options: {
1520
1584
  secretKey?: string;
1521
1585
  platformUrl?: string;
1586
+ /**
1587
+ * Expected `aud` claim. When supplied, the token is rejected unless its
1588
+ * audience matches (string) or is one of the allowed audiences (array,
1589
+ * for multi-audience resource servers). Omitting it skips audience
1590
+ * validation — only do this if the token's audience is irrelevant to the
1591
+ * caller. See the function doc; new code should always pass this.
1592
+ */
1593
+ audience?: string | string[];
1522
1594
  }): Promise<AccessTokenPayload>;
1523
1595
  interface WebhookPayload {
1524
1596
  event: string;
@@ -3016,11 +3016,14 @@ async function verifyAccessToken(token, options) {
3016
3016
  try {
3017
3017
  const jwk = await importJWK(key, "RS256");
3018
3018
  const { payload } = await jwtVerify(token, jwk, {
3019
- issuer: platformUrl
3019
+ issuer: platformUrl,
3020
+ ...options.audience !== void 0 ? { audience: options.audience } : {}
3020
3021
  });
3021
3022
  if (!isAccessTokenPayload(payload)) {
3022
3023
  throw new Error("Invalid token payload structure");
3023
3024
  }
3025
+ const cnfRaw = payload.cnf;
3026
+ const cnf = cnfRaw && typeof cnfRaw === "object" && "jkt" in cnfRaw ? { jkt: typeof cnfRaw.jkt === "string" ? cnfRaw.jkt : void 0 } : void 0;
3024
3027
  return {
3025
3028
  sub: payload.sub,
3026
3029
  pid: payload.pid,
@@ -3030,8 +3033,14 @@ async function verifyAccessToken(token, options) {
3030
3033
  email_verified: payload.email_verified,
3031
3034
  app_id: payload.app_id,
3032
3035
  role: payload.role,
3036
+ // Org context — preserved so downstream org-scoped authz works
3037
+ // (ADR-2062). Absent on plain user tokens.
3038
+ org_id: typeof payload.org_id === "string" ? payload.org_id : void 0,
3039
+ org_slug: typeof payload.org_slug === "string" ? payload.org_slug : void 0,
3040
+ org_role: typeof payload.org_role === "string" ? payload.org_role : void 0,
3033
3041
  iat: payload.iat,
3034
- exp: payload.exp
3042
+ exp: payload.exp,
3043
+ ...cnf ? { cnf } : {}
3035
3044
  };
3036
3045
  } catch (err) {
3037
3046
  lastError = err;