javascript-solid-server 0.0.213 → 0.0.214
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/auth.js +50 -0
- package/docs/configuration.md +8 -3
- package/package.json +1 -1
- package/test/auth-agent.test.js +100 -0
package/auth.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public authentication api (#584).
|
|
3
|
+
*
|
|
4
|
+
* The stable import path for applications that authenticate their own
|
|
5
|
+
* traffic — app plugins mounted via `appPaths` (#582), external Fastify
|
|
6
|
+
* compositions, MCP-adjacent tooling. Everything in src/ is internal and
|
|
7
|
+
* may move; this file is the contract.
|
|
8
|
+
*
|
|
9
|
+
* import { getAgent } from 'javascript-solid-server/auth.js';
|
|
10
|
+
*
|
|
11
|
+
* const agent = await getAgent(request); // string | null
|
|
12
|
+
*
|
|
13
|
+
* Covers every credential scheme the server itself accepts, uniformly:
|
|
14
|
+
* IdP-issued Bearer tokens, Solid-OIDC DPoP, Nostr NIP-98 signatures,
|
|
15
|
+
* LWS10-CID, and WebID-TLS client certificates.
|
|
16
|
+
* The returned identifier is usually an HTTP(S) WebID;
|
|
17
|
+
* for NIP-98 it can be a `did:nostr:...` DID when no WebID mapping
|
|
18
|
+
* exists — DID agents are first-class here, which is why this is
|
|
19
|
+
* getAgent and not getWebId. Key your app's users on the string.
|
|
20
|
+
*
|
|
21
|
+
* Authentication only — deliberately not authorization:
|
|
22
|
+
* apps under an appPaths prefix own their own permissioning (WAC stays
|
|
23
|
+
* out of their jurisdiction, and `request.webId` is never set there).
|
|
24
|
+
*
|
|
25
|
+
* This is the pre-loader shape of the seam; when the plugin loader (#206)
|
|
26
|
+
* lands, `api.auth.getAgent` will be this same function handed to
|
|
27
|
+
* `activate(api)`.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { getWebIdFromRequestAsync } from './src/auth/token.js';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Resolve the authenticated agent of a request.
|
|
34
|
+
* @param {object} request - a Fastify request. Pass the real request object:
|
|
35
|
+
* Bearer verification only reads `headers`, but DPoP (Solid-OIDC),
|
|
36
|
+
* NIP-98 and LWS-CID verification also read `method`, `url`, `protocol`
|
|
37
|
+
* and `hostname` to check what the credential was signed over.
|
|
38
|
+
* @returns {Promise<string|null>} the verified agent identifier — an
|
|
39
|
+
* HTTP(S) WebID, or a `did:nostr:` DID for NIP-98 agents without a
|
|
40
|
+
* WebID mapping — or null when anonymous / invalid. Never throws on
|
|
41
|
+
* bad credentials.
|
|
42
|
+
*/
|
|
43
|
+
export async function getAgent(request) {
|
|
44
|
+
try {
|
|
45
|
+
const { webId } = await getWebIdFromRequestAsync(request);
|
|
46
|
+
return webId || null;
|
|
47
|
+
} catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
package/docs/configuration.md
CHANGED
|
@@ -374,9 +374,14 @@ longer than `/`; anything else is ignored.
|
|
|
374
374
|
|
|
375
375
|
Because the WAC hook is also what populates `request.webId`, requests under
|
|
376
376
|
an app path never carry it — don't rely on `request.webId` in app handlers.
|
|
377
|
-
Resolve identity
|
|
378
|
-
|
|
379
|
-
|
|
377
|
+
Resolve identity with the public accessor
|
|
378
|
+
([#584](https://github.com/JavaScriptSolidServer/JavaScriptSolidServer/issues/584)):
|
|
379
|
+
|
|
380
|
+
```js
|
|
381
|
+
import { getAgent } from 'javascript-solid-server/auth.js';
|
|
382
|
+
|
|
383
|
+
const agent = await getAgent(request); // WebID or did:nostr DID, null if anonymous
|
|
384
|
+
```
|
|
380
385
|
|
|
381
386
|
See [#582](https://github.com/JavaScriptSolidServer/JavaScriptSolidServer/issues/582)
|
|
382
387
|
for the design discussion and
|
package/package.json
CHANGED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* getAgent — public request → agent-identifier accessor (#584).
|
|
3
|
+
*
|
|
4
|
+
* Apps that own their own auth (appPaths mounts, #582) need to ask "who is
|
|
5
|
+
* this?" without reaching into src/auth internals. auth.js at the package
|
|
6
|
+
* root is the stable contract; these tests pin its shape so auth refactors
|
|
7
|
+
* keep the seam:
|
|
8
|
+
*
|
|
9
|
+
* - anonymous / malformed credentials -> null, never a throw
|
|
10
|
+
* - a real IdP-issued Bearer token -> the account's WebID
|
|
11
|
+
*
|
|
12
|
+
* The identifier is an HTTP(S) WebID or, for NIP-98 agents without a WebID
|
|
13
|
+
* mapping, a did:nostr DID. The individual credential schemes (Bearer,
|
|
14
|
+
* DPoP, NIP-98, LWS10-CID, WebID-TLS) are exercised by their own suites;
|
|
15
|
+
* this one pins the public wrapper.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { describe, it, before, after, afterEach } from 'node:test';
|
|
19
|
+
import assert from 'node:assert';
|
|
20
|
+
import { createServer } from '../src/server.js';
|
|
21
|
+
import { getAgent } from '../auth.js';
|
|
22
|
+
import fs from 'fs-extra';
|
|
23
|
+
|
|
24
|
+
const TEST_DATA_DIR = './test-data-auth-agent';
|
|
25
|
+
|
|
26
|
+
let server;
|
|
27
|
+
let baseUrl;
|
|
28
|
+
let originalDataRoot;
|
|
29
|
+
|
|
30
|
+
describe('public getAgent accessor (#584)', () => {
|
|
31
|
+
before(() => {
|
|
32
|
+
originalDataRoot = process.env.DATA_ROOT;
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(async () => {
|
|
36
|
+
if (server) {
|
|
37
|
+
await server.close();
|
|
38
|
+
server = null;
|
|
39
|
+
}
|
|
40
|
+
await fs.remove(TEST_DATA_DIR);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
after(() => {
|
|
44
|
+
if (originalDataRoot === undefined) delete process.env.DATA_ROOT;
|
|
45
|
+
else process.env.DATA_ROOT = originalDataRoot;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('returns null for anonymous requests', async () => {
|
|
49
|
+
assert.strictEqual(await getAgent({ headers: {} }), null);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('returns null (not a throw) for malformed credentials', async () => {
|
|
53
|
+
assert.strictEqual(await getAgent({ headers: { authorization: 'Bearer garbage' } }), null);
|
|
54
|
+
assert.strictEqual(await getAgent({ headers: { authorization: 'Nonsense scheme' } }), null);
|
|
55
|
+
assert.strictEqual(await getAgent({ headers: { authorization: '' } }), null);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('resolves a real IdP Bearer token to the account WebID', async () => {
|
|
59
|
+
await fs.emptyDir(TEST_DATA_DIR);
|
|
60
|
+
// The IdP needs its real issuer up front — reserve a port first (same
|
|
61
|
+
// pattern as test/well-known-did-nostr.test.js).
|
|
62
|
+
const net = await import('node:net');
|
|
63
|
+
const port = await new Promise((resolve, reject) => {
|
|
64
|
+
const probe = net.createServer();
|
|
65
|
+
probe.once('error', reject); // fail loudly, don't hang, if listen errors
|
|
66
|
+
probe.listen(0, '127.0.0.1', () => {
|
|
67
|
+
const p = probe.address().port;
|
|
68
|
+
probe.close(() => resolve(p));
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
baseUrl = `http://127.0.0.1:${port}`;
|
|
72
|
+
server = createServer({
|
|
73
|
+
logger: false,
|
|
74
|
+
forceCloseConnections: true,
|
|
75
|
+
root: TEST_DATA_DIR,
|
|
76
|
+
idp: true,
|
|
77
|
+
idpIssuer: baseUrl,
|
|
78
|
+
});
|
|
79
|
+
await server.listen({ port, host: '127.0.0.1' });
|
|
80
|
+
|
|
81
|
+
let res = await fetch(`${baseUrl}/idp/register`, {
|
|
82
|
+
method: 'POST',
|
|
83
|
+
headers: { 'Content-Type': 'application/json' },
|
|
84
|
+
body: JSON.stringify({ username: 'agent', password: 'secret-word', confirmPassword: 'secret-word' }),
|
|
85
|
+
});
|
|
86
|
+
assert.ok(res.status < 400, `register failed: ${res.status}`);
|
|
87
|
+
|
|
88
|
+
res = await fetch(`${baseUrl}/idp/credentials`, {
|
|
89
|
+
method: 'POST',
|
|
90
|
+
headers: { 'Content-Type': 'application/json' },
|
|
91
|
+
body: JSON.stringify({ username: 'agent', password: 'secret-word' }),
|
|
92
|
+
});
|
|
93
|
+
assert.strictEqual(res.status, 200, `credentials failed: ${res.status}`);
|
|
94
|
+
const cred = await res.json();
|
|
95
|
+
assert.ok(cred.access_token, 'no token issued');
|
|
96
|
+
|
|
97
|
+
const webId = await getAgent({ headers: { authorization: `Bearer ${cred.access_token}` } });
|
|
98
|
+
assert.strictEqual(webId, cred.webid);
|
|
99
|
+
});
|
|
100
|
+
});
|