@fabrika/auth-core 0.9.0-dev.35 → 0.9.0-dev.37

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.35",
3
+ "version": "0.9.0-dev.37",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "repository": {
@@ -32,7 +32,11 @@ export function canonicalizeArtifact(artifact: ContractArtifact): ContractArtifa
32
32
  return {
33
33
  format: artifact.format,
34
34
  app: artifact.app,
35
- dimensions: artifact.dimensions,
35
+ dimensions: mapValues(
36
+ artifact.dimensions,
37
+ (dimension) =>
38
+ dimension.values === undefined ? dimension : { ...dimension, values: [...dimension.values].sort((a, b) => compare(a.value, b.value)) },
39
+ ),
36
40
  actions: artifact.actions,
37
41
  roles: mapValues(artifact.roles, canonicalRole),
38
42
  ...(artifact.relations === undefined ? {} : { relations: artifact.relations }),
package/src/contract.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  import type { Locale } from './locale'
5
5
  import type { MailTemplates } from './mail'
6
6
  import type { DeclaredEgress } from './public-egress'
7
+ import type { DimensionValue } from './scope-values'
7
8
  import type { TerminalArtifact } from './terminal'
8
9
 
9
10
  /**
@@ -497,6 +498,8 @@ export interface DimensionArtifact {
497
498
  readonly members: { readonly manage: string; readonly list: string } | null
498
499
  /** `labels`: the member directory shows labels to `members.list` holders. Absent unless declared, so digests stay. */
499
500
  readonly directory?: 'labels'
501
+ /** The values the console offers for a grant in this dimension (ADR-0085). Absent unless declared, so digests stay. */
502
+ readonly values?: readonly DimensionValue[]
500
503
  }
501
504
 
502
505
  export interface RoleArtifact {
package/src/index.ts CHANGED
@@ -26,6 +26,7 @@ export * from './permissions'
26
26
  export * from './public-egress'
27
27
  export * from './requirement'
28
28
  export * from './rpc'
29
+ export * from './scope-values'
29
30
  export { sha256Hex } from './sha256'
30
31
  export * from './terminal'
31
32
  export * from './token'
@@ -0,0 +1,36 @@
1
+ // The values a dimension lists for the person who grants (ADR-0085): information for the console, never authorization.
2
+
3
+ import { MAX_MEMBERSHIP_SCOPE_PART_LENGTH } from './membership'
4
+
5
+ /** One value a dimension lists, with the text the console shows beside it. */
6
+ export interface DimensionValue {
7
+ readonly value: string
8
+ readonly label?: string
9
+ }
10
+
11
+ /** The most values a dimension lists. */
12
+ export const MAX_DIMENSION_VALUES = 500
13
+
14
+ /** The longest label of a listed value. */
15
+ export const MAX_DIMENSION_VALUE_LABEL_LENGTH = 120
16
+
17
+ /**
18
+ * Why `values` is not a dimension's list, or `null`: at least one and at most `MAX_DIMENSION_VALUES` values, each a
19
+ * non-empty value no longer than a scope value IAM takes, listed once, with a non-empty label when it has one.
20
+ */
21
+ export function dimensionValuesProblem(values: readonly DimensionValue[]): string | null {
22
+ if (values.length === 0) return 'lists no value (allowed: at least one, or no values field)'
23
+ if (values.length > MAX_DIMENSION_VALUES) return `lists ${values.length} values (allowed: at most ${MAX_DIMENSION_VALUES})`
24
+ const seen = new Set<string>()
25
+ for (const { value, label } of values) {
26
+ if (value === '' || value.length > MAX_MEMBERSHIP_SCOPE_PART_LENGTH) {
27
+ return `lists the value '${value}' (allowed: 1 to ${MAX_MEMBERSHIP_SCOPE_PART_LENGTH} characters)`
28
+ }
29
+ if (seen.has(value)) return `lists the value '${value}' twice`
30
+ seen.add(value)
31
+ if (label !== undefined && (label.trim() === '' || label.length > MAX_DIMENSION_VALUE_LABEL_LENGTH)) {
32
+ return `labels the value '${value}' with '${label}' (allowed: 1 to ${MAX_DIMENSION_VALUE_LABEL_LENGTH} characters)`
33
+ }
34
+ }
35
+ return null
36
+ }
package/src/token.ts CHANGED
@@ -183,6 +183,11 @@ export interface OAuthTokenClaims {
183
183
  grantId: string
184
184
  /** Unique token id, fresh on every mint. */
185
185
  tokenId: string
186
+ /**
187
+ * The keys of the roles the person holds at an external application that declares roles; absent on every other
188
+ * token. The application decides what each role allows; IAM grants it and names it here.
189
+ */
190
+ roles?: string[]
186
191
  }
187
192
 
188
193
  /** Which tokens a verifier accepts: native app tokens, or OAuth tokens minted for one resource of one app. */
@@ -252,6 +257,10 @@ export interface ResourceAccessTokenParams {
252
257
  scopes: string[]
253
258
  grantId: string
254
259
  tokenId: string
260
+ /** The person's normalized mailbox; an identity resource's token carries it. */
261
+ email?: string
262
+ /** The roles the person holds at an external application that declares roles (`OAuthTokenClaims.roles`). */
263
+ roles?: string[]
255
264
  }
256
265
 
257
266
  /**
@@ -274,6 +283,8 @@ export function buildResourceAccessClaims(params: ResourceAccessTokenParams): Re
274
283
  scope: params.scopes.join(' '),
275
284
  grant: params.grantId,
276
285
  jti: params.tokenId,
286
+ ...(params.email === undefined ? {} : { email: params.email }),
287
+ ...(params.roles === undefined ? {} : { roles: params.roles }),
277
288
  }
278
289
  if (parseAccessClaims(payload)?.oauth === undefined) throw new Error('Invalid resource access token claims')
279
290
  return payload
@@ -523,7 +534,23 @@ function parseOAuthClaims(payload: unknown): OAuthTokenClaims | null | undefined
523
534
  ) {
524
535
  return undefined
525
536
  }
526
- return { app, clientId, scopes, grantId, tokenId }
537
+ const roles = parseTokenRoles(prop(payload, 'roles'))
538
+ if (roles === undefined) return undefined
539
+ return roles === null ? { app, clientId, scopes, grantId, tokenId } : { app, clientId, scopes, grantId, tokenId, roles }
540
+ }
541
+
542
+ const TOKEN_ROLE = /^[a-z0-9][a-z0-9-]{0,62}$/
543
+
544
+ /** Absent → null; distinct role keys → them; anything else → undefined. */
545
+ function parseTokenRoles(value: unknown): string[] | null | undefined {
546
+ if (value === undefined) return null
547
+ if (!Array.isArray(value) || value.length > 32) return undefined
548
+ const roles: string[] = []
549
+ for (const role of value) {
550
+ if (typeof role !== 'string' || !TOKEN_ROLE.test(role) || roles.includes(role)) return undefined
551
+ roles.push(role)
552
+ }
553
+ return roles
527
554
  }
528
555
 
529
556
  /**
package/src/types.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { Locale } from './locale'
2
2
  import type { MailTemplates } from './mail'
3
+ import type { DimensionValue } from './scope-values'
3
4
 
4
5
  export type PrincipalType = 'user' | 'service'
5
6
 
@@ -163,6 +164,8 @@ export interface AppScopeDef {
163
164
  * answers a lookup only for one of them, held on the directory's value. Absent, no endpoint looks one up.
164
165
  */
165
166
  lookups?: string[]
167
+ /** The values the console offers for a grant in this dimension (ADR-0085); absent, it takes free text. */
168
+ values?: DimensionValue[]
166
169
  }
167
170
 
168
171
  /**
@@ -1,6 +1,7 @@
1
1
  import type { Locale } from './locale';
2
2
  import type { MailTemplates } from './mail';
3
3
  import type { DeclaredEgress } from './public-egress';
4
+ import type { DimensionValue } from './scope-values';
4
5
  import type { TerminalArtifact } from './terminal';
5
6
  /**
6
7
  * The artifact format this release writes. A release that changes the artifact raises it, and Control upcasts every
@@ -444,6 +445,8 @@ export interface DimensionArtifact {
444
445
  } | null;
445
446
  /** `labels`: the member directory shows labels to `members.list` holders. Absent unless declared, so digests stay. */
446
447
  readonly directory?: 'labels';
448
+ /** The values the console offers for a grant in this dimension (ADR-0085). Absent unless declared, so digests stay. */
449
+ readonly values?: readonly DimensionValue[];
447
450
  }
448
451
  export interface RoleArtifact {
449
452
  readonly grants: readonly string[];
@@ -25,6 +25,7 @@ export * from './permissions';
25
25
  export * from './public-egress';
26
26
  export * from './requirement';
27
27
  export * from './rpc';
28
+ export * from './scope-values';
28
29
  export { sha256Hex } from './sha256';
29
30
  export * from './terminal';
30
31
  export * from './token';
@@ -0,0 +1,14 @@
1
+ /** One value a dimension lists, with the text the console shows beside it. */
2
+ export interface DimensionValue {
3
+ readonly value: string;
4
+ readonly label?: string;
5
+ }
6
+ /** The most values a dimension lists. */
7
+ export declare const MAX_DIMENSION_VALUES = 500;
8
+ /** The longest label of a listed value. */
9
+ export declare const MAX_DIMENSION_VALUE_LABEL_LENGTH = 120;
10
+ /**
11
+ * Why `values` is not a dimension's list, or `null`: at least one and at most `MAX_DIMENSION_VALUES` values, each a
12
+ * non-empty value no longer than a scope value IAM takes, listed once, with a non-empty label when it has one.
13
+ */
14
+ export declare function dimensionValuesProblem(values: readonly DimensionValue[]): string | null;
@@ -156,6 +156,11 @@ export interface OAuthTokenClaims {
156
156
  grantId: string;
157
157
  /** Unique token id, fresh on every mint. */
158
158
  tokenId: string;
159
+ /**
160
+ * The keys of the roles the person holds at an external application that declares roles; absent on every other
161
+ * token. The application decides what each role allows; IAM grants it and names it here.
162
+ */
163
+ roles?: string[];
159
164
  }
160
165
  /** Which tokens a verifier accepts: native app tokens, or OAuth tokens minted for one resource of one app. */
161
166
  export type TokenAudienceProfile = {
@@ -218,6 +223,10 @@ export interface ResourceAccessTokenParams {
218
223
  scopes: string[];
219
224
  grantId: string;
220
225
  tokenId: string;
226
+ /** The person's normalized mailbox; an identity resource's token carries it. */
227
+ email?: string;
228
+ /** The roles the person holds at an external application that declares roles (`OAuthTokenClaims.roles`). */
229
+ roles?: string[];
221
230
  }
222
231
  /**
223
232
  * Assemble an OAuth resource access token. Always a user principal. The wire carries the standard
@@ -1,5 +1,6 @@
1
1
  import type { Locale } from './locale';
2
2
  import type { MailTemplates } from './mail';
3
+ import type { DimensionValue } from './scope-values';
3
4
  export type PrincipalType = 'user' | 'service';
4
5
  /**
5
6
  * Whether a person belongs to the organization (ADR-0051). An `external` principal holds only grants of a role its
@@ -132,6 +133,8 @@ export interface AppScopeDef {
132
133
  * answers a lookup only for one of them, held on the directory's value. Absent, no endpoint looks one up.
133
134
  */
134
135
  lookups?: string[];
136
+ /** The values the console offers for a grant in this dimension (ADR-0085); absent, it takes free text. */
137
+ values?: DimensionValue[];
135
138
  }
136
139
  /**
137
140
  * The role keys IAM defines itself for every app. A built-in wins over an app role of the same key at