@fabrika/auth-core 0.9.0-dev.31 → 0.9.0-dev.32

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/auth-core",
3
- "version": "0.9.0-dev.31",
3
+ "version": "0.9.0-dev.32",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "repository": {
package/src/token.ts CHANGED
@@ -105,7 +105,10 @@ export interface AccessTokenClaims {
105
105
  iat: number
106
106
  /** Expiry (unix seconds). */
107
107
  exp: number
108
- /** Resolved/effective permissions; `can(action, scope?)` = `permits(perms, action, scope)`. */
108
+ /**
109
+ * Resolved/effective permissions; `can(action, scope?)` = `permits(perms, action, scope)`. On the wire a compact token
110
+ * carries its global grants in `gperms` (`AccessTokenPayload`); the parser reads them back into this list.
111
+ */
109
112
  perms: PermissionEntry[]
110
113
  /** Present only when the token is bound to a principal (`sub` is that principal). */
111
114
  ptype?: TokenPrincipalType
@@ -282,6 +285,47 @@ export function matchesTokenAudience(claims: AccessTokenClaims, profile: TokenAu
282
285
  return claims.oauth !== undefined && claims.aud === profile.resource && claims.oauth.app === profile.app
283
286
  }
284
287
 
288
+ /**
289
+ * How an access token's claims travel. `entries` writes every permission into `perms`. `compact` writes each global
290
+ * grant (scope null, source `grant`) into `gperms` as its bare action name and the rest into `perms`; it is a third of
291
+ * the size for a person holding a few hundred actions. `parseAccessClaims` reads both into the same `perms`.
292
+ */
293
+ export type AccessTokenLayout = 'entries' | 'compact'
294
+
295
+ /** The signed payload: the claims, with the global grants of the compact layout in `gperms`. */
296
+ export interface AccessTokenPayload extends AccessTokenClaims {
297
+ /** The compact layout's global grants, by action name; absent in the entries layout and when there are none. */
298
+ gperms?: string[]
299
+ }
300
+
301
+ /** The payload that carries `claims` in `layout`. */
302
+ export function accessTokenPayload(claims: AccessTokenClaims, layout: AccessTokenLayout): AccessTokenPayload {
303
+ const gperms = layout === 'compact' ? claims.perms.filter(isGlobalGrant).map((entry) => entry.action) : []
304
+ return gperms.length === 0 ? { ...claims } : { ...claims, perms: claims.perms.filter((entry) => !isGlobalGrant(entry)), gperms }
305
+ }
306
+
307
+ /**
308
+ * Sign `claims` in the entries layout, or in the compact one when the entries layout is above `bound` bytes; the caller
309
+ * refuses a result still above it. A reader older than `gperms` reads a compact token without its global grants, which
310
+ * denies more and never allows more, and receives one only where the entries layout would have been refused anyway, so
311
+ * a platform release never changes a token an older framework release or a pinned proxy reads (ADR-0062).
312
+ */
313
+ export async function signAccessClaims(
314
+ claims: AccessTokenClaims,
315
+ bound: number,
316
+ sign: (payload: AccessTokenPayload) => Promise<string>,
317
+ ): Promise<string> {
318
+ const entries = await sign(accessTokenPayload(claims, 'entries'))
319
+ if (entries.length <= bound || !claims.perms.some(isGlobalGrant)) {
320
+ return entries
321
+ }
322
+ return sign(accessTokenPayload(claims, 'compact'))
323
+ }
324
+
325
+ function isGlobalGrant(entry: PermissionEntry): boolean {
326
+ return entry.scope === null && entry.source === 'grant'
327
+ }
328
+
285
329
  /** Assemble the claim object for an access token. Pure — the caller signs the result. */
286
330
  export function buildAccessClaims(params: AccessTokenParams): AccessTokenClaims {
287
331
  const claims: AccessTokenClaims = {
@@ -340,15 +384,17 @@ export function parseAccessClaims(payload: unknown): AccessTokenClaims | null {
340
384
  const sub = stringField(payload, 'sub')
341
385
  const iat = numberField(payload, 'iat')
342
386
  const exp = numberField(payload, 'exp')
343
- const perms = parsePermissionEntries(prop(payload, 'perms'))
387
+ const entries = parsePermissionEntries(prop(payload, 'perms'))
388
+ const globalGrants = parseGlobalGrants(prop(payload, 'gperms'))
344
389
  const label = parseNullableString(prop(payload, 'label'))
345
390
  const ptype = parseOptionalPrincipalType(prop(payload, 'ptype'))
346
391
  if (iss === undefined || aud === undefined || sub === undefined || iat === undefined || exp === undefined) {
347
392
  return null
348
393
  }
349
- if (perms === null || label === undefined || ptype === undefined) {
394
+ if (entries === null || globalGrants === null || label === undefined || ptype === undefined) {
350
395
  return null
351
396
  }
397
+ const perms = [...entries, ...globalGrants]
352
398
  const oauth = parseOAuthClaims(payload)
353
399
  if (oauth === undefined || (oauth !== null && (ptype !== 'user' || !isOAuthResourceUri(aud)))) {
354
400
  return null
@@ -539,6 +585,24 @@ function parseSource(value: unknown): PermissionSource | null {
539
585
  return null
540
586
  }
541
587
 
588
+ /** The compact layout's `gperms`: absent → none; a non-empty array of non-empty action names → their global grants. */
589
+ function parseGlobalGrants(value: unknown): PermissionEntry[] | null {
590
+ if (value === undefined) {
591
+ return []
592
+ }
593
+ if (!Array.isArray(value) || value.length === 0) {
594
+ return null
595
+ }
596
+ const out: PermissionEntry[] = []
597
+ for (const action of value) {
598
+ if (typeof action !== 'string' || action === '') {
599
+ return null
600
+ }
601
+ out.push({ action, scope: null, source: 'grant' })
602
+ }
603
+ return out
604
+ }
605
+
542
606
  function parsePermissionEntries(value: unknown): PermissionEntry[] | null {
543
607
  if (!Array.isArray(value)) {
544
608
  return null
@@ -87,7 +87,10 @@ export interface AccessTokenClaims {
87
87
  iat: number;
88
88
  /** Expiry (unix seconds). */
89
89
  exp: number;
90
- /** Resolved/effective permissions; `can(action, scope?)` = `permits(perms, action, scope)`. */
90
+ /**
91
+ * Resolved/effective permissions; `can(action, scope?)` = `permits(perms, action, scope)`. On the wire a compact token
92
+ * carries its global grants in `gperms` (`AccessTokenPayload`); the parser reads them back into this list.
93
+ */
91
94
  perms: PermissionEntry[];
92
95
  /** Present only when the token is bound to a principal (`sub` is that principal). */
93
96
  ptype?: TokenPrincipalType;
@@ -224,6 +227,26 @@ export interface ResourceAccessTokenParams {
224
227
  export declare function buildResourceAccessClaims(params: ResourceAccessTokenParams): Record<string, unknown>;
225
228
  /** A token is accepted only by the profile it was minted for: an app token never passes as a resource token, and vice versa. */
226
229
  export declare function matchesTokenAudience(claims: AccessTokenClaims, profile: TokenAudienceProfile): boolean;
230
+ /**
231
+ * How an access token's claims travel. `entries` writes every permission into `perms`. `compact` writes each global
232
+ * grant (scope null, source `grant`) into `gperms` as its bare action name and the rest into `perms`; it is a third of
233
+ * the size for a person holding a few hundred actions. `parseAccessClaims` reads both into the same `perms`.
234
+ */
235
+ export type AccessTokenLayout = 'entries' | 'compact';
236
+ /** The signed payload: the claims, with the global grants of the compact layout in `gperms`. */
237
+ export interface AccessTokenPayload extends AccessTokenClaims {
238
+ /** The compact layout's global grants, by action name; absent in the entries layout and when there are none. */
239
+ gperms?: string[];
240
+ }
241
+ /** The payload that carries `claims` in `layout`. */
242
+ export declare function accessTokenPayload(claims: AccessTokenClaims, layout: AccessTokenLayout): AccessTokenPayload;
243
+ /**
244
+ * Sign `claims` in the entries layout, or in the compact one when the entries layout is above `bound` bytes; the caller
245
+ * refuses a result still above it. A reader older than `gperms` reads a compact token without its global grants, which
246
+ * denies more and never allows more, and receives one only where the entries layout would have been refused anyway, so
247
+ * a platform release never changes a token an older framework release or a pinned proxy reads (ADR-0062).
248
+ */
249
+ export declare function signAccessClaims(claims: AccessTokenClaims, bound: number, sign: (payload: AccessTokenPayload) => Promise<string>): Promise<string>;
227
250
  /** Assemble the claim object for an access token. Pure — the caller signs the result. */
228
251
  export declare function buildAccessClaims(params: AccessTokenParams): AccessTokenClaims;
229
252
  /**