@crombolo/data 0.65.1 → 0.67.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 +2 -2
- package/src/audit.ts +32 -7
- package/src/content-query.ts +21 -6
- package/src/index.ts +3 -0
- package/src/repositories/documents.ts +3 -1
- package/src/repositories/external-logins.ts +51 -1
- package/src/repositories/jobs.ts +14 -0
- package/src/repositories/members.ts +21 -2
- package/src/repositories/remote-exchange-ids.ts +45 -0
- package/src/schema/cli-tokens.ts +46 -0
- package/src/schema/plan.ts +4 -0
- package/src/schema/remote-exchange.ts +50 -0
- package/src/schema/seed.ts +8 -1
- package/src/tables.gen.ts +20 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crombolo/data",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.67.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.
|
|
32
|
+
"@crombolo/core": "0.67.0",
|
|
33
33
|
"kysely": "0.29.6"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/src/audit.ts
CHANGED
|
@@ -181,6 +181,10 @@ function toEntry(row: Record<string, unknown>): AuditEntry {
|
|
|
181
181
|
*/
|
|
182
182
|
export interface AuditContext {
|
|
183
183
|
viewing?: { userKey: string; groups: string }
|
|
184
|
+
/** A person acting through one of their command-line tokens (story 351), by its name. */
|
|
185
|
+
token?: { userKey: string; name: string }
|
|
186
|
+
/** A person acting through a token exchanged at the remote (story 364), by the provider's name. */
|
|
187
|
+
exchange?: { userKey: string; provider: string }
|
|
184
188
|
}
|
|
185
189
|
|
|
186
190
|
const contexts = new AsyncLocalStorage<AuditContext>()
|
|
@@ -188,18 +192,39 @@ const contexts = new AsyncLocalStorage<AuditContext>()
|
|
|
188
192
|
/** Runs `work` with an audit context of its own, which it fills in as it learns who is asking. */
|
|
189
193
|
export const withAuditContext = <T>(work: () => T): T => contexts.run({}, work)
|
|
190
194
|
|
|
191
|
-
|
|
192
|
-
export const currentAuditContext = (): AuditContext | undefined => contexts.getStore()
|
|
195
|
+
let processContext: AuditContext | undefined
|
|
193
196
|
|
|
194
|
-
/**
|
|
197
|
+
/**
|
|
198
|
+
* The context for a whole process that answers no requests, such as one
|
|
199
|
+
* command (story 351); a request's own context always comes first.
|
|
200
|
+
*/
|
|
201
|
+
export const setProcessAuditContext = (context: AuditContext | undefined): void => {
|
|
202
|
+
processContext = context
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The context of the request being answered, or of the process; undefined outside both. */
|
|
206
|
+
export const currentAuditContext = (): AuditContext | undefined =>
|
|
207
|
+
contexts.getStore() ?? processContext
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The detail an entry is written with: "while viewing as Editors", or "through
|
|
211
|
+
* the token deploy", added to the person's own.
|
|
212
|
+
*/
|
|
195
213
|
export function detailInContext(
|
|
196
214
|
entry: Pick<AuditEntryInput, 'userKey' | 'detail'>,
|
|
197
215
|
context = currentAuditContext(),
|
|
198
216
|
): string | null | undefined {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
217
|
+
if (!context || !entry.userKey || !UUID.test(entry.userKey)) return entry.detail
|
|
218
|
+
const who = normaliseUuid(entry.userKey)
|
|
219
|
+
const notes = [
|
|
220
|
+
context.viewing?.userKey === who ? `while viewing as ${context.viewing.groups}` : undefined,
|
|
221
|
+
context.token?.userKey === who ? `through the token ${context.token.name}` : undefined,
|
|
222
|
+
context.exchange?.userKey === who
|
|
223
|
+
? `signed in through ${context.exchange.provider}`
|
|
224
|
+
: undefined,
|
|
225
|
+
].filter((note): note is string => note !== undefined)
|
|
226
|
+
if (notes.length === 0) return entry.detail
|
|
227
|
+
const note = notes.join(', ')
|
|
203
228
|
return entry.detail ? `${entry.detail}, ${note}` : `${note[0]?.toUpperCase()}${note.slice(1)}`
|
|
204
229
|
}
|
|
205
230
|
|
package/src/content-query.ts
CHANGED
|
@@ -274,22 +274,34 @@ interface PropertyTarget {
|
|
|
274
274
|
storage: ValueStorageTypeName
|
|
275
275
|
}
|
|
276
276
|
|
|
277
|
-
/**
|
|
277
|
+
/**
|
|
278
|
+
* The documents a query matches, as the scope may see them; with `library`, the
|
|
279
|
+
* Library's elements instead (story 361), placed under its folders.
|
|
280
|
+
*/
|
|
278
281
|
export async function queryDocuments(
|
|
279
282
|
db: Db,
|
|
280
283
|
query: ContentQuery,
|
|
281
284
|
scope: QueryScope,
|
|
285
|
+
options: { library?: boolean } = {},
|
|
282
286
|
): Promise<QueryResult> {
|
|
283
287
|
const run = async (tx: Db) => {
|
|
284
288
|
if (tx.dialect.name === 'postgres')
|
|
285
289
|
await tx.exec(`SET LOCAL statement_timeout = ${QUERY_TIMEOUT_MS}`)
|
|
286
|
-
return runQuery(tx, query, scope)
|
|
290
|
+
return runQuery(tx, query, scope, options.library === true)
|
|
287
291
|
}
|
|
288
292
|
// Postgres's timeout is per transaction; SQLite has none, so the window bounds it.
|
|
289
293
|
return db.dialect.name === 'postgres' ? db.transaction(run) : run(db)
|
|
290
294
|
}
|
|
291
295
|
|
|
292
|
-
async function runQuery(
|
|
296
|
+
async function runQuery(
|
|
297
|
+
db: Db,
|
|
298
|
+
query: ContentQuery,
|
|
299
|
+
scope: QueryScope,
|
|
300
|
+
library: boolean,
|
|
301
|
+
): Promise<QueryResult> {
|
|
302
|
+
const objectType = library ? ObjectTypes.Element : ObjectTypes.Document
|
|
303
|
+
/** What `under` and `parent` may name: a page, or an element or its folder. */
|
|
304
|
+
const places = library ? [ObjectTypes.Element, ObjectTypes.ElementContainer] : [objectType]
|
|
293
305
|
const bool = (value: boolean) => db.dialect.boolValue(value) as boolean
|
|
294
306
|
|
|
295
307
|
// ---- culture
|
|
@@ -326,7 +338,7 @@ async function runQuery(db: Db, query: ContentQuery, scope: QueryScope): Promise
|
|
|
326
338
|
join.onRef('dcv.node_id', '=', 'n.id').on('dcv.language_id', '=', languageId as number),
|
|
327
339
|
),
|
|
328
340
|
)
|
|
329
|
-
.where('n.node_object_type', '=',
|
|
341
|
+
.where('n.node_object_type', '=', objectType)
|
|
330
342
|
|
|
331
343
|
// ---- types
|
|
332
344
|
let typeIds: number[] | undefined
|
|
@@ -356,9 +368,12 @@ async function runQuery(db: Db, query: ContentQuery, scope: QueryScope): Promise
|
|
|
356
368
|
.selectFrom('node')
|
|
357
369
|
.select(['id', 'path', 'level'])
|
|
358
370
|
.where('unique_id', '=', key.toLowerCase())
|
|
359
|
-
.where('node_object_type', '
|
|
371
|
+
.where('node_object_type', 'in', places)
|
|
360
372
|
.executeTakeFirst()
|
|
361
|
-
if (!row)
|
|
373
|
+
if (!row)
|
|
374
|
+
throw new ContentQueryRefused(
|
|
375
|
+
`"${field}" names no ${library ? 'Library folder' : 'page'}: ${key}.`,
|
|
376
|
+
)
|
|
362
377
|
return { id: Number(row.id), path: String(row.path), level: Number(row.level) }
|
|
363
378
|
}
|
|
364
379
|
const under = query.under ? await nodeByKey(query.under, 'under') : undefined
|
package/src/index.ts
CHANGED
|
@@ -47,6 +47,7 @@ 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'
|
|
50
51
|
export * from './repositories/remote-jobs.ts'
|
|
51
52
|
export * from './repositories/remote-trust.ts'
|
|
52
53
|
export * from './repositories/tags.ts'
|
|
@@ -75,6 +76,7 @@ export * from './schema/bundles-disabled.ts'
|
|
|
75
76
|
export * from './schema/bundles-in-settings.ts'
|
|
76
77
|
export * from './schema/bundles-rename.ts'
|
|
77
78
|
export * from './schema/change-report.ts'
|
|
79
|
+
export * from './schema/cli-tokens.ts'
|
|
78
80
|
export * from './schema/client-id-prefix.ts'
|
|
79
81
|
export * from './schema/content.ts'
|
|
80
82
|
export * from './schema/content-applied.ts'
|
|
@@ -113,6 +115,7 @@ export * from './schema/pruned-permissions.ts'
|
|
|
113
115
|
export * from './schema/public-access.ts'
|
|
114
116
|
export * from './schema/recycle-bin.ts'
|
|
115
117
|
export * from './schema/remote.ts'
|
|
118
|
+
export * from './schema/remote-exchange.ts'
|
|
116
119
|
export * from './schema/remote-trust.ts'
|
|
117
120
|
export * from './schema/rich-text-ui.ts'
|
|
118
121
|
export * from './schema/search-index.ts'
|
|
@@ -1566,7 +1566,9 @@ export class DocumentRepository {
|
|
|
1566
1566
|
if (!source) return undefined
|
|
1567
1567
|
if (targetKey) {
|
|
1568
1568
|
const target = await this.#nodes.byKey(targetKey)
|
|
1569
|
-
|
|
1569
|
+
const container = KINDS[this.kind].container
|
|
1570
|
+
if (!target || (target.objectType !== this.objectType && target.objectType !== container))
|
|
1571
|
+
return undefined
|
|
1570
1572
|
}
|
|
1571
1573
|
const copyOne = async (from: DocumentAggregate, parentKey: string | null): Promise<string> => {
|
|
1572
1574
|
const siblings = await this.#siblingNames(parentKey)
|
|
@@ -22,13 +22,27 @@ export interface ExternalLogin {
|
|
|
22
22
|
loginProvider: string
|
|
23
23
|
/** The provider's id for the account: its `sub`. */
|
|
24
24
|
providerKey: string
|
|
25
|
+
/**
|
|
26
|
+
* What names the person across the provider's apps (`oid` for Entra), and
|
|
27
|
+
* the issuer it came from (story 364): set at each sign-in since 0.67.0.
|
|
28
|
+
*/
|
|
29
|
+
identity: ProviderIdentity | undefined
|
|
25
30
|
createDate: Date
|
|
26
31
|
}
|
|
27
32
|
|
|
33
|
+
export interface ProviderIdentity {
|
|
34
|
+
value: string
|
|
35
|
+
issuer: string
|
|
36
|
+
}
|
|
37
|
+
|
|
28
38
|
const toLogin = (row: Record<string, unknown>): ExternalLogin => ({
|
|
29
39
|
ownerKey: normaliseUuid(String(row.user_or_member_key)),
|
|
30
40
|
loginProvider: String(row.login_provider),
|
|
31
41
|
providerKey: String(row.provider_key),
|
|
42
|
+
identity:
|
|
43
|
+
typeof row.identity_value === 'string' && typeof row.identity_issuer === 'string'
|
|
44
|
+
? { value: row.identity_value, issuer: row.identity_issuer }
|
|
45
|
+
: undefined,
|
|
32
46
|
createDate: DbDate.fromDb(row.create_date) ?? new Date(0),
|
|
33
47
|
})
|
|
34
48
|
|
|
@@ -50,6 +64,35 @@ export class ExternalLoginRepository {
|
|
|
50
64
|
return row ? toLogin(row) : undefined
|
|
51
65
|
}
|
|
52
66
|
|
|
67
|
+
/** Who the person a provider's identifying claim names is linked to, from that issuer only. */
|
|
68
|
+
async findByIdentity(
|
|
69
|
+
loginProvider: string,
|
|
70
|
+
identity: ProviderIdentity,
|
|
71
|
+
): Promise<ExternalLogin | undefined> {
|
|
72
|
+
const row = await this.#db.q
|
|
73
|
+
.selectFrom('external_login')
|
|
74
|
+
.selectAll()
|
|
75
|
+
.where('login_provider', '=', loginProvider)
|
|
76
|
+
.where('identity_value', '=', identity.value)
|
|
77
|
+
.where('identity_issuer', '=', identity.issuer)
|
|
78
|
+
.executeTakeFirst()
|
|
79
|
+
return row ? toLogin(row) : undefined
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Records what names the person at the provider on their link, as a sign-in reads it. */
|
|
83
|
+
async recordIdentity(
|
|
84
|
+
loginProvider: string,
|
|
85
|
+
providerKey: string,
|
|
86
|
+
identity: ProviderIdentity | undefined,
|
|
87
|
+
): Promise<void> {
|
|
88
|
+
await this.#db.q
|
|
89
|
+
.updateTable('external_login')
|
|
90
|
+
.set({ identity_value: identity?.value ?? null, identity_issuer: identity?.issuer ?? null })
|
|
91
|
+
.where('login_provider', '=', loginProvider)
|
|
92
|
+
.where('provider_key', '=', providerKey)
|
|
93
|
+
.execute()
|
|
94
|
+
}
|
|
95
|
+
|
|
53
96
|
/** Every provider account linked to a member or user. */
|
|
54
97
|
async forOwner(ownerKey: string): Promise<ExternalLogin[]> {
|
|
55
98
|
const rows = await this.#db.q
|
|
@@ -62,7 +105,12 @@ export class ExternalLoginRepository {
|
|
|
62
105
|
}
|
|
63
106
|
|
|
64
107
|
/** Links a provider account to a member or user, replacing an earlier link to that provider. */
|
|
65
|
-
async link(
|
|
108
|
+
async link(
|
|
109
|
+
ownerKey: string,
|
|
110
|
+
loginProvider: string,
|
|
111
|
+
providerKey: string,
|
|
112
|
+
identity?: ProviderIdentity,
|
|
113
|
+
): Promise<void> {
|
|
66
114
|
await this.#db.transaction(async (tx) => {
|
|
67
115
|
await removeLink(tx, ownerKey, loginProvider)
|
|
68
116
|
await tx.q
|
|
@@ -71,6 +119,8 @@ export class ExternalLoginRepository {
|
|
|
71
119
|
user_or_member_key: normaliseUuid(ownerKey),
|
|
72
120
|
login_provider: loginProvider,
|
|
73
121
|
provider_key: providerKey,
|
|
122
|
+
identity_value: identity?.value ?? null,
|
|
123
|
+
identity_issuer: identity?.issuer ?? null,
|
|
74
124
|
create_date: DbDate.toDb(new Date()),
|
|
75
125
|
})
|
|
76
126
|
.execute()
|
package/src/repositories/jobs.ts
CHANGED
|
@@ -111,6 +111,20 @@ export class JobRepository {
|
|
|
111
111
|
return row ? rowOf(row) : undefined
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
/** The jobs whose id starts with `prefix`, at most `take` of them, newest first. */
|
|
115
|
+
async byPrefix(prefix: string, take = 10): Promise<JobRow[]> {
|
|
116
|
+
const wanted = prefix.toLowerCase().replace(/[^0-9a-f-]/g, '')
|
|
117
|
+
if (!wanted) return []
|
|
118
|
+
const rows = await this.db.q
|
|
119
|
+
.selectFrom('job')
|
|
120
|
+
.selectAll()
|
|
121
|
+
.where((eb) => eb(eb.cast('id', 'text'), 'like', `${wanted}%`))
|
|
122
|
+
.orderBy('created_at', 'desc')
|
|
123
|
+
.limit(take)
|
|
124
|
+
.execute()
|
|
125
|
+
return rows.map(rowOf)
|
|
126
|
+
}
|
|
127
|
+
|
|
114
128
|
/** The newest first, for the administrators' view; `owner` and `state` narrow it. */
|
|
115
129
|
async list(
|
|
116
130
|
options: { owner?: string; state?: JobState; skip?: number; take?: number } = {},
|
|
@@ -25,6 +25,8 @@ import {
|
|
|
25
25
|
} from './documents.ts'
|
|
26
26
|
import { NodeRepository } from './nodes.ts'
|
|
27
27
|
|
|
28
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
29
|
+
|
|
28
30
|
/** The sign-in facet beside a member's content. */
|
|
29
31
|
export interface MemberIdentity {
|
|
30
32
|
email: string
|
|
@@ -209,6 +211,23 @@ export class MemberRepository {
|
|
|
209
211
|
return node ? this.#groupKeysOf(node.id) : []
|
|
210
212
|
}
|
|
211
213
|
|
|
214
|
+
/** Those of `keys` that name no member group. */
|
|
215
|
+
async notGroups(keys: readonly string[]): Promise<string[]> {
|
|
216
|
+
const missing: string[] = []
|
|
217
|
+
for (const key of keys) if (!(await this.#group(key))) missing.push(key)
|
|
218
|
+
return missing
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* The member group a key names. Anything not shaped as a key names none, as
|
|
223
|
+
* Postgres's uuid column would refuse it.
|
|
224
|
+
*/
|
|
225
|
+
async #group(key: string) {
|
|
226
|
+
if (!UUID.test(key)) return undefined
|
|
227
|
+
const node = await this.#nodes.byKey(key)
|
|
228
|
+
return node?.objectType === ObjectTypes.MemberGroup ? node : undefined
|
|
229
|
+
}
|
|
230
|
+
|
|
212
231
|
/** Whether a username or an e-mail is taken by anyone other than `exceptKey`. */
|
|
213
232
|
async taken(field: 'username' | 'email', value: string, exceptKey?: string): Promise<boolean> {
|
|
214
233
|
const column = field === 'username' ? 'm.login_name' : 'm.email'
|
|
@@ -233,8 +252,8 @@ export class MemberRepository {
|
|
|
233
252
|
async #writeGroups(nodeId: number, groupKeys: readonly string[]): Promise<void> {
|
|
234
253
|
await this.#db.q.deleteFrom('member_group_member').where('member_id', '=', nodeId).execute()
|
|
235
254
|
for (const key of groupKeys) {
|
|
236
|
-
const group = await this.#
|
|
237
|
-
if (!group
|
|
255
|
+
const group = await this.#group(key)
|
|
256
|
+
if (!group) continue
|
|
238
257
|
await this.#db.q
|
|
239
258
|
.insertInto('member_group_member')
|
|
240
259
|
.values({ member_id: nodeId, member_group_id: group.id })
|
|
@@ -0,0 +1,45 @@
|
|
|
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.
|
|
5
|
+
*/
|
|
6
|
+
import type { Db } from '../database.ts'
|
|
7
|
+
import { DbDate } from '../dialect.ts'
|
|
8
|
+
|
|
9
|
+
export class RemoteExchangeIds {
|
|
10
|
+
#db: Db
|
|
11
|
+
|
|
12
|
+
constructor(db: Db) {
|
|
13
|
+
this.#db = db
|
|
14
|
+
}
|
|
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> {
|
|
23
|
+
await this.#db.q
|
|
24
|
+
.deleteFrom('remote_exchange_id')
|
|
25
|
+
.where('expires_at', '<', DbDate.toDb(now))
|
|
26
|
+
.execute()
|
|
27
|
+
const seen = await this.#db.q
|
|
28
|
+
.selectFrom('remote_exchange_id')
|
|
29
|
+
.select('id')
|
|
30
|
+
.where('issuer', '=', issuer)
|
|
31
|
+
.where('token_id', '=', tokenId)
|
|
32
|
+
.executeTakeFirst()
|
|
33
|
+
if (seen) return false
|
|
34
|
+
try {
|
|
35
|
+
await this.#db.q
|
|
36
|
+
.insertInto('remote_exchange_id')
|
|
37
|
+
.values({ issuer, token_id: tokenId, expires_at: DbDate.toDb(expiresAt) })
|
|
38
|
+
.execute()
|
|
39
|
+
return true
|
|
40
|
+
} catch {
|
|
41
|
+
// Two exchanges of one token at once: the unique index lets one in.
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration 078 — command-line tokens (story 351): each one named, its
|
|
3
|
+
* person's, kept only as a hash, with when it expires, was last used and was
|
|
4
|
+
* revoked. A name is used once per person among those not revoked.
|
|
5
|
+
* Administrators are given `Crombolo.CliTokens.Create` as the seed gives it,
|
|
6
|
+
* through `grantBuiltInPermissions`, as migration 045 gave the media verbs.
|
|
7
|
+
*/
|
|
8
|
+
import type { Db } from '../database.ts'
|
|
9
|
+
import type { Migration } from '../migrations.ts'
|
|
10
|
+
import { sql } from '../query.ts'
|
|
11
|
+
import { grantBuiltInPermissions } from './seed.ts'
|
|
12
|
+
import { STATE_SESSION_VIEW_AS } from './session-view-as.ts'
|
|
13
|
+
|
|
14
|
+
export const STATE_CLI_TOKENS = '{3c8e1f57-0078-4d2a-b6e9-1a5f7c3d0078}'
|
|
15
|
+
|
|
16
|
+
export const cliTokensMigration: Migration = {
|
|
17
|
+
from: STATE_SESSION_VIEW_AS,
|
|
18
|
+
to: STATE_CLI_TOKENS,
|
|
19
|
+
name: 'AddCliTokens',
|
|
20
|
+
kind: 'expand',
|
|
21
|
+
release: '0.66.0',
|
|
22
|
+
async up(db: Db) {
|
|
23
|
+
const t = db.dialect.types
|
|
24
|
+
await db.q.schema
|
|
25
|
+
.createTable('cli_token')
|
|
26
|
+
.addColumn('id', sql.raw(t.identity))
|
|
27
|
+
.addColumn('key', sql.raw(t.uuid), (c) => c.notNull().unique())
|
|
28
|
+
.addColumn('user_id', sql.raw(t.integer), (c) => c.notNull())
|
|
29
|
+
.addColumn('name', sql.raw(t.varchar(100)), (c) => c.notNull())
|
|
30
|
+
.addColumn('token_hash', sql.raw(t.varchar(64)), (c) => c.notNull().unique())
|
|
31
|
+
.addColumn('created_at', sql.raw(t.timestamp), (c) => c.notNull())
|
|
32
|
+
.addColumn('expires_at', sql.raw(t.timestamp), (c) => c.notNull())
|
|
33
|
+
.addColumn('last_used_at', sql.raw(t.timestamp))
|
|
34
|
+
.addColumn('revoked_at', sql.raw(t.timestamp))
|
|
35
|
+
.addColumn('revoked_by', sql.raw(t.varchar(64)))
|
|
36
|
+
.execute()
|
|
37
|
+
await db.q.schema
|
|
38
|
+
.createIndex('ux_cli_token_name')
|
|
39
|
+
.unique()
|
|
40
|
+
.on('cli_token')
|
|
41
|
+
.columns(['user_id', 'name'])
|
|
42
|
+
.where(sql.ref('revoked_at'), 'is', null)
|
|
43
|
+
.execute()
|
|
44
|
+
await grantBuiltInPermissions(db)
|
|
45
|
+
},
|
|
46
|
+
}
|
package/src/schema/plan.ts
CHANGED
|
@@ -28,6 +28,7 @@ import { bundlesDisabledMigration } from './bundles-disabled.ts'
|
|
|
28
28
|
import { bundlesInSettingsMigration } from './bundles-in-settings.ts'
|
|
29
29
|
import { bundlesRenameMigration } from './bundles-rename.ts'
|
|
30
30
|
import { changeReportMigration } from './change-report.ts'
|
|
31
|
+
import { cliTokensMigration } from './cli-tokens.ts'
|
|
31
32
|
import { clientIdPrefixMigration } from './client-id-prefix.ts'
|
|
32
33
|
import { contentMigration } from './content.ts'
|
|
33
34
|
import { contentAppliedMigration } from './content-applied.ts'
|
|
@@ -68,6 +69,7 @@ import { publicAccessMigration } from './public-access.ts'
|
|
|
68
69
|
import { recycleBinMigration } from './recycle-bin.ts'
|
|
69
70
|
import { redirectsMigration } from './redirects.ts'
|
|
70
71
|
import { remoteMigration } from './remote.ts'
|
|
72
|
+
import { remoteExchangeMigration } from './remote-exchange.ts'
|
|
71
73
|
import { remoteTrustMigration } from './remote-trust.ts'
|
|
72
74
|
import { richTextUiMigration } from './rich-text-ui.ts'
|
|
73
75
|
import { searchIndexMigration } from './search-index.ts'
|
|
@@ -169,4 +171,6 @@ export const cromboloPlan = new MigrationPlan([
|
|
|
169
171
|
prunedPermissionsMigration,
|
|
170
172
|
userStartNodesDroppedMigration,
|
|
171
173
|
sessionViewAsMigration,
|
|
174
|
+
cliTokensMigration,
|
|
175
|
+
remoteExchangeMigration,
|
|
172
176
|
])
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Migration 079 — a person signed in through a provider reaches the remote
|
|
3
|
+
* (story 364). `external_login` gains the value of the claim that names the
|
|
4
|
+
* person across the provider's apps (`oid` for Entra) and the issuer it came
|
|
5
|
+
* from, which the exchange finds them by; `remote_exchange_id` keeps each
|
|
6
|
+
* exchanged token's one-time id until the token expires, so none is used twice.
|
|
7
|
+
*/
|
|
8
|
+
import type { Db } from '../database.ts'
|
|
9
|
+
import type { Migration } from '../migrations.ts'
|
|
10
|
+
import { sql } from '../query.ts'
|
|
11
|
+
import { STATE_CLI_TOKENS } from './cli-tokens.ts'
|
|
12
|
+
|
|
13
|
+
export const STATE_REMOTE_EXCHANGE = '{7d2a9c4e-0079-4b1f-a3e8-5c6d0f2b0079}'
|
|
14
|
+
|
|
15
|
+
export const remoteExchangeMigration: Migration = {
|
|
16
|
+
from: STATE_CLI_TOKENS,
|
|
17
|
+
to: STATE_REMOTE_EXCHANGE,
|
|
18
|
+
name: 'AddRemoteExchange',
|
|
19
|
+
kind: 'expand',
|
|
20
|
+
release: '0.67.0',
|
|
21
|
+
async up(db: Db) {
|
|
22
|
+
const t = db.dialect.types
|
|
23
|
+
await db.q.schema
|
|
24
|
+
.alterTable('external_login')
|
|
25
|
+
.addColumn('identity_value', sql.raw(t.varchar(255)))
|
|
26
|
+
.execute()
|
|
27
|
+
await db.q.schema
|
|
28
|
+
.alterTable('external_login')
|
|
29
|
+
.addColumn('identity_issuer', sql.raw(t.varchar(255)))
|
|
30
|
+
.execute()
|
|
31
|
+
await db.q.schema
|
|
32
|
+
.createIndex('ix_external_login_identity')
|
|
33
|
+
.on('external_login')
|
|
34
|
+
.columns(['login_provider', 'identity_value'])
|
|
35
|
+
.execute()
|
|
36
|
+
await db.q.schema
|
|
37
|
+
.createTable('remote_exchange_id')
|
|
38
|
+
.addColumn('id', sql.raw(t.identity))
|
|
39
|
+
.addColumn('issuer', sql.raw(t.varchar(255)), (c) => c.notNull())
|
|
40
|
+
.addColumn('token_id', sql.raw(t.varchar(255)), (c) => c.notNull())
|
|
41
|
+
.addColumn('expires_at', sql.raw(t.timestamp), (c) => c.notNull())
|
|
42
|
+
.execute()
|
|
43
|
+
await db.q.schema
|
|
44
|
+
.createIndex('ux_remote_exchange_id')
|
|
45
|
+
.unique()
|
|
46
|
+
.on('remote_exchange_id')
|
|
47
|
+
.columns(['issuer', 'token_id'])
|
|
48
|
+
.execute()
|
|
49
|
+
},
|
|
50
|
+
}
|
package/src/schema/seed.ts
CHANGED
|
@@ -4,7 +4,12 @@
|
|
|
4
4
|
* Password hashing is injected rather than imported so this package stays free of
|
|
5
5
|
* a dependency on @crombolo/auth.
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
CliTokenPermissions,
|
|
9
|
+
EXPLICIT_PERMISSIONS,
|
|
10
|
+
MediaPermissions,
|
|
11
|
+
RemotePermissions,
|
|
12
|
+
} from '@crombolo/core'
|
|
8
13
|
import type { Db } from '../database.ts'
|
|
9
14
|
import { DbDate } from '../dialect.ts'
|
|
10
15
|
|
|
@@ -64,6 +69,8 @@ export const BUILT_IN_GROUP_PERMISSIONS: Record<string, readonly string[]> = {
|
|
|
64
69
|
// Never one a group has to name (story 241): administrators are not given it.
|
|
65
70
|
...Object.values(RemotePermissions).filter((verb) => !EXPLICIT_PERMISSIONS.has(verb)),
|
|
66
71
|
...MEDIA_FULL,
|
|
72
|
+
// Explicit, so given by name (story 351): taking it away turns tokens off for administrators.
|
|
73
|
+
CliTokenPermissions.Create,
|
|
67
74
|
],
|
|
68
75
|
editor: [
|
|
69
76
|
'Umb.Document.Create',
|
package/src/tables.gen.ts
CHANGED
|
@@ -153,6 +153,18 @@ export interface CoreTables {
|
|
|
153
153
|
subject_name: string | null
|
|
154
154
|
subject_type: string
|
|
155
155
|
}
|
|
156
|
+
cli_token: {
|
|
157
|
+
created_at: Timestamp
|
|
158
|
+
expires_at: Timestamp
|
|
159
|
+
id: Generated<number>
|
|
160
|
+
key: string
|
|
161
|
+
last_used_at: Timestamp | null
|
|
162
|
+
name: string
|
|
163
|
+
revoked_at: Timestamp | null
|
|
164
|
+
revoked_by: string | null
|
|
165
|
+
token_hash: string
|
|
166
|
+
user_id: number
|
|
167
|
+
}
|
|
156
168
|
content: {
|
|
157
169
|
content_type_id: number
|
|
158
170
|
creator_member_key: string | null
|
|
@@ -398,6 +410,8 @@ export interface CoreTables {
|
|
|
398
410
|
external_login: {
|
|
399
411
|
create_date: Timestamp
|
|
400
412
|
id: Generated<number>
|
|
413
|
+
identity_issuer: string | null
|
|
414
|
+
identity_value: string | null
|
|
401
415
|
login_provider: string
|
|
402
416
|
provider_key: string
|
|
403
417
|
user_data: string | null
|
|
@@ -663,6 +677,12 @@ export interface CoreTables {
|
|
|
663
677
|
rule_key: string
|
|
664
678
|
token_hash: string
|
|
665
679
|
}
|
|
680
|
+
remote_exchange_id: {
|
|
681
|
+
expires_at: Timestamp
|
|
682
|
+
id: Generated<number>
|
|
683
|
+
issuer: string
|
|
684
|
+
token_id: string
|
|
685
|
+
}
|
|
666
686
|
remote_job: {
|
|
667
687
|
create_date: Timestamp
|
|
668
688
|
decided_at: Timestamp | null
|