diff --git a/.changeset/delegated-transaction-caller.md b/.changeset/delegated-transaction-caller.md new file mode 100644 index 0000000..97b7781 --- /dev/null +++ b/.changeset/delegated-transaction-caller.md @@ -0,0 +1,5 @@ +--- +"@_linked/server": patch +--- + +A store transaction started in a delegated call (`runAsDelegate`, `@_linked/server-utils` 1.18) is bound to the user the delegate acts for, the same identity as that user's own request, instead of `system`. `RemoteDataset` sends no user token for a delegated call: it has a user and no session token. diff --git a/package.json b/package.json index 5582556..2cb4892 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "dependencies": { "@_linked/cli": "^1.58.0", "@_linked/core": "^2.47.0", - "@_linked/server-utils": "^1.16.1", + "@_linked/server-utils": "^1.18.0", "autoprefixer": "^10.4.18", "compression": "^1.7.4", "cors": "^2.8.5", diff --git a/readme.md b/readme.md index 063aa59..f16910d 100644 --- a/readme.md +++ b/readme.md @@ -368,7 +368,7 @@ semantics are under [Error semantics](#error-semantics). `token` is ignored. `x-linked-user-token: ` (exported as `USER_TOKEN_HEADER`) and `DPoP: `, beside the bearer. auth answers for a session signed in through an issuer that gave a DPoP-bound JWT access token (webid.email does), while the token has - more than 30 seconds left; other sessions, a system context (`runAsSystem`), an anonymous request + more than 30 seconds left; other sessions, a system context (`runAsSystem`), a delegated call (`runAsDelegate`: it has a user but no session token), an anonymous request and code outside any call send neither. A hook that throws or answers something else sends neither (one warning per process) and the query still goes. The token still decides which data the store reaches; the data server verifies the user's token and proof (`@_linked/auth`'s @@ -448,7 +448,9 @@ POST /call/@_linked/server/Query {"args": [ ({ kind: 'http', request: { linkedAuth }, response: {} }) as const; + +describe('transactionCaller', () => { + it('is the session user in an http call, and requires one', () => { + expect(runWithCallContext(http({ userAccount: { id: 'http://ex/me' } }), transactionCaller)).toBe('user:http://ex/me'); + expect(() => runWithCallContext(http(), transactionCaller)).toThrow(ServerCallError); + }); + + it('is the user a delegate acts for in a delegated call', () => { + const caller = runAsDelegate({ id: 'http://ex/me' }, { id: 'http://ex/connection/1' }, transactionCaller); + expect(caller).toBe('user:http://ex/me'); + // the same user through their own request and through a delegate share a caller identity + expect(caller).toBe(runWithCallContext(http({ userAccount: { id: 'http://ex/me' } }), transactionCaller)); + }); + + it('refuses a delegated call whose account has no id', () => { + let status: number | undefined; + try { + runAsDelegate({}, { id: 'http://ex/connection/1' }, transactionCaller); + } catch (err: any) { + status = err?.status; + } + expect(status).toBe(401); + }); + + it('is system for system work and outside any call (control)', () => { + expect(transactionCaller()).toBe('system'); + expect(runAsSystem(transactionCaller, 'job')).toBe('system'); + expect(runAsDelegate({ id: 'http://ex/me' }, { id: 'c' }, () => runAsSystem(transactionCaller))).toBe('system'); + }); +}); diff --git a/src/utils/serverTransactions.ts b/src/utils/serverTransactions.ts index 606473d..bc9f02b 100644 --- a/src/utils/serverTransactions.ts +++ b/src/utils/serverTransactions.ts @@ -44,12 +44,13 @@ export function assertStoresHoldTransactions(): void { /** * Who a transaction is bound to: the session's user account in an HTTP call (a session is - * required, as for a write without an access rule), `system` for work with no request behind it. + * required, as for a write without an access rule), the user a delegate acts for in a delegated + * call (`runAsDelegate`), `system` for work with no user behind it. */ export function transactionCaller(): string { const ctx = getCallContext(); - if (!ctx || ctx.kind !== 'http') return 'system'; - const id = ctx.request?.linkedAuth?.userAccount?.id; + if (!ctx || (ctx.kind !== 'http' && ctx.kind !== 'delegated')) return 'system'; + const id = ctx.kind === 'http' ? ctx.request?.linkedAuth?.userAccount?.id : ctx.userAccount?.id; if (typeof id !== 'string' || !id) throw new ServerCallError(401, 'Authentication required'); return `user:${id}`; }