Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/delegated-transaction-caller.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ semantics are under [Error semantics](#error-semantics). `token` is ignored.
`x-linked-user-token: <the user's Solid-OIDC access token>` (exported as `USER_TOKEN_HEADER`) and
`DPoP: <a proof for this one request>`, 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
Expand Down Expand Up @@ -448,7 +448,9 @@ POST /call/@_linked/server/<x>Query {"args": [<query.toJSON(), with "tx
if it were sent alone; a refused or failed one rolls the transaction back in every store. An
idle transaction is rolled back after 30 seconds.
- **The caller.** A transaction needs a session (401 without one) and is bound to the session's
user account; a backend-to-backend call with no request behind it is `system`. Anyone else's
user account; a delegated call (`runAsDelegate` in `@_linked/server-utils`) is bound to the user
the delegate acts for, the same identity as that user's own request; a backend-to-backend call
with no user behind it is `system`. Anyone else's
request with that txId is refused and leaves the transaction as it was.
- **Refusals** (`{error, code}`): 501 `transactions-unsupported`, 409 `transaction-unknown` (never
begun, ended, failed or expired; a `rollback` of one resolves), 403 `transaction-not-yours`. A
Expand Down
39 changes: 39 additions & 0 deletions src/tests/transaction-caller.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from '@jest/globals';
import { runAsDelegate, runAsSystem, runWithCallContext } from '@_linked/server-utils/utils/CallContext';
import { ServerCallError } from '@_linked/server-utils/utils/ServerCallError';
import { transactionCaller } from '../utils/serverTransactions.js';

// Who a store transaction is bound to. A delegated call (work for a known user, outside that
// user's request) must be bound to its user: bound to `system`, any system work could continue it.

const http = (linkedAuth?: any) => ({ 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');
});
});
7 changes: 4 additions & 3 deletions src/utils/serverTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
Expand Down
Loading