@crombolo/data 0.67.0 → 0.68.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/data",
3
- "version": "0.67.0",
3
+ "version": "0.68.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -29,7 +29,7 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@crombolo/core": "0.67.0",
32
+ "@crombolo/core": "0.68.0",
33
33
  "kysely": "0.29.6"
34
34
  }
35
35
  }
package/src/audit.ts CHANGED
@@ -51,9 +51,17 @@ export interface AuditEntryInput {
51
51
  ipAddress?: string | null
52
52
  forwardedFor?: string | null
53
53
  detail?: string | null
54
+ /** Who acted for the user: the host's tool an exchanged token names (story 366). */
55
+ actor?: AuditActor | null
54
56
  occurredAt?: Date
55
57
  }
56
58
 
59
+ /** The tool acting for a person, by the id its token gave and the name the site knows it by. */
60
+ export interface AuditActor {
61
+ id: string
62
+ name: string | null
63
+ }
64
+
57
65
  export interface AuditEntry {
58
66
  id: number
59
67
  occurredAt: Date
@@ -68,6 +76,7 @@ export interface AuditEntry {
68
76
  ipAddress: string | null
69
77
  forwardedFor: string | null
70
78
  detail: string | null
79
+ actor: AuditActor | null
71
80
  }
72
81
 
73
82
  export interface AuditFilter {
@@ -117,6 +126,7 @@ export function auditRecord(entry: AuditEntry) {
117
126
  ipAddress: entry.ipAddress,
118
127
  forwardedFor: entry.forwardedFor,
119
128
  detail: entry.detail,
129
+ actor: entry.actor,
120
130
  }
121
131
  }
122
132
 
@@ -140,6 +150,7 @@ const LIMITS = {
140
150
  ipAddress: 64,
141
151
  forwardedFor: 255,
142
152
  detail: 4000,
153
+ actor: 255,
143
154
  } as const
144
155
 
145
156
  type Condition = (eb: ExpressionBuilder<CoreTables, 'audit_entry'>) => Expression<SqlBool>
@@ -152,6 +163,11 @@ function clip(value: string | null | undefined, limit: number): string | null {
152
163
  return value.length > limit ? value.slice(0, limit) : value
153
164
  }
154
165
 
166
+ const actorColumns = (actor: AuditActor | null) => ({
167
+ actor_id: clip(actor?.id, LIMITS.actor),
168
+ actor_name: clip(actor?.name, LIMITS.actor),
169
+ })
170
+
155
171
  function text(value: unknown): string | null {
156
172
  return value === null || value === undefined ? null : String(value)
157
173
  }
@@ -171,6 +187,7 @@ function toEntry(row: Record<string, unknown>): AuditEntry {
171
187
  ipAddress: text(row.ip_address),
172
188
  forwardedFor: text(row.forwarded_for),
173
189
  detail: text(row.detail),
190
+ actor: row.actor_id ? { id: String(row.actor_id), name: text(row.actor_name) } : null,
174
191
  }
175
192
  }
176
193
 
@@ -184,7 +201,7 @@ export interface AuditContext {
184
201
  /** A person acting through one of their command-line tokens (story 351), by its name. */
185
202
  token?: { userKey: string; name: string }
186
203
  /** A person acting through a token exchanged at the remote (story 364), by the provider's name. */
187
- exchange?: { userKey: string; provider: string }
204
+ exchange?: { userKey: string; provider: string; actor?: AuditActor }
188
205
  }
189
206
 
190
207
  const contexts = new AsyncLocalStorage<AuditContext>()
@@ -220,7 +237,7 @@ export function detailInContext(
220
237
  context.viewing?.userKey === who ? `while viewing as ${context.viewing.groups}` : undefined,
221
238
  context.token?.userKey === who ? `through the token ${context.token.name}` : undefined,
222
239
  context.exchange?.userKey === who
223
- ? `signed in through ${context.exchange.provider}`
240
+ ? `signed in through ${context.exchange.provider}${byActor(context.exchange.actor)}`
224
241
  : undefined,
225
242
  ].filter((note): note is string => note !== undefined)
226
243
  if (notes.length === 0) return entry.detail
@@ -228,6 +245,21 @@ export function detailInContext(
228
245
  return entry.detail ? `${entry.detail}, ${note}` : `${note[0]?.toUpperCase()}${note.slice(1)}`
229
246
  }
230
247
 
248
+ /** ", by Acme console": who acted for the person, by name or else by id. */
249
+ export const byActor = (actor: AuditActor | null | undefined): string =>
250
+ actor ? `, by ${actor.name ?? actor.id}` : ''
251
+
252
+ /** The actor an entry is written with: its own, or the request's for the same person. */
253
+ function actorInContext(
254
+ entry: Pick<AuditEntryInput, 'userKey' | 'actor'>,
255
+ context = currentAuditContext(),
256
+ ): AuditActor | null {
257
+ if (entry.actor) return entry.actor
258
+ const actor = context?.exchange?.actor
259
+ if (!actor || !entry.userKey || !UUID.test(entry.userKey)) return null
260
+ return context?.exchange?.userKey === normaliseUuid(entry.userKey) ? actor : null
261
+ }
262
+
231
263
  export class AuditLog {
232
264
  #db: Db
233
265
 
@@ -259,6 +291,7 @@ export class AuditLog {
259
291
  ip_address: clip(entry.ipAddress, LIMITS.ipAddress),
260
292
  forwarded_for: clip(entry.forwardedFor, LIMITS.forwardedFor),
261
293
  detail: clip(detailInContext(entry), LIMITS.detail),
294
+ ...actorColumns(actorInContext(entry)),
262
295
  })
263
296
  .execute()
264
297
  }
package/src/index.ts CHANGED
@@ -90,6 +90,7 @@ export * from './schema/delivery-member-tokens.ts'
90
90
  export * from './schema/diagnostics.ts'
91
91
  export * from './schema/drop-assistant.ts'
92
92
  export * from './schema/elements.ts'
93
+ export * from './schema/exchange-actor.ts'
93
94
  export * from './schema/extension-activity.ts'
94
95
  export * from './schema/extension-settings.ts'
95
96
  export * from './schema/form-library.ts'
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Migration 080 — the actor an exchanged token names is in the history (story
3
+ * 366). A device signed in by exchange keeps the actor its token named, the
4
+ * host's tool acting for the person, and every audit entry can carry the actor
5
+ * it was written for.
6
+ */
7
+ import type { Db } from '../database.ts'
8
+ import type { Migration } from '../migrations.ts'
9
+ import { sql } from '../query.ts'
10
+ import { STATE_REMOTE_EXCHANGE } from './remote-exchange.ts'
11
+
12
+ export const STATE_EXCHANGE_ACTOR = '{3b8e1f60-0080-4c2d-9a7e-1d4f6b2c0080}'
13
+
14
+ export const exchangeActorMigration: Migration = {
15
+ from: STATE_REMOTE_EXCHANGE,
16
+ to: STATE_EXCHANGE_ACTOR,
17
+ name: 'AddExchangeActor',
18
+ kind: 'expand',
19
+ release: '0.68.0',
20
+ async up(db: Db) {
21
+ const t = db.dialect.types
22
+ for (const table of ['device_authorization', 'audit_entry'] as const) {
23
+ await db.q.schema
24
+ .alterTable(table)
25
+ .addColumn('actor_id', sql.raw(t.varchar(255)))
26
+ .execute()
27
+ await db.q.schema
28
+ .alterTable(table)
29
+ .addColumn('actor_name', sql.raw(t.varchar(255)))
30
+ .execute()
31
+ }
32
+ },
33
+ }
@@ -42,6 +42,7 @@ import { deliveryMemberTokensMigration } from './delivery-member-tokens.ts'
42
42
  import { diagnosticsMigration } from './diagnostics.ts'
43
43
  import { dropAssistantMigration } from './drop-assistant.ts'
44
44
  import { elementsMigration } from './elements.ts'
45
+ import { exchangeActorMigration } from './exchange-actor.ts'
45
46
  import { extensionActivityMigration } from './extension-activity.ts'
46
47
  import { extensionSettingsMigration } from './extension-settings.ts'
47
48
  import { formLibraryMigration } from './form-library.ts'
@@ -173,4 +174,5 @@ export const cromboloPlan = new MigrationPlan([
173
174
  sessionViewAsMigration,
174
175
  cliTokensMigration,
175
176
  remoteExchangeMigration,
177
+ exchangeActorMigration,
176
178
  ])
package/src/tables.gen.ts CHANGED
@@ -51,6 +51,8 @@ export interface CoreTables {
51
51
  }
52
52
  audit_entry: {
53
53
  action: string
54
+ actor_id: string | null
55
+ actor_name: string | null
54
56
  category: string
55
57
  detail: string | null
56
58
  entity_key: string | null
@@ -330,6 +332,8 @@ export interface CoreTables {
330
332
  scopes: string
331
333
  }
332
334
  device_authorization: {
335
+ actor_id: string | null
336
+ actor_name: string | null
333
337
  client_id: string
334
338
  client_name: string | null
335
339
  create_date: Timestamp