@fabrika/iam-contract 0.9.0-dev.32 → 0.9.0-dev.34
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/index.ts +35 -2
- package/src/oauth.ts +149 -15
- package/src/password-policy.ts +1 -1
- package/types/src/index.d.ts +31 -2
- package/types/src/oauth.d.ts +67 -6
- package/types/src/password-policy.d.ts +1 -1
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.34",
|
|
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.34",
|
|
31
|
+
"@fabrika/auth-core": "0.9.0-dev.34",
|
|
32
|
+
"@fabrika/proxy-core": "0.9.0-dev.34"
|
|
31
33
|
}
|
|
32
34
|
}
|
package/src/index.ts
CHANGED
|
@@ -271,7 +271,7 @@ export interface ApiKeyDto {
|
|
|
271
271
|
|
|
272
272
|
/**
|
|
273
273
|
* One API key with its live credential, as `apiKeys.get` and each row of `apiKeys.list` answer it. Its permissions are
|
|
274
|
-
* its grants, edited with `grants.create` and `grants.delete` by an installation administrator and read again at every
|
|
274
|
+
* its grants, edited with `grants.create`, `grants.update` and `grants.delete` by an installation administrator and read again at every
|
|
275
275
|
* token exchange. An IAM older than the list's credential answers list rows without `credential`.
|
|
276
276
|
*/
|
|
277
277
|
export interface ApiKeyDetail extends ApiKeyDto {
|
|
@@ -279,6 +279,17 @@ export interface ApiKeyDetail extends ApiKeyDto {
|
|
|
279
279
|
credential: ApiKeyCredentialDto | null
|
|
280
280
|
}
|
|
281
281
|
|
|
282
|
+
/** A grant of `apiKeys.get`'s key, naming who granted it. */
|
|
283
|
+
export interface ApiKeyGrantDto extends GrantDto {
|
|
284
|
+
/** The granting principal's label, or its id when IAM no longer knows it; null when nobody granted it. */
|
|
285
|
+
grantedByLabel: string | null
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** `apiKeys.get`: one key whose grants name who granted them. */
|
|
289
|
+
export interface GetApiKeyResponse extends ApiKeyDetail {
|
|
290
|
+
grants: ApiKeyGrantDto[]
|
|
291
|
+
}
|
|
292
|
+
|
|
282
293
|
export interface ApiKeyCredentialDto {
|
|
283
294
|
/** The app the key mints tokens for; null = every app. A grant at another app never reaches the key's token. */
|
|
284
295
|
app: string | null
|
|
@@ -767,7 +778,7 @@ export interface IamAdminRpcContract {
|
|
|
767
778
|
}
|
|
768
779
|
apiKeys: {
|
|
769
780
|
list: RpcProcedure<ListApiKeysInput, CursorList<ApiKeyDetail>>
|
|
770
|
-
get: RpcProcedure<ApiKeyIdInput,
|
|
781
|
+
get: RpcProcedure<ApiKeyIdInput, GetApiKeyResponse>
|
|
771
782
|
provision: RpcProcedure<ProvisionApiKeyRequest, ProvisionApiKeyResponse>
|
|
772
783
|
rotate: RpcProcedure<RotateApiKeyInput, RotateApiKeyResponse>
|
|
773
784
|
revoke: RpcProcedure<ApiKeyIdInput, OkResponse>
|
|
@@ -826,6 +837,28 @@ export interface IamAdminRpcContract {
|
|
|
826
837
|
get: RpcProcedure<void, PasswordPolicyDto>
|
|
827
838
|
set: RpcProcedure<SetPasswordPolicyRequest, PasswordPolicyDto>
|
|
828
839
|
}
|
|
840
|
+
/**
|
|
841
|
+
* A person's second factor (TOTP), an installation administrator's (`iam.admin`) to read and to reset. The person
|
|
842
|
+
* enrols at IAM's `SECOND_FACTOR_ENROL_PATH`, never here. A reset removes the authenticator and its recovery codes
|
|
843
|
+
* and writes `iam.second_factor.reset`; the next sign-in asks for no code.
|
|
844
|
+
*/
|
|
845
|
+
secondFactor: {
|
|
846
|
+
get: RpcProcedure<PrincipalIdInput, SecondFactorDto>
|
|
847
|
+
reset: RpcProcedure<PrincipalIdInput, SecondFactorDto>
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
/** Where a signed-in administrator enrols an authenticator app, on IAM's own origin. */
|
|
852
|
+
export const SECOND_FACTOR_ENROL_PATH = '/auth/second-factor/enrol'
|
|
853
|
+
|
|
854
|
+
/** A person's second factor as an installation administrator sees it. */
|
|
855
|
+
export interface SecondFactorDto {
|
|
856
|
+
/** When the person confirmed the authenticator, in epoch seconds; null when none is enrolled. */
|
|
857
|
+
enrolledAt: number | null
|
|
858
|
+
/** Recovery codes not used yet; 0 without an authenticator. */
|
|
859
|
+
recoveryCodesLeft: number
|
|
860
|
+
/** Whether this installation can enrol an authenticator at all: it holds the key that seals the secrets. */
|
|
861
|
+
available: boolean
|
|
829
862
|
}
|
|
830
863
|
|
|
831
864
|
export interface UserConnectionDto {
|
package/src/oauth.ts
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
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
|
+
|
|
4
15
|
/** Managed CIMD registrations only; public dynamic registration is not part of this contract. */
|
|
5
16
|
export interface OAuthManagedClient {
|
|
6
|
-
provider:
|
|
17
|
+
provider: OAuthClientProvider
|
|
7
18
|
clientId: string
|
|
8
19
|
redirectUris: string[]
|
|
9
20
|
}
|
|
@@ -36,13 +47,28 @@ export type OAuthClientMetadataDocument =
|
|
|
36
47
|
/** How a token or revocation request authenticates its client: no credential, or a JWT assertion. */
|
|
37
48
|
export type OAuthClientCredential = { method: 'none' } | { method: 'private_key_jwt'; assertion: string }
|
|
38
49
|
|
|
50
|
+
export interface OAuthScopeGrant {
|
|
51
|
+
scope: string
|
|
52
|
+
permissions: KeyGrant[]
|
|
53
|
+
/**
|
|
54
|
+
* A scope dimension type. Such a scope is requested only as `<scope>:<value>`, and each of its scope-less permissions
|
|
55
|
+
* is narrowed to `{ type: parameter, value }`: `deploy:alpha` with parameter `app` holds its actions at app `alpha` only.
|
|
56
|
+
*/
|
|
57
|
+
parameter?: string
|
|
58
|
+
}
|
|
59
|
+
|
|
39
60
|
export interface OAuthResourceDto {
|
|
40
61
|
app: string
|
|
41
62
|
resource: string
|
|
42
63
|
/** IAM resolves these app-owned grants; OAuth scope text alone never authorizes an action. */
|
|
43
|
-
scopeGrants:
|
|
64
|
+
scopeGrants: OAuthScopeGrant[]
|
|
44
65
|
/** Used only when scope is omitted. No configured default means invalid_scope, never all scopes. */
|
|
45
66
|
defaultScopes?: string[]
|
|
67
|
+
/**
|
|
68
|
+
* `cli`: served only to the installation's command-line client, through the device grant. Absent: served only to the
|
|
69
|
+
* operator's managed clients, through an authorization code. A token for one never comes from the other's flow.
|
|
70
|
+
*/
|
|
71
|
+
client?: 'cli'
|
|
46
72
|
}
|
|
47
73
|
|
|
48
74
|
/** No access token, refresh token, hash, or authorization code appears in a grant DTO. */
|
|
@@ -76,9 +102,37 @@ export type OAuthAuthorizationParseResult =
|
|
|
76
102
|
| { kind: 'invalid_scope'; coordinates: OAuthAuthorizationCoordinates; rawScope: string }
|
|
77
103
|
| { kind: 'invalid_request' }
|
|
78
104
|
|
|
105
|
+
/** RFC 8628 §3.4: the grant type a device client polls the token endpoint with. */
|
|
106
|
+
export const OAUTH_DEVICE_CODE_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code'
|
|
107
|
+
|
|
79
108
|
export type OAuthTokenRequest =
|
|
80
109
|
| { grant_type: 'authorization_code'; client_id: string; code: string; redirect_uri: string; code_verifier: string; resource: string }
|
|
81
110
|
| { grant_type: 'refresh_token'; client_id: string; refresh_token: string; resource: string }
|
|
111
|
+
| { grant_type: typeof OAUTH_DEVICE_CODE_GRANT_TYPE; client_id: string; device_code: string; resource: string }
|
|
112
|
+
|
|
113
|
+
/** RFC 8628 §3.1, with every resource the grant is asked for (RFC 8707): each token names one of them. */
|
|
114
|
+
export interface OAuthDeviceAuthorizationRequest {
|
|
115
|
+
client_id: string
|
|
116
|
+
scope: string[]
|
|
117
|
+
resources: string[]
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** RFC 8628 §3.2. */
|
|
121
|
+
export interface OAuthDeviceAuthorizationResponse {
|
|
122
|
+
device_code: string
|
|
123
|
+
user_code: string
|
|
124
|
+
verification_uri: string
|
|
125
|
+
verification_uri_complete: string
|
|
126
|
+
expires_in: number
|
|
127
|
+
interval: number
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The lifetimes a person picks between when approving a device: an hour, a day, a week. */
|
|
131
|
+
export const OAUTH_DEVICE_GRANT_LIFETIMES_SECONDS = [3600, 86_400, 604_800] as const
|
|
132
|
+
export type OAuthDeviceGrantLifetime = (typeof OAUTH_DEVICE_GRANT_LIFETIMES_SECONDS)[number]
|
|
133
|
+
|
|
134
|
+
/** How many resources one device grant may name. */
|
|
135
|
+
export const MAX_OAUTH_DEVICE_RESOURCES = 8
|
|
82
136
|
|
|
83
137
|
export interface OAuthTokenResponse {
|
|
84
138
|
access_token: string
|
|
@@ -99,9 +153,10 @@ export interface OAuthAuthorizationServerMetadata {
|
|
|
99
153
|
authorization_endpoint: string
|
|
100
154
|
token_endpoint: string
|
|
101
155
|
revocation_endpoint: string
|
|
156
|
+
device_authorization_endpoint: string
|
|
102
157
|
jwks_uri: string
|
|
103
158
|
response_types_supported: ['code']
|
|
104
|
-
grant_types_supported: ['authorization_code', 'refresh_token']
|
|
159
|
+
grant_types_supported: ['authorization_code', 'refresh_token', typeof OAUTH_DEVICE_CODE_GRANT_TYPE]
|
|
105
160
|
code_challenge_methods_supported: ['S256']
|
|
106
161
|
token_endpoint_auth_methods_supported: ['none', 'private_key_jwt']
|
|
107
162
|
token_endpoint_auth_signing_alg_values_supported: OAuthClientAssertionAlgorithm[]
|
|
@@ -134,7 +189,7 @@ export function parseOAuthManagedClient(value: unknown): OAuthManagedClient | nu
|
|
|
134
189
|
const provider = field(value, 'provider')
|
|
135
190
|
const clientId = field(value, 'clientId')
|
|
136
191
|
const uris = redirectUris(field(value, 'redirectUris'))
|
|
137
|
-
return (provider
|
|
192
|
+
return isOAuthClientProvider(provider) && isCimdClientId(clientId) && uris !== null
|
|
138
193
|
? { provider, clientId, redirectUris: uris }
|
|
139
194
|
: null
|
|
140
195
|
}
|
|
@@ -199,28 +254,54 @@ function parseClientGrantTypes(value: unknown): OAuthClientMetadataDocument['gra
|
|
|
199
254
|
return grants.includes('authorization_code') ? grants : null
|
|
200
255
|
}
|
|
201
256
|
|
|
257
|
+
const SCOPE_GRANT_KEYS = ['scope', 'permissions', 'parameter']
|
|
258
|
+
const PERMISSION_KEYS = ['action', 'scope']
|
|
259
|
+
const PERMISSION_SCOPE_KEYS = ['type', 'value']
|
|
260
|
+
|
|
261
|
+
function hasOnlyKeys(value: unknown, allowed: readonly string[]): boolean {
|
|
262
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) && Object.keys(value).every((key) => allowed.includes(key))
|
|
263
|
+
}
|
|
264
|
+
|
|
202
265
|
export function parseOAuthResourceDto(value: unknown): OAuthResourceDto | null {
|
|
203
266
|
const app = field(value, 'app')
|
|
204
267
|
const resource = field(value, 'resource')
|
|
205
268
|
const rawGrants = field(value, 'scopeGrants')
|
|
206
269
|
const rawDefaultScopes = field(value, 'defaultScopes')
|
|
270
|
+
const client = field(value, 'client')
|
|
271
|
+
if (client !== undefined && client !== 'cli') return null
|
|
207
272
|
if (
|
|
208
273
|
!isNonemptyText(app) || app.includes('*') || !isOAuthResourceUri(resource) || !Array.isArray(rawGrants) || rawGrants.length === 0
|
|
209
274
|
|| rawGrants.length > 64
|
|
210
275
|
) return null
|
|
211
|
-
const scopeGrants:
|
|
276
|
+
const scopeGrants: OAuthScopeGrant[] = []
|
|
212
277
|
for (const entry of rawGrants) {
|
|
278
|
+
// An unknown key is refused: a misspelled `parameter` would otherwise leave the scope unnarrowed.
|
|
279
|
+
if (!hasOnlyKeys(entry, SCOPE_GRANT_KEYS)) return null
|
|
213
280
|
const scope = field(entry, 'scope')
|
|
214
281
|
const permissions = parseResourcePermissions(field(entry, 'permissions'))
|
|
215
|
-
|
|
282
|
+
const parameter = field(entry, 'parameter')
|
|
283
|
+
if (
|
|
284
|
+
typeof scope !== 'string' || parseOAuthScopes([scope]) === null || scope.includes(SCOPE_PARAMETER_SEPARATOR) || permissions === null
|
|
285
|
+
|| scopeGrants.some((grant) => grant.scope === scope)
|
|
286
|
+
) {
|
|
216
287
|
return null
|
|
217
288
|
}
|
|
218
|
-
|
|
289
|
+
if (parameter === undefined) {
|
|
290
|
+
scopeGrants.push({ scope, permissions })
|
|
291
|
+
continue
|
|
292
|
+
}
|
|
293
|
+
// A parameter narrows scope-less permissions only; a permission that names its own scope would ignore it.
|
|
294
|
+
if (!isScopeParameterText(parameter) || permissions.some((permission) => permission.scope != null)) return null
|
|
295
|
+
scopeGrants.push({ scope, permissions, parameter })
|
|
219
296
|
}
|
|
220
|
-
const dto: OAuthResourceDto = { app, resource, scopeGrants }
|
|
297
|
+
const dto: OAuthResourceDto = { app, resource, scopeGrants, ...(client === undefined ? {} : { client }) }
|
|
221
298
|
if (rawDefaultScopes !== undefined) {
|
|
222
299
|
const defaultScopes = parseOAuthScopes(rawDefaultScopes)
|
|
223
|
-
if (
|
|
300
|
+
if (
|
|
301
|
+
defaultScopes === null || !defaultScopes.every((scope) => scopeGrants.some((grant) => grant.scope === scope && grant.parameter === undefined))
|
|
302
|
+
) {
|
|
303
|
+
return null
|
|
304
|
+
}
|
|
224
305
|
dto.defaultScopes = defaultScopes
|
|
225
306
|
}
|
|
226
307
|
return dto
|
|
@@ -232,23 +313,47 @@ function parseResourcePermissions(value: unknown): KeyGrant[] | null {
|
|
|
232
313
|
for (const entry of value) {
|
|
233
314
|
const action = field(entry, 'action')
|
|
234
315
|
const rawScope = field(entry, 'scope')
|
|
235
|
-
if (!isNonemptyText(action)) return null
|
|
316
|
+
if (!hasOnlyKeys(entry, PERMISSION_KEYS) || !isNonemptyText(action)) return null
|
|
236
317
|
if (rawScope === undefined) permissions.push({ action })
|
|
237
318
|
else if (rawScope === null) permissions.push({ action, scope: null })
|
|
238
319
|
else {
|
|
239
320
|
const type = field(rawScope, 'type')
|
|
240
321
|
const scopeValue = field(rawScope, 'value')
|
|
241
|
-
if (!isNonemptyText(type) || !isNonemptyText(scopeValue)) return null
|
|
322
|
+
if (!hasOnlyKeys(rawScope, PERMISSION_SCOPE_KEYS) || !isNonemptyText(type) || !isNonemptyText(scopeValue)) return null
|
|
242
323
|
permissions.push({ action, scope: { type, value: scopeValue } })
|
|
243
324
|
}
|
|
244
325
|
}
|
|
245
326
|
return permissions
|
|
246
327
|
}
|
|
247
328
|
|
|
329
|
+
/** Separates a parameterized scope from its value: `deploy:alpha`. */
|
|
330
|
+
export const SCOPE_PARAMETER_SEPARATOR = ':'
|
|
331
|
+
|
|
332
|
+
/** A scope dimension type, or a value of one, as a scope token may carry it. */
|
|
333
|
+
function isScopeParameterText(value: unknown): value is string {
|
|
334
|
+
return typeof value === 'string' && value.length <= 100 && /^[A-Za-z0-9][A-Za-z0-9._@-]*$/.test(value)
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The resource's grant a requested scope token selects and the narrowed permissions it holds, or null when the token
|
|
339
|
+
* selects none: an unknown scope, a parameterized scope without its value, a value on a scope that takes none.
|
|
340
|
+
*/
|
|
341
|
+
export function resolveOAuthScopeToken(token: string, resource: OAuthResourceDto): KeyGrant[] | null {
|
|
342
|
+
const separator = token.indexOf(SCOPE_PARAMETER_SEPARATOR)
|
|
343
|
+
const name = separator === -1 ? token : token.slice(0, separator)
|
|
344
|
+
const grant = resource.scopeGrants.find((entry) => entry.scope === name)
|
|
345
|
+
if (grant === undefined) return null
|
|
346
|
+
const type = grant.parameter
|
|
347
|
+
if (type === undefined) return separator === -1 ? grant.permissions : null
|
|
348
|
+
const value = separator === -1 ? null : token.slice(separator + 1)
|
|
349
|
+
if (value === null || !isScopeParameterText(value)) return null
|
|
350
|
+
return grant.permissions.map((permission) => ({ action: permission.action, scope: { type, value } }))
|
|
351
|
+
}
|
|
352
|
+
|
|
248
353
|
export function resolveOAuthAuthorizationScopes(scope: string | undefined, resource: OAuthResourceDto): string[] | null {
|
|
249
354
|
const scopes = scope === undefined ? resource.defaultScopes : parseOAuthScopes(scope.split(' '))
|
|
250
355
|
if (!scopes || scopes.length === 0) return null
|
|
251
|
-
return scopes.every((entry) => resource
|
|
356
|
+
return scopes.every((entry) => resolveOAuthScopeToken(entry, resource) !== null)
|
|
252
357
|
? [...scopes]
|
|
253
358
|
: null
|
|
254
359
|
}
|
|
@@ -291,7 +396,7 @@ export function parseOAuthTokenRequest(value: unknown): OAuthTokenRequest | null
|
|
|
291
396
|
const clientId = field(value, 'client_id')
|
|
292
397
|
const resource = field(value, 'resource')
|
|
293
398
|
const grantType = field(value, 'grant_type')
|
|
294
|
-
if (!
|
|
399
|
+
if (!isOAuthClientId(clientId) || !isOAuthResourceUri(resource)) return null
|
|
295
400
|
if (grantType === 'authorization_code') {
|
|
296
401
|
const code = field(value, 'code')
|
|
297
402
|
const redirectUri = field(value, 'redirect_uri')
|
|
@@ -305,9 +410,38 @@ export function parseOAuthTokenRequest(value: unknown): OAuthTokenRequest | null
|
|
|
305
410
|
const refreshToken = field(value, 'refresh_token')
|
|
306
411
|
return isNonemptyText(refreshToken) ? { grant_type: grantType, client_id: clientId, refresh_token: refreshToken, resource } : null
|
|
307
412
|
}
|
|
413
|
+
if (grantType === OAUTH_DEVICE_CODE_GRANT_TYPE) {
|
|
414
|
+
const deviceCode = field(value, 'device_code')
|
|
415
|
+
return isNonemptyText(deviceCode) && deviceCode.length <= 256
|
|
416
|
+
? { grant_type: grantType, client_id: clientId, device_code: deviceCode, resource }
|
|
417
|
+
: null
|
|
418
|
+
}
|
|
308
419
|
return null
|
|
309
420
|
}
|
|
310
421
|
|
|
422
|
+
/**
|
|
423
|
+
* A device authorization request: its client, at least one scope and one to `MAX_OAUTH_DEVICE_RESOURCES` distinct
|
|
424
|
+
* resources. `resource` is the one parameter that may repeat (RFC 8707 §2), so the caller collects it into a list.
|
|
425
|
+
*/
|
|
426
|
+
export function parseOAuthDeviceAuthorizationRequest(
|
|
427
|
+
value: { fields: Record<string, string>; resources: string[] },
|
|
428
|
+
): OAuthDeviceAuthorizationRequest | null {
|
|
429
|
+
const clientId = field(value.fields, 'client_id')
|
|
430
|
+
const scope = field(value.fields, 'scope')
|
|
431
|
+
if (!isOAuthClientId(clientId) || typeof scope !== 'string') return null
|
|
432
|
+
const scopes = parseOAuthScopes(scope.split(' '))
|
|
433
|
+
const resources = value.resources
|
|
434
|
+
if (
|
|
435
|
+
scopes === null || scopes.length === 0 || resources.length === 0 || resources.length > MAX_OAUTH_DEVICE_RESOURCES
|
|
436
|
+
|| !resources.every(isOAuthResourceUri) || new Set(resources).size !== resources.length
|
|
437
|
+
) return null
|
|
438
|
+
return { client_id: clientId, scope: scopes, resources: [...resources] }
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
export function isOAuthDeviceGrantLifetime(value: number): value is OAuthDeviceGrantLifetime {
|
|
442
|
+
return OAUTH_DEVICE_GRANT_LIFETIMES_SECONDS.some((lifetime) => lifetime === value)
|
|
443
|
+
}
|
|
444
|
+
|
|
311
445
|
const MAX_CLIENT_ASSERTION_LENGTH = 16_384
|
|
312
446
|
|
|
313
447
|
/**
|
|
@@ -327,7 +461,7 @@ export function parseOAuthRevocationRequest(value: unknown): OAuthRevocationRequ
|
|
|
327
461
|
const token = field(value, 'token')
|
|
328
462
|
const hint = field(value, 'token_type_hint')
|
|
329
463
|
if (
|
|
330
|
-
!
|
|
464
|
+
!isOAuthClientId(clientId) || typeof token !== 'string' || token.length === 0 || token.length > 32768
|
|
331
465
|
|| (hint !== undefined && (typeof hint !== 'string' || hint.length > 200))
|
|
332
466
|
) return null
|
|
333
467
|
const request: OAuthRevocationRequest = { client_id: clientId, token }
|
package/src/password-policy.ts
CHANGED
|
@@ -14,7 +14,7 @@ export const PASSWORD_MIN_LENGTH_FLOOR = 4
|
|
|
14
14
|
/** The most code points a password may have, whatever the minimum. */
|
|
15
15
|
export const PASSWORD_MAX_LENGTH = 256
|
|
16
16
|
|
|
17
|
-
/** Below this NIST asks for a second factor, which IAM
|
|
17
|
+
/** Below this NIST asks for a second factor, which IAM gives only an enrolled administrator; the console says so. */
|
|
18
18
|
export const PASSWORD_SINGLE_FACTOR_FLOOR = 8
|
|
19
19
|
|
|
20
20
|
/** The password policy as IAM applies it now. */
|
package/types/src/index.d.ts
CHANGED
|
@@ -222,13 +222,22 @@ export interface ApiKeyDto {
|
|
|
222
222
|
}
|
|
223
223
|
/**
|
|
224
224
|
* One API key with its live credential, as `apiKeys.get` and each row of `apiKeys.list` answer it. Its permissions are
|
|
225
|
-
* its grants, edited with `grants.create` and `grants.delete` by an installation administrator and read again at every
|
|
225
|
+
* its grants, edited with `grants.create`, `grants.update` and `grants.delete` by an installation administrator and read again at every
|
|
226
226
|
* token exchange. An IAM older than the list's credential answers list rows without `credential`.
|
|
227
227
|
*/
|
|
228
228
|
export interface ApiKeyDetail extends ApiKeyDto {
|
|
229
229
|
/** The key's live credential, or null when none is live (a revoked key). */
|
|
230
230
|
credential: ApiKeyCredentialDto | null;
|
|
231
231
|
}
|
|
232
|
+
/** A grant of `apiKeys.get`'s key, naming who granted it. */
|
|
233
|
+
export interface ApiKeyGrantDto extends GrantDto {
|
|
234
|
+
/** The granting principal's label, or its id when IAM no longer knows it; null when nobody granted it. */
|
|
235
|
+
grantedByLabel: string | null;
|
|
236
|
+
}
|
|
237
|
+
/** `apiKeys.get`: one key whose grants name who granted them. */
|
|
238
|
+
export interface GetApiKeyResponse extends ApiKeyDetail {
|
|
239
|
+
grants: ApiKeyGrantDto[];
|
|
240
|
+
}
|
|
232
241
|
export interface ApiKeyCredentialDto {
|
|
233
242
|
/** The app the key mints tokens for; null = every app. A grant at another app never reaches the key's token. */
|
|
234
243
|
app: string | null;
|
|
@@ -669,7 +678,7 @@ export interface IamAdminRpcContract {
|
|
|
669
678
|
};
|
|
670
679
|
apiKeys: {
|
|
671
680
|
list: RpcProcedure<ListApiKeysInput, CursorList<ApiKeyDetail>>;
|
|
672
|
-
get: RpcProcedure<ApiKeyIdInput,
|
|
681
|
+
get: RpcProcedure<ApiKeyIdInput, GetApiKeyResponse>;
|
|
673
682
|
provision: RpcProcedure<ProvisionApiKeyRequest, ProvisionApiKeyResponse>;
|
|
674
683
|
rotate: RpcProcedure<RotateApiKeyInput, RotateApiKeyResponse>;
|
|
675
684
|
revoke: RpcProcedure<ApiKeyIdInput, OkResponse>;
|
|
@@ -728,6 +737,26 @@ export interface IamAdminRpcContract {
|
|
|
728
737
|
get: RpcProcedure<void, PasswordPolicyDto>;
|
|
729
738
|
set: RpcProcedure<SetPasswordPolicyRequest, PasswordPolicyDto>;
|
|
730
739
|
};
|
|
740
|
+
/**
|
|
741
|
+
* A person's second factor (TOTP), an installation administrator's (`iam.admin`) to read and to reset. The person
|
|
742
|
+
* enrols at IAM's `SECOND_FACTOR_ENROL_PATH`, never here. A reset removes the authenticator and its recovery codes
|
|
743
|
+
* and writes `iam.second_factor.reset`; the next sign-in asks for no code.
|
|
744
|
+
*/
|
|
745
|
+
secondFactor: {
|
|
746
|
+
get: RpcProcedure<PrincipalIdInput, SecondFactorDto>;
|
|
747
|
+
reset: RpcProcedure<PrincipalIdInput, SecondFactorDto>;
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
/** Where a signed-in administrator enrols an authenticator app, on IAM's own origin. */
|
|
751
|
+
export declare const SECOND_FACTOR_ENROL_PATH = "/auth/second-factor/enrol";
|
|
752
|
+
/** A person's second factor as an installation administrator sees it. */
|
|
753
|
+
export interface SecondFactorDto {
|
|
754
|
+
/** When the person confirmed the authenticator, in epoch seconds; null when none is enrolled. */
|
|
755
|
+
enrolledAt: number | null;
|
|
756
|
+
/** Recovery codes not used yet; 0 without an authenticator. */
|
|
757
|
+
recoveryCodesLeft: number;
|
|
758
|
+
/** Whether this installation can enrol an authenticator at all: it holds the key that seals the secrets. */
|
|
759
|
+
available: boolean;
|
|
731
760
|
}
|
|
732
761
|
export interface UserConnectionDto {
|
|
733
762
|
id: string;
|
package/types/src/oauth.d.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
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];
|
|
2
8
|
/** Managed CIMD registrations only; public dynamic registration is not part of this contract. */
|
|
3
9
|
export interface OAuthManagedClient {
|
|
4
|
-
provider:
|
|
10
|
+
provider: OAuthClientProvider;
|
|
5
11
|
clientId: string;
|
|
6
12
|
redirectUris: string[];
|
|
7
13
|
}
|
|
@@ -33,16 +39,27 @@ export type OAuthClientCredential = {
|
|
|
33
39
|
method: 'private_key_jwt';
|
|
34
40
|
assertion: string;
|
|
35
41
|
};
|
|
42
|
+
export interface OAuthScopeGrant {
|
|
43
|
+
scope: string;
|
|
44
|
+
permissions: KeyGrant[];
|
|
45
|
+
/**
|
|
46
|
+
* A scope dimension type. Such a scope is requested only as `<scope>:<value>`, and each of its scope-less permissions
|
|
47
|
+
* is narrowed to `{ type: parameter, value }`: `deploy:alpha` with parameter `app` holds its actions at app `alpha` only.
|
|
48
|
+
*/
|
|
49
|
+
parameter?: string;
|
|
50
|
+
}
|
|
36
51
|
export interface OAuthResourceDto {
|
|
37
52
|
app: string;
|
|
38
53
|
resource: string;
|
|
39
54
|
/** IAM resolves these app-owned grants; OAuth scope text alone never authorizes an action. */
|
|
40
|
-
scopeGrants:
|
|
41
|
-
scope: string;
|
|
42
|
-
permissions: KeyGrant[];
|
|
43
|
-
}[];
|
|
55
|
+
scopeGrants: OAuthScopeGrant[];
|
|
44
56
|
/** Used only when scope is omitted. No configured default means invalid_scope, never all scopes. */
|
|
45
57
|
defaultScopes?: string[];
|
|
58
|
+
/**
|
|
59
|
+
* `cli`: served only to the installation's command-line client, through the device grant. Absent: served only to the
|
|
60
|
+
* operator's managed clients, through an authorization code. A token for one never comes from the other's flow.
|
|
61
|
+
*/
|
|
62
|
+
client?: 'cli';
|
|
46
63
|
}
|
|
47
64
|
/** No access token, refresh token, hash, or authorization code appears in a grant DTO. */
|
|
48
65
|
export interface OAuthGrantDto {
|
|
@@ -77,6 +94,8 @@ export type OAuthAuthorizationParseResult = {
|
|
|
77
94
|
} | {
|
|
78
95
|
kind: 'invalid_request';
|
|
79
96
|
};
|
|
97
|
+
/** RFC 8628 §3.4: the grant type a device client polls the token endpoint with. */
|
|
98
|
+
export declare const OAUTH_DEVICE_CODE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:device_code";
|
|
80
99
|
export type OAuthTokenRequest = {
|
|
81
100
|
grant_type: 'authorization_code';
|
|
82
101
|
client_id: string;
|
|
@@ -89,7 +108,32 @@ export type OAuthTokenRequest = {
|
|
|
89
108
|
client_id: string;
|
|
90
109
|
refresh_token: string;
|
|
91
110
|
resource: string;
|
|
111
|
+
} | {
|
|
112
|
+
grant_type: typeof OAUTH_DEVICE_CODE_GRANT_TYPE;
|
|
113
|
+
client_id: string;
|
|
114
|
+
device_code: string;
|
|
115
|
+
resource: string;
|
|
92
116
|
};
|
|
117
|
+
/** RFC 8628 §3.1, with every resource the grant is asked for (RFC 8707): each token names one of them. */
|
|
118
|
+
export interface OAuthDeviceAuthorizationRequest {
|
|
119
|
+
client_id: string;
|
|
120
|
+
scope: string[];
|
|
121
|
+
resources: string[];
|
|
122
|
+
}
|
|
123
|
+
/** RFC 8628 §3.2. */
|
|
124
|
+
export interface OAuthDeviceAuthorizationResponse {
|
|
125
|
+
device_code: string;
|
|
126
|
+
user_code: string;
|
|
127
|
+
verification_uri: string;
|
|
128
|
+
verification_uri_complete: string;
|
|
129
|
+
expires_in: number;
|
|
130
|
+
interval: number;
|
|
131
|
+
}
|
|
132
|
+
/** The lifetimes a person picks between when approving a device: an hour, a day, a week. */
|
|
133
|
+
export declare const OAUTH_DEVICE_GRANT_LIFETIMES_SECONDS: readonly [3600, 86400, 604800];
|
|
134
|
+
export type OAuthDeviceGrantLifetime = (typeof OAUTH_DEVICE_GRANT_LIFETIMES_SECONDS)[number];
|
|
135
|
+
/** How many resources one device grant may name. */
|
|
136
|
+
export declare const MAX_OAUTH_DEVICE_RESOURCES = 8;
|
|
93
137
|
export interface OAuthTokenResponse {
|
|
94
138
|
access_token: string;
|
|
95
139
|
token_type: 'Bearer';
|
|
@@ -107,9 +151,10 @@ export interface OAuthAuthorizationServerMetadata {
|
|
|
107
151
|
authorization_endpoint: string;
|
|
108
152
|
token_endpoint: string;
|
|
109
153
|
revocation_endpoint: string;
|
|
154
|
+
device_authorization_endpoint: string;
|
|
110
155
|
jwks_uri: string;
|
|
111
156
|
response_types_supported: ['code'];
|
|
112
|
-
grant_types_supported: ['authorization_code', 'refresh_token'];
|
|
157
|
+
grant_types_supported: ['authorization_code', 'refresh_token', typeof OAUTH_DEVICE_CODE_GRANT_TYPE];
|
|
113
158
|
code_challenge_methods_supported: ['S256'];
|
|
114
159
|
token_endpoint_auth_methods_supported: ['none', 'private_key_jwt'];
|
|
115
160
|
token_endpoint_auth_signing_alg_values_supported: OAuthClientAssertionAlgorithm[];
|
|
@@ -123,10 +168,26 @@ export declare function parseOAuthManagedClient(value: unknown): OAuthManagedCli
|
|
|
123
168
|
/** A fetched document cannot enlarge the managed registration's exact redirect allowlist. */
|
|
124
169
|
export declare function parseOAuthClientMetadata(value: unknown, registration: OAuthManagedClient): OAuthClientMetadataDocument | null;
|
|
125
170
|
export declare function parseOAuthResourceDto(value: unknown): OAuthResourceDto | null;
|
|
171
|
+
/** Separates a parameterized scope from its value: `deploy:alpha`. */
|
|
172
|
+
export declare const SCOPE_PARAMETER_SEPARATOR = ":";
|
|
173
|
+
/**
|
|
174
|
+
* The resource's grant a requested scope token selects and the narrowed permissions it holds, or null when the token
|
|
175
|
+
* selects none: an unknown scope, a parameterized scope without its value, a value on a scope that takes none.
|
|
176
|
+
*/
|
|
177
|
+
export declare function resolveOAuthScopeToken(token: string, resource: OAuthResourceDto): KeyGrant[] | null;
|
|
126
178
|
export declare function resolveOAuthAuthorizationScopes(scope: string | undefined, resource: OAuthResourceDto): string[] | null;
|
|
127
179
|
export declare function parseOAuthAuthorizationAttempt(value: unknown): OAuthAuthorizationParseResult;
|
|
128
180
|
export declare function parseOAuthAuthorizationRequest(value: unknown): OAuthAuthorizationRequest | null;
|
|
129
181
|
export declare function parseOAuthTokenRequest(value: unknown): OAuthTokenRequest | null;
|
|
182
|
+
/**
|
|
183
|
+
* A device authorization request: its client, at least one scope and one to `MAX_OAUTH_DEVICE_RESOURCES` distinct
|
|
184
|
+
* resources. `resource` is the one parameter that may repeat (RFC 8707 §2), so the caller collects it into a list.
|
|
185
|
+
*/
|
|
186
|
+
export declare function parseOAuthDeviceAuthorizationRequest(value: {
|
|
187
|
+
fields: Record<string, string>;
|
|
188
|
+
resources: string[];
|
|
189
|
+
}): OAuthDeviceAuthorizationRequest | null;
|
|
190
|
+
export declare function isOAuthDeviceGrantLifetime(value: number): value is OAuthDeviceGrantLifetime;
|
|
130
191
|
/**
|
|
131
192
|
* The client credential of a token or revocation request (RFC 7521 §4.2). Neither assertion field is no
|
|
132
193
|
* credential; a half-present pair, another assertion type or an oversized assertion is null.
|
|
@@ -7,7 +7,7 @@ export declare const PASSWORD_DEFAULT_MIN_LENGTH = 15;
|
|
|
7
7
|
export declare const PASSWORD_MIN_LENGTH_FLOOR = 4;
|
|
8
8
|
/** The most code points a password may have, whatever the minimum. */
|
|
9
9
|
export declare const PASSWORD_MAX_LENGTH = 256;
|
|
10
|
-
/** Below this NIST asks for a second factor, which IAM
|
|
10
|
+
/** Below this NIST asks for a second factor, which IAM gives only an enrolled administrator; the console says so. */
|
|
11
11
|
export declare const PASSWORD_SINGLE_FACTOR_FLOOR = 8;
|
|
12
12
|
/** The password policy as IAM applies it now. */
|
|
13
13
|
export interface PasswordPolicyDto {
|