@minca/core 0.1.6 → 0.1.8

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 (55) hide show
  1. package/README.md +247 -215
  2. package/dist/elements/auth-styles.d.ts +1 -1
  3. package/dist/elements/auth-styles.d.ts.map +1 -1
  4. package/dist/elements/auth-styles.js +16 -8
  5. package/dist/elements/auth-styles.js.map +1 -1
  6. package/dist/elements/minca-auth.d.ts +7 -0
  7. package/dist/elements/minca-auth.d.ts.map +1 -1
  8. package/dist/elements/minca-auth.js +107 -49
  9. package/dist/elements/minca-auth.js.map +1 -1
  10. package/dist/node/api.d.ts +1 -0
  11. package/dist/node/api.d.ts.map +1 -1
  12. package/dist/node/api.js +7 -1
  13. package/dist/node/api.js.map +1 -1
  14. package/dist/node/node.d.ts.map +1 -1
  15. package/dist/node/node.js +135 -57
  16. package/dist/node/node.js.map +1 -1
  17. package/dist/node/types.d.ts +8 -0
  18. package/dist/node/types.d.ts.map +1 -1
  19. package/dist/node/writer-keys.d.ts +4 -1
  20. package/dist/node/writer-keys.d.ts.map +1 -1
  21. package/dist/node/writer-keys.js +25 -6
  22. package/dist/node/writer-keys.js.map +1 -1
  23. package/dist/node/writers.d.ts.map +1 -1
  24. package/dist/node/writers.js +10 -3
  25. package/dist/node/writers.js.map +1 -1
  26. package/dist/react/minca-auth.d.ts +3 -1
  27. package/dist/react/minca-auth.d.ts.map +1 -1
  28. package/dist/react/minca-auth.js +5 -3
  29. package/dist/react/minca-auth.js.map +1 -1
  30. package/dist/session/account-hint.d.ts +13 -0
  31. package/dist/session/account-hint.d.ts.map +1 -0
  32. package/dist/session/account-hint.js +51 -0
  33. package/dist/session/account-hint.js.map +1 -0
  34. package/dist/session/auth.d.ts +51 -3
  35. package/dist/session/auth.d.ts.map +1 -1
  36. package/dist/session/auth.js +254 -35
  37. package/dist/session/auth.js.map +1 -1
  38. package/dist/session/connect.d.ts.map +1 -1
  39. package/dist/session/connect.js +6 -2
  40. package/dist/session/connect.js.map +1 -1
  41. package/dist/session/index.d.ts +1 -0
  42. package/dist/session/index.d.ts.map +1 -1
  43. package/dist/session/index.js +1 -0
  44. package/dist/session/index.js.map +1 -1
  45. package/dist/session/pairing.d.ts +2 -0
  46. package/dist/session/pairing.d.ts.map +1 -1
  47. package/dist/session/pairing.js +8 -0
  48. package/dist/session/pairing.js.map +1 -1
  49. package/dist/session/places.d.ts +5 -0
  50. package/dist/session/places.d.ts.map +1 -1
  51. package/dist/session/places.js +12 -0
  52. package/dist/session/places.js.map +1 -1
  53. package/docs/node.md +24 -2
  54. package/docs/sign-in.md +123 -24
  55. package/package.json +2 -2
@@ -20,7 +20,7 @@ import { base64UrlEncode } from '../utils/encoding.js';
20
20
  import { startPageNode } from '../node/worker.js';
21
21
  import { copyAccountData } from '../node/copy.js';
22
22
  import { isProtocolError, protocolError } from '../utils/errors.js';
23
- import { deleteBrowserData, forgetPod, inspectPod, listAccounts, pickPod, recallPod, rememberPod, describeStores, storesFor, } from './places.js';
23
+ import { deleteBrowserData, forgetPod, inspectPod, listAccounts, pickPod, podToReopen, recallPod, rememberPod, describeStores, storesFor, } from './places.js';
24
24
  import { createStaySignedIn, defaultStorage, guardedStorage, rememberedSessionKey, } from './stay-signed-in.js';
25
25
  import { grantTerms, GRANT_VERSION, MAX_GRANT_DAYS, checkKeyProof, requestTerms, } from './connect.js';
26
26
  import { fromProposal, proposalSpaces, sameAutomation } from '../space/own-automations.js';
@@ -114,6 +114,8 @@ async function addAutomations(node, origin, automations) {
114
114
  const EXPIRY_SWEEP_MS = 15 * 60 * 1000;
115
115
  /** How long signing out waits for its revokes to reach the peers here, at most, unless configured: offline, they wait until next time */
116
116
  const SIGN_OUT_GRACE_MS = 5000;
117
+ /** How long `confirm` lets the recovery code be read, or the account offered to a phone */
118
+ const CONFIRMED_MS = 5 * 60 * 1000;
117
119
  /** Creates the sign-in flow for this page. */
118
120
  export function createMincaAuth(config = {}) {
119
121
  const rpId = config.rpId ?? globalThis.location?.hostname ?? 'localhost';
@@ -144,8 +146,11 @@ export function createMincaAuth(config = {}) {
144
146
  folderAvailable: isFolderStorageAvailable(),
145
147
  pairing: readPairingTicket(),
146
148
  pairingStage: null,
149
+ pairingArrival: null,
147
150
  podChoice: null,
148
151
  moved: null,
152
+ signingOut: false,
153
+ podToReopen: null,
149
154
  });
150
155
  const listeners = new Set();
151
156
  const update = (patch) => {
@@ -155,6 +160,22 @@ export function createMincaAuth(config = {}) {
155
160
  };
156
161
  /** The seed behind the open session. Never leaves this closure except as the recovery code. */
157
162
  let seed = null;
163
+ /** When the person last proved it is them (`confirm`); nothing that hands out the seed works without it */
164
+ let confirmedAt = 0;
165
+ /** Which account that proof was for, so it never carries over to another */
166
+ let confirmedFor = null;
167
+ /** When the open account was last opened with a password, passkey or the recovery code here, and which */
168
+ let signedInAt = 0;
169
+ let signedInFor = null;
170
+ const provedSignIn = (did) => {
171
+ signedInAt = Date.now();
172
+ signedInFor = did;
173
+ };
174
+ /**
175
+ * Whether a session just landed and is choosing its first way in: only `arrive`
176
+ * opens this, so no call can step back into setup to skip `confirm`.
177
+ */
178
+ let setupOpen = false;
158
179
  let localKey = null;
159
180
  let stopFollowingName = null;
160
181
  let expiryTimer = null;
@@ -216,6 +237,11 @@ export function createMincaAuth(config = {}) {
216
237
  clearInterval(expiryTimer);
217
238
  expiryTimer = null;
218
239
  seed = null;
240
+ confirmedAt = 0;
241
+ confirmedFor = null;
242
+ signedInAt = 0;
243
+ signedInFor = null;
244
+ setupOpen = false;
219
245
  localKey = null;
220
246
  await node?.close();
221
247
  }
@@ -267,7 +293,11 @@ export function createMincaAuth(config = {}) {
267
293
  }
268
294
  }
269
295
  writeConnections(auth.connections().filter((known) => !goes(known)));
270
- return { delivered: Promise.all(revoked).then((taken) => taken.every(Boolean)) };
296
+ // A home that closes once delivered must also have taken the removed automations' wakes off its carriers.
297
+ const refilled = node && waitMs > 0 ? node.carriers.settled(waitMs) : Promise.resolve(true);
298
+ return {
299
+ delivered: Promise.all([Promise.all(revoked), refilled]).then(([taken]) => taken.every(Boolean)),
300
+ };
271
301
  }
272
302
  /**
273
303
  * Revokes the key a connection was given, where it could write; never this node's own.
@@ -453,13 +483,12 @@ export function createMincaAuth(config = {}) {
453
483
  * Lands a new session: in, or on through the setup screens.
454
484
  *
455
485
  * With no reason to set up, or a passkey or password already here, that is
456
- * the app. Otherwise the recovery code is shown first — a new account's for
457
- * the first time, a restored one's as a reminder to keep it apart from the
458
- * password about to be set, which a password manager may file in its place —
459
- * then a way in is chosen. A paired device skips the code: it was never
460
- * shown one, and the device it came from keeps it.
486
+ * the app. Otherwise a new account's recovery code is shown first, then a way
487
+ * in is chosen. A restored account skips the code, since the person just
488
+ * typed it, and so does a paired device, which was never shown one.
461
489
  */
462
490
  async function arrive(session, setup) {
491
+ update({ podToReopen: null });
463
492
  const place = state.place;
464
493
  const entry = place ? await openEntry(place, session.account.id) : null;
465
494
  const landed = {
@@ -471,15 +500,18 @@ export function createMincaAuth(config = {}) {
471
500
  if (setup === null || hasEverydayWay(entry)) {
472
501
  update({ ...landed, stage: 'ready', setup: null, freshCode: null });
473
502
  }
474
- else if (setup === 'paired') {
475
- update({ ...landed, stage: 'unlock', setup });
503
+ else if (setup === 'paired' || setup === 'restored') {
504
+ setupOpen = true;
505
+ update({ ...landed, stage: 'unlock', setup, freshCode: null });
476
506
  }
477
507
  else {
508
+ setupOpen = true;
478
509
  update({ ...landed, stage: 'recovery', setup, freshCode: seed ? seedToRecoveryCode(seed) : null });
479
510
  }
480
511
  }
481
512
  /** After a way in is set up: offer a pod to a new account that could use one, or go in. */
482
513
  function afterUnlock() {
514
+ setupOpen = false;
483
515
  const offerPod = config.offerPod !== false &&
484
516
  state.setup === 'new' &&
485
517
  state.folderAvailable &&
@@ -487,8 +519,12 @@ export function createMincaAuth(config = {}) {
487
519
  update(offerPod ? { stage: 'pod' } : { stage: 'ready', setup: null });
488
520
  }
489
521
  /** Runs a way in. One that took the recovery code may lead on to setting up an everyday one. */
522
+ // Every way in says whether it proved someone is there, so a new one can't count by omission.
490
523
  const enter = (open) => run(async () => {
491
- const { session, byCode } = await open();
524
+ const { session, byCode, proved } = await open();
525
+ // Opening it took the password, a passkey or the recovery code: proof enough to give an app access, for a few minutes.
526
+ if (proved)
527
+ provedSignIn(session.did);
492
528
  await arrive(session, byCode ? 'restored' : null);
493
529
  }).then(() => undefined);
494
530
  /**
@@ -563,10 +599,31 @@ export function createMincaAuth(config = {}) {
563
599
  update({ session: Object.freeze({ ...session, account: renamed }), accounts: await listAccounts(place) });
564
600
  return renamed;
565
601
  }
566
- /** Changes to the open account's shortcuts, then re-reads them. During setup, moves the flow on. */
602
+ /** Whether the person proved it is them (`confirm`) in the last few minutes */
603
+ const isConfirmed = () => seed !== null &&
604
+ confirmedFor !== null &&
605
+ state.session?.did === confirmedFor &&
606
+ Date.now() - confirmedAt < CONFIRMED_MS;
607
+ /**
608
+ * Whether an app may be given access now: the person proved it is them, or
609
+ * opened the account with a password, passkey or the recovery code, in the
610
+ * last few minutes. A sign-in kept from before is neither.
611
+ */
612
+ const mayGrant = () => isConfirmed() ||
613
+ (seed !== null &&
614
+ signedInFor !== null &&
615
+ state.session?.did === signedInFor &&
616
+ Date.now() - signedInAt < CONFIRMED_MS);
617
+ /**
618
+ * Changes to the open account's shortcuts, then re-reads them. During setup,
619
+ * moves the flow on. Outside setup they need `confirm` first: a new password
620
+ * would otherwise both open the gate and lock the owner out.
621
+ */
567
622
  const changeShortcut = (apply) => run(async () => {
568
623
  if (!seed)
569
624
  throw new Error('Not signed in.');
625
+ if (!setupOpen && !isConfirmed())
626
+ throw new Error('Confirm it is you first.');
570
627
  await apply(seed);
571
628
  await refreshEntry();
572
629
  if (state.stage === 'unlock' && hasEverydayWay(state.entry))
@@ -588,6 +645,31 @@ export function createMincaAuth(config = {}) {
588
645
  set(`${prefix}.connections:${account}`, JSON.stringify(connections));
589
646
  update({});
590
647
  }
648
+ /** An account a pairing brought, held while a signed-in device asks whether to take it */
649
+ let heldHandover = null;
650
+ /** Counts pairings begun and dismissed: one that ends after a dismiss is dropped */
651
+ let pairingRun = 0;
652
+ /** Stops the pairing under way, so a dismissed one leaves the room rather than waiting it out */
653
+ let stopPairing = null;
654
+ /** Signs in with what a pairing brought, then joins its spaces */
655
+ async function takeHandover(handover) {
656
+ const onStage = (stage) => update({ pairingStage: stage });
657
+ // Starting the node takes a moment; the code on screen is done with.
658
+ onStage({ kind: 'sending' });
659
+ const opened = await run(async () => {
660
+ await arrive(await openWithCode(seedToRecoveryCode(handover.seed), null), 'paired');
661
+ });
662
+ const session = state.session;
663
+ if (!opened || !session)
664
+ return;
665
+ let joined = 0;
666
+ for (const [index, invite] of handover.spaces.entries()) {
667
+ onStage({ kind: 'joining', joined: index, total: handover.spaces.length });
668
+ if (await session.node.spaces.join(invite).then(() => true, () => false))
669
+ joined++;
670
+ }
671
+ onStage({ kind: 'received', spaces: joined });
672
+ }
591
673
  // ─── The flow ──────────────────────────────────────────────────────
592
674
  const auth = {
593
675
  getState: () => state,
@@ -612,11 +694,16 @@ export function createMincaAuth(config = {}) {
612
694
  const pod = await recallPod(false).catch(() => null);
613
695
  const place = pod ?? (await browserPlace());
614
696
  const accounts = await refresh(place);
697
+ // Remembered, but the browser wants a click first: say so, rather than start as if new.
698
+ if (!pod)
699
+ update({ podToReopen: await podToReopen().catch(() => null) });
615
700
  if (accounts.length > 0) {
616
701
  const kept = await stay.recall(place.kind).catch(() => null);
617
702
  const summary = kept && accounts.find((account) => account.id === kept.accountId);
618
703
  if (kept && summary) {
619
- update({ session: await begin(place, summary, kept.seed), stage: 'ready' });
704
+ const session = await begin(place, summary, kept.seed);
705
+ // A pairing link still asks first: it would put another account here (spec 01 §15.3).
706
+ update({ session, stage: state.pairing ? 'pair' : 'ready' });
620
707
  return;
621
708
  }
622
709
  }
@@ -638,6 +725,30 @@ export function createMincaAuth(config = {}) {
638
725
  })();
639
726
  return started;
640
727
  },
728
+ async reopenPod() {
729
+ await run(async () => {
730
+ // Signed in meanwhile (another tab's sign-in taken up, say): moving pods asks first, through choosePod.
731
+ if (state.session) {
732
+ update({ podToReopen: null });
733
+ return;
734
+ }
735
+ const pod = await recallPod(true);
736
+ if (!pod)
737
+ throw new Error('The browser did not open the folder. Try again, or choose it with “Open a folder”.');
738
+ update({ podToReopen: null });
739
+ const accounts = await refresh(pod);
740
+ // Kept signed in there: straight in, as a page load with the permission would have been.
741
+ const kept = await stay.recall(pod.kind).catch(() => null);
742
+ const summary = kept && accounts.find((account) => account.id === kept.accountId);
743
+ if (kept && summary) {
744
+ const session = await begin(pod, summary, kept.seed);
745
+ // A pairing link still asks first, as at start (spec 01 §15.3).
746
+ update({ session, stage: state.pairing ? 'pair' : 'ready' });
747
+ return;
748
+ }
749
+ update({ stage: state.pairing ? 'pair' : afterStorage(accounts) });
750
+ });
751
+ },
641
752
  async choosePod() {
642
753
  const picked = await run(async () => {
643
754
  const pod = await pickPod();
@@ -678,7 +789,11 @@ export function createMincaAuth(config = {}) {
678
789
  update({ entry: await openEntry(state.place, accountId) });
679
790
  },
680
791
  signInWithCode(code) {
681
- return enter(async () => ({ session: await openWithCode(code, selectedAccount()), byCode: true }));
792
+ return enter(async () => ({
793
+ session: await openWithCode(code, selectedAccount()),
794
+ byCode: true,
795
+ proved: true,
796
+ }));
682
797
  },
683
798
  signInWithPassword(password) {
684
799
  return enter(async () => {
@@ -698,12 +813,16 @@ export function createMincaAuth(config = {}) {
698
813
  failed = error instanceof Error ? error : new Error(String(error));
699
814
  }
700
815
  if (unlocked)
701
- return { session: await begin(place, entry.summary, unlocked), byCode: false };
816
+ return { session: await begin(place, entry.summary, unlocked), byCode: false, proved: true };
702
817
  }
703
818
  if (failed && !code)
704
819
  throw failed;
705
820
  if (code)
706
- return { session: await openWithCode(code, entry?.summary ?? selectedAccount()), byCode: true };
821
+ return {
822
+ session: await openWithCode(code, entry?.summary ?? selectedAccount()),
823
+ byCode: true,
824
+ proved: true,
825
+ };
707
826
  throw protocolError('VAULT_UNLOCK_FAILED', 'No password is set for this account here.', 'Use your recovery code, and you can set one up afterwards.');
708
827
  });
709
828
  },
@@ -724,6 +843,8 @@ export function createMincaAuth(config = {}) {
724
843
  return {
725
844
  session: await begin(place, entry.summary, await unwrapSeedWithDeviceKey(target, key, entry.vault.did)),
726
845
  byCode: false,
846
+ // A wrap with no passkey behind it skipped the check that someone is there.
847
+ proved: Boolean(target.credentialId),
727
848
  };
728
849
  });
729
850
  },
@@ -764,17 +885,20 @@ export function createMincaAuth(config = {}) {
764
885
  // shows it. Only at creation and on rename — never on a plain start,
765
886
  // where a device that has not synced yet would publish a stale name.
766
887
  await session.node.account.setName(summary.name).catch(() => { });
888
+ // Made just now, by whoever is here: an app asking next needs no more proof.
889
+ provedSignIn(session.did);
767
890
  await arrive(session, 'new');
768
891
  });
769
892
  },
770
893
  codeSaved() {
771
- if (!state.session)
894
+ if (!state.session || state.stage !== 'recovery')
772
895
  return;
773
896
  update({ freshCode: null, stage: 'unlock' });
774
897
  },
775
898
  finishSetup() {
776
- if (!state.session)
899
+ if (!state.session || state.stage !== 'pod')
777
900
  return;
901
+ setupOpen = false;
778
902
  update({ stage: 'ready', setup: null, error: null });
779
903
  },
780
904
  async acceptPairing() {
@@ -784,34 +908,77 @@ export function createMincaAuth(config = {}) {
784
908
  if (!state.place)
785
909
  update({ place: await browserPlace() });
786
910
  const onStage = (stage) => update({ pairingStage: stage });
787
- let spaces = [];
911
+ const mine = ++pairingRun;
912
+ const stop = new AbortController();
913
+ stopPairing = stop;
914
+ const got = { handover: null };
788
915
  // The account arrives over the exchange, after the person has compared
789
916
  // the codes; the link only says where to meet.
790
- const opened = await run(async () => {
791
- const handover = await receiveFromDesktop(ticket, onStage);
792
- spaces = handover.spaces;
793
- // Starting the node takes a moment; the code on screen is done with.
794
- onStage({ kind: 'sending' });
795
- await arrive(await openWithCode(seedToRecoveryCode(handover.seed), null), 'paired');
917
+ await run(async () => {
918
+ try {
919
+ got.handover = await receiveFromDesktop(ticket, onStage, {
920
+ transport: config.pairingTransport,
921
+ signal: stop.signal,
922
+ });
923
+ }
924
+ catch (error) {
925
+ // Stopped by "Not now": nothing went wrong to tell the person about.
926
+ if (!stop.signal.aborted)
927
+ throw error;
928
+ }
796
929
  });
930
+ const arrived = got.handover;
931
+ // "Not now" while the code showed: the computer may still have sent it, but it is not taken.
932
+ if (mine !== pairingRun) {
933
+ arrived?.seed.fill(0);
934
+ return;
935
+ }
797
936
  clearPairingTicket();
798
937
  update({ pairing: null });
799
- const session = state.session;
800
- if (!opened || !session)
938
+ if (!arrived)
801
939
  return;
802
- let joined = 0;
803
- for (const [index, invite] of spaces.entries()) {
804
- onStage({ kind: 'joining', joined: index, total: spaces.length });
805
- if (await session.node.spaces.join(invite).then(() => true, () => false))
806
- joined++;
940
+ // Signed in to another account, the device asks first: add the one that
941
+ // arrived, or switch to it if it is already here (spec 01 §15.3).
942
+ const signedIn = state.session;
943
+ if (signedIn) {
944
+ const { did } = await createIdentityManager().fromSeed(arrived.seed);
945
+ // "Not now" while the account was being looked at: let it go.
946
+ if (mine !== pairingRun) {
947
+ arrived.seed.fill(0);
948
+ return;
949
+ }
950
+ if (did !== signedIn.did) {
951
+ heldHandover = arrived;
952
+ update({
953
+ pairingArrival: { did, account: state.accounts.find((account) => account.did === did) ?? null },
954
+ });
955
+ return;
956
+ }
807
957
  }
808
- onStage({ kind: 'received', spaces: joined });
958
+ await takeHandover(arrived);
959
+ },
960
+ async answerPairing(take) {
961
+ const arrived = heldHandover;
962
+ heldHandover = null;
963
+ update({ pairingArrival: null });
964
+ if (!arrived)
965
+ return;
966
+ if (take)
967
+ return takeHandover(arrived);
968
+ arrived.seed.fill(0);
969
+ update({ pairingStage: null, stage: state.session ? 'ready' : afterStorage(state.accounts) });
809
970
  },
810
971
  dismissPairing() {
972
+ pairingRun++;
973
+ stopPairing?.abort();
974
+ stopPairing = null;
811
975
  clearPairingTicket();
976
+ heldHandover?.seed.fill(0);
977
+ heldHandover = null;
812
978
  update({
813
979
  pairing: null,
814
980
  pairingStage: null,
981
+ pairingArrival: null,
815
982
  stage: state.session ? 'ready' : afterStorage(state.accounts),
816
983
  });
817
984
  },
@@ -942,19 +1109,63 @@ export function createMincaAuth(config = {}) {
942
1109
  dismissMoved() {
943
1110
  update({ moved: null });
944
1111
  },
1112
+ confirm(proof) {
1113
+ return run(async () => {
1114
+ const { entry, place } = state;
1115
+ const unlocked = seed;
1116
+ if (!unlocked || !entry || !place)
1117
+ throw new Error('Sign in first.');
1118
+ const same = (other) => other.length === unlocked.length && other.every((byte, at) => byte === unlocked[at]);
1119
+ const wrong = (message) => protocolError('VAULT_UNLOCK_FAILED', message);
1120
+ if ('code' in proof) {
1121
+ if (!isValidRecoveryCode(proof.code) || !same(recoveryCodeToSeed(proof.code)))
1122
+ throw wrong('That is not this account’s recovery code.');
1123
+ }
1124
+ else if ('password' in proof) {
1125
+ const wraps = entry.vault.wraps.filter((candidate) => candidate.kind === 'passphrase');
1126
+ let opened = false;
1127
+ for (const wrap of wraps) {
1128
+ const tried = await unwrapSeedWithPassphrase(wrap, proof.password, entry.vault.did).catch(() => null);
1129
+ if (tried && same(tried))
1130
+ opened = true;
1131
+ }
1132
+ if (!opened)
1133
+ throw wrong('That is not the password.');
1134
+ }
1135
+ else {
1136
+ const target = entry.shortcuts[entry.shortcuts.length - 1];
1137
+ // The passkey's own check that someone is there: a fingerprint, a face or a PIN.
1138
+ // A wrap with no passkey behind it would skip that check, so it is no proof.
1139
+ if (!target?.credentialId)
1140
+ throw wrong('No passkey on this device opens this account.');
1141
+ await authenticatePasskey(target.credentialId, { rpId });
1142
+ const key = await getDeviceKey(target.deviceKeyId);
1143
+ if (!key || !same(await unwrapSeedWithDeviceKey(target, key, entry.vault.did)))
1144
+ throw wrong('That passkey does not open this account here.');
1145
+ }
1146
+ confirmedAt = Date.now();
1147
+ confirmedFor = state.session?.did ?? null;
1148
+ });
1149
+ },
1150
+ isConfirmed,
1151
+ mayGrant,
945
1152
  recoveryCode() {
946
- return seed ? seedToRecoveryCode(seed) : null;
1153
+ return seed && isConfirmed() ? seedToRecoveryCode(seed) : null;
947
1154
  },
948
1155
  offerToPhone(onStage) {
949
1156
  const session = state.session;
950
1157
  if (!session || !seed)
951
1158
  return Promise.reject(new Error('Sign in first.'));
1159
+ // It sends the whole account: the same proof as reading the recovery code.
1160
+ if (!isConfirmed())
1161
+ return Promise.reject(new Error('Confirm it is you first.'));
952
1162
  const location = globalThis.location;
953
1163
  return offerPairing({
954
1164
  seed,
955
1165
  invites: async () => Promise.all((await session.node.spaces.list()).map((space) => session.node.spaces.invite(space.id))),
956
1166
  relays: config.network?.relays ?? [],
957
1167
  link: config.pairingLink ?? (location ? `${location.origin}${location.pathname}` : ''),
1168
+ transport: config.pairingTransport,
958
1169
  }, onStage);
959
1170
  },
960
1171
  async grant(choice) {
@@ -976,6 +1187,10 @@ export function createMincaAuth(config = {}) {
976
1187
  if (recordsOnly && request.contacts)
977
1188
  throw new Error('A key given records only is not given your contacts.');
978
1189
  const whole = request.scope === 'account';
1190
+ // Like any sign-in elsewhere: the person proves it is them before an app gets anything,
1191
+ // not only a home that stayed signed in on a computer anyone may walk up to.
1192
+ if (!mayGrant())
1193
+ throw new Error('Confirm it is you first.');
979
1194
  const created = [];
980
1195
  for (const params of request.create ?? [])
981
1196
  created.push(await node.spaces.create(params));
@@ -1095,6 +1310,8 @@ export function createMincaAuth(config = {}) {
1095
1310
  const problem = await audienceProblem(request, origin);
1096
1311
  if (problem)
1097
1312
  throw new Error(problem);
1313
+ if (!mayGrant())
1314
+ throw new Error('Confirm it is you first.');
1098
1315
  // One carrier per origin: a new key there replaces the old one. The same key again keeps its
1099
1316
  // carry space (`carriers.add`). An app at the same origin is not it, and stays (§2.10).
1100
1317
  const carrierHere = (known) => known.origin === origin && known.access === 'carry';
@@ -1159,7 +1376,7 @@ export function createMincaAuth(config = {}) {
1159
1376
  },
1160
1377
  async signOut(options = {}) {
1161
1378
  // Not ready from now on: nothing may be granted from this account while it goes.
1162
- update({ stage: 'starting' });
1379
+ update({ stage: 'starting', signingOut: true });
1163
1380
  await stay.forget();
1164
1381
  const leaving = state.session?.node;
1165
1382
  // Every app and the programs connected through it; carriers stay, holding nothing that writes.
@@ -1174,6 +1391,8 @@ export function createMincaAuth(config = {}) {
1174
1391
  await Promise.all([
1175
1392
  disconnected?.delivered,
1176
1393
  leaving?.revokeSelf({ waitMs: signOutGraceMs }).catch(() => { }),
1394
+ // Whatever changed for the carriers just now is written for them before the node stops.
1395
+ leaving?.carriers.settled(signOutGraceMs).catch(() => false),
1177
1396
  ]);
1178
1397
  update({
1179
1398
  stage: 'starting',
@@ -1186,7 +1405,7 @@ export function createMincaAuth(config = {}) {
1186
1405
  });
1187
1406
  await stopNode(leaving).catch(() => { });
1188
1407
  const place = state.place;
1189
- update({ stage: place ? afterStorage(await refresh(place)) : 'welcome' });
1408
+ update({ stage: place ? afterStorage(await refresh(place)) : 'welcome', signingOut: false });
1190
1409
  },
1191
1410
  };
1192
1411
  return auth;