@johnhenry/browsermesh-core 0.0.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/LICENSE +21 -0
- package/README.md +42 -0
- package/package.json +32 -0
- package/src/acl.mjs +530 -0
- package/src/capabilities.mjs +852 -0
- package/src/compat.mjs +29 -0
- package/src/group-keys.mjs +610 -0
- package/src/handshake.mjs +760 -0
- package/src/hardening.mjs +1183 -0
- package/src/identity-base.mjs +491 -0
- package/src/identity-tools.mjs +383 -0
- package/src/identity-wallet.mjs +505 -0
- package/src/identity.mjs +1349 -0
- package/src/index.mjs +15 -0
- package/src/keyring.mjs +741 -0
- package/src/peer-tools.mjs +959 -0
- package/src/peer.mjs +391 -0
- package/src/silent-catch.mjs +55 -0
- package/src/trust.mjs +425 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Clawser Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# browsermesh-core
|
|
2
|
+
|
|
3
|
+
Identity, crypto, peer management, and trust primitives for BrowserMesh.
|
|
4
|
+
|
|
5
|
+
## Provenance
|
|
6
|
+
|
|
7
|
+
Extracted from the private `clawser` monorepo (previously `packages/browsermesh-core`), where it was manually published to npm, unscoped, as `browsermesh-core@0.1.0` (2026-07-17) with no CI ever automating that publish. This is its first release as part of the `@johnhenry/browsermesh` monorepo; the version restarts at `0.0.0` per family convention.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## Modules
|
|
11
|
+
|
|
12
|
+
| Module | Key Exports |
|
|
13
|
+
|--------|-------------|
|
|
14
|
+
| identity | `MeshIdentityManager`, `AutoIdentityManager`, `IdentitySelector`, `PodIdentity`, `derivePodId` |
|
|
15
|
+
| identity-tools | `IdentityCreateTool`, `IdentityListTool`, `IdentitySwitchTool`, `registerIdentityTools` |
|
|
16
|
+
| keyring | `MeshKeyring`, `KeyLink`, `SignedKeyLink`, `SuccessionPolicy` |
|
|
17
|
+
| group-keys | `GroupKeyManager`, `GroupState` |
|
|
18
|
+
| peer | `PeerState`, `MeshPeerManager` |
|
|
19
|
+
| peer-tools | `MeshPeerToolsContext`, `registerMeshPeerTools` + 30 BrowserTool subclasses |
|
|
20
|
+
| handshake | `HandshakeCoordinator`, `SignalingClient`, `DirectInputHandshake` |
|
|
21
|
+
| acl | `MeshACL`, `ScopeTemplate`, `RosterEntry`, `InvitationToken` |
|
|
22
|
+
| capabilities | `CapabilityToken`, `CapabilityChain`, `CapabilityValidator`, `WasmSandbox` |
|
|
23
|
+
| trust | `TrustGraph` |
|
|
24
|
+
| hardening | `RetryWithBackoff`, `TransportHealthCheck`, `ConnectionPool`, `TransportFailover` |
|
|
25
|
+
| identity-base | `IdentityManager`, `compileSystemPrompt`, `detectIdentityFormat` |
|
|
26
|
+
| identity-wallet | `IdentityWallet` |
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install @johnhenry/browsermesh-core @johnhenry/browsermesh-primitives
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
import { MeshIdentityManager, MeshKeyring, TrustGraph } from 'browsermesh-core';
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@johnhenry/browsermesh-core",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Identity, crypto, peer management, and trust primitives for BrowserMesh",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.mjs",
|
|
9
|
+
"./compat": "./src/compat.mjs"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src",
|
|
13
|
+
"LICENSE",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/johnhenry/browsermesh",
|
|
20
|
+
"directory": "packages/browsermesh-core"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"@johnhenry/browsermesh-primitives": ">=0.0.0"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "node --import ./test/_setup-globals.mjs --test test/*.test.mjs"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://opensource.johnhenry.me/browsermesh/",
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=24.0.0"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/acl.mjs
ADDED
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
/**
|
|
2
|
+
// STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
|
|
3
|
+
* clawser-mesh-acl.js -- Remote access control for BrowserMesh.
|
|
4
|
+
*
|
|
5
|
+
* ScopeTemplate bundles, roster management, invitation tokens, and
|
|
6
|
+
* access checking. Wraps mesh-primitives ACLEngine with higher-level
|
|
7
|
+
* identity-centric management.
|
|
8
|
+
*
|
|
9
|
+
* No browser-only imports at module level.
|
|
10
|
+
*
|
|
11
|
+
* Run tests:
|
|
12
|
+
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-acl.test.mjs
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
ACLEngine,
|
|
17
|
+
AccessGrant,
|
|
18
|
+
Permission,
|
|
19
|
+
generateGrantId,
|
|
20
|
+
matchScope,
|
|
21
|
+
} from '@johnhenry/browsermesh-primitives';
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// ScopeTemplate
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Named capability bundle — maps a friendly name to a set of scope strings.
|
|
29
|
+
*/
|
|
30
|
+
export class ScopeTemplate {
|
|
31
|
+
/**
|
|
32
|
+
* @param {object} opts
|
|
33
|
+
* @param {string} opts.name
|
|
34
|
+
* @param {string[]} opts.scopes
|
|
35
|
+
* @param {string} [opts.description]
|
|
36
|
+
*/
|
|
37
|
+
constructor({ name, scopes, description }) {
|
|
38
|
+
this.name = name;
|
|
39
|
+
this.scopes = [...scopes];
|
|
40
|
+
this.description = description;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Check if this template covers a given scope.
|
|
45
|
+
* Uses matchScope from mesh-primitives for wildcard support.
|
|
46
|
+
* @param {string} scope
|
|
47
|
+
* @returns {boolean}
|
|
48
|
+
*/
|
|
49
|
+
matches(scope) {
|
|
50
|
+
return this.scopes.some(s => matchScope(s, scope));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
toJSON() {
|
|
54
|
+
return {
|
|
55
|
+
name: this.name,
|
|
56
|
+
scopes: [...this.scopes],
|
|
57
|
+
description: this.description,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static fromJSON(data) {
|
|
62
|
+
return new ScopeTemplate({
|
|
63
|
+
name: data.name,
|
|
64
|
+
scopes: data.scopes,
|
|
65
|
+
description: data.description,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
// DEFAULT_TEMPLATES
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
export const DEFAULT_TEMPLATES = Object.freeze({
|
|
75
|
+
guest: new ScopeTemplate({
|
|
76
|
+
name: 'guest',
|
|
77
|
+
scopes: ['chat:read', 'files:read'],
|
|
78
|
+
description: 'Read-only access to chat and files',
|
|
79
|
+
}),
|
|
80
|
+
collaborator: new ScopeTemplate({
|
|
81
|
+
name: 'collaborator',
|
|
82
|
+
scopes: ['chat:*', 'files:read', 'files:write', 'compute:submit'],
|
|
83
|
+
description: 'Full chat, file read/write, and compute submission',
|
|
84
|
+
}),
|
|
85
|
+
admin: new ScopeTemplate({
|
|
86
|
+
name: 'admin',
|
|
87
|
+
scopes: ['*:*'],
|
|
88
|
+
description: 'Full access to all resources',
|
|
89
|
+
}),
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
// RosterEntry
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* An identity in the access roster.
|
|
98
|
+
*/
|
|
99
|
+
export class RosterEntry {
|
|
100
|
+
/**
|
|
101
|
+
* @param {object} opts
|
|
102
|
+
* @param {string} opts.identity - Fingerprint of the identity
|
|
103
|
+
* @param {string} opts.templateName - Name of the assigned template
|
|
104
|
+
* @param {string[]} [opts.scopes] - Additional scope overrides
|
|
105
|
+
* @param {object} [opts.quotas] - Usage quotas
|
|
106
|
+
* @param {string} [opts.label] - Human-friendly label
|
|
107
|
+
* @param {number} [opts.expires] - Expiration timestamp (ms)
|
|
108
|
+
* @param {number} [opts.created] - Creation timestamp (ms)
|
|
109
|
+
*/
|
|
110
|
+
constructor({ identity, templateName, scopes, quotas, label, expires, created }) {
|
|
111
|
+
this.identity = identity;
|
|
112
|
+
this.templateName = templateName;
|
|
113
|
+
this.scopes = scopes ? [...scopes] : undefined;
|
|
114
|
+
this.quotas = quotas ? { ...quotas } : undefined;
|
|
115
|
+
this.label = label;
|
|
116
|
+
this.expires = expires;
|
|
117
|
+
this.created = created ?? Date.now();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @param {number} [now]
|
|
122
|
+
* @returns {boolean}
|
|
123
|
+
*/
|
|
124
|
+
isExpired(now = Date.now()) {
|
|
125
|
+
if (this.expires == null) return false;
|
|
126
|
+
return now >= this.expires;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
toJSON() {
|
|
130
|
+
return {
|
|
131
|
+
identity: this.identity,
|
|
132
|
+
templateName: this.templateName,
|
|
133
|
+
scopes: this.scopes ? [...this.scopes] : undefined,
|
|
134
|
+
quotas: this.quotas ? { ...this.quotas } : undefined,
|
|
135
|
+
label: this.label,
|
|
136
|
+
expires: this.expires,
|
|
137
|
+
created: this.created,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
static fromJSON(data) {
|
|
142
|
+
return new RosterEntry({
|
|
143
|
+
identity: data.identity,
|
|
144
|
+
templateName: data.templateName,
|
|
145
|
+
scopes: data.scopes,
|
|
146
|
+
quotas: data.quotas,
|
|
147
|
+
label: data.label,
|
|
148
|
+
expires: data.expires,
|
|
149
|
+
created: data.created,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ---------------------------------------------------------------------------
|
|
155
|
+
// InvitationToken
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
/** @type {number} */
|
|
159
|
+
let _nonceSeq = 0;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Single-use invitation token.
|
|
163
|
+
*/
|
|
164
|
+
export class InvitationToken {
|
|
165
|
+
/**
|
|
166
|
+
* @param {object} opts
|
|
167
|
+
* @param {string} opts.owner - Fingerprint of the inviter
|
|
168
|
+
* @param {string} opts.templateName - Template to grant on redemption
|
|
169
|
+
* @param {number} [opts.expires] - Expiration timestamp (ms), default 15 min from now
|
|
170
|
+
* @param {string} [opts.nonce] - Unique nonce, auto-generated if omitted
|
|
171
|
+
*/
|
|
172
|
+
constructor({ owner, templateName, expires, nonce }) {
|
|
173
|
+
this.owner = owner;
|
|
174
|
+
this.templateName = templateName;
|
|
175
|
+
this.expires = expires ?? (Date.now() + 15 * 60 * 1000);
|
|
176
|
+
this.nonce = nonce ?? `inv_${Date.now().toString(36)}_${(++_nonceSeq).toString(36)}`;
|
|
177
|
+
/** @type {boolean} */
|
|
178
|
+
this._used = false;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** @param {number} [now] */
|
|
182
|
+
isExpired(now = Date.now()) {
|
|
183
|
+
return now >= this.expires;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
isUsed() {
|
|
187
|
+
return this._used;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
markUsed() {
|
|
191
|
+
this._used = true;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
toJSON() {
|
|
195
|
+
return {
|
|
196
|
+
owner: this.owner,
|
|
197
|
+
templateName: this.templateName,
|
|
198
|
+
expires: this.expires,
|
|
199
|
+
nonce: this.nonce,
|
|
200
|
+
used: this._used,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
static fromJSON(data) {
|
|
205
|
+
const tok = new InvitationToken({
|
|
206
|
+
owner: data.owner,
|
|
207
|
+
templateName: data.templateName,
|
|
208
|
+
expires: data.expires,
|
|
209
|
+
nonce: data.nonce,
|
|
210
|
+
});
|
|
211
|
+
tok._used = data.used ?? false;
|
|
212
|
+
return tok;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ---------------------------------------------------------------------------
|
|
217
|
+
// MeshACL
|
|
218
|
+
// ---------------------------------------------------------------------------
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Top-level remote access control manager.
|
|
222
|
+
*
|
|
223
|
+
* Combines templates (scope bundles), a roster (identity→template mapping),
|
|
224
|
+
* an internal ACLEngine for access checks, and invitation tokens.
|
|
225
|
+
*/
|
|
226
|
+
export class MeshACL {
|
|
227
|
+
/**
|
|
228
|
+
* @param {object} opts
|
|
229
|
+
* @param {string} opts.owner - Fingerprint of the workspace owner
|
|
230
|
+
* @param {function} [opts.onLog]
|
|
231
|
+
*/
|
|
232
|
+
constructor({ owner, onLog }) {
|
|
233
|
+
this.owner = owner;
|
|
234
|
+
this._onLog = onLog || (() => {});
|
|
235
|
+
|
|
236
|
+
/** @type {Map<string, ScopeTemplate>} */
|
|
237
|
+
this._templates = new Map();
|
|
238
|
+
/** @type {Map<string, RosterEntry>} */
|
|
239
|
+
this._roster = new Map();
|
|
240
|
+
/** @type {ACLEngine} */
|
|
241
|
+
this._engine = new ACLEngine();
|
|
242
|
+
/** @type {Map<string, InvitationToken>} */
|
|
243
|
+
this._invitations = new Map();
|
|
244
|
+
|
|
245
|
+
// Seed default templates
|
|
246
|
+
for (const [name, tpl] of Object.entries(DEFAULT_TEMPLATES)) {
|
|
247
|
+
this._templates.set(name, tpl);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ── Template management ────────────────────────────────────────────
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* @param {string} name
|
|
255
|
+
* @param {string[]} scopes
|
|
256
|
+
* @param {string} [description]
|
|
257
|
+
* @returns {ScopeTemplate}
|
|
258
|
+
*/
|
|
259
|
+
addTemplate(name, scopes, description) {
|
|
260
|
+
const t = new ScopeTemplate({ name, scopes, description });
|
|
261
|
+
this._templates.set(name, t);
|
|
262
|
+
return t;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** @param {string} name @returns {boolean} */
|
|
266
|
+
removeTemplate(name) {
|
|
267
|
+
return this._templates.delete(name);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** @param {string} name @returns {ScopeTemplate|null} */
|
|
271
|
+
getTemplate(name) {
|
|
272
|
+
return this._templates.get(name) ?? null;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** @returns {ScopeTemplate[]} */
|
|
276
|
+
listTemplates() {
|
|
277
|
+
return [...this._templates.values()];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ── Roster management ──────────────────────────────────────────────
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* @param {string} identity
|
|
284
|
+
* @param {string} templateName
|
|
285
|
+
* @param {object} [opts]
|
|
286
|
+
* @returns {RosterEntry}
|
|
287
|
+
*/
|
|
288
|
+
addEntry(identity, templateName, opts = {}) {
|
|
289
|
+
const tpl = this._templates.get(templateName);
|
|
290
|
+
if (!tpl) throw new Error(`Unknown template: ${templateName}`);
|
|
291
|
+
|
|
292
|
+
const entry = new RosterEntry({
|
|
293
|
+
identity,
|
|
294
|
+
templateName,
|
|
295
|
+
label: opts.label,
|
|
296
|
+
quotas: opts.quotas,
|
|
297
|
+
expires: opts.expires,
|
|
298
|
+
});
|
|
299
|
+
this._roster.set(identity, entry);
|
|
300
|
+
|
|
301
|
+
// Create internal ACLEngine grant
|
|
302
|
+
this._syncGrant(identity, tpl, entry);
|
|
303
|
+
|
|
304
|
+
return entry;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** @param {string} identity @returns {boolean} */
|
|
308
|
+
removeEntry(identity) {
|
|
309
|
+
const had = this._roster.delete(identity);
|
|
310
|
+
if (had) {
|
|
311
|
+
this._engine.revokeAll(identity);
|
|
312
|
+
}
|
|
313
|
+
return had;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** @param {string} identity @returns {RosterEntry|null} */
|
|
317
|
+
getEntry(identity) {
|
|
318
|
+
return this._roster.get(identity) ?? null;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** @returns {RosterEntry[]} */
|
|
322
|
+
listEntries() {
|
|
323
|
+
return [...this._roster.values()];
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// ── Access checking ────────────────────────────────────────────────
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Check if an identity is allowed to perform an action on a resource.
|
|
330
|
+
* Owner is always allowed.
|
|
331
|
+
*
|
|
332
|
+
* @param {string} identity
|
|
333
|
+
* @param {string} resource
|
|
334
|
+
* @param {string} action
|
|
335
|
+
* @returns {{ allowed: boolean, reason?: string }}
|
|
336
|
+
*/
|
|
337
|
+
check(identity, resource, action) {
|
|
338
|
+
// Owner bypass
|
|
339
|
+
if (identity === this.owner) {
|
|
340
|
+
return { allowed: true, reason: 'owner' };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Check roster entry expiry
|
|
344
|
+
const entry = this._roster.get(identity);
|
|
345
|
+
if (!entry) {
|
|
346
|
+
return { allowed: false, reason: 'not_in_roster' };
|
|
347
|
+
}
|
|
348
|
+
if (entry.isExpired()) {
|
|
349
|
+
return { allowed: false, reason: 'entry_expired' };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Check via template scope matching
|
|
353
|
+
const tpl = this._templates.get(entry.templateName);
|
|
354
|
+
if (!tpl) {
|
|
355
|
+
return { allowed: false, reason: 'template_missing' };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Build the scope string as resource:action
|
|
359
|
+
const scope = `${resource}:${action}`;
|
|
360
|
+
if (tpl.matches(scope)) {
|
|
361
|
+
return { allowed: true };
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
return { allowed: false, reason: 'scope_denied' };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// ── Invitation flow ────────────────────────────────────────────────
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* @param {string} templateName
|
|
371
|
+
* @param {object} [opts]
|
|
372
|
+
* @param {number} [opts.expires]
|
|
373
|
+
* @returns {InvitationToken}
|
|
374
|
+
*/
|
|
375
|
+
createInvitation(templateName, opts = {}) {
|
|
376
|
+
if (!this._templates.has(templateName)) {
|
|
377
|
+
throw new Error(`Unknown template: ${templateName}`);
|
|
378
|
+
}
|
|
379
|
+
const tok = new InvitationToken({
|
|
380
|
+
owner: this.owner,
|
|
381
|
+
templateName,
|
|
382
|
+
expires: opts.expires,
|
|
383
|
+
});
|
|
384
|
+
this._invitations.set(tok.nonce, tok);
|
|
385
|
+
return tok;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Redeem an invitation, creating a roster entry.
|
|
390
|
+
* @param {InvitationToken} token
|
|
391
|
+
* @param {string} identity
|
|
392
|
+
* @returns {RosterEntry}
|
|
393
|
+
*/
|
|
394
|
+
redeemInvitation(token, identity) {
|
|
395
|
+
if (token.isExpired()) {
|
|
396
|
+
throw new Error('Invitation expired');
|
|
397
|
+
}
|
|
398
|
+
if (token.isUsed()) {
|
|
399
|
+
throw new Error('Invitation already used');
|
|
400
|
+
}
|
|
401
|
+
token.markUsed();
|
|
402
|
+
return this.addEntry(identity, token.templateName);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// ── Revocation ─────────────────────────────────────────────────────
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Update an existing roster entry with a new template.
|
|
409
|
+
* @param {string} identity
|
|
410
|
+
* @param {string} templateName
|
|
411
|
+
* @returns {RosterEntry|null}
|
|
412
|
+
*/
|
|
413
|
+
updateEntry(identity, templateName) {
|
|
414
|
+
if (!this._roster.has(identity)) return null;
|
|
415
|
+
this._engine.revokeAll(identity);
|
|
416
|
+
this._roster.delete(identity);
|
|
417
|
+
return this.addEntry(identity, templateName);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** Convenience alias for {@link addEntry}. */
|
|
421
|
+
grant(identity, templateName, opts) { return this.addEntry(identity, templateName, opts) }
|
|
422
|
+
|
|
423
|
+
/** Convenience alias for {@link revokeAll}. */
|
|
424
|
+
revoke(identity) { return this.revokeAll(identity) }
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Revoke all access for an identity and remove from roster.
|
|
428
|
+
* @param {string} identity
|
|
429
|
+
* @returns {number}
|
|
430
|
+
*/
|
|
431
|
+
revokeAll(identity) {
|
|
432
|
+
const had = this._roster.has(identity);
|
|
433
|
+
this._roster.delete(identity);
|
|
434
|
+
const count = this._engine.revokeAll(identity);
|
|
435
|
+
return had ? Math.max(count, 1) : 0;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// ── Maintenance ────────────────────────────────────────────────────
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Remove expired roster entries.
|
|
442
|
+
* @param {number} [now]
|
|
443
|
+
* @returns {number}
|
|
444
|
+
*/
|
|
445
|
+
pruneExpired(now = Date.now()) {
|
|
446
|
+
let count = 0;
|
|
447
|
+
for (const [id, entry] of this._roster) {
|
|
448
|
+
if (entry.isExpired(now)) {
|
|
449
|
+
this._roster.delete(id);
|
|
450
|
+
this._engine.revokeAll(id);
|
|
451
|
+
count++;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return count;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// ── Serialization ──────────────────────────────────────────────────
|
|
458
|
+
|
|
459
|
+
toJSON() {
|
|
460
|
+
return {
|
|
461
|
+
owner: this.owner,
|
|
462
|
+
templates: [...this._templates.values()]
|
|
463
|
+
.filter(t => !DEFAULT_TEMPLATES[t.name]) // only custom templates
|
|
464
|
+
.map(t => t.toJSON()),
|
|
465
|
+
roster: [...this._roster.values()].map(e => e.toJSON()),
|
|
466
|
+
invitations: [...this._invitations.values()].map(t => t.toJSON()),
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
static fromJSON(data) {
|
|
471
|
+
const acl = new MeshACL({ owner: data.owner });
|
|
472
|
+
|
|
473
|
+
// Restore custom templates
|
|
474
|
+
if (data.templates) {
|
|
475
|
+
for (const td of data.templates) {
|
|
476
|
+
const t = ScopeTemplate.fromJSON(td);
|
|
477
|
+
acl._templates.set(t.name, t);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// Restore roster entries (with engine grants)
|
|
482
|
+
if (data.roster) {
|
|
483
|
+
for (const rd of data.roster) {
|
|
484
|
+
const entry = RosterEntry.fromJSON(rd);
|
|
485
|
+
acl._roster.set(entry.identity, entry);
|
|
486
|
+
const tpl = acl._templates.get(entry.templateName);
|
|
487
|
+
if (tpl) {
|
|
488
|
+
acl._syncGrant(entry.identity, tpl, entry);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// Restore invitations
|
|
494
|
+
if (data.invitations) {
|
|
495
|
+
for (const td of data.invitations) {
|
|
496
|
+
const tok = InvitationToken.fromJSON(td);
|
|
497
|
+
acl._invitations.set(tok.nonce, tok);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
return acl;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// ── Internal helpers ───────────────────────────────────────────────
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Sync an ACLEngine grant for an identity based on its template.
|
|
508
|
+
* @param {string} identity
|
|
509
|
+
* @param {ScopeTemplate} tpl
|
|
510
|
+
* @param {RosterEntry} entry
|
|
511
|
+
*/
|
|
512
|
+
_syncGrant(identity, tpl, entry) {
|
|
513
|
+
const permissions = tpl.scopes.map(scope => {
|
|
514
|
+
const parts = scope.split(':');
|
|
515
|
+
const resource = parts[0] === '*' ? '*' : parts[0];
|
|
516
|
+
const action = parts[1] === '*' ? '*' : (parts[1] || '*');
|
|
517
|
+
return new Permission({ resource, actions: [action] });
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
const grant = new AccessGrant({
|
|
521
|
+
id: generateGrantId(),
|
|
522
|
+
grantee: identity,
|
|
523
|
+
grantor: this.owner,
|
|
524
|
+
permissions,
|
|
525
|
+
conditions: entry.expires ? { expires: entry.expires } : {},
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
this._engine.addGrant(grant);
|
|
529
|
+
}
|
|
530
|
+
}
|