@crombolo/data 0.67.0 → 0.69.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.69.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.69.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
@@ -47,9 +47,9 @@ export * from './repositories/notifications.ts'
47
47
  export * from './repositories/public-access.ts'
48
48
  export * from './repositories/redirects.ts'
49
49
  export * from './repositories/references.ts'
50
- export * from './repositories/remote-exchange-ids.ts'
51
50
  export * from './repositories/remote-jobs.ts'
52
51
  export * from './repositories/remote-trust.ts'
52
+ export * from './repositories/single-use-tokens.ts'
53
53
  export * from './repositories/tags.ts'
54
54
  export * from './repositories/templates.ts'
55
55
  export * from './repositories/transfer-answers.ts'
@@ -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'
@@ -123,6 +124,7 @@ export * from './schema/search-removals.ts'
123
124
  export * from './schema/seed.ts'
124
125
  export * from './schema/session-view-as.ts'
125
126
  export * from './schema/since-version.ts'
127
+ export * from './schema/single-use-token.ts'
126
128
  export * from './schema/site-search.ts'
127
129
  export * from './schema/site-settings-types.ts'
128
130
  export * from './schema/transfer-answers.ts'
@@ -1,31 +1,26 @@
1
1
  /**
2
- * The one-time ids of tokens a provider issued for the remote's exchange
3
- * (story 364), each kept until its token expires, so no token is exchanged
4
- * twice.
2
+ * Tokens that may be used once: the remote's exchanged provider tokens (story
3
+ * 364) and preview links (story 368), by issuer and id, each kept until its
4
+ * token expires so none is used twice.
5
5
  */
6
6
  import type { Db } from '../database.ts'
7
7
  import { DbDate } from '../dialect.ts'
8
8
 
9
- export class RemoteExchangeIds {
9
+ export class SingleUseTokens {
10
10
  #db: Db
11
11
 
12
12
  constructor(db: Db) {
13
13
  this.#db = db
14
14
  }
15
15
 
16
- /** Spends an id; false when it was spent before. Sweeps the ids whose tokens have expired. */
17
- async spend(
18
- issuer: string,
19
- tokenId: string,
20
- expiresAt: Date,
21
- now = new Date(),
22
- ): Promise<boolean> {
16
+ /** Uses a token's id; false when it was used before. Sweeps the ids whose tokens have expired. */
17
+ async use(issuer: string, tokenId: string, expiresAt: Date, now = new Date()): Promise<boolean> {
23
18
  await this.#db.q
24
- .deleteFrom('remote_exchange_id')
19
+ .deleteFrom('single_use_token')
25
20
  .where('expires_at', '<', DbDate.toDb(now))
26
21
  .execute()
27
22
  const seen = await this.#db.q
28
- .selectFrom('remote_exchange_id')
23
+ .selectFrom('single_use_token')
29
24
  .select('id')
30
25
  .where('issuer', '=', issuer)
31
26
  .where('token_id', '=', tokenId)
@@ -33,12 +28,12 @@ export class RemoteExchangeIds {
33
28
  if (seen) return false
34
29
  try {
35
30
  await this.#db.q
36
- .insertInto('remote_exchange_id')
31
+ .insertInto('single_use_token')
37
32
  .values({ issuer, token_id: tokenId, expires_at: DbDate.toDb(expiresAt) })
38
33
  .execute()
39
34
  return true
40
35
  } catch {
41
- // Two exchanges of one token at once: the unique index lets one in.
36
+ // Two uses of one token at once: the unique index lets one in.
42
37
  return false
43
38
  }
44
39
  }
@@ -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'
@@ -77,6 +78,7 @@ import { searchRemovalsMigration } from './search-removals.ts'
77
78
  import { serverRoleMigration } from './server-role.ts'
78
79
  import { sessionViewAsMigration } from './session-view-as.ts'
79
80
  import { sinceVersionMigration } from './since-version.ts'
81
+ import { singleUseTokenMigration } from './single-use-token.ts'
80
82
  import { siteSearchMigration } from './site-search.ts'
81
83
  import { siteSettingsTypesMigration } from './site-settings-types.ts'
82
84
  import { transferAnswersMigration } from './transfer-answers.ts'
@@ -173,4 +175,6 @@ export const cromboloPlan = new MigrationPlan([
173
175
  sessionViewAsMigration,
174
176
  cliTokensMigration,
175
177
  remoteExchangeMigration,
178
+ exchangeActorMigration,
179
+ singleUseTokenMigration,
176
180
  ])
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Migration 081 — single-use tokens (story 368). 364's `remote_exchange_id`
3
+ * becomes `single_use_token`: the ids of tokens that may be used once, kept
4
+ * until each expires, for the remote's exchange and for preview links alike.
5
+ */
6
+ import type { Db } from '../database.ts'
7
+ import type { Migration } from '../migrations.ts'
8
+ import { STATE_EXCHANGE_ACTOR } from './exchange-actor.ts'
9
+
10
+ export const STATE_SINGLE_USE_TOKEN = '{5c9a2e71-0081-4d3b-8f6a-2e7b1c4d0081}'
11
+
12
+ export const singleUseTokenMigration: Migration = {
13
+ from: STATE_EXCHANGE_ACTOR,
14
+ to: STATE_SINGLE_USE_TOKEN,
15
+ name: 'RenameSingleUseToken',
16
+ kind: 'expand',
17
+ release: '0.69.0',
18
+ async up(db: Db) {
19
+ await db.q.schema.dropIndex('ux_remote_exchange_id').execute()
20
+ await db.q.schema.alterTable('remote_exchange_id').renameTo('single_use_token').execute()
21
+ await db.q.schema
22
+ .createIndex('ux_single_use_token')
23
+ .unique()
24
+ .on('single_use_token')
25
+ .columns(['issuer', 'token_id'])
26
+ .execute()
27
+ },
28
+ }
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
@@ -677,12 +681,6 @@ export interface CoreTables {
677
681
  rule_key: string
678
682
  token_hash: string
679
683
  }
680
- remote_exchange_id: {
681
- expires_at: Timestamp
682
- id: Generated<number>
683
- issuer: string
684
- token_id: string
685
- }
686
684
  remote_job: {
687
685
  create_date: Timestamp
688
686
  decided_at: Timestamp | null
@@ -761,6 +759,12 @@ export interface CoreTables {
761
759
  served_checkpoint: BigInteger | null
762
760
  version: string | null
763
761
  }
762
+ single_use_token: {
763
+ expires_at: Timestamp
764
+ id: Generated<number>
765
+ issuer: string
766
+ token_id: string
767
+ }
764
768
  site_search_document: {
765
769
  culture: string
766
770
  high: string