@crombolo/data 0.68.0 → 0.69.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/data",
3
- "version": "0.68.0",
3
+ "version": "0.69.1",
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.68.0",
32
+ "@crombolo/core": "0.69.1",
33
33
  "kysely": "0.29.6"
34
34
  }
35
35
  }
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'
@@ -124,6 +124,7 @@ export * from './schema/search-removals.ts'
124
124
  export * from './schema/seed.ts'
125
125
  export * from './schema/session-view-as.ts'
126
126
  export * from './schema/since-version.ts'
127
+ export * from './schema/single-use-token.ts'
127
128
  export * from './schema/site-search.ts'
128
129
  export * from './schema/site-settings-types.ts'
129
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
  }
@@ -78,6 +78,7 @@ import { searchRemovalsMigration } from './search-removals.ts'
78
78
  import { serverRoleMigration } from './server-role.ts'
79
79
  import { sessionViewAsMigration } from './session-view-as.ts'
80
80
  import { sinceVersionMigration } from './since-version.ts'
81
+ import { singleUseTokenMigration } from './single-use-token.ts'
81
82
  import { siteSearchMigration } from './site-search.ts'
82
83
  import { siteSettingsTypesMigration } from './site-settings-types.ts'
83
84
  import { transferAnswersMigration } from './transfer-answers.ts'
@@ -175,4 +176,5 @@ export const cromboloPlan = new MigrationPlan([
175
176
  cliTokensMigration,
176
177
  remoteExchangeMigration,
177
178
  exchangeActorMigration,
179
+ singleUseTokenMigration,
178
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
@@ -681,12 +681,6 @@ export interface CoreTables {
681
681
  rule_key: string
682
682
  token_hash: string
683
683
  }
684
- remote_exchange_id: {
685
- expires_at: Timestamp
686
- id: Generated<number>
687
- issuer: string
688
- token_id: string
689
- }
690
684
  remote_job: {
691
685
  create_date: Timestamp
692
686
  decided_at: Timestamp | null
@@ -765,6 +759,12 @@ export interface CoreTables {
765
759
  served_checkpoint: BigInteger | null
766
760
  version: string | null
767
761
  }
762
+ single_use_token: {
763
+ expires_at: Timestamp
764
+ id: Generated<number>
765
+ issuer: string
766
+ token_id: string
767
+ }
768
768
  site_search_document: {
769
769
  culture: string
770
770
  high: string