@crombolo/server 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/server",
3
- "version": "0.67.0",
3
+ "version": "0.68.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. The crombolo HTTP server: routing, configuration, media stores and background jobs.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -32,19 +32,19 @@
32
32
  "access": "public"
33
33
  },
34
34
  "dependencies": {
35
- "@crombolo/api-delivery": "0.67.0",
36
- "@crombolo/api-management": "0.67.0",
37
- "@crombolo/auth": "0.67.0",
38
- "@crombolo/backoffice-host": "0.67.0",
39
- "@crombolo/common": "0.67.0",
40
- "@crombolo/content": "0.67.0",
41
- "@crombolo/contracts": "0.67.0",
42
- "@crombolo/core": "0.67.0",
43
- "@crombolo/data": "0.67.0",
44
- "@crombolo/extension": "0.67.0",
45
- "@crombolo/render": "0.67.0",
46
- "@crombolo/schema": "0.67.0",
47
- "@crombolo/transfer": "0.67.0",
35
+ "@crombolo/api-delivery": "0.68.0",
36
+ "@crombolo/api-management": "0.68.0",
37
+ "@crombolo/auth": "0.68.0",
38
+ "@crombolo/backoffice-host": "0.68.0",
39
+ "@crombolo/common": "0.68.0",
40
+ "@crombolo/content": "0.68.0",
41
+ "@crombolo/contracts": "0.68.0",
42
+ "@crombolo/core": "0.68.0",
43
+ "@crombolo/data": "0.68.0",
44
+ "@crombolo/extension": "0.68.0",
45
+ "@crombolo/render": "0.68.0",
46
+ "@crombolo/schema": "0.68.0",
47
+ "@crombolo/transfer": "0.68.0",
48
48
  "@logtape/logtape": "^2.3.8",
49
49
  "@opentelemetry/api": "1.10.0",
50
50
  "@opentelemetry/context-async-hooks": "3.0.0",
package/src/config.ts CHANGED
@@ -1290,7 +1290,7 @@ export function describeDatabase(
1290
1290
  }
1291
1291
  }
1292
1292
 
1293
- export const VERSION = '0.67.0'
1293
+ export const VERSION = '0.68.0'
1294
1294
 
1295
1295
  /** A whole number of seconds from the environment; undefined when unset or not one. */
1296
1296
  /** A positive number from a setting, else the default. */
@@ -53,8 +53,16 @@ export interface IdentityProviderOptions {
53
53
  * provider's tokens for a sign-in as the person they name. The token's `aud`
54
54
  * must be `audience`, the site's origin by default, and never a sign-in
55
55
  * client id. `CROMBOLO_REMOTE_EXCHANGE_PROVIDERS` turns it on too.
56
+ * `actors` names the clients a token can be issued to, by client id, for the
57
+ * history (story 366).
56
58
  */
57
- remoteExchange?: boolean | { audience?: string }
59
+ remoteExchange?: boolean | RemoteExchangeOptions
60
+ }
61
+
62
+ /** How a provider's tokens are exchanged at the remote (stories 364 and 366). */
63
+ export interface RemoteExchangeOptions {
64
+ audience?: string
65
+ actors?: Readonly<Record<string, string>>
58
66
  }
59
67
 
60
68
  /** Which preset made a provider: what the Entra bundle looks for. */
@@ -74,8 +82,8 @@ export interface IdentityProvider {
74
82
  memberType: string | undefined
75
83
  /** Declared `locked: true`. */
76
84
  locked: boolean
77
- /** Declared `remoteExchange`: the audience its exchanged tokens carry, when not the site's origin. */
78
- remoteExchange: { audience?: string } | undefined
85
+ /** Declared `remoteExchange`: the audience its exchanged tokens carry, and the actors it names. */
86
+ remoteExchange: RemoteExchangeOptions | undefined
79
87
  /** Why it cannot sign anyone in as it stands, such as a new issuer with no secret of its own. */
80
88
  unavailable?: string
81
89
  }
package/src/index.ts CHANGED
@@ -143,6 +143,7 @@ export * from './remote-ci.ts'
143
143
  export * from './remote-credentials.ts'
144
144
  export * from './remote-media.ts'
145
145
  export { FILE_NAME_HEADER } from './remote-media-library.ts'
146
+ export { actorOf, type ExchangeActor } from './remote-people.ts'
146
147
  export * from './restart-pending.ts'
147
148
  export * from './routes.ts'
148
149
  export * from './schema.ts'
@@ -40,6 +40,46 @@ const text = (claims: JWTPayload, name: string | undefined): string | undefined
40
40
  return typeof value === 'string' && value !== '' ? value : undefined
41
41
  }
42
42
 
43
+ /** Text from a token for the history: control characters gone, and no longer than `limit`. */
44
+ const plain = (value: string, limit: number) =>
45
+ value
46
+ .replace(/\p{Cc}/gu, '')
47
+ .trim()
48
+ .slice(0, limit)
49
+
50
+ /** Who acts for the person (story 366): the tool a token names, by id and by name. */
51
+ export interface ExchangeActor {
52
+ id: string
53
+ name: string | null
54
+ }
55
+
56
+ /**
57
+ * The actor a verified token names: its `act` (RFC 8693), or else the client it
58
+ * was issued to (`azp`, or Entra v1's `appid`), named by the provider's `actors`.
59
+ * `malformed` for an `act` with no string `sub`.
60
+ */
61
+ export function actorOf(
62
+ claims: JWTPayload,
63
+ actors: Readonly<Record<string, string>> = {},
64
+ ): ExchangeActor | undefined | 'malformed' {
65
+ if (claims.act !== undefined) {
66
+ const act = claims.act
67
+ if (!act || typeof act !== 'object' || Array.isArray(act)) return 'malformed'
68
+ const { sub, name } = act as Record<string, unknown>
69
+ const id = typeof sub === 'string' ? plain(sub, 200) : ''
70
+ if (!id) return 'malformed'
71
+ const named = typeof name === 'string' ? plain(name, 100) : ''
72
+ return { id, name: named || (Object.hasOwn(actors, id) ? (actors[id] ?? null) : null) }
73
+ }
74
+ const client = text(claims, 'azp') ?? text(claims, 'appid')
75
+ const id = client ? plain(client, 200) : ''
76
+ if (!id) return undefined
77
+ return { id, name: Object.hasOwn(actors, id) ? (actors[id] ?? null) : null }
78
+ }
79
+
80
+ /** Entra's v1 issuer for a tenant, which the exchange recognises only to refuse. */
81
+ const entraV1Issuer = (tenantId: string) => `https://sts.windows.net/${tenantId}`
82
+
43
83
  const list = (claims: JWTPayload, name: string | undefined): string[] => {
44
84
  const value = name ? claims[name] : undefined
45
85
  if (Array.isArray(value)) return value.filter((v): v is string => typeof v === 'string')
@@ -59,11 +99,17 @@ export interface ExchangeAccounts {
59
99
  provider: { id: string; name: string },
60
100
  seconds: number,
61
101
  ip: string | undefined,
102
+ actor: ExchangeActor | undefined,
62
103
  ): Promise<{ accessToken: string } | { refused: string }>
63
104
  }
64
105
 
65
106
  export type PeopleExchangeOutcome =
66
- | { ok: true; provider: IdentityProvider; account: ExternalAccount }
107
+ | {
108
+ ok: true
109
+ provider: IdentityProvider
110
+ account: ExternalAccount
111
+ actor: ExchangeActor | undefined
112
+ }
67
113
  | { ok: false; reason: string; provider?: IdentityProvider }
68
114
 
69
115
  export interface PeopleExchange {
@@ -103,6 +149,18 @@ export function createPeopleExchange(options: {
103
149
  provider.remoteExchange?.audience ?? origin
104
150
  const usable = (provider: IdentityProvider) =>
105
151
  !provider.unavailable && usableIssuer(issuerOf(provider))
152
+ /** The exchanging provider a token's issuer is, and whether it came as Entra's v1 token. */
153
+ const providerFor = async (issuer: string | undefined) => {
154
+ if (!issuer) return undefined
155
+ const bare = issuer.replace(/\/+$/, '')
156
+ for (const p of await effective()) {
157
+ if (!usable(p)) continue
158
+ if (issuerOf(p) === issuer) return { provider: p, v1: false }
159
+ if (p.kind === 'entra' && p.tenantId && entraV1Issuer(p.tenantId) === bare)
160
+ return { provider: p, v1: true }
161
+ }
162
+ return undefined
163
+ }
106
164
 
107
165
  const json = (body: unknown, status: number) =>
108
166
  Response.json(body, { status, headers: { 'cache-control': 'no-store' } })
@@ -169,7 +227,7 @@ export function createPeopleExchange(options: {
169
227
  },
170
228
 
171
229
  async handles(issuer) {
172
- return (await effective()).some((p) => usable(p) && issuerOf(p) === issuer)
230
+ return (await providerFor(issuer)) !== undefined
173
231
  },
174
232
 
175
233
  async exchange(request, url, ip) {
@@ -188,11 +246,19 @@ export function createPeopleExchange(options: {
188
246
  const token = form.get('subject_token') ?? ''
189
247
 
190
248
  const issuer = unverifiedIssuer(token)
191
- const provider = (await effective()).find((p) => usable(p) && issuerOf(p) === issuer)
192
- if (!issuer || !provider)
249
+ const found = await providerFor(issuer)
250
+ if (!issuer || !found)
193
251
  return refuse(request, ip, 'an issuer that is not an exchanging provider')
194
- if (limiter.blocked(`issuer:${issuer}`))
252
+ const { provider } = found
253
+ if (limiter.blocked(`issuer:${issuerOf(provider)}`))
195
254
  return json({ error: 'slow_down', error_description: 'Too many refused tokens.' }, 429)
255
+ if (found.v1)
256
+ return refuse(
257
+ request,
258
+ ip,
259
+ 'it is an Entra v1 token: set requestedAccessTokenVersion to 2 in the manifest of the app it was issued for',
260
+ provider,
261
+ )
196
262
 
197
263
  const audience = audienceOf(provider, url.origin)
198
264
  const signInClients = await Promise.all(
@@ -229,13 +295,16 @@ export function createPeopleExchange(options: {
229
295
  if (!tokenId) return refuse(request, ip, 'the token has no one-time id', provider)
230
296
  if (!(await ids.spend(issuer, tokenId, new Date(exp * 1000))))
231
297
  return refuse(request, ip, 'the token was already used', provider)
298
+ const actor = actorOf(claims, provider.remoteExchange?.actors)
299
+ if (actor === 'malformed')
300
+ return refuse(request, ip, 'its act claim names no actor by a string sub', provider)
232
301
 
233
- const found = await accountFor(provider, issuer, claims)
234
- if (typeof found === 'string') return refuse(request, ip, found, provider)
235
- const refusal = await options.accounts.refusal(found.key)
302
+ const person = await accountFor(provider, issuer, claims)
303
+ if (typeof person === 'string') return refuse(request, ip, person, provider)
304
+ const refusal = await options.accounts.refusal(person.key)
236
305
  if (refusal) return refuse(request, ip, refusal, provider)
237
306
 
238
- let account = found
307
+ let account = person
239
308
  if (Object.keys(provider.groupMap).length > 0) {
240
309
  const groups = syncedGroups(
241
310
  account.groups,
@@ -252,9 +321,10 @@ export function createPeopleExchange(options: {
252
321
  { id: provider.oidc.id, name: provider.oidc.name },
253
322
  PERSON_TOKEN_SECONDS,
254
323
  ip,
324
+ actor,
255
325
  )
256
326
  if ('refused' in signedIn) return refuse(request, ip, signedIn.refused, provider)
257
- await options.onExchange({ ok: true, provider, account }, request)
327
+ await options.onExchange({ ok: true, provider, account, actor }, request)
258
328
  return json(
259
329
  {
260
330
  access_token: signedIn.accessToken,
package/src/remote.ts CHANGED
@@ -87,7 +87,7 @@ import type { UserPorts } from './user-admin.ts'
87
87
 
88
88
  export const REMOTE_PROTOCOL_VERSION = '1'
89
89
  export { REMOTE_DISCOVERY_PATH }
90
- export const REMOTE_API_PATH = '/umbraco/remote/v1'
90
+ export const REMOTE_API_PATH = '/crombolo/remote/v1'
91
91
 
92
92
  /** How long a finished job's file stays downloadable, and its row with it. */
93
93
  const JOB_FILE_SECONDS = 60 * 60
@@ -1533,22 +1533,25 @@ export function createRemote(options: RemoteOptions): Remote {
1533
1533
  : new Response(null, { status: 405 })
1534
1534
  }
1535
1535
  if (!pathname.startsWith(`${REMOTE_API_PATH}/`)) return undefined
1536
- if (!settings.enabled)
1537
- return problem(404, { code: 'not-enabled', title: 'This site is not a remote' })
1536
+ const notEnabled = () =>
1537
+ problem(404, { code: 'not-enabled', title: 'This site is not a remote' })
1538
1538
  if (pathname === `${REMOTE_API_PATH}/token`) {
1539
+ const people = options.people
1540
+ if (!settings.enabled && !people) return notEnabled()
1539
1541
  if (request.method !== 'POST') return new Response(null, { status: 405 })
1540
1542
  const ip = request.headers.get('x-forwarded-for')?.split(',')[0]?.trim()
1541
1543
  // One endpoint for both: the token's issuer says whether it is CI's or a person's.
1542
- const people = options.people
1544
+ // A person's exchange answers with the remote off too (story 365); CI's does not.
1543
1545
  const issuer = people
1544
1546
  ? unverifiedIssuer(
1545
1547
  new URLSearchParams(await request.clone().text()).get('subject_token') ?? '',
1546
1548
  )
1547
1549
  : undefined
1548
- return people && issuer && (await people.handles(issuer))
1549
- ? people.exchange(request, url, ip)
1550
- : ci.exchange(request, url, ip)
1550
+ if (people && issuer && (await people.handles(issuer)))
1551
+ return people.exchange(request, url, ip)
1552
+ return settings.enabled ? ci.exchange(request, url, ip) : notEnabled()
1551
1553
  }
1554
+ if (!settings.enabled) return notEnabled()
1552
1555
  // A CI token is good here and nowhere else; a person's is the back office's own.
1553
1556
  const principal = (await ci.principalOf(request)) ?? (await options.authenticate(request))
1554
1557
  if (!principal)
package/src/server.ts CHANGED
@@ -89,6 +89,7 @@ import {
89
89
  } from '@crombolo/core'
90
90
  import {
91
91
  appendCacheInstruction,
92
+ byActor,
92
93
  currentAuditContext,
93
94
  type Db,
94
95
  ensureFormLibraryTypes,
@@ -688,6 +689,7 @@ export async function createServer(
688
689
  provider:
689
690
  config.backofficeIdentityProviders.find((p) => p.oidc.id === identity.exchangedThrough)
690
691
  ?.oidc.name ?? identity.exchangedThrough,
692
+ ...(identity.exchangedActor ? { actor: identity.exchangedActor } : {}),
691
693
  }
692
694
  if (context && principal.viewingAs)
693
695
  context.viewing = {
@@ -942,9 +944,12 @@ export async function createServer(
942
944
  { id },
943
945
  )
944
946
  if (exchanging.length > 0)
945
- log.info('The remote exchanges tokens from {providers}', {
946
- providers: exchanging.map((p) => p.oidc.id).join(', '),
947
- })
947
+ log.info(
948
+ config.remote.enabled
949
+ ? 'The remote exchanges tokens from {providers}'
950
+ : "The remote is off, and its token endpoint answers only {providers}'s exchange for a person",
951
+ { providers: exchanging.map((p) => p.oidc.id).join(', ') },
952
+ )
948
953
  const people =
949
954
  exchanging.length === 0
950
955
  ? undefined
@@ -980,10 +985,16 @@ export async function createServer(
980
985
  ? undefined
981
986
  : 'the person has no access to the back office'
982
987
  },
983
- signIn: async (key, provider, seconds, ip) => {
988
+ signIn: async (key, provider, seconds, ip, actor) => {
984
989
  const user = await users.byKey(key)
985
990
  if (!user) return { refused: 'the person no longer exists' }
986
- const result = await auth.signInByExchange(user.userName, provider, seconds, ip)
991
+ const result = await auth.signInByExchange(
992
+ user.userName,
993
+ provider,
994
+ seconds,
995
+ ip,
996
+ actor,
997
+ )
987
998
  if (result.status === 'success') return { accessToken: result.accessToken }
988
999
  return {
989
1000
  refused:
@@ -1004,8 +1015,9 @@ export async function createServer(
1004
1015
  entityKey: outcome.ok ? outcome.account.key : null,
1005
1016
  entityName: outcome.ok ? outcome.account.email : null,
1006
1017
  detail: outcome.ok
1007
- ? `Signed in through ${outcome.provider.oidc.name}, by exchange at the remote`
1018
+ ? `Signed in through ${outcome.provider.oidc.name}${byActor(outcome.actor)}, by exchange at the remote`
1008
1019
  : `Refused${outcome.provider ? ` through ${outcome.provider.oidc.name}` : ''}: ${outcome.reason}`,
1020
+ actor: outcome.ok ? (outcome.actor ?? null) : null,
1009
1021
  ...clientAddress(request),
1010
1022
  }),
1011
1023
  })
package/src/telemetry.ts CHANGED
@@ -361,6 +361,7 @@ export function withTraceHeaders(headers: Record<string, string>): Record<string
361
361
  export function routeOf(pathname: string, backOfficePath: string): string {
362
362
  if (pathname === '/health') return '/health'
363
363
  if (pathname.startsWith('/umbraco/delivery/api/')) return '/umbraco/delivery/api/*'
364
+ if (pathname.startsWith('/crombolo/remote/v1/')) return '/crombolo/remote/v1/*'
364
365
  if (pathname.startsWith(`${backOfficePath}/management/api/`))
365
366
  return `${backOfficePath}/management/api/*`
366
367
  if (pathname === backOfficePath || pathname.startsWith(`${backOfficePath}/`))