@keyneom/sync-kit 0.2.0-rc.1 → 0.2.0-rc.10

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.
Files changed (35) hide show
  1. package/dist/sharing/appdata-identity-store.d.ts +36 -0
  2. package/dist/sharing/appdata-identity-store.d.ts.map +1 -0
  3. package/dist/sharing/appdata-identity-store.js +49 -0
  4. package/dist/sharing/appdata-identity-store.js.map +1 -0
  5. package/dist/sharing/controller.d.ts +58 -3
  6. package/dist/sharing/controller.d.ts.map +1 -1
  7. package/dist/sharing/controller.js +316 -117
  8. package/dist/sharing/controller.js.map +1 -1
  9. package/dist/sharing/index.d.ts +1 -0
  10. package/dist/sharing/index.d.ts.map +1 -1
  11. package/dist/sharing/index.js +1 -0
  12. package/dist/sharing/index.js.map +1 -1
  13. package/dist/sharing/link-exchange.d.ts +51 -0
  14. package/dist/sharing/link-exchange.d.ts.map +1 -0
  15. package/dist/sharing/link-exchange.js +131 -0
  16. package/dist/sharing/link-exchange.js.map +1 -0
  17. package/dist/sharing/migrating-identity-store.d.ts +29 -0
  18. package/dist/sharing/migrating-identity-store.d.ts.map +1 -0
  19. package/dist/sharing/migrating-identity-store.js +37 -0
  20. package/dist/sharing/migrating-identity-store.js.map +1 -0
  21. package/dist/sharing/transport.d.ts +2 -0
  22. package/dist/sharing/transport.d.ts.map +1 -1
  23. package/dist/stores/google-drive/index.d.ts +20 -0
  24. package/dist/stores/google-drive/index.d.ts.map +1 -1
  25. package/dist/stores/google-drive/index.js +64 -0
  26. package/dist/stores/google-drive/index.js.map +1 -1
  27. package/dist/stores/google-drive/picker.d.ts +20 -0
  28. package/dist/stores/google-drive/picker.d.ts.map +1 -1
  29. package/dist/stores/google-drive/picker.js +64 -0
  30. package/dist/stores/google-drive/picker.js.map +1 -1
  31. package/dist/stores/google-drive/sharing.d.ts +2 -0
  32. package/dist/stores/google-drive/sharing.d.ts.map +1 -1
  33. package/dist/stores/google-drive/sharing.js +83 -6
  34. package/dist/stores/google-drive/sharing.js.map +1 -1
  35. package/package.json +15 -3
@@ -3,6 +3,13 @@ import { canAdministerSharedBackup, parseSharedBackupEnvelopeV1, sharedBackupPar
3
3
  import { acceptSharingPublicKeyResponseV1, createSharedBackupEnvelopeV1, createSharingInvitationV1, createSharingPublicKeyResponseV1, decryptSharedBackupEnvelopeV1, verifySharedBackupEnvelopeV1, verifySharingInvitationV1, } from "./web-crypto.js";
4
4
  import { formatSharingInviteEmailMessage, appendSharingJoinParams } from "./join.js";
5
5
  export { IndexedDbSharedBackupRegistry } from "./registry-indexeddb.js";
6
+ /**
7
+ * Placeholder recipient permission id for the link-carried exchange, which has
8
+ * no Drive exchange grant. Set identically on the invitation and passed to
9
+ * accept, so the anti-substitution equality check passes; account provenance in
10
+ * this flow comes from the response signature, not a Drive permission.
11
+ */
12
+ const SHARING_LINK_PERMISSION_ID = "link";
6
13
  export class MemorySharedBackupRegistry {
7
14
  records = new Map();
8
15
  get(datasetId) {
@@ -56,6 +63,49 @@ export class SharedBackupController {
56
63
  return result(stored, value, "created");
57
64
  });
58
65
  }
66
+ /**
67
+ * Reconnect to an existing dataset this identity can already decrypt —
68
+ * e.g. after a reinstall, or when an interrupted setup left the remote
69
+ * file without a local registry record. The owner key is pinned from the
70
+ * envelope itself (trust-on-first-use), so callers recovering their own
71
+ * dataset should pass `requireOwned` to insist this identity is the
72
+ * dataset's owner.
73
+ */
74
+ adoptDataset(datasetId, options) {
75
+ return this.serialized(async () => {
76
+ const stored = await this.readDatasetById(datasetId);
77
+ const previous = await this.options.registry.get(datasetId);
78
+ const record = previous ?? this.initialOwnerRecord(stored);
79
+ await verifySharedBackupEnvelopeV1(stored.envelope, this.crypto(), {
80
+ trustedOwnerKeyId: record.trustedOwnerKeyId,
81
+ });
82
+ const identity = await this.options.identity();
83
+ if (options?.requireOwned) {
84
+ const self = sharedBackupParticipant(stored.envelope, identity.publicKey.keyId);
85
+ if (self?.role !== "owner") {
86
+ throw new SyncKitError("authorization", `This identity does not own dataset ${datasetId}.`);
87
+ }
88
+ }
89
+ const value = await decryptSharedBackupEnvelopeV1(stored.envelope, this.options.codec, identity, this.crypto(), { trustedOwnerKeyId: record.trustedOwnerKeyId });
90
+ await this.persistHead(stored, record.trustedOwnerKeyId, previous ?? undefined);
91
+ return result(stored, value, "adopted");
92
+ });
93
+ }
94
+ /** Delete a dataset file from the transport and forget its local record. */
95
+ deleteDataset(datasetId) {
96
+ return this.serialized(async () => {
97
+ requireNonEmpty(datasetId, "datasetId");
98
+ const fileId = (await this.options.registry.get(datasetId))?.fileId ??
99
+ (await this.options.transport.listDatasets()).find((candidate) => candidate.datasetId === datasetId)?.fileId;
100
+ if (fileId) {
101
+ if (!this.options.transport.deleteDataset) {
102
+ throw new SyncKitError("state", "This transport does not support deleting datasets.");
103
+ }
104
+ await this.options.transport.deleteDataset(fileId);
105
+ }
106
+ await this.options.registry.delete(datasetId);
107
+ });
108
+ }
59
109
  loadDataset(datasetId) {
60
110
  return this.serialized(async () => {
61
111
  const stored = await this.readDatasetById(datasetId);
@@ -246,128 +296,301 @@ export class SharedBackupController {
246
296
  if (verifiedAccount) {
247
297
  await this.options.transport.deleteExchange(input.responseFileId);
248
298
  }
249
- const results = [];
250
- for (const grant of accepted) {
251
- try {
252
- const stored = await this.readDatasetById(grant.datasetId);
253
- const record = (await this.options.registry.get(grant.datasetId)) ??
254
- this.initialOwnerRecord(stored);
255
- await this.verifyHead(stored, record);
256
- const value = await decryptSharedBackupEnvelopeV1(stored.envelope, this.options.codec, identity, this.crypto(), { trustedOwnerKeyId: record.trustedOwnerKeyId });
257
- const participants = participantInputs(stored.envelope).filter((participant) => participant.publicKey.keyId !==
258
- grant.participant.publicKey.keyId);
259
- participants.push(grant.participant);
260
- const next = await createSharedBackupEnvelopeV1(value, this.options.codec, identity, {
261
- appId: this.options.appId,
262
- backupId: grant.datasetId,
263
- participants,
264
- previous: stored.envelope,
265
- }, this.cryptoOptions());
266
- const updated = await this.options.transport.writeDataset(stored, next);
267
- const permission = await this.options.transport.setDatasetPermission(stored.fileId, input.recipientEmailAddress, grant.participant.role === "owner"
268
- ? "admin"
269
- : grant.participant.role, {
270
- hasInheritedReadAccess: true,
271
- });
272
- const participantPermissionIds = {
273
- ...record.participantPermissionIds,
274
- ...(permission.permissionId
275
- ? {
276
- [grant.participant.publicKey.keyId]: permission.permissionId,
277
- }
278
- : {}),
279
- };
280
- const nextRecord = {
281
- ...record,
282
- fileId: updated.fileId,
283
- lastRevisionId: updated.envelope.revisionId,
284
- participantPermissionIds,
285
- };
286
- await this.options.registry.set(nextRecord);
287
- results.push({
288
- datasetId: grant.datasetId,
289
- fileId: updated.fileId,
290
- revisionId: updated.envelope.revisionId,
291
- ...(permission.permissionId
292
- ? { permissionId: permission.permissionId }
293
- : {}),
294
- status: "accepted",
295
- });
299
+ return this.applyAcceptedGrants(accepted, identity, input.recipientEmailAddress);
300
+ });
301
+ }
302
+ /**
303
+ * Applies verified acceptance grants to each dataset: add the participant,
304
+ * re-encrypt, write, and per-email-share the dataset file. Shared by the
305
+ * Drive-file accept path and the link-payload accept path.
306
+ */
307
+ async applyAcceptedGrants(accepted, identity, recipientEmailAddress) {
308
+ const results = [];
309
+ for (const grant of accepted) {
310
+ try {
311
+ const stored = await this.readDatasetById(grant.datasetId);
312
+ const record = (await this.options.registry.get(grant.datasetId)) ??
313
+ this.initialOwnerRecord(stored);
314
+ await this.verifyHead(stored, record);
315
+ const value = await decryptSharedBackupEnvelopeV1(stored.envelope, this.options.codec, identity, this.crypto(), { trustedOwnerKeyId: record.trustedOwnerKeyId });
316
+ const participants = participantInputs(stored.envelope).filter((participant) => participant.publicKey.keyId !==
317
+ grant.participant.publicKey.keyId);
318
+ participants.push(grant.participant);
319
+ const next = await createSharedBackupEnvelopeV1(value, this.options.codec, identity, {
320
+ appId: this.options.appId,
321
+ backupId: grant.datasetId,
322
+ participants,
323
+ previous: stored.envelope,
324
+ }, this.cryptoOptions());
325
+ const updated = await this.options.transport.writeDataset(stored, next);
326
+ const permission = await this.options.transport.setDatasetPermission(stored.fileId, recipientEmailAddress, grant.participant.role === "owner"
327
+ ? "admin"
328
+ : grant.participant.role, {
329
+ hasInheritedReadAccess: true,
330
+ });
331
+ const participantPermissionIds = {
332
+ ...record.participantPermissionIds,
333
+ ...(permission.permissionId
334
+ ? {
335
+ [grant.participant.publicKey.keyId]: permission.permissionId,
336
+ }
337
+ : {}),
338
+ };
339
+ const nextRecord = {
340
+ ...record,
341
+ fileId: updated.fileId,
342
+ lastRevisionId: updated.envelope.revisionId,
343
+ participantPermissionIds,
344
+ };
345
+ await this.options.registry.set(nextRecord);
346
+ results.push({
347
+ datasetId: grant.datasetId,
348
+ fileId: updated.fileId,
349
+ revisionId: updated.envelope.revisionId,
350
+ ...(permission.permissionId
351
+ ? { permissionId: permission.permissionId }
352
+ : {}),
353
+ status: "accepted",
354
+ });
355
+ }
356
+ catch (error) {
357
+ results.push({
358
+ datasetId: grant.datasetId,
359
+ status: "failed",
360
+ error,
361
+ });
362
+ }
363
+ }
364
+ return results;
365
+ }
366
+ /**
367
+ * Owner side of the link-carried exchange. Shares each granted dataset file
368
+ * with the recipient's email (so it lands in their Picker) and returns the
369
+ * signed invitation plus the file list to embed in the join link. Unlike
370
+ * {@link inviteParticipant} it writes no Drive `exchanges/` invitation file.
371
+ */
372
+ inviteParticipantForLink(input) {
373
+ return this.serialized(async () => {
374
+ const identity = await this.options.identity();
375
+ const trustedOwnerKeyIds = new Set();
376
+ const files = [];
377
+ for (const grant of input.requestedGrants) {
378
+ const stored = await this.readDatasetById(grant.datasetId);
379
+ const record = await this.requiredRegistry(grant.datasetId);
380
+ const verified = await verifySharedBackupEnvelopeV1(stored.envelope, this.crypto(), { trustedOwnerKeyId: record.trustedOwnerKeyId });
381
+ const actor = sharedBackupParticipant(verified, identity.publicKey.keyId);
382
+ if (!actor || !canAdministerSharedBackup(actor.role)) {
383
+ throw new SyncKitError("authorization", `This identity cannot invite participants to ${grant.datasetId}.`);
296
384
  }
297
- catch (error) {
298
- results.push({
385
+ trustedOwnerKeyIds.add(record.trustedOwnerKeyId);
386
+ await this.options.transport.setDatasetPermission(stored.fileId, input.emailAddress, grant.role, { hasInheritedReadAccess: false });
387
+ files.push({
388
+ datasetId: grant.datasetId,
389
+ fileId: stored.fileId,
390
+ role: grant.role,
391
+ });
392
+ }
393
+ if (trustedOwnerKeyIds.size !== 1) {
394
+ throw new SyncKitError("configuration", "One invitation cannot combine datasets with different owner trust roots.");
395
+ }
396
+ const trustedOwnerKeyId = [...trustedOwnerKeyIds][0];
397
+ if (!trustedOwnerKeyId) {
398
+ throw new SyncKitError("state", "The invitation has no owner trust root.");
399
+ }
400
+ const storage = await this.options.transport.ensureStorage();
401
+ const invitation = await createSharingInvitationV1(identity, {
402
+ appId: this.options.appId,
403
+ appFolderId: storage.appFolderId,
404
+ recipientDrivePermissionId: SHARING_LINK_PERMISSION_ID,
405
+ requestedGrants: input.requestedGrants,
406
+ trustedOwnerKeyId,
407
+ ...(input.expiresAt ? { expiresAt: input.expiresAt } : {}),
408
+ }, this.cryptoOptions());
409
+ return { invitation, files };
410
+ });
411
+ }
412
+ /**
413
+ * Recipient side of the link-carried exchange. Verifies the invitation carried
414
+ * in the join link, registers the granted datasets with their file ids (so
415
+ * later reads/writes resolve without listing the owner's folder), and returns
416
+ * a signed key response to send back to the owner. Reads/writes no Drive
417
+ * `exchanges/` file.
418
+ */
419
+ submitKeyResponseFromInvitation(invitation, datasetFiles) {
420
+ return this.serialized(async () => {
421
+ const verified = await verifySharingInvitationV1(invitation, {
422
+ crypto: this.crypto(),
423
+ ...(this.options.now ? { now: this.options.now } : {}),
424
+ });
425
+ if (verified.appId !== this.options.appId) {
426
+ throw new SyncKitError("compatibility", "The invitation belongs to another app.");
427
+ }
428
+ const identity = await this.options.identity();
429
+ const accountBinding = await this.options.createAccountBinding?.({
430
+ appId: this.options.appId,
431
+ exchangeId: verified.exchangeId,
432
+ sharingKeyId: identity.publicKey.keyId,
433
+ });
434
+ const response = await createSharingPublicKeyResponseV1(identity, {
435
+ appId: this.options.appId,
436
+ exchangeId: verified.exchangeId,
437
+ ...(accountBinding ? { accountBinding } : {}),
438
+ }, this.cryptoOptions());
439
+ const fileById = new Map(datasetFiles.map((file) => [file.datasetId, file.fileId]));
440
+ for (const grant of verified.requestedGrants) {
441
+ const existing = await this.options.registry.get(grant.datasetId);
442
+ if (existing &&
443
+ existing.trustedOwnerKeyId !== verified.trustedOwnerKeyId) {
444
+ throw new SyncKitError("conflict", `Dataset ${grant.datasetId} is already pinned to another owner key.`);
445
+ }
446
+ const fileId = fileById.get(grant.datasetId);
447
+ await this.options.registry.set(existing
448
+ ? { ...existing, ...(fileId ? { fileId } : {}) }
449
+ : {
299
450
  datasetId: grant.datasetId,
300
- status: "failed",
301
- error,
451
+ ...(fileId ? { fileId } : {}),
452
+ trustedOwnerKeyId: verified.trustedOwnerKeyId,
302
453
  });
303
- }
304
454
  }
305
- return results;
455
+ return response;
456
+ });
457
+ }
458
+ /**
459
+ * Owner side. Accepts a key response carried in a response link (no Drive
460
+ * `exchanges/` read), adds the recipient to each granted dataset, and
461
+ * per-email shares the dataset files (re-affirming the invite-time share).
462
+ * The invitation must be supplied by the caller (persisted at invite time,
463
+ * keyed by exchange id).
464
+ */
465
+ acceptKeyResponseFromPayload(input) {
466
+ return this.serialized(async () => {
467
+ const identity = await this.options.identity();
468
+ const binding = input.response.accountBinding;
469
+ if (this.options.requireAccountBinding && !binding) {
470
+ throw new SyncKitError("authorization", "The key response has no required account binding.");
471
+ }
472
+ const verifiedAccount = binding && this.options.verifyAccountBinding
473
+ ? await this.options.verifyAccountBinding(binding, {
474
+ appId: this.options.appId,
475
+ exchangeId: input.invitation.exchangeId,
476
+ sharingKeyId: input.response.keyId,
477
+ credentialId: binding.passkey.credentialId,
478
+ })
479
+ : undefined;
480
+ const accepted = await acceptSharingPublicKeyResponseV1(input.invitation, input.response, {
481
+ acceptedByKeyId: identity.publicKey.keyId,
482
+ drivePermissionId: input.invitation.recipientDrivePermissionId,
483
+ ...(verifiedAccount ? { googleSubject: verifiedAccount.subject } : {}),
484
+ }, this.cryptoOptions());
485
+ return this.applyAcceptedGrants(accepted, identity, input.recipientEmailAddress);
306
486
  });
307
487
  }
308
488
  setDatasetRole(input) {
309
- return this.changeParticipants(input.datasetId, (stored, value) => {
489
+ return this.serialized(async () => {
490
+ requireNonEmpty(input.datasetId, "datasetId");
491
+ requireNonEmpty(input.keyId, "keyId");
492
+ requireNonEmpty(input.emailAddress, "emailAddress");
493
+ const stored = await this.readDatasetById(input.datasetId);
494
+ const record = await this.requiredRegistry(input.datasetId);
495
+ await this.verifyHead(stored, record);
496
+ const identity = await this.options.identity();
497
+ const actor = sharedBackupParticipant(stored.envelope, identity.publicKey.keyId);
498
+ if (!actor || !canAdministerSharedBackup(actor.role)) {
499
+ throw new SyncKitError("authorization", "Only a current owner or admin can change dataset access.");
500
+ }
501
+ const value = await decryptSharedBackupEnvelopeV1(stored.envelope, this.options.codec, identity, this.crypto(), { trustedOwnerKeyId: record.trustedOwnerKeyId });
310
502
  const participants = participantInputs(stored.envelope);
311
503
  const participant = participants.find((candidate) => candidate.publicKey.keyId === input.keyId);
312
504
  if (!participant) {
313
505
  throw new SyncKitError("not-found", `Participant ${input.keyId} is not in this dataset.`);
314
506
  }
315
507
  participant.role = input.role;
316
- return Promise.resolve({
508
+ const existingPermission = await this.findDirectDatasetPermission(stored.fileId, record.participantPermissionIds?.[input.keyId], input.emailAddress);
509
+ const permission = await this.options.transport.setDatasetPermission(stored.fileId, input.emailAddress, input.role, {
510
+ ...(existingPermission
511
+ ? { existingDirectPermissionId: existingPermission.permissionId }
512
+ : {}),
513
+ ...(input.role === "viewer"
514
+ ? { hasInheritedReadAccess: true }
515
+ : {}),
516
+ });
517
+ const participantPermissionIds = permission.permissionId
518
+ ? {
519
+ ...record.participantPermissionIds,
520
+ [input.keyId]: permission.permissionId,
521
+ }
522
+ : Object.fromEntries(Object.entries(record.participantPermissionIds ?? {}).filter(([keyId]) => keyId !== input.keyId));
523
+ const next = await createSharedBackupEnvelopeV1(value, this.options.codec, identity, {
524
+ appId: this.options.appId,
525
+ backupId: input.datasetId,
317
526
  participants,
318
- value,
319
- afterWrite: async (updated, record) => {
320
- const permission = await this.options.transport.setDatasetPermission(updated.fileId, input.emailAddress, input.role, {
321
- ...(record.participantPermissionIds?.[input.keyId]
322
- ? {
323
- existingDirectPermissionId: record.participantPermissionIds[input.keyId],
324
- }
325
- : {}),
326
- ...(input.role === "viewer"
327
- ? { hasInheritedReadAccess: true }
328
- : {}),
329
- });
330
- const participantPermissionIds = {
331
- ...record.participantPermissionIds,
332
- ...(permission.permissionId
333
- ? { [input.keyId]: permission.permissionId }
334
- : {}),
335
- };
336
- await this.options.registry.set({
337
- ...record,
338
- participantPermissionIds,
339
- });
340
- },
527
+ previous: stored.envelope,
528
+ }, this.cryptoOptions());
529
+ const updated = await this.options.transport.writeDataset(stored, next);
530
+ await this.persistHead(updated, record.trustedOwnerKeyId, {
531
+ ...record,
532
+ participantPermissionIds,
341
533
  });
534
+ return result(updated, value, "updated");
342
535
  });
343
536
  }
344
537
  revokeDatasetKey(input) {
345
- return this.changeParticipants(input.datasetId, (stored, value) => {
538
+ return this.serialized(async () => {
539
+ const stored = await this.readDatasetById(input.datasetId);
540
+ const record = await this.requiredRegistry(input.datasetId);
541
+ await this.verifyHead(stored, record);
542
+ const identity = await this.options.identity();
543
+ const actor = sharedBackupParticipant(stored.envelope, identity.publicKey.keyId);
544
+ if (!actor || !canAdministerSharedBackup(actor.role)) {
545
+ throw new SyncKitError("authorization", "Only a current owner or admin can change dataset access.");
546
+ }
547
+ const value = await decryptSharedBackupEnvelopeV1(stored.envelope, this.options.codec, identity, this.crypto(), { trustedOwnerKeyId: record.trustedOwnerKeyId });
346
548
  const participant = sharedBackupParticipant(stored.envelope, input.keyId);
347
549
  if (!participant) {
348
- throw new SyncKitError("not-found", `Participant ${input.keyId} is not in this dataset.`);
550
+ const permission = await this.findDirectDatasetPermission(stored.fileId, record.participantPermissionIds?.[input.keyId], input.emailAddress);
551
+ if (permission) {
552
+ await this.options.transport.removeDatasetPermission(stored.fileId, permission.permissionId);
553
+ }
554
+ await this.options.registry.set({
555
+ ...record,
556
+ participantPermissionIds: Object.fromEntries(Object.entries(record.participantPermissionIds ?? {}).filter(([keyId]) => keyId !== input.keyId)),
557
+ });
558
+ return result(stored, value, "unchanged");
349
559
  }
350
560
  if (participant.role === "owner") {
351
561
  throw new SyncKitError("authorization", "Owner transfer or removal is not supported by sharing v1.");
352
562
  }
563
+ const permission = await this.findDirectDatasetPermission(stored.fileId, record.participantPermissionIds?.[input.keyId], input.emailAddress);
564
+ if (permission) {
565
+ await this.options.transport.removeDatasetPermission(stored.fileId, permission.permissionId);
566
+ }
353
567
  const participants = participantInputs(stored.envelope).filter((candidate) => candidate.publicKey.keyId !== input.keyId);
354
- return Promise.resolve({
568
+ const next = await createSharedBackupEnvelopeV1(value, this.options.codec, identity, {
569
+ appId: this.options.appId,
570
+ backupId: input.datasetId,
355
571
  participants,
356
- value,
357
- afterWrite: async (_updated, record) => {
358
- const permissionId = record.participantPermissionIds?.[input.keyId];
359
- if (permissionId) {
360
- await this.options.transport.removeDatasetPermission(stored.fileId, permissionId);
361
- }
362
- const participantPermissionIds = Object.fromEntries(Object.entries(record.participantPermissionIds ?? {}).filter(([keyId]) => keyId !== input.keyId));
363
- await this.options.registry.set({
364
- ...record,
365
- participantPermissionIds,
366
- });
367
- },
572
+ previous: stored.envelope,
573
+ }, this.cryptoOptions());
574
+ const updated = await this.options.transport.writeDataset(stored, next);
575
+ await this.persistHead(updated, record.trustedOwnerKeyId, {
576
+ ...record,
577
+ participantPermissionIds: Object.fromEntries(Object.entries(record.participantPermissionIds ?? {}).filter(([keyId]) => keyId !== input.keyId)),
368
578
  });
579
+ return result(updated, value, "updated");
369
580
  });
370
581
  }
582
+ async findDirectDatasetPermission(fileId, permissionId, emailAddress) {
583
+ const directPermissions = (await this.options.transport.listDatasetPermissions(fileId)).filter((permission) => !permission.inherited);
584
+ if (permissionId) {
585
+ const byId = directPermissions.find((permission) => permission.permissionId === permissionId);
586
+ if (byId)
587
+ return byId;
588
+ }
589
+ const normalizedEmail = emailAddress?.trim().toLowerCase();
590
+ if (!normalizedEmail)
591
+ return undefined;
592
+ return directPermissions.find((permission) => permission.emailAddress?.toLowerCase() === normalizedEmail);
593
+ }
371
594
  rotateLocalKey(replacementIdentity, datasetIds) {
372
595
  return this.serialized(async () => {
373
596
  const currentIdentity = await this.options.identity();
@@ -544,30 +767,6 @@ export class SharedBackupController {
544
767
  return { datasetId: input.datasetId, actions };
545
768
  });
546
769
  }
547
- changeParticipants(datasetId, change) {
548
- return this.serialized(async () => {
549
- const stored = await this.readDatasetById(datasetId);
550
- const record = await this.requiredRegistry(datasetId);
551
- await this.verifyHead(stored, record);
552
- const identity = await this.options.identity();
553
- const actor = sharedBackupParticipant(stored.envelope, identity.publicKey.keyId);
554
- if (!actor || !canAdministerSharedBackup(actor.role)) {
555
- throw new SyncKitError("authorization", "Only a current owner or admin can change dataset access.");
556
- }
557
- const value = await decryptSharedBackupEnvelopeV1(stored.envelope, this.options.codec, identity, this.crypto(), { trustedOwnerKeyId: record.trustedOwnerKeyId });
558
- const changed = await change(stored, value);
559
- const next = await createSharedBackupEnvelopeV1(changed.value, this.options.codec, identity, {
560
- appId: this.options.appId,
561
- backupId: datasetId,
562
- participants: changed.participants,
563
- previous: stored.envelope,
564
- }, this.cryptoOptions());
565
- const updated = await this.options.transport.writeDataset(stored, next);
566
- const nextRecord = await this.persistHead(updated, record.trustedOwnerKeyId, record);
567
- await changed.afterWrite?.(updated, nextRecord);
568
- return result(updated, changed.value, "updated");
569
- });
570
- }
571
770
  async readDatasetById(datasetId) {
572
771
  requireNonEmpty(datasetId, "datasetId");
573
772
  const record = await this.options.registry.get(datasetId);