@fabrika/iam-contract 0.9.0-dev.45 → 0.9.0-dev.47

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/iam-contract",
3
- "version": "0.9.0-dev.45",
3
+ "version": "0.9.0-dev.47",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -27,8 +27,8 @@
27
27
  "test": "bun test"
28
28
  },
29
29
  "dependencies": {
30
- "@fabrika/app": "0.9.0-dev.45",
31
- "@fabrika/auth-core": "0.9.0-dev.45",
32
- "@fabrika/proxy-core": "0.9.0-dev.45"
30
+ "@fabrika/app": "0.9.0-dev.47",
31
+ "@fabrika/auth-core": "0.9.0-dev.47",
32
+ "@fabrika/proxy-core": "0.9.0-dev.47"
33
33
  }
34
34
  }
package/src/oauth.ts CHANGED
@@ -1,4 +1,13 @@
1
- import { BUILTIN_ROLE_KEYS, isCimdClientId, isOAuthClientId, isOAuthResourceUri, parseOAuthScopes } from '@fabrika/auth-core'
1
+ import {
2
+ AGENT_CLIENT_ID,
3
+ BUILTIN_ROLE_KEYS,
4
+ isAgentLabel,
5
+ isCimdClientId,
6
+ isOAuthClientId,
7
+ isOAuthResourceUri,
8
+ isScopeParameterText,
9
+ parseOAuthScopes,
10
+ } from '@fabrika/auth-core'
2
11
  import type { KeyGrant } from '@fabrika/auth-core'
3
12
 
4
13
  /**
@@ -139,8 +148,11 @@ export type OAuthTokenRequest =
139
148
  /** RFC 8628 §3.1, with every resource the grant is asked for (RFC 8707): each token names one of them. */
140
149
  export interface OAuthDeviceAuthorizationRequest {
141
150
  client_id: string
151
+ /** Empty only for the agent client, which then asks for its default scope (ADR-0090 §2). */
142
152
  scope: string[]
143
153
  resources: string[]
154
+ /** The agent client's name for itself, shown to the person as the agent's own words; ignored for any other client. */
155
+ agent_label?: string
144
156
  }
145
157
 
146
158
  /** RFC 8628 §3.2. */
@@ -416,11 +428,6 @@ function parseResourcePermissions(value: unknown): KeyGrant[] | null {
416
428
  /** Separates a parameterized scope from its value: `deploy:alpha`. */
417
429
  export const SCOPE_PARAMETER_SEPARATOR = ':'
418
430
 
419
- /** A scope dimension type, or a value of one, as a scope token may carry it. */
420
- function isScopeParameterText(value: unknown): value is string {
421
- return typeof value === 'string' && value.length <= 100 && /^[A-Za-z0-9][A-Za-z0-9._@-]*$/.test(value)
422
- }
423
-
424
431
  /**
425
432
  * The resource's grant a requested scope token selects and the narrowed permissions it holds, or null when the token
426
433
  * selects none: an unknown scope, a parameterized scope without its value, a value on a scope that takes none.
@@ -507,22 +514,28 @@ export function parseOAuthTokenRequest(value: unknown): OAuthTokenRequest | null
507
514
  }
508
515
 
509
516
  /**
510
- * A device authorization request: its client, at least one scope and one to `MAX_OAUTH_DEVICE_RESOURCES` distinct
511
- * resources. `resource` is the one parameter that may repeat (RFC 8707 §2), so the caller collects it into a list.
517
+ * A device authorization request: its client, at least one scope (the agent client may name none) and one to
518
+ * `MAX_OAUTH_DEVICE_RESOURCES` distinct resources. `resource` is the one parameter that may repeat (RFC 8707 §2), so the
519
+ * caller collects it into a list.
512
520
  */
513
521
  export function parseOAuthDeviceAuthorizationRequest(
514
522
  value: { fields: Record<string, string>; resources: string[] },
515
523
  ): OAuthDeviceAuthorizationRequest | null {
516
524
  const clientId = field(value.fields, 'client_id')
517
525
  const scope = field(value.fields, 'scope')
518
- if (!isOAuthClientId(clientId) || typeof scope !== 'string') return null
519
- const scopes = parseOAuthScopes(scope.split(' '))
526
+ if (!isOAuthClientId(clientId)) return null
527
+ const agent = clientId === AGENT_CLIENT_ID
528
+ // The agent client may omit the scope, and a parameter without a value is omitted (RFC 6749 §3.1); IAM then picks `read`.
529
+ const scopes = agent && (scope === undefined || scope === '') ? [] : typeof scope === 'string' ? parseOAuthScopes(scope.split(' ')) : null
520
530
  const resources = value.resources
521
531
  if (
522
- scopes === null || scopes.length === 0 || resources.length === 0 || resources.length > MAX_OAUTH_DEVICE_RESOURCES
532
+ scopes === null || (scopes.length === 0 && !agent) || resources.length === 0 || resources.length > MAX_OAUTH_DEVICE_RESOURCES
523
533
  || !resources.every(isOAuthResourceUri) || new Set(resources).size !== resources.length
524
534
  ) return null
525
- return { client_id: clientId, scope: scopes, resources: [...resources] }
535
+ const label = field(value.fields, 'agent_label')
536
+ if (!agent || label === undefined) return { client_id: clientId, scope: scopes, resources: [...resources] }
537
+ if (typeof label !== 'string' || !isAgentLabel(label)) return null
538
+ return { client_id: clientId, scope: scopes, resources: [...resources], agent_label: label }
526
539
  }
527
540
 
528
541
  export function isOAuthDeviceGrantLifetime(value: number): value is OAuthDeviceGrantLifetime {
@@ -139,8 +139,11 @@ export type OAuthTokenRequest = {
139
139
  /** RFC 8628 §3.1, with every resource the grant is asked for (RFC 8707): each token names one of them. */
140
140
  export interface OAuthDeviceAuthorizationRequest {
141
141
  client_id: string;
142
+ /** Empty only for the agent client, which then asks for its default scope (ADR-0090 §2). */
142
143
  scope: string[];
143
144
  resources: string[];
145
+ /** The agent client's name for itself, shown to the person as the agent's own words; ignored for any other client. */
146
+ agent_label?: string;
144
147
  }
145
148
  /** RFC 8628 §3.2. */
146
149
  export interface OAuthDeviceAuthorizationResponse {
@@ -212,8 +215,9 @@ export declare function parseOAuthAuthorizationAttempt(value: unknown): OAuthAut
212
215
  export declare function parseOAuthAuthorizationRequest(value: unknown): OAuthAuthorizationRequest | null;
213
216
  export declare function parseOAuthTokenRequest(value: unknown): OAuthTokenRequest | null;
214
217
  /**
215
- * A device authorization request: its client, at least one scope and one to `MAX_OAUTH_DEVICE_RESOURCES` distinct
216
- * resources. `resource` is the one parameter that may repeat (RFC 8707 §2), so the caller collects it into a list.
218
+ * A device authorization request: its client, at least one scope (the agent client may name none) and one to
219
+ * `MAX_OAUTH_DEVICE_RESOURCES` distinct resources. `resource` is the one parameter that may repeat (RFC 8707 §2), so the
220
+ * caller collects it into a list.
217
221
  */
218
222
  export declare function parseOAuthDeviceAuthorizationRequest(value: {
219
223
  fields: Record<string, string>;