@fabrika/iam-contract 0.9.0-dev.33 → 0.9.0-dev.35
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 +7 -5
- package/src/oauth.ts +184 -16
- package/types/src/oauth.d.ts +81 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/iam-contract",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.35",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
14
|
"src",
|
|
15
|
+
"!src/**/__tests__/**",
|
|
15
16
|
"types"
|
|
16
17
|
],
|
|
17
18
|
"exports": {
|
|
@@ -22,11 +23,12 @@
|
|
|
22
23
|
}
|
|
23
24
|
},
|
|
24
25
|
"scripts": {
|
|
25
|
-
"typecheck": "tsrs --build tsconfig.
|
|
26
|
+
"typecheck": "tsrs --build tsconfig.test.json",
|
|
27
|
+
"test": "bun test"
|
|
26
28
|
},
|
|
27
29
|
"dependencies": {
|
|
28
|
-
"@fabrika/app": "0.9.0-dev.
|
|
29
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
30
|
-
"@fabrika/proxy-core": "0.9.0-dev.
|
|
30
|
+
"@fabrika/app": "0.9.0-dev.35",
|
|
31
|
+
"@fabrika/auth-core": "0.9.0-dev.35",
|
|
32
|
+
"@fabrika/proxy-core": "0.9.0-dev.35"
|
|
31
33
|
}
|
|
32
34
|
}
|
package/src/oauth.ts
CHANGED
|
@@ -1,11 +1,36 @@
|
|
|
1
|
-
import { isCimdClientId, isOAuthResourceUri, parseOAuthScopes } from '@fabrika/auth-core'
|
|
1
|
+
import { isCimdClientId, isOAuthClientId, isOAuthResourceUri, parseOAuthScopes } from '@fabrika/auth-core'
|
|
2
2
|
import type { KeyGrant } from '@fabrika/auth-core'
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* The kinds of managed client an installation registers. A label only: every kind gets the same authorization code
|
|
6
|
+
* flow with S256 PKCE, the same RFC 8707 resources and the same scope ceilings (ADR-0049).
|
|
7
|
+
*/
|
|
8
|
+
export const OAUTH_CLIENT_PROVIDERS = ['claude', 'chatgpt', 'external'] as const
|
|
9
|
+
export type OAuthClientProvider = (typeof OAUTH_CLIENT_PROVIDERS)[number]
|
|
10
|
+
|
|
11
|
+
function isOAuthClientProvider(value: unknown): value is OAuthClientProvider {
|
|
12
|
+
return OAUTH_CLIENT_PROVIDERS.some((provider) => provider === value)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The one resource an identity-only client is served: its own, outside the installation. A token for it carries the
|
|
17
|
+
* person's identity and no permission, under `app`, an id no installation service or application holds.
|
|
18
|
+
*/
|
|
19
|
+
export interface OAuthIdentityResource {
|
|
20
|
+
app: string
|
|
21
|
+
resource: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The one scope of an identity resource: it names the person and grants nothing. */
|
|
25
|
+
export const OAUTH_IDENTITY_SCOPE = 'identity'
|
|
26
|
+
|
|
4
27
|
/** Managed CIMD registrations only; public dynamic registration is not part of this contract. */
|
|
5
28
|
export interface OAuthManagedClient {
|
|
6
|
-
provider:
|
|
29
|
+
provider: OAuthClientProvider
|
|
7
30
|
clientId: string
|
|
8
31
|
redirectUris: string[]
|
|
32
|
+
/** Present only on an `external` client: it is served this resource alone, never a registered one. */
|
|
33
|
+
identity?: OAuthIdentityResource
|
|
9
34
|
}
|
|
10
35
|
|
|
11
36
|
/** RFC 7523 §2.2: the `client_assertion_type` of a JWT client assertion (`private_key_jwt`). */
|
|
@@ -36,13 +61,28 @@ export type OAuthClientMetadataDocument =
|
|
|
36
61
|
/** How a token or revocation request authenticates its client: no credential, or a JWT assertion. */
|
|
37
62
|
export type OAuthClientCredential = { method: 'none' } | { method: 'private_key_jwt'; assertion: string }
|
|
38
63
|
|
|
64
|
+
export interface OAuthScopeGrant {
|
|
65
|
+
scope: string
|
|
66
|
+
permissions: KeyGrant[]
|
|
67
|
+
/**
|
|
68
|
+
* A scope dimension type. Such a scope is requested only as `<scope>:<value>`, and each of its scope-less permissions
|
|
69
|
+
* is narrowed to `{ type: parameter, value }`: `deploy:alpha` with parameter `app` holds its actions at app `alpha` only.
|
|
70
|
+
*/
|
|
71
|
+
parameter?: string
|
|
72
|
+
}
|
|
73
|
+
|
|
39
74
|
export interface OAuthResourceDto {
|
|
40
75
|
app: string
|
|
41
76
|
resource: string
|
|
42
77
|
/** IAM resolves these app-owned grants; OAuth scope text alone never authorizes an action. */
|
|
43
|
-
scopeGrants:
|
|
78
|
+
scopeGrants: OAuthScopeGrant[]
|
|
44
79
|
/** Used only when scope is omitted. No configured default means invalid_scope, never all scopes. */
|
|
45
80
|
defaultScopes?: string[]
|
|
81
|
+
/**
|
|
82
|
+
* `cli`: served only to the installation's command-line client, through the device grant. Absent: served only to the
|
|
83
|
+
* operator's managed clients, through an authorization code. A token for one never comes from the other's flow.
|
|
84
|
+
*/
|
|
85
|
+
client?: 'cli'
|
|
46
86
|
}
|
|
47
87
|
|
|
48
88
|
/** No access token, refresh token, hash, or authorization code appears in a grant DTO. */
|
|
@@ -76,9 +116,37 @@ export type OAuthAuthorizationParseResult =
|
|
|
76
116
|
| { kind: 'invalid_scope'; coordinates: OAuthAuthorizationCoordinates; rawScope: string }
|
|
77
117
|
| { kind: 'invalid_request' }
|
|
78
118
|
|
|
119
|
+
/** RFC 8628 §3.4: the grant type a device client polls the token endpoint with. */
|
|
120
|
+
export const OAUTH_DEVICE_CODE_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code'
|
|
121
|
+
|
|
79
122
|
export type OAuthTokenRequest =
|
|
80
123
|
| { grant_type: 'authorization_code'; client_id: string; code: string; redirect_uri: string; code_verifier: string; resource: string }
|
|
81
124
|
| { grant_type: 'refresh_token'; client_id: string; refresh_token: string; resource: string }
|
|
125
|
+
| { grant_type: typeof OAUTH_DEVICE_CODE_GRANT_TYPE; client_id: string; device_code: string; resource: string }
|
|
126
|
+
|
|
127
|
+
/** RFC 8628 §3.1, with every resource the grant is asked for (RFC 8707): each token names one of them. */
|
|
128
|
+
export interface OAuthDeviceAuthorizationRequest {
|
|
129
|
+
client_id: string
|
|
130
|
+
scope: string[]
|
|
131
|
+
resources: string[]
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** RFC 8628 §3.2. */
|
|
135
|
+
export interface OAuthDeviceAuthorizationResponse {
|
|
136
|
+
device_code: string
|
|
137
|
+
user_code: string
|
|
138
|
+
verification_uri: string
|
|
139
|
+
verification_uri_complete: string
|
|
140
|
+
expires_in: number
|
|
141
|
+
interval: number
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The lifetimes a person picks between when approving a device: an hour, a day, a week. */
|
|
145
|
+
export const OAUTH_DEVICE_GRANT_LIFETIMES_SECONDS = [3600, 86_400, 604_800] as const
|
|
146
|
+
export type OAuthDeviceGrantLifetime = (typeof OAUTH_DEVICE_GRANT_LIFETIMES_SECONDS)[number]
|
|
147
|
+
|
|
148
|
+
/** How many resources one device grant may name. */
|
|
149
|
+
export const MAX_OAUTH_DEVICE_RESOURCES = 8
|
|
82
150
|
|
|
83
151
|
export interface OAuthTokenResponse {
|
|
84
152
|
access_token: string
|
|
@@ -99,9 +167,10 @@ export interface OAuthAuthorizationServerMetadata {
|
|
|
99
167
|
authorization_endpoint: string
|
|
100
168
|
token_endpoint: string
|
|
101
169
|
revocation_endpoint: string
|
|
170
|
+
device_authorization_endpoint: string
|
|
102
171
|
jwks_uri: string
|
|
103
172
|
response_types_supported: ['code']
|
|
104
|
-
grant_types_supported: ['authorization_code', 'refresh_token']
|
|
173
|
+
grant_types_supported: ['authorization_code', 'refresh_token', typeof OAUTH_DEVICE_CODE_GRANT_TYPE]
|
|
105
174
|
code_challenge_methods_supported: ['S256']
|
|
106
175
|
token_endpoint_auth_methods_supported: ['none', 'private_key_jwt']
|
|
107
176
|
token_endpoint_auth_signing_alg_values_supported: OAuthClientAssertionAlgorithm[]
|
|
@@ -134,11 +203,31 @@ export function parseOAuthManagedClient(value: unknown): OAuthManagedClient | nu
|
|
|
134
203
|
const provider = field(value, 'provider')
|
|
135
204
|
const clientId = field(value, 'clientId')
|
|
136
205
|
const uris = redirectUris(field(value, 'redirectUris'))
|
|
137
|
-
|
|
138
|
-
|
|
206
|
+
const rawIdentity = field(value, 'identity')
|
|
207
|
+
if (!isOAuthClientProvider(provider) || !isCimdClientId(clientId) || uris === null) return null
|
|
208
|
+
if (rawIdentity === undefined) return { provider, clientId, redirectUris: uris }
|
|
209
|
+
const identity = parseOAuthIdentityResource(rawIdentity)
|
|
210
|
+
// Claude and ChatGPT are authoring clients; only an external application signs people in for itself.
|
|
211
|
+
return identity === null || provider !== 'external' ? null : { provider, clientId, redirectUris: uris, identity }
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const IDENTITY_KEYS = ['app', 'resource']
|
|
215
|
+
/** An app id as every fabrika config writes one; no `@`, so never an application environment's id. */
|
|
216
|
+
const IDENTITY_APP = /^[a-z0-9][a-z0-9-]{0,62}$/
|
|
217
|
+
|
|
218
|
+
function parseOAuthIdentityResource(value: unknown): OAuthIdentityResource | null {
|
|
219
|
+
const app = field(value, 'app')
|
|
220
|
+
const resource = field(value, 'resource')
|
|
221
|
+
return hasOnlyKeys(value, IDENTITY_KEYS) && typeof app === 'string' && IDENTITY_APP.test(app) && isOAuthResourceUri(resource)
|
|
222
|
+
? { app, resource }
|
|
139
223
|
: null
|
|
140
224
|
}
|
|
141
225
|
|
|
226
|
+
/** The scopes an identity resource's authorization asks for: none, or exactly `identity`. */
|
|
227
|
+
export function resolveOAuthIdentityScopes(scope: string | undefined): string[] | null {
|
|
228
|
+
return scope === undefined || scope === OAUTH_IDENTITY_SCOPE ? [OAUTH_IDENTITY_SCOPE] : null
|
|
229
|
+
}
|
|
230
|
+
|
|
142
231
|
/** A fetched document cannot enlarge the managed registration's exact redirect allowlist. */
|
|
143
232
|
export function parseOAuthClientMetadata(value: unknown, registration: OAuthManagedClient): OAuthClientMetadataDocument | null {
|
|
144
233
|
const managed = parseOAuthManagedClient(registration)
|
|
@@ -199,28 +288,54 @@ function parseClientGrantTypes(value: unknown): OAuthClientMetadataDocument['gra
|
|
|
199
288
|
return grants.includes('authorization_code') ? grants : null
|
|
200
289
|
}
|
|
201
290
|
|
|
291
|
+
const SCOPE_GRANT_KEYS = ['scope', 'permissions', 'parameter']
|
|
292
|
+
const PERMISSION_KEYS = ['action', 'scope']
|
|
293
|
+
const PERMISSION_SCOPE_KEYS = ['type', 'value']
|
|
294
|
+
|
|
295
|
+
function hasOnlyKeys(value: unknown, allowed: readonly string[]): boolean {
|
|
296
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) && Object.keys(value).every((key) => allowed.includes(key))
|
|
297
|
+
}
|
|
298
|
+
|
|
202
299
|
export function parseOAuthResourceDto(value: unknown): OAuthResourceDto | null {
|
|
203
300
|
const app = field(value, 'app')
|
|
204
301
|
const resource = field(value, 'resource')
|
|
205
302
|
const rawGrants = field(value, 'scopeGrants')
|
|
206
303
|
const rawDefaultScopes = field(value, 'defaultScopes')
|
|
304
|
+
const client = field(value, 'client')
|
|
305
|
+
if (client !== undefined && client !== 'cli') return null
|
|
207
306
|
if (
|
|
208
307
|
!isNonemptyText(app) || app.includes('*') || !isOAuthResourceUri(resource) || !Array.isArray(rawGrants) || rawGrants.length === 0
|
|
209
308
|
|| rawGrants.length > 64
|
|
210
309
|
) return null
|
|
211
|
-
const scopeGrants:
|
|
310
|
+
const scopeGrants: OAuthScopeGrant[] = []
|
|
212
311
|
for (const entry of rawGrants) {
|
|
312
|
+
// An unknown key is refused: a misspelled `parameter` would otherwise leave the scope unnarrowed.
|
|
313
|
+
if (!hasOnlyKeys(entry, SCOPE_GRANT_KEYS)) return null
|
|
213
314
|
const scope = field(entry, 'scope')
|
|
214
315
|
const permissions = parseResourcePermissions(field(entry, 'permissions'))
|
|
215
|
-
|
|
316
|
+
const parameter = field(entry, 'parameter')
|
|
317
|
+
if (
|
|
318
|
+
typeof scope !== 'string' || parseOAuthScopes([scope]) === null || scope.includes(SCOPE_PARAMETER_SEPARATOR) || permissions === null
|
|
319
|
+
|| scopeGrants.some((grant) => grant.scope === scope)
|
|
320
|
+
) {
|
|
216
321
|
return null
|
|
217
322
|
}
|
|
218
|
-
|
|
323
|
+
if (parameter === undefined) {
|
|
324
|
+
scopeGrants.push({ scope, permissions })
|
|
325
|
+
continue
|
|
326
|
+
}
|
|
327
|
+
// A parameter narrows scope-less permissions only; a permission that names its own scope would ignore it.
|
|
328
|
+
if (!isScopeParameterText(parameter) || permissions.some((permission) => permission.scope != null)) return null
|
|
329
|
+
scopeGrants.push({ scope, permissions, parameter })
|
|
219
330
|
}
|
|
220
|
-
const dto: OAuthResourceDto = { app, resource, scopeGrants }
|
|
331
|
+
const dto: OAuthResourceDto = { app, resource, scopeGrants, ...(client === undefined ? {} : { client }) }
|
|
221
332
|
if (rawDefaultScopes !== undefined) {
|
|
222
333
|
const defaultScopes = parseOAuthScopes(rawDefaultScopes)
|
|
223
|
-
if (
|
|
334
|
+
if (
|
|
335
|
+
defaultScopes === null || !defaultScopes.every((scope) => scopeGrants.some((grant) => grant.scope === scope && grant.parameter === undefined))
|
|
336
|
+
) {
|
|
337
|
+
return null
|
|
338
|
+
}
|
|
224
339
|
dto.defaultScopes = defaultScopes
|
|
225
340
|
}
|
|
226
341
|
return dto
|
|
@@ -232,23 +347,47 @@ function parseResourcePermissions(value: unknown): KeyGrant[] | null {
|
|
|
232
347
|
for (const entry of value) {
|
|
233
348
|
const action = field(entry, 'action')
|
|
234
349
|
const rawScope = field(entry, 'scope')
|
|
235
|
-
if (!isNonemptyText(action)) return null
|
|
350
|
+
if (!hasOnlyKeys(entry, PERMISSION_KEYS) || !isNonemptyText(action)) return null
|
|
236
351
|
if (rawScope === undefined) permissions.push({ action })
|
|
237
352
|
else if (rawScope === null) permissions.push({ action, scope: null })
|
|
238
353
|
else {
|
|
239
354
|
const type = field(rawScope, 'type')
|
|
240
355
|
const scopeValue = field(rawScope, 'value')
|
|
241
|
-
if (!isNonemptyText(type) || !isNonemptyText(scopeValue)) return null
|
|
356
|
+
if (!hasOnlyKeys(rawScope, PERMISSION_SCOPE_KEYS) || !isNonemptyText(type) || !isNonemptyText(scopeValue)) return null
|
|
242
357
|
permissions.push({ action, scope: { type, value: scopeValue } })
|
|
243
358
|
}
|
|
244
359
|
}
|
|
245
360
|
return permissions
|
|
246
361
|
}
|
|
247
362
|
|
|
363
|
+
/** Separates a parameterized scope from its value: `deploy:alpha`. */
|
|
364
|
+
export const SCOPE_PARAMETER_SEPARATOR = ':'
|
|
365
|
+
|
|
366
|
+
/** A scope dimension type, or a value of one, as a scope token may carry it. */
|
|
367
|
+
function isScopeParameterText(value: unknown): value is string {
|
|
368
|
+
return typeof value === 'string' && value.length <= 100 && /^[A-Za-z0-9][A-Za-z0-9._@-]*$/.test(value)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* The resource's grant a requested scope token selects and the narrowed permissions it holds, or null when the token
|
|
373
|
+
* selects none: an unknown scope, a parameterized scope without its value, a value on a scope that takes none.
|
|
374
|
+
*/
|
|
375
|
+
export function resolveOAuthScopeToken(token: string, resource: OAuthResourceDto): KeyGrant[] | null {
|
|
376
|
+
const separator = token.indexOf(SCOPE_PARAMETER_SEPARATOR)
|
|
377
|
+
const name = separator === -1 ? token : token.slice(0, separator)
|
|
378
|
+
const grant = resource.scopeGrants.find((entry) => entry.scope === name)
|
|
379
|
+
if (grant === undefined) return null
|
|
380
|
+
const type = grant.parameter
|
|
381
|
+
if (type === undefined) return separator === -1 ? grant.permissions : null
|
|
382
|
+
const value = separator === -1 ? null : token.slice(separator + 1)
|
|
383
|
+
if (value === null || !isScopeParameterText(value)) return null
|
|
384
|
+
return grant.permissions.map((permission) => ({ action: permission.action, scope: { type, value } }))
|
|
385
|
+
}
|
|
386
|
+
|
|
248
387
|
export function resolveOAuthAuthorizationScopes(scope: string | undefined, resource: OAuthResourceDto): string[] | null {
|
|
249
388
|
const scopes = scope === undefined ? resource.defaultScopes : parseOAuthScopes(scope.split(' '))
|
|
250
389
|
if (!scopes || scopes.length === 0) return null
|
|
251
|
-
return scopes.every((entry) => resource
|
|
390
|
+
return scopes.every((entry) => resolveOAuthScopeToken(entry, resource) !== null)
|
|
252
391
|
? [...scopes]
|
|
253
392
|
: null
|
|
254
393
|
}
|
|
@@ -291,7 +430,7 @@ export function parseOAuthTokenRequest(value: unknown): OAuthTokenRequest | null
|
|
|
291
430
|
const clientId = field(value, 'client_id')
|
|
292
431
|
const resource = field(value, 'resource')
|
|
293
432
|
const grantType = field(value, 'grant_type')
|
|
294
|
-
if (!
|
|
433
|
+
if (!isOAuthClientId(clientId) || !isOAuthResourceUri(resource)) return null
|
|
295
434
|
if (grantType === 'authorization_code') {
|
|
296
435
|
const code = field(value, 'code')
|
|
297
436
|
const redirectUri = field(value, 'redirect_uri')
|
|
@@ -305,9 +444,38 @@ export function parseOAuthTokenRequest(value: unknown): OAuthTokenRequest | null
|
|
|
305
444
|
const refreshToken = field(value, 'refresh_token')
|
|
306
445
|
return isNonemptyText(refreshToken) ? { grant_type: grantType, client_id: clientId, refresh_token: refreshToken, resource } : null
|
|
307
446
|
}
|
|
447
|
+
if (grantType === OAUTH_DEVICE_CODE_GRANT_TYPE) {
|
|
448
|
+
const deviceCode = field(value, 'device_code')
|
|
449
|
+
return isNonemptyText(deviceCode) && deviceCode.length <= 256
|
|
450
|
+
? { grant_type: grantType, client_id: clientId, device_code: deviceCode, resource }
|
|
451
|
+
: null
|
|
452
|
+
}
|
|
308
453
|
return null
|
|
309
454
|
}
|
|
310
455
|
|
|
456
|
+
/**
|
|
457
|
+
* A device authorization request: its client, at least one scope and one to `MAX_OAUTH_DEVICE_RESOURCES` distinct
|
|
458
|
+
* resources. `resource` is the one parameter that may repeat (RFC 8707 §2), so the caller collects it into a list.
|
|
459
|
+
*/
|
|
460
|
+
export function parseOAuthDeviceAuthorizationRequest(
|
|
461
|
+
value: { fields: Record<string, string>; resources: string[] },
|
|
462
|
+
): OAuthDeviceAuthorizationRequest | null {
|
|
463
|
+
const clientId = field(value.fields, 'client_id')
|
|
464
|
+
const scope = field(value.fields, 'scope')
|
|
465
|
+
if (!isOAuthClientId(clientId) || typeof scope !== 'string') return null
|
|
466
|
+
const scopes = parseOAuthScopes(scope.split(' '))
|
|
467
|
+
const resources = value.resources
|
|
468
|
+
if (
|
|
469
|
+
scopes === null || scopes.length === 0 || resources.length === 0 || resources.length > MAX_OAUTH_DEVICE_RESOURCES
|
|
470
|
+
|| !resources.every(isOAuthResourceUri) || new Set(resources).size !== resources.length
|
|
471
|
+
) return null
|
|
472
|
+
return { client_id: clientId, scope: scopes, resources: [...resources] }
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
export function isOAuthDeviceGrantLifetime(value: number): value is OAuthDeviceGrantLifetime {
|
|
476
|
+
return OAUTH_DEVICE_GRANT_LIFETIMES_SECONDS.some((lifetime) => lifetime === value)
|
|
477
|
+
}
|
|
478
|
+
|
|
311
479
|
const MAX_CLIENT_ASSERTION_LENGTH = 16_384
|
|
312
480
|
|
|
313
481
|
/**
|
|
@@ -327,7 +495,7 @@ export function parseOAuthRevocationRequest(value: unknown): OAuthRevocationRequ
|
|
|
327
495
|
const token = field(value, 'token')
|
|
328
496
|
const hint = field(value, 'token_type_hint')
|
|
329
497
|
if (
|
|
330
|
-
!
|
|
498
|
+
!isOAuthClientId(clientId) || typeof token !== 'string' || token.length === 0 || token.length > 32768
|
|
331
499
|
|| (hint !== undefined && (typeof hint !== 'string' || hint.length > 200))
|
|
332
500
|
) return null
|
|
333
501
|
const request: OAuthRevocationRequest = { client_id: clientId, token }
|
package/types/src/oauth.d.ts
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import type { KeyGrant } from '@fabrika/auth-core';
|
|
2
|
+
/**
|
|
3
|
+
* The kinds of managed client an installation registers. A label only: every kind gets the same authorization code
|
|
4
|
+
* flow with S256 PKCE, the same RFC 8707 resources and the same scope ceilings (ADR-0049).
|
|
5
|
+
*/
|
|
6
|
+
export declare const OAUTH_CLIENT_PROVIDERS: readonly ["claude", "chatgpt", "external"];
|
|
7
|
+
export type OAuthClientProvider = (typeof OAUTH_CLIENT_PROVIDERS)[number];
|
|
8
|
+
/**
|
|
9
|
+
* The one resource an identity-only client is served: its own, outside the installation. A token for it carries the
|
|
10
|
+
* person's identity and no permission, under `app`, an id no installation service or application holds.
|
|
11
|
+
*/
|
|
12
|
+
export interface OAuthIdentityResource {
|
|
13
|
+
app: string;
|
|
14
|
+
resource: string;
|
|
15
|
+
}
|
|
16
|
+
/** The one scope of an identity resource: it names the person and grants nothing. */
|
|
17
|
+
export declare const OAUTH_IDENTITY_SCOPE = "identity";
|
|
2
18
|
/** Managed CIMD registrations only; public dynamic registration is not part of this contract. */
|
|
3
19
|
export interface OAuthManagedClient {
|
|
4
|
-
provider:
|
|
20
|
+
provider: OAuthClientProvider;
|
|
5
21
|
clientId: string;
|
|
6
22
|
redirectUris: string[];
|
|
23
|
+
/** Present only on an `external` client: it is served this resource alone, never a registered one. */
|
|
24
|
+
identity?: OAuthIdentityResource;
|
|
7
25
|
}
|
|
8
26
|
/** RFC 7523 §2.2: the `client_assertion_type` of a JWT client assertion (`private_key_jwt`). */
|
|
9
27
|
export declare const OAUTH_CLIENT_ASSERTION_TYPE = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer";
|
|
@@ -33,16 +51,27 @@ export type OAuthClientCredential = {
|
|
|
33
51
|
method: 'private_key_jwt';
|
|
34
52
|
assertion: string;
|
|
35
53
|
};
|
|
54
|
+
export interface OAuthScopeGrant {
|
|
55
|
+
scope: string;
|
|
56
|
+
permissions: KeyGrant[];
|
|
57
|
+
/**
|
|
58
|
+
* A scope dimension type. Such a scope is requested only as `<scope>:<value>`, and each of its scope-less permissions
|
|
59
|
+
* is narrowed to `{ type: parameter, value }`: `deploy:alpha` with parameter `app` holds its actions at app `alpha` only.
|
|
60
|
+
*/
|
|
61
|
+
parameter?: string;
|
|
62
|
+
}
|
|
36
63
|
export interface OAuthResourceDto {
|
|
37
64
|
app: string;
|
|
38
65
|
resource: string;
|
|
39
66
|
/** IAM resolves these app-owned grants; OAuth scope text alone never authorizes an action. */
|
|
40
|
-
scopeGrants:
|
|
41
|
-
scope: string;
|
|
42
|
-
permissions: KeyGrant[];
|
|
43
|
-
}[];
|
|
67
|
+
scopeGrants: OAuthScopeGrant[];
|
|
44
68
|
/** Used only when scope is omitted. No configured default means invalid_scope, never all scopes. */
|
|
45
69
|
defaultScopes?: string[];
|
|
70
|
+
/**
|
|
71
|
+
* `cli`: served only to the installation's command-line client, through the device grant. Absent: served only to the
|
|
72
|
+
* operator's managed clients, through an authorization code. A token for one never comes from the other's flow.
|
|
73
|
+
*/
|
|
74
|
+
client?: 'cli';
|
|
46
75
|
}
|
|
47
76
|
/** No access token, refresh token, hash, or authorization code appears in a grant DTO. */
|
|
48
77
|
export interface OAuthGrantDto {
|
|
@@ -77,6 +106,8 @@ export type OAuthAuthorizationParseResult = {
|
|
|
77
106
|
} | {
|
|
78
107
|
kind: 'invalid_request';
|
|
79
108
|
};
|
|
109
|
+
/** RFC 8628 §3.4: the grant type a device client polls the token endpoint with. */
|
|
110
|
+
export declare const OAUTH_DEVICE_CODE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:device_code";
|
|
80
111
|
export type OAuthTokenRequest = {
|
|
81
112
|
grant_type: 'authorization_code';
|
|
82
113
|
client_id: string;
|
|
@@ -89,7 +120,32 @@ export type OAuthTokenRequest = {
|
|
|
89
120
|
client_id: string;
|
|
90
121
|
refresh_token: string;
|
|
91
122
|
resource: string;
|
|
123
|
+
} | {
|
|
124
|
+
grant_type: typeof OAUTH_DEVICE_CODE_GRANT_TYPE;
|
|
125
|
+
client_id: string;
|
|
126
|
+
device_code: string;
|
|
127
|
+
resource: string;
|
|
92
128
|
};
|
|
129
|
+
/** RFC 8628 §3.1, with every resource the grant is asked for (RFC 8707): each token names one of them. */
|
|
130
|
+
export interface OAuthDeviceAuthorizationRequest {
|
|
131
|
+
client_id: string;
|
|
132
|
+
scope: string[];
|
|
133
|
+
resources: string[];
|
|
134
|
+
}
|
|
135
|
+
/** RFC 8628 §3.2. */
|
|
136
|
+
export interface OAuthDeviceAuthorizationResponse {
|
|
137
|
+
device_code: string;
|
|
138
|
+
user_code: string;
|
|
139
|
+
verification_uri: string;
|
|
140
|
+
verification_uri_complete: string;
|
|
141
|
+
expires_in: number;
|
|
142
|
+
interval: number;
|
|
143
|
+
}
|
|
144
|
+
/** The lifetimes a person picks between when approving a device: an hour, a day, a week. */
|
|
145
|
+
export declare const OAUTH_DEVICE_GRANT_LIFETIMES_SECONDS: readonly [3600, 86400, 604800];
|
|
146
|
+
export type OAuthDeviceGrantLifetime = (typeof OAUTH_DEVICE_GRANT_LIFETIMES_SECONDS)[number];
|
|
147
|
+
/** How many resources one device grant may name. */
|
|
148
|
+
export declare const MAX_OAUTH_DEVICE_RESOURCES = 8;
|
|
93
149
|
export interface OAuthTokenResponse {
|
|
94
150
|
access_token: string;
|
|
95
151
|
token_type: 'Bearer';
|
|
@@ -107,9 +163,10 @@ export interface OAuthAuthorizationServerMetadata {
|
|
|
107
163
|
authorization_endpoint: string;
|
|
108
164
|
token_endpoint: string;
|
|
109
165
|
revocation_endpoint: string;
|
|
166
|
+
device_authorization_endpoint: string;
|
|
110
167
|
jwks_uri: string;
|
|
111
168
|
response_types_supported: ['code'];
|
|
112
|
-
grant_types_supported: ['authorization_code', 'refresh_token'];
|
|
169
|
+
grant_types_supported: ['authorization_code', 'refresh_token', typeof OAUTH_DEVICE_CODE_GRANT_TYPE];
|
|
113
170
|
code_challenge_methods_supported: ['S256'];
|
|
114
171
|
token_endpoint_auth_methods_supported: ['none', 'private_key_jwt'];
|
|
115
172
|
token_endpoint_auth_signing_alg_values_supported: OAuthClientAssertionAlgorithm[];
|
|
@@ -120,13 +177,31 @@ export interface OAuthAuthorizationServerMetadata {
|
|
|
120
177
|
client_id_metadata_document_supported: true;
|
|
121
178
|
}
|
|
122
179
|
export declare function parseOAuthManagedClient(value: unknown): OAuthManagedClient | null;
|
|
180
|
+
/** The scopes an identity resource's authorization asks for: none, or exactly `identity`. */
|
|
181
|
+
export declare function resolveOAuthIdentityScopes(scope: string | undefined): string[] | null;
|
|
123
182
|
/** A fetched document cannot enlarge the managed registration's exact redirect allowlist. */
|
|
124
183
|
export declare function parseOAuthClientMetadata(value: unknown, registration: OAuthManagedClient): OAuthClientMetadataDocument | null;
|
|
125
184
|
export declare function parseOAuthResourceDto(value: unknown): OAuthResourceDto | null;
|
|
185
|
+
/** Separates a parameterized scope from its value: `deploy:alpha`. */
|
|
186
|
+
export declare const SCOPE_PARAMETER_SEPARATOR = ":";
|
|
187
|
+
/**
|
|
188
|
+
* The resource's grant a requested scope token selects and the narrowed permissions it holds, or null when the token
|
|
189
|
+
* selects none: an unknown scope, a parameterized scope without its value, a value on a scope that takes none.
|
|
190
|
+
*/
|
|
191
|
+
export declare function resolveOAuthScopeToken(token: string, resource: OAuthResourceDto): KeyGrant[] | null;
|
|
126
192
|
export declare function resolveOAuthAuthorizationScopes(scope: string | undefined, resource: OAuthResourceDto): string[] | null;
|
|
127
193
|
export declare function parseOAuthAuthorizationAttempt(value: unknown): OAuthAuthorizationParseResult;
|
|
128
194
|
export declare function parseOAuthAuthorizationRequest(value: unknown): OAuthAuthorizationRequest | null;
|
|
129
195
|
export declare function parseOAuthTokenRequest(value: unknown): OAuthTokenRequest | null;
|
|
196
|
+
/**
|
|
197
|
+
* A device authorization request: its client, at least one scope and one to `MAX_OAUTH_DEVICE_RESOURCES` distinct
|
|
198
|
+
* resources. `resource` is the one parameter that may repeat (RFC 8707 §2), so the caller collects it into a list.
|
|
199
|
+
*/
|
|
200
|
+
export declare function parseOAuthDeviceAuthorizationRequest(value: {
|
|
201
|
+
fields: Record<string, string>;
|
|
202
|
+
resources: string[];
|
|
203
|
+
}): OAuthDeviceAuthorizationRequest | null;
|
|
204
|
+
export declare function isOAuthDeviceGrantLifetime(value: number): value is OAuthDeviceGrantLifetime;
|
|
130
205
|
/**
|
|
131
206
|
* The client credential of a token or revocation request (RFC 7521 §4.2). Neither assertion field is no
|
|
132
207
|
* credential; a half-present pair, another assertion type or an oversized assertion is null.
|