@fabrika/auth-core 0.0.26 → 0.3.1
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 +1 -1
- package/src/device.ts +290 -0
- package/src/index.ts +2 -1
- package/src/json.ts +11 -0
- package/src/rpc.ts +21 -32
- package/src/token.ts +7 -12
- package/src/types.ts +3 -3
- package/src/ids.ts +0 -91
package/package.json
CHANGED
package/src/device.ts
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { prop } from './json'
|
|
2
|
+
import type { Scope } from './types'
|
|
3
|
+
|
|
4
|
+
export interface StartDeviceAuthorizationInput {
|
|
5
|
+
app: string
|
|
6
|
+
label: string
|
|
7
|
+
permissions: string[]
|
|
8
|
+
keyLifetimeSeconds: number
|
|
9
|
+
redemptionChallenge: string
|
|
10
|
+
approvalChallenge: string
|
|
11
|
+
}
|
|
12
|
+
export interface DeviceAuthorizationStarted {
|
|
13
|
+
attemptId: string
|
|
14
|
+
expiresAt: number
|
|
15
|
+
pollIntervalSeconds: 3
|
|
16
|
+
}
|
|
17
|
+
export interface DeviceAuthorizationProof {
|
|
18
|
+
attemptId: string
|
|
19
|
+
redemptionVerifier: string
|
|
20
|
+
}
|
|
21
|
+
export interface DeviceAuthorizationConsent {
|
|
22
|
+
consentId: string
|
|
23
|
+
app: string
|
|
24
|
+
label: string
|
|
25
|
+
permissions: string[]
|
|
26
|
+
scope: Scope
|
|
27
|
+
keyExpiresAt: number
|
|
28
|
+
}
|
|
29
|
+
export interface DeviceAuthorizationReceipt {
|
|
30
|
+
attemptId: string
|
|
31
|
+
consent: DeviceAuthorizationConsent
|
|
32
|
+
credentialId: string
|
|
33
|
+
principalId: string
|
|
34
|
+
issuedAt: number
|
|
35
|
+
revokedAt: number | null
|
|
36
|
+
}
|
|
37
|
+
export type DeviceAuthorizationTerminalStatus = 'denied' | 'cancelled' | 'expired' | 'authority_lost'
|
|
38
|
+
export type DeviceAuthorizationStatus =
|
|
39
|
+
| { status: 'pending'; expiresAt: number }
|
|
40
|
+
| { status: 'approved'; consent: DeviceAuthorizationConsent }
|
|
41
|
+
| { status: 'issued'; receipt: DeviceAuthorizationReceipt }
|
|
42
|
+
| { status: DeviceAuthorizationTerminalStatus }
|
|
43
|
+
export interface ExchangeDeviceAuthorizationInput extends DeviceAuthorizationProof {
|
|
44
|
+
consentId: string
|
|
45
|
+
}
|
|
46
|
+
export type ExchangeDeviceAuthorizationResult =
|
|
47
|
+
| { ok: true; token: string; receipt: DeviceAuthorizationReceipt }
|
|
48
|
+
| { ok: false; reason: 'already_issued'; receipt: DeviceAuthorizationReceipt }
|
|
49
|
+
| { ok: false; reason: 'invalid_attempt' | 'pending' | DeviceAuthorizationTerminalStatus | 'consent_mismatch' }
|
|
50
|
+
export interface DeviceAuthorizationCaller {
|
|
51
|
+
app: string
|
|
52
|
+
credential: string
|
|
53
|
+
requestId: string
|
|
54
|
+
}
|
|
55
|
+
export interface InspectDeviceAuthorizationInput extends DeviceAuthorizationCaller {
|
|
56
|
+
attemptId: string
|
|
57
|
+
approvalVerifier: string
|
|
58
|
+
}
|
|
59
|
+
export interface DeviceAuthorizationRequest {
|
|
60
|
+
attemptId: string
|
|
61
|
+
app: string
|
|
62
|
+
label: string
|
|
63
|
+
permissions: string[]
|
|
64
|
+
keyLifetimeSeconds: number
|
|
65
|
+
expiresAt: number
|
|
66
|
+
}
|
|
67
|
+
export interface ApproveDeviceAuthorizationInput extends InspectDeviceAuthorizationInput {
|
|
68
|
+
scope: Scope
|
|
69
|
+
}
|
|
70
|
+
export interface ListOwnDeviceAuthorizationsInput extends DeviceAuthorizationCaller {
|
|
71
|
+
before?: string
|
|
72
|
+
limit?: number
|
|
73
|
+
}
|
|
74
|
+
export interface DeviceAuthorizationFailure {
|
|
75
|
+
ok: false
|
|
76
|
+
reason: 'invalid_attempt' | 'not_allowed' | 'invalid_input' | 'expired' | 'conflict' | 'authority_lost'
|
|
77
|
+
}
|
|
78
|
+
export type InspectDeviceAuthorizationResult = { ok: true; request: DeviceAuthorizationRequest } | DeviceAuthorizationFailure
|
|
79
|
+
export type ApproveDeviceAuthorizationResult = { ok: true; consent: DeviceAuthorizationConsent } | DeviceAuthorizationFailure
|
|
80
|
+
export type DenyDeviceAuthorizationResult = { ok: true } | DeviceAuthorizationFailure
|
|
81
|
+
export type ListOwnDeviceAuthorizationsResult =
|
|
82
|
+
| { ok: true; items: DeviceAuthorizationReceipt[]; nextCursor: string | null }
|
|
83
|
+
| DeviceAuthorizationFailure
|
|
84
|
+
export type StartDeviceAuthorizationResult = { ok: true } & DeviceAuthorizationStarted | {
|
|
85
|
+
ok: false
|
|
86
|
+
reason: 'invalid_input' | 'not_allowed' | 'rate_limited'
|
|
87
|
+
}
|
|
88
|
+
export type PollDeviceAuthorizationResult = { ok: true } & DeviceAuthorizationStatus | { ok: false; reason: 'invalid_attempt' | 'slow_down' }
|
|
89
|
+
export type CancelDeviceAuthorizationResult = { ok: true } | { ok: false; reason: 'invalid_attempt' | 'already_issued' }
|
|
90
|
+
|
|
91
|
+
function text(value: unknown, max = 200): value is string {
|
|
92
|
+
return typeof value === 'string' && value.length > 0 && value.length <= max
|
|
93
|
+
}
|
|
94
|
+
function seconds(value: unknown): value is number {
|
|
95
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0
|
|
96
|
+
}
|
|
97
|
+
function id(value: unknown): value is string {
|
|
98
|
+
return typeof value === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value)
|
|
99
|
+
}
|
|
100
|
+
function proof(value: unknown): value is string {
|
|
101
|
+
return typeof value === 'string' && /^[A-Za-z0-9_-]{43}$/.test(value)
|
|
102
|
+
}
|
|
103
|
+
function audience(value: unknown): value is string {
|
|
104
|
+
return text(value, 100) && /^[a-z0-9][a-z0-9-]*$/.test(value)
|
|
105
|
+
}
|
|
106
|
+
function actions(value: unknown): value is string[] {
|
|
107
|
+
return Array.isArray(value) && value.length > 0 && value.length <= 32
|
|
108
|
+
&& value.every((item: unknown) => text(item, 100) && /^[a-zA-Z0-9_.*-]+$/.test(item)) && new Set(value).size === value.length
|
|
109
|
+
}
|
|
110
|
+
function scope(value: unknown): Scope | null {
|
|
111
|
+
const type = prop(value, 'type')
|
|
112
|
+
const valueField = prop(value, 'value')
|
|
113
|
+
return text(type, 100) && text(valueField, 200) ? { type, value: valueField } : null
|
|
114
|
+
}
|
|
115
|
+
export function parseStartDeviceAuthorizationInput(value: unknown): StartDeviceAuthorizationInput | null {
|
|
116
|
+
const app = prop(value, 'app')
|
|
117
|
+
const label = prop(value, 'label')
|
|
118
|
+
const permissions = prop(value, 'permissions')
|
|
119
|
+
const keyLifetimeSeconds = prop(value, 'keyLifetimeSeconds')
|
|
120
|
+
const redemptionChallenge = prop(value, 'redemptionChallenge')
|
|
121
|
+
const approvalChallenge = prop(value, 'approvalChallenge')
|
|
122
|
+
return audience(app) && text(label) && actions(permissions) && seconds(keyLifetimeSeconds) && proof(redemptionChallenge) && proof(approvalChallenge)
|
|
123
|
+
&& redemptionChallenge !== approvalChallenge
|
|
124
|
+
? { app, label, permissions, keyLifetimeSeconds, redemptionChallenge, approvalChallenge }
|
|
125
|
+
: null
|
|
126
|
+
}
|
|
127
|
+
export function parseDeviceAuthorizationStarted(value: unknown): DeviceAuthorizationStarted | null {
|
|
128
|
+
const attemptId = prop(value, 'attemptId')
|
|
129
|
+
const expiresAt = prop(value, 'expiresAt')
|
|
130
|
+
return id(attemptId) && seconds(expiresAt) && prop(value, 'pollIntervalSeconds') === 3 ? { attemptId, expiresAt, pollIntervalSeconds: 3 } : null
|
|
131
|
+
}
|
|
132
|
+
export function parseDeviceAuthorizationProof(value: unknown): DeviceAuthorizationProof | null {
|
|
133
|
+
const attemptId = prop(value, 'attemptId')
|
|
134
|
+
const redemptionVerifier = prop(value, 'redemptionVerifier')
|
|
135
|
+
return id(attemptId) && proof(redemptionVerifier) ? { attemptId, redemptionVerifier } : null
|
|
136
|
+
}
|
|
137
|
+
export function parseExchangeDeviceAuthorizationInput(value: unknown): ExchangeDeviceAuthorizationInput | null {
|
|
138
|
+
const parsed = parseDeviceAuthorizationProof(value)
|
|
139
|
+
const consentId = prop(value, 'consentId')
|
|
140
|
+
return parsed && id(consentId) ? { ...parsed, consentId } : null
|
|
141
|
+
}
|
|
142
|
+
export function parseDeviceAuthorizationConsent(value: unknown): DeviceAuthorizationConsent | null {
|
|
143
|
+
const consentId = prop(value, 'consentId')
|
|
144
|
+
const app = prop(value, 'app')
|
|
145
|
+
const label = prop(value, 'label')
|
|
146
|
+
const permissions = prop(value, 'permissions')
|
|
147
|
+
const parsedScope = scope(prop(value, 'scope'))
|
|
148
|
+
const keyExpiresAt = prop(value, 'keyExpiresAt')
|
|
149
|
+
return id(consentId) && audience(app) && text(label) && actions(permissions) && parsedScope && seconds(keyExpiresAt)
|
|
150
|
+
? { consentId, app, label, permissions, scope: parsedScope, keyExpiresAt }
|
|
151
|
+
: null
|
|
152
|
+
}
|
|
153
|
+
export function parseDeviceAuthorizationReceipt(value: unknown): DeviceAuthorizationReceipt | null {
|
|
154
|
+
const attemptId = prop(value, 'attemptId')
|
|
155
|
+
const credentialId = prop(value, 'credentialId')
|
|
156
|
+
const principalId = prop(value, 'principalId')
|
|
157
|
+
const consent = parseDeviceAuthorizationConsent(prop(value, 'consent'))
|
|
158
|
+
const issuedAt = prop(value, 'issuedAt')
|
|
159
|
+
const revokedAt = prop(value, 'revokedAt')
|
|
160
|
+
return id(attemptId) && id(credentialId) && id(principalId) && consent && seconds(issuedAt) && (revokedAt === null || seconds(revokedAt))
|
|
161
|
+
? { attemptId, credentialId, principalId, consent, issuedAt, revokedAt }
|
|
162
|
+
: null
|
|
163
|
+
}
|
|
164
|
+
export function parseDeviceAuthorizationRequest(value: unknown): DeviceAuthorizationRequest | null {
|
|
165
|
+
const attemptId = prop(value, 'attemptId')
|
|
166
|
+
const app = prop(value, 'app')
|
|
167
|
+
const label = prop(value, 'label')
|
|
168
|
+
const permissions = prop(value, 'permissions')
|
|
169
|
+
const keyLifetimeSeconds = prop(value, 'keyLifetimeSeconds')
|
|
170
|
+
const expiresAt = prop(value, 'expiresAt')
|
|
171
|
+
return id(attemptId) && audience(app) && text(label) && actions(permissions) && seconds(keyLifetimeSeconds) && keyLifetimeSeconds <= 2592000
|
|
172
|
+
&& seconds(expiresAt)
|
|
173
|
+
? { attemptId, app, label, permissions, keyLifetimeSeconds, expiresAt }
|
|
174
|
+
: null
|
|
175
|
+
}
|
|
176
|
+
export function parseDeviceAuthorizationCaller(value: unknown): DeviceAuthorizationCaller | null {
|
|
177
|
+
const app = prop(value, 'app')
|
|
178
|
+
const credential = prop(value, 'credential')
|
|
179
|
+
const requestId = prop(value, 'requestId')
|
|
180
|
+
return audience(app) && text(credential, 16384) && text(requestId) ? { app, credential, requestId } : null
|
|
181
|
+
}
|
|
182
|
+
export function parseInspectDeviceAuthorizationInput(value: unknown): InspectDeviceAuthorizationInput | null {
|
|
183
|
+
const caller = parseDeviceAuthorizationCaller(value)
|
|
184
|
+
const attemptId = prop(value, 'attemptId')
|
|
185
|
+
const approvalVerifier = prop(value, 'approvalVerifier')
|
|
186
|
+
return caller && id(attemptId) && proof(approvalVerifier) ? { ...caller, attemptId, approvalVerifier } : null
|
|
187
|
+
}
|
|
188
|
+
export function parseApproveDeviceAuthorizationInput(value: unknown): ApproveDeviceAuthorizationInput | null {
|
|
189
|
+
const inspect = parseInspectDeviceAuthorizationInput(value)
|
|
190
|
+
const parsedScope = scope(prop(value, 'scope'))
|
|
191
|
+
return inspect && parsedScope ? { ...inspect, scope: parsedScope } : null
|
|
192
|
+
}
|
|
193
|
+
export function parseListOwnDeviceAuthorizationsInput(value: unknown): ListOwnDeviceAuthorizationsInput | null {
|
|
194
|
+
const caller = parseDeviceAuthorizationCaller(value)
|
|
195
|
+
const before = prop(value, 'before')
|
|
196
|
+
const limit = prop(value, 'limit')
|
|
197
|
+
if (!caller || (before !== undefined && !id(before)) || (limit !== undefined && (!seconds(limit) || limit > 100))) return null
|
|
198
|
+
return { ...caller, ...(before === undefined ? {} : { before }), ...(limit === undefined ? {} : { limit }) }
|
|
199
|
+
}
|
|
200
|
+
function terminal(value: unknown): value is DeviceAuthorizationTerminalStatus {
|
|
201
|
+
return value === 'denied' || value === 'cancelled' || value === 'expired' || value === 'authority_lost'
|
|
202
|
+
}
|
|
203
|
+
export function parseDeviceAuthorizationStatus(value: unknown): DeviceAuthorizationStatus | null {
|
|
204
|
+
const status = prop(value, 'status')
|
|
205
|
+
if (terminal(status)) return { status }
|
|
206
|
+
if (status === 'pending') {
|
|
207
|
+
const expiresAt = prop(value, 'expiresAt')
|
|
208
|
+
return seconds(expiresAt) ? { status, expiresAt } : null
|
|
209
|
+
}
|
|
210
|
+
if (status === 'approved') {
|
|
211
|
+
const consent = parseDeviceAuthorizationConsent(prop(value, 'consent'))
|
|
212
|
+
return consent ? { status, consent } : null
|
|
213
|
+
}
|
|
214
|
+
if (status === 'issued') {
|
|
215
|
+
const receipt = parseDeviceAuthorizationReceipt(prop(value, 'receipt'))
|
|
216
|
+
return receipt ? { status, receipt } : null
|
|
217
|
+
}
|
|
218
|
+
return null
|
|
219
|
+
}
|
|
220
|
+
export function parseExchangeDeviceAuthorizationResult(value: unknown): ExchangeDeviceAuthorizationResult | null {
|
|
221
|
+
const ok = prop(value, 'ok')
|
|
222
|
+
const reason = prop(value, 'reason')
|
|
223
|
+
if (ok === true || (ok === false && reason === 'already_issued')) {
|
|
224
|
+
const receipt = parseDeviceAuthorizationReceipt(prop(value, 'receipt'))
|
|
225
|
+
if (!receipt) return null
|
|
226
|
+
if (ok === false) return { ok, reason: 'already_issued', receipt }
|
|
227
|
+
const token = prop(value, 'token')
|
|
228
|
+
return text(token, 200) && token.startsWith('px_') ? { ok, token, receipt } : null
|
|
229
|
+
}
|
|
230
|
+
return ok === false && (terminal(reason) || reason === 'invalid_attempt' || reason === 'pending' || reason === 'consent_mismatch')
|
|
231
|
+
? { ok, reason }
|
|
232
|
+
: null
|
|
233
|
+
}
|
|
234
|
+
export function parseStartDeviceAuthorizationResult(value: unknown): StartDeviceAuthorizationResult | null {
|
|
235
|
+
if (prop(value, 'ok') === true) {
|
|
236
|
+
const started = parseDeviceAuthorizationStarted(value)
|
|
237
|
+
return started ? { ok: true, ...started } : null
|
|
238
|
+
}
|
|
239
|
+
const reason = prop(value, 'reason')
|
|
240
|
+
return prop(value, 'ok') === false && (reason === 'invalid_input' || reason === 'not_allowed' || reason === 'rate_limited')
|
|
241
|
+
? { ok: false, reason }
|
|
242
|
+
: null
|
|
243
|
+
}
|
|
244
|
+
export function parsePollDeviceAuthorizationResult(value: unknown): PollDeviceAuthorizationResult | null {
|
|
245
|
+
if (prop(value, 'ok') === true) {
|
|
246
|
+
const status = parseDeviceAuthorizationStatus(value)
|
|
247
|
+
return status ? { ok: true, ...status } : null
|
|
248
|
+
}
|
|
249
|
+
const reason = prop(value, 'reason')
|
|
250
|
+
return prop(value, 'ok') === false && (reason === 'invalid_attempt' || reason === 'slow_down') ? { ok: false, reason } : null
|
|
251
|
+
}
|
|
252
|
+
export function parseCancelDeviceAuthorizationResult(value: unknown): CancelDeviceAuthorizationResult | null {
|
|
253
|
+
if (prop(value, 'ok') === true) return { ok: true }
|
|
254
|
+
const reason = prop(value, 'reason')
|
|
255
|
+
return prop(value, 'ok') === false && (reason === 'invalid_attempt' || reason === 'already_issued') ? { ok: false, reason } : null
|
|
256
|
+
}
|
|
257
|
+
function failure(value: unknown): DeviceAuthorizationFailure | null {
|
|
258
|
+
const reason = prop(value, 'reason')
|
|
259
|
+
return prop(value, 'ok') === false
|
|
260
|
+
&& (reason === 'invalid_attempt' || reason === 'not_allowed' || reason === 'invalid_input' || reason === 'expired' || reason === 'conflict'
|
|
261
|
+
|| reason === 'authority_lost')
|
|
262
|
+
? { ok: false, reason }
|
|
263
|
+
: null
|
|
264
|
+
}
|
|
265
|
+
export function parseInspectDeviceAuthorizationResult(value: unknown): InspectDeviceAuthorizationResult | null {
|
|
266
|
+
if (prop(value, 'ok') !== true) return failure(value)
|
|
267
|
+
const request = parseDeviceAuthorizationRequest(prop(value, 'request'))
|
|
268
|
+
return request ? { ok: true, request } : null
|
|
269
|
+
}
|
|
270
|
+
export function parseApproveDeviceAuthorizationResult(value: unknown): ApproveDeviceAuthorizationResult | null {
|
|
271
|
+
if (prop(value, 'ok') !== true) return failure(value)
|
|
272
|
+
const consent = parseDeviceAuthorizationConsent(prop(value, 'consent'))
|
|
273
|
+
return consent ? { ok: true, consent } : null
|
|
274
|
+
}
|
|
275
|
+
export function parseDenyDeviceAuthorizationResult(value: unknown): DenyDeviceAuthorizationResult | null {
|
|
276
|
+
return prop(value, 'ok') === true ? { ok: true } : failure(value)
|
|
277
|
+
}
|
|
278
|
+
export function parseListOwnDeviceAuthorizationsResult(value: unknown): ListOwnDeviceAuthorizationsResult | null {
|
|
279
|
+
if (prop(value, 'ok') !== true) return failure(value)
|
|
280
|
+
const raw = prop(value, 'items')
|
|
281
|
+
const nextCursor = prop(value, 'nextCursor')
|
|
282
|
+
if (!Array.isArray(raw) || raw.length > 100 || (nextCursor !== null && !id(nextCursor))) return null
|
|
283
|
+
const items: DeviceAuthorizationReceipt[] = []
|
|
284
|
+
for (const item of raw) {
|
|
285
|
+
const receipt = parseDeviceAuthorizationReceipt(item)
|
|
286
|
+
if (!receipt) return null
|
|
287
|
+
items.push(receipt)
|
|
288
|
+
}
|
|
289
|
+
return { ok: true, items, nextCursor }
|
|
290
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// @fabrika/auth-core — pure logic, types, and wire names shared across auth components and apps.
|
|
2
|
+
export * from './device'
|
|
2
3
|
export * from './environment'
|
|
3
4
|
export * from './gates'
|
|
4
|
-
export
|
|
5
|
+
export { parseJsonStringArray } from './json'
|
|
5
6
|
export * from './permissions'
|
|
6
7
|
export * from './rpc'
|
|
7
8
|
export * from './token'
|
package/src/json.ts
CHANGED
|
@@ -23,3 +23,14 @@ export function numberField(value: unknown, key: string): number | undefined {
|
|
|
23
23
|
const v = prop(value, key)
|
|
24
24
|
return typeof v === 'number' && Number.isFinite(v) ? v : undefined
|
|
25
25
|
}
|
|
26
|
+
|
|
27
|
+
/** The string entries of a JSON array; non-string entries are dropped, and malformed or non-array text yields []. */
|
|
28
|
+
export function parseJsonStringArray(text: string): string[] {
|
|
29
|
+
let parsed: unknown
|
|
30
|
+
try {
|
|
31
|
+
parsed = JSON.parse(text)
|
|
32
|
+
} catch {
|
|
33
|
+
return []
|
|
34
|
+
}
|
|
35
|
+
return Array.isArray(parsed) ? parsed.filter((value) => typeof value === 'string') : []
|
|
36
|
+
}
|
package/src/rpc.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ApproveDeviceAuthorizationInput,
|
|
3
|
+
ApproveDeviceAuthorizationResult,
|
|
4
|
+
DenyDeviceAuthorizationResult,
|
|
5
|
+
InspectDeviceAuthorizationInput,
|
|
6
|
+
InspectDeviceAuthorizationResult,
|
|
7
|
+
ListOwnDeviceAuthorizationsInput,
|
|
8
|
+
ListOwnDeviceAuthorizationsResult,
|
|
9
|
+
} from './device'
|
|
1
10
|
import type { Jwks } from './token'
|
|
2
11
|
import type { PermissionEntry, PrincipalType, Scope } from './types'
|
|
3
12
|
|
|
@@ -43,7 +52,7 @@ export type MintFromKeyResult =
|
|
|
43
52
|
| { ok: true; token: string; expiresAt: number }
|
|
44
53
|
| { ok: false; reason: 'invalid_key' | 'unknown_principal' | 'disabled' }
|
|
45
54
|
|
|
46
|
-
// ── Session handoff (ADR-
|
|
55
|
+
// ── Session handoff (ADR-0022, ADR-0023) ──────────────────────────────────────
|
|
47
56
|
//
|
|
48
57
|
// The ONE way a browser that authenticated at IAM ends up authenticated at an app, whether or not the
|
|
49
58
|
// two share a domain: no cookie of IAM's is ever readable from an app's host. IAM issues a single-use
|
|
@@ -57,7 +66,7 @@ export type MintFromKeyResult =
|
|
|
57
66
|
// app's own session, server-side.
|
|
58
67
|
//
|
|
59
68
|
// This lives on the PROXY surface (`/auth/mint/*`, gated by its own key), not on `IamRpc`. That is
|
|
60
|
-
// ADR-
|
|
69
|
+
// ADR-0022's least-privilege split: the proxy is the only publicly-routed component and holds a key
|
|
61
70
|
// scoped to the handful of calls it needs.
|
|
62
71
|
|
|
63
72
|
export interface ExchangeAuthCodeInput {
|
|
@@ -116,13 +125,12 @@ export const HANDOFF_COOKIE_TTL_SECONDS = 10 * 60
|
|
|
116
125
|
*/
|
|
117
126
|
export const AUTH_CODE_TTL_SECONDS = 120
|
|
118
127
|
|
|
119
|
-
// ── Issuing credentials (issueKey)
|
|
128
|
+
// ── Issuing credentials (issueKey) ──────────────────────────────────────────────
|
|
120
129
|
//
|
|
121
|
-
// `issueKey` mints an opaque, stored, revocable `px_` credential
|
|
122
|
-
//
|
|
123
|
-
// is resolved server-side from the forwarded credentials and may only grant what it itself holds.
|
|
130
|
+
// `issueKey` mints an opaque, stored, revocable `px_` credential. It is DELEGATED: the issuer is
|
|
131
|
+
// resolved server-side from the forwarded credentials and may only grant what it itself holds.
|
|
124
132
|
|
|
125
|
-
/** One grant on a `px_` credential
|
|
133
|
+
/** One grant on a `px_` credential — an action pattern + optional scope. */
|
|
126
134
|
export interface KeyGrant {
|
|
127
135
|
action: string
|
|
128
136
|
/** Omitted/null = global (the issuer must hold the action globally). */
|
|
@@ -176,23 +184,6 @@ export type IssueKeyResult =
|
|
|
176
184
|
| { ok: true; token: string; id: string; principalId?: string }
|
|
177
185
|
| { ok: false; reason: 'missing_token' | 'invalid_token' | 'unknown_principal' | 'disabled' | 'not_allowed' }
|
|
178
186
|
|
|
179
|
-
export interface IssueJwtInput {
|
|
180
|
-
app: string
|
|
181
|
-
/** The propustka-native credential the ISSUER is resolved from (px_token / px_ key); null = absent. */
|
|
182
|
-
credential: string | null
|
|
183
|
-
requestId: string
|
|
184
|
-
/** Inline grants the passthrough token carries (frozen at issue). */
|
|
185
|
-
permissions: KeyGrant[]
|
|
186
|
-
label?: string
|
|
187
|
-
/** Requested lifetime (seconds); capped by the server. Defaults to the standard token TTL. */
|
|
188
|
-
ttl?: number
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export type IssueJwtResult =
|
|
192
|
-
/** The signed passthrough token returned ONCE; `id` is its audit reference (no DB row to revoke). */
|
|
193
|
-
| { ok: true; token: string; expiresAt: number; id: string }
|
|
194
|
-
| { ok: false; reason: 'missing_token' | 'invalid_token' | 'unknown_principal' | 'disabled' | 'not_allowed' }
|
|
195
|
-
|
|
196
187
|
/**
|
|
197
188
|
* A resolved caller identity + its frozen permission snapshot. Produced by `accessClaimsToResolved`
|
|
198
189
|
* from a verified access token (`token.ts`); consumed where a principal's resolved perms are needed
|
|
@@ -209,11 +200,11 @@ export interface ResolvedPrincipal {
|
|
|
209
200
|
export interface AuditInput {
|
|
210
201
|
app: string
|
|
211
202
|
requestId: string
|
|
212
|
-
/** NULL for an anonymous-credential event (a share link
|
|
203
|
+
/** NULL for an anonymous-credential event (a share link has no principal). */
|
|
213
204
|
principalId: string | null
|
|
214
205
|
/** Snapshot — survives principal deletion (the credential/token label for anonymous events). */
|
|
215
206
|
principalLabel: string
|
|
216
|
-
/** Set for an anonymous-credential event — the issuing credential
|
|
207
|
+
/** Set for an anonymous-credential event — the issuing credential id. */
|
|
217
208
|
credentialId?: string
|
|
218
209
|
action: string
|
|
219
210
|
resourceType: string
|
|
@@ -277,6 +268,10 @@ export type ListPrincipalsResult =
|
|
|
277
268
|
* serializable objects (RPC-friendly).
|
|
278
269
|
*/
|
|
279
270
|
export interface IamRpc {
|
|
271
|
+
inspectDeviceAuthorization(input: InspectDeviceAuthorizationInput): Promise<InspectDeviceAuthorizationResult>
|
|
272
|
+
approveDeviceAuthorization(input: ApproveDeviceAuthorizationInput): Promise<ApproveDeviceAuthorizationResult>
|
|
273
|
+
denyDeviceAuthorization(input: InspectDeviceAuthorizationInput): Promise<DenyDeviceAuthorizationResult>
|
|
274
|
+
listOwnDeviceAuthorizations(input: ListOwnDeviceAuthorizationsInput): Promise<ListOwnDeviceAuthorizationsResult>
|
|
280
275
|
/**
|
|
281
276
|
* Mint a short-lived, per-app permission token from the browser's SSO session — the
|
|
282
277
|
* propustka-native auth path. The SDK middleware calls this only when its cached permission
|
|
@@ -297,12 +292,6 @@ export interface IamRpc {
|
|
|
297
292
|
* plaintext token ONCE; `id` is the durable handle for revoke.
|
|
298
293
|
*/
|
|
299
294
|
issueKey(input: IssueKeyInput): Promise<IssueKeyResult>
|
|
300
|
-
/**
|
|
301
|
-
* Sign a stateless passthrough access token (audit-only, TTL-bounded, NOT revocable). Delegated
|
|
302
|
-
* like `issueKey`. The caller carries the returned JWT directly; apps verify it locally with no
|
|
303
|
-
* propustka round-trip.
|
|
304
|
-
*/
|
|
305
|
-
issueJwt(input: IssueJwtInput): Promise<IssueJwtResult>
|
|
306
295
|
/**
|
|
307
296
|
* The public signing key set. The SDK fetches it ONCE per isolate (cached) over the binding —
|
|
308
297
|
* which never traverses the Access edge — then verifies every permission token locally.
|
package/src/token.ts
CHANGED
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
* ONE shape, no `kind` discriminator — the optional `ptype` carries the distinction. Every token carries
|
|
16
16
|
* `perms: PermissionEntry[]` (authorized via `permits` — scopes + wildcards) and an OPTIONAL
|
|
17
17
|
* principal (`ptype`): present means the token is bound to a user/service principal; absent means an
|
|
18
|
-
* anonymous credential (a share link
|
|
18
|
+
* anonymous credential (a share link), whose `perms` are narrow scoped entries and
|
|
19
19
|
* whose audit actor is `label`. `sub` is the subject id either way — a principal id when bound, else
|
|
20
|
-
* the issuing credential
|
|
20
|
+
* the issuing credential id.
|
|
21
21
|
*
|
|
22
22
|
* Pure: no jose, no I/O. Signing/verifying (key material) stays in the packages that own it.
|
|
23
23
|
*/
|
|
@@ -52,11 +52,6 @@ export const TOKEN_ALG = 'ES256'
|
|
|
52
52
|
|
|
53
53
|
/** Default per-app access-token lifetime. Revocation/role changes take effect within this. */
|
|
54
54
|
export const DEFAULT_TOKEN_TTL_SECONDS = 300
|
|
55
|
-
/**
|
|
56
|
-
* Hard cap on a `issueJwt` passthrough token's lifetime. A passthrough JWT is NOT revocable (no
|
|
57
|
-
* stored row), so its TTL is its whole security window — bounded here (24 h) regardless of request.
|
|
58
|
-
*/
|
|
59
|
-
export const MAX_PASSTHROUGH_TTL_SECONDS = 24 * 60 * 60
|
|
60
55
|
/**
|
|
61
56
|
* The SDK refreshes an access token this many seconds BEFORE it expires, so a request never rides a
|
|
62
57
|
* token that expires mid-flight. Must be < the token TTL.
|
|
@@ -68,14 +63,14 @@ export const TOKEN_REFRESH_SKEW_SECONDS = 30
|
|
|
68
63
|
/**
|
|
69
64
|
* The propustka access token. `perms` is always present (matched via `permits`); `ptype` is present
|
|
70
65
|
* only when `sub` is a principal. `label` is the audit actor — the principal's label when bound, the
|
|
71
|
-
* credential
|
|
66
|
+
* credential label otherwise (or null).
|
|
72
67
|
*/
|
|
73
68
|
export interface AccessTokenClaims {
|
|
74
69
|
/** Issuer — propustka's own origin (e.g. `https://propustka.example.com`). */
|
|
75
70
|
iss: string
|
|
76
71
|
/** The app id this token is scoped to. The SDK REJECTS a token whose `aud` is not its app. */
|
|
77
72
|
aud: string
|
|
78
|
-
/** Subject id: a principal id when `ptype` is set, else the issuing credential
|
|
73
|
+
/** Subject id: a principal id when `ptype` is set, else the issuing credential id. */
|
|
79
74
|
sub: string
|
|
80
75
|
/** Issued-at (unix seconds). */
|
|
81
76
|
iat: number
|
|
@@ -85,7 +80,7 @@ export interface AccessTokenClaims {
|
|
|
85
80
|
perms: PermissionEntry[]
|
|
86
81
|
/** Present only when the token is bound to a principal (`sub` is that principal). */
|
|
87
82
|
ptype?: PrincipalType
|
|
88
|
-
/** Audit actor label — the principal's label, or the credential
|
|
83
|
+
/** Audit actor label — the principal's label, or the credential label, or null (anonymous). */
|
|
89
84
|
label: string | null
|
|
90
85
|
}
|
|
91
86
|
|
|
@@ -119,9 +114,9 @@ export interface AccessTokenParams {
|
|
|
119
114
|
iss: string
|
|
120
115
|
/** App id this token is for (becomes `aud`). */
|
|
121
116
|
app: string
|
|
122
|
-
/** Subject id (becomes `sub`) — a principal id when `type` is given, else a credential
|
|
117
|
+
/** Subject id (becomes `sub`) — a principal id when `type` is given, else a credential id. */
|
|
123
118
|
subject: string
|
|
124
|
-
/** Principal type — omit for an anonymous credential (share link
|
|
119
|
+
/** Principal type — omit for an anonymous credential (share link). */
|
|
125
120
|
type?: PrincipalType
|
|
126
121
|
/** Audit actor label, or null for an unlabeled anonymous token. */
|
|
127
122
|
label: string | null
|
package/src/types.ts
CHANGED
|
@@ -73,14 +73,14 @@ export interface AppSchema {
|
|
|
73
73
|
// no IAM reconcile for these: gate rules are deployed verbatim in the proxy manifest, and the proxy
|
|
74
74
|
// decides WHICH credential KIND a path requires. Three kinds mirror the three old edge decisions:
|
|
75
75
|
// - public → no credential (the old `bypass`/everyone carve-out)
|
|
76
|
-
// - service → a machine `px_` key
|
|
76
|
+
// - service → a machine `px_` key (the old `non_identity`/service-token gate)
|
|
77
77
|
// - human → a logged-in human via `px_session`/`px_token` (the old `allow`-by-audience gate)
|
|
78
78
|
// WHO is an admitted human stays centrally owned by propustka (decided at `/auth/callback`, the
|
|
79
79
|
// admission allowlist), exactly as the old central edge audience did — a `human` gate only asserts
|
|
80
80
|
// "a resolved user principal exists". Auth-surface paths (`/auth/*`, `/.well-known/jwks.json`) live
|
|
81
81
|
// on PROPUSTKA's host, not the app's, so apps never declare them.
|
|
82
82
|
|
|
83
|
-
/** Where a propustka
|
|
83
|
+
/** Where a propustka `px_` credential rides on a request. */
|
|
84
84
|
export interface CredentialLocation {
|
|
85
85
|
/** Transport carrying the credential. */
|
|
86
86
|
in: 'header' | 'query' | 'cookie'
|
|
@@ -93,7 +93,7 @@ export type GateKind =
|
|
|
93
93
|
/** Anyone — no credential. Terminal; resolves to an ANONYMOUS AuthContext (`principal: null`). */
|
|
94
94
|
| { kind: 'public' }
|
|
95
95
|
/**
|
|
96
|
-
* A machine `px_` key
|
|
96
|
+
* A machine `px_` key — a bearer (default `Authorization: Bearer`) or the
|
|
97
97
|
* declared `credential` location. ABSENT → falls through to the next matching rule; PRESENT but
|
|
98
98
|
* invalid → fail closed (no fall-through).
|
|
99
99
|
*/
|
package/src/ids.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generate a UUIDv7 (RFC 9562): time-sortable, 128 bits, monotonic WITHIN a millisecond as well as
|
|
3
|
-
* across one — `audit_events` is listed `ORDER BY id DESC` with an `id < ?` keyset cursor, and a
|
|
4
|
-
* request that writes two audit rows writes them inside the same millisecond routinely.
|
|
5
|
-
*
|
|
6
|
-
* Layout, with §6.2's "fixed bit-length dedicated counter" monotonic variant:
|
|
7
|
-
* - bytes 0..5 : 48-bit big-endian Unix-millis timestamp
|
|
8
|
-
* - byte 6 : version nibble `7` in the high 4 bits, the high 4 bits of a 12-bit counter below
|
|
9
|
-
* - byte 7 : the low 8 bits of that counter (bytes 6..7 low bits are `rand_a`)
|
|
10
|
-
* - byte 8 : RFC 4122 variant bits `10` in the high 2 bits, random in the low 6
|
|
11
|
-
* - bytes 9..15 : random (`crypto.getRandomValues`)
|
|
12
|
-
*
|
|
13
|
-
* That leaves **62 random bits per id** — down from the 74 of the unguarded Method 0 this used to be
|
|
14
|
-
* — plus 8 fresh random bits seeding the counter at each new millisecond.
|
|
15
|
-
*
|
|
16
|
-
* The counter is re-seeded from 8 random bits (the top 4 left zero, the RFC's rollover guard, so at
|
|
17
|
-
* least 3840 ids per millisecond are guaranteed) whenever the millisecond advances, and incremented
|
|
18
|
-
* otherwise. Because the timestamp occupies the most-significant bytes and the counter the bytes
|
|
19
|
-
* right after it, ids sort lexicographically in mint order — strictly, not just at millisecond
|
|
20
|
-
* granularity.
|
|
21
|
-
*
|
|
22
|
-
* Two clock hazards are handled explicitly rather than by wrapping:
|
|
23
|
-
* - counter exhausted inside one millisecond → borrow one millisecond from the future (RFC 9562
|
|
24
|
-
* §6.2) and re-seed; the borrowed time is repaid as soon as the wall clock catches up.
|
|
25
|
-
* - clock jumps BACKWARDS → keep the last effective millisecond and keep counting, so the sequence
|
|
26
|
-
* never repeats and never decreases.
|
|
27
|
-
*
|
|
28
|
-
* The counter is per GENERATOR — one process, one Worker isolate. Two ids minted in the same
|
|
29
|
-
* millisecond by two isolates tie only if their independently seeded counters collide (~1/256), and
|
|
30
|
-
* the 62 random bits break that; within one isolate the order is exact. That is the whole promise:
|
|
31
|
-
* strictly increasing per generator, chronological to the millisecond across generators.
|
|
32
|
-
*
|
|
33
|
-
* No dependency, by design — `@fabrika/control` and `@fabrika/platform-node` carry deliberate copies
|
|
34
|
-
* of this same algorithm because neither may depend on this package (see the note in each).
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
/** `rand_a` is 12 bits, all of it counter. */
|
|
38
|
-
const COUNTER_MAX = 0xfff
|
|
39
|
-
/** Seed from 8 random bits only — the leading 4 stay zero so a millisecond has room to count. */
|
|
40
|
-
const COUNTER_SEED_MASK = 0xff
|
|
41
|
-
|
|
42
|
-
/** Effective millisecond of the last id. NEVER decreases; a backwards clock is absorbed here. */
|
|
43
|
-
let lastMillis = -1
|
|
44
|
-
let counter = 0
|
|
45
|
-
|
|
46
|
-
export function uuidv7(): string {
|
|
47
|
-
const bytes = new Uint8Array(16)
|
|
48
|
-
|
|
49
|
-
// Fill bytes 6..15 with randomness; the timestamp, version, variant and counter overwrite the
|
|
50
|
-
// fields they own below.
|
|
51
|
-
const random = new Uint8Array(10)
|
|
52
|
-
crypto.getRandomValues(random)
|
|
53
|
-
bytes.set(random, 6)
|
|
54
|
-
|
|
55
|
-
// Byte 7 is read as the counter seed BEFORE the counter overwrites it, so the seed costs no extra
|
|
56
|
-
// randomness.
|
|
57
|
-
const seed = bytes[7]! & COUNTER_SEED_MASK
|
|
58
|
-
const millis = Date.now()
|
|
59
|
-
if (millis > lastMillis) {
|
|
60
|
-
lastMillis = millis
|
|
61
|
-
counter = seed
|
|
62
|
-
} else if (counter < COUNTER_MAX) {
|
|
63
|
-
// Same millisecond, or a clock that went backwards: hold the effective timestamp, count on.
|
|
64
|
-
counter += 1
|
|
65
|
-
} else {
|
|
66
|
-
// Exhausted — borrow a millisecond from the future rather than wrap back under the last id.
|
|
67
|
-
lastMillis += 1
|
|
68
|
-
counter = seed
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 48-bit big-endian Unix-millis timestamp in the first 6 bytes. It fits in 48 bits (well under
|
|
72
|
-
// 2^53), so the high bits are zero.
|
|
73
|
-
bytes[0] = Math.floor(lastMillis / 0x10000000000) & 0xff
|
|
74
|
-
bytes[1] = Math.floor(lastMillis / 0x100000000) & 0xff
|
|
75
|
-
bytes[2] = Math.floor(lastMillis / 0x1000000) & 0xff
|
|
76
|
-
bytes[3] = Math.floor(lastMillis / 0x10000) & 0xff
|
|
77
|
-
bytes[4] = Math.floor(lastMillis / 0x100) & 0xff
|
|
78
|
-
bytes[5] = lastMillis & 0xff
|
|
79
|
-
|
|
80
|
-
// Version: high nibble of byte 6 = 0b0111 (7); the counter takes the 12 bits of `rand_a` below it.
|
|
81
|
-
bytes[6] = 0x70 | (counter >>> 8)
|
|
82
|
-
bytes[7] = counter & 0xff
|
|
83
|
-
// Variant: high two bits of byte 8 = 0b10.
|
|
84
|
-
bytes[8] = (bytes[8]! & 0x3f) | 0x80
|
|
85
|
-
|
|
86
|
-
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0'))
|
|
87
|
-
|
|
88
|
-
return `${hex[0]}${hex[1]}${hex[2]}${hex[3]}-${hex[4]}${hex[5]}-${hex[6]}${hex[7]}-${hex[8]}${hex[9]}-${hex[10]}${hex[11]}${hex[12]}${hex[13]}${
|
|
89
|
-
hex[14]
|
|
90
|
-
}${hex[15]}`
|
|
91
|
-
}
|